Singer类
描述
实现一个Singer类,通过以下测试:
输入
输入包含两行,第一行为歌手s1的信息,第二行为歌手s2的信息,每位歌手的信息包括姓名(不包含空格)、性别、年龄 和 分数;姓名、性别、年龄和分数之间用空格分隔
输出
输出为三行,前两行分别是歌手s1和s2的信息,第三行根据s1和s2比较结果输出(s1和s2的比较结果和他们的分数的比较结果一致),具体参见主函数
nt main()
{
Singer s1,s2;
cin>>s1>>s2;
cout<<s1<<"\n"<<s2<<endl;
if(s1>s2)
cout<<s1.getName()<<"'s score is higher than "<<s2.getName()<<"'s.\n";
else if(s1==s2)
cout<<s1.getName()<<"'s score is equal to "<<s2.getName()<<"'s.\n";
else
cout<<s1.getName()<<"'s score is lower than "<<s2.getName()<<"'s.\n";
return 0;
}
输入样例 1
Mary F 28 99.5
Peter M 26 98
输出样例 1
Mary F 28 99.5
Peter M 26 98
Mary's score is higher than Peter's.
#include<iostream>
using namespace std;
class Singer
{
private:
string name;
char sex;
int age;
double grade;
public:
string getName();
Singer(string name = "", char s = '?', int a = 0, double n = 0);
friend istream& operator>>(istream& stream, Singer& s);
friend ostream& operator<<(ostream& stream, const Singer& s);
friend bool operator>(const Singer& s1, const Singer& s2)
{
if (s1.grade > s2.grade)
return true;
else
return false;
}
friend bool operator==(const Singer& s1, const Singer& s2)
{
if (s1.grade == s2.grade)
return true;
else
return false;
}
};
Singer::Singer(string name, char s, int a,double n ):name(name),sex(s),age(a),grade(n){}
istream& operator>>(istream& stream, Singer& s)
{
stream >> s.name >> s.sex >> s.age >> s.grade;
return stream;
}
ostream& operator<<(ostream& stream, const Singer& s)
{
stream << s.name << " " << s.sex << " " << s.age << " " << s.grade;
return stream;
}
string Singer::getName()
{
return name;
}
int main()
{
Singer s1, s2;
cin >> s1 >> s2;
cout << s1 << "\n" << s2 << endl;
if (s1 > s2)
cout << s1.getName() << "'s score is higher than " << s2.getName() << "'s.\n";
else if (s1 == s2)
cout << s1.getName() << "'s score is equal to " << s2.getName() << "'s.\n";
else
cout << s1.getName() << "'s score is lower than " << s2.getName() << "'s.\n";
return 0;
}
第二题:
Sales_data类
描述
实现以下Sales_data类(包括它的友元函数):
class Sales_data {
//依次输入书号、销量和收入
friend istream & operator>>(istream&, Sales_data &);
//依次输出书号、销量、收入和均价
friend ostream & operator<<(ostream &, const Sales_data &);
friend bool operator==(const Sales_data &, const Sales_data &);
friend bool operator!=(const Sales_data &, const Sales_data &);
// for "+", assume that both objects refer to the same book
friend Sales_data operator+(const Sales_data &, const Sales_data &);
public:
Sales_data(): units_sold(0), revenue(0.0) {}
Sales_data(const string & s, unsigned n, double r): bookNo(s), units_sold(n), revenue(r) {}
string get_bookNo() const;
// for "+=", assume that both objects refer to the same book
Sales_data & operator+=(const Sales_data &);
private:
double avg_price() const; //均价,等于收入除以销量
string bookNo; //书号
unsigned units_sold; //销量
double revenue; //收入
};
通过以下main函数的测试
int main(){
Sales_data item1,item2;
while(cin>>item1>>item2){
cout<
if(item1==item2)
cout<
if(item1!=item2)
cout<
cout<<(item1+item2)<<"\n";
item1 += item2;
cout<
}
return 0;
}
输入
输入多组数据,每组数据两行,每行表示1个Sales_data对象,依次是书号、销量和收入
输出
对于每组数据,输出5行,具体参见main函数和输出样例
输入样例 1
001 10 100.0
001 10 100.0
输出样例 1
001 10 100 10
001 10 100 10
001 equals 001
001 20 200 10
001 20 200 10
输入样例 2
002 5 250
003 8 400
输出样例 2
002 5 250 50
003 8 400 50
002 doesn't equal 003
002 13 650 50
002 13 650 50
#include<iostream>
using namespace std;
class Sales_data
{
//依次输入书号、销量和收入
friend istream& operator>>(istream&, Sales_data&);
//依次输出书号、销量、收入和均价
friend ostream& operator<<(ostream&, const Sales_data&);
friend bool operator==(const Sales_data&, const Sales_data&);
friend bool operator!=(const Sales_data&, const Sales_data&);
// for "+", assume that both objects refer to the same book
friend Sales_data operator+(const Sales_data&, const Sales_data&);
public:
Sales_data() : units_sold(0), revenue(0.0) {}
Sales_data(const string& s, unsigned n, double r) : bookNo(s), units_sold(n), revenue(r) {}
string get_bookNo() const;
// for "+=", assume that both objects refer to the same book
Sales_data& operator+=(const Sales_data&);
private:
double avg_price() const; //均价,等于收入除以销量
string bookNo; //书号
unsigned units_sold; //销量
double revenue; //收入
};
double Sales_data::avg_price() const
{
return(revenue / units_sold);
}
ostream& operator<<(ostream& stream, const Sales_data& s)
{
stream << s.bookNo << " " << s.units_sold << " " << s.revenue << " " << s.avg_price(); //函数的调用
return stream;
}
istream& operator>>(istream& stream, Sales_data& s)
{
stream >> s.bookNo >> s.units_sold >> s.revenue;
return stream;
}
bool operator==(const Sales_data& s1, const Sales_data& s2)
{
if (s1.units_sold == s2.units_sold)
return true;
else
return false;
}
bool operator!=(const Sales_data& s1, const Sales_data& s2)
{
if (s1.units_sold != s2.units_sold)
return true;
else
return false;
}
Sales_data operator+(const Sales_data& s1, const Sales_data& s2)
{
Sales_data s;
s.bookNo = s1.bookNo;
s.units_sold = s1.units_sold + s2.units_sold;
s.revenue = s1.revenue + s2.revenue;
return s;
}
string Sales_data::get_bookNo() const
{
return bookNo;
}
Sales_data& Sales_data::operator+=(const Sales_data& s2)
{
units_sold += s2.units_sold;
revenue += s2.revenue;
return *this;
}
int main() {
Sales_data item1, item2;
while (cin >> item1 >> item2) {
cout << item1 << "\n" << item2 << "\n";
if (item1 == item2)
cout << item1.get_bookNo() << " equals " << item2.get_bookNo() << "\n";
if (item1 != item2)
cout << item1.get_bookNo() << " doesn't equal " << item2.get_bookNo() << "\n";
cout << (item1 + item2) << "\n";
item1 += item2;
cout << item1 << "\n";
}
return 0;
}
Complex类
描述
实现以下复数类Complex,通过运算符重截,实现复数的输入输出以及相关运算。
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0);
Complex & operator+=(const Complex &);
Complex & operator-=(const Complex &);
Complex & operator*=(const Complex &);
Complex & operator/=(const Complex &);
friend Complex operator+(const Complex &, const Complex &);
friend Complex operator-(const Complex &, const Complex &);
friend Complex operator*(const Complex &, const Complex &);
friend Complex operator/(const Complex &, const Complex &);
friend bool operator==(const Complex &, const Complex &);
friend bool operator!=(const Complex &, const Complex &);
friend ostream & operator<<(ostream &, const Complex &);
friend istream & operator>>(istream &, Complex &);
};
通过以下主函数测试:
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1 = " << c1 << "\n" << "c2 = " << c2 << endl;
cout << "c1+c2 = " << c1 + c2 << endl;
cout << "c1-c2 = " << c1 - c2 << endl;
cout << "c1*c2 = " << c1 * c2 << endl;
cout << "c1/c2 = " << c1 / c2 << endl;
cout << (c1 += c2) << endl;
cout << (c1 -= c2) << endl;
cout << (c1 *= c2) << endl;
cout << (c1 /= c2) << endl;
cout << (c1 == c2) << " " << (c1 != c2) << endl;
return 0;
}
输入
输入有两行,每行输入两个表示复数c1和c2的浮点数。
输出
输出一共有11行,分别表示复数之间的各项操作,具体参见主函数和输出样例
输入样例 1
-4 6
2 5
输出样例 1
c1 = -4 + 6i
c2 = 2 + 5i
c1+c2 = -2 + 11i
c1-c2 = -6 + 1i
c1*c2 = -38 + -8i
c1/c2 = 0.758621 + 1.10345i
-2 + 11i
-4 + 6i
-38 + -8i
-4 + 6i
0 1
提示
复数加法公式:(a + bi) + (c + di) = (a + c) + (b + d)i
复数减法公式:(a + bi) - (c + di) = (a - c) + (b - d)i
复数乘法公式:(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
复数除法公式:(a + bi) / (c + di) = [(ac + bd) / (c * c + d * d)] + [(bc - ad) / (c * c + d * d)]i
#include <iostream>
#include<string>
using namespace std;
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0);
Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
Complex& operator/=(const Complex&);
friend Complex operator+(const Complex&, const Complex&);
friend Complex operator-(const Complex&, const Complex&);
friend Complex operator*(const Complex&, const Complex&);
friend Complex operator/(const Complex&, const Complex&);
friend bool operator==(const Complex&, const Complex&);
friend bool operator!=(const Complex&, const Complex&);
friend ostream& operator<<(ostream&, const Complex&);
friend istream& operator>>(istream&, Complex&);
};
Complex::Complex(double x, double y) {
this->x = x;
this->y = y;
}
Complex operator*(const Complex& b1, const Complex& b2)
{
Complex s;
s.x = b1.x * b2.x - b1.y * b2.y;
s.y = b1.x * b2.y + b1.y * b2.x;
return s;
}
Complex& Complex::operator+=(const Complex& s)
{
x += s.x;
y += s.y;
return *this;
}
Complex& Complex::operator-=(const Complex& s)
{
x -= s.x;
y -= s.y;
return *this;
}
Complex operator/(const Complex& s1, const Complex& s2)
{
Complex s;
s.x = (s1.x * s2.x + s1.y * s2.y) / (s2.x * s2.x + s2.y * s2.y);
s.y = (s1.y * s2.x - s1.x * s2.y) / (s2.x * s2.x + s2.y * s2.y);
return s;
}
Complex& Complex::operator*=(const Complex& c)
{
*this = *this * c;
return *this;
}
Complex& Complex::operator/=(const Complex& p)
{
*this = *this / p;
return *this;
}
Complex operator+(const Complex& s1, const Complex& s2)
{
Complex s;
s.x = s1.x + s2.x;
s.y = s1.y + s2.y;
return s;
}
Complex operator-(const Complex& s1, const Complex& s2)
{
Complex s;
s.x = s1.x - s2.x;
s.y = s1.y - s2.y;
return s;
}
bool operator==(const Complex& s1, const Complex& s2)
{
if (s1.x == s2.x || s1.y == s2.y)
return true;
else
return false;
}
bool operator!=(const Complex& s1, const Complex& s2)
{
if (s1.x != s2.x || s1.y != s2.y)
return true;
else
return false;
}
istream& operator>>(istream& stream, Complex& s)
{
stream >> s.x >> s.y;
return stream;
}
ostream& operator<<(ostream& stream, const Complex& s)
{
stream << s.x << " + " << s.y << "i";
return stream;
}
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1 = " << c1 << "\n" << "c2 = " << c2 << endl;
cout << "c1+c2 = " << c1 + c2 << endl;
cout << "c1-c2 = " << c1 - c2 << endl;
cout << "c1*c2 = " << c1 * c2 << endl;
cout << "c1/c2 = " << c1 / c2 << endl;
cout << (c1 += c2) << endl;
cout << (c1 -= c2) << endl;
cout << (c1 *= c2) << endl;
cout << (c1 /= c2) << endl;
cout << (c1 == c2) << " " << (c1 != c2) << endl;
return 0;
}
描述
实现以下String类:
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0);
Complex & operator+=(const Complex &);
Complex & operator-=(const Complex &);
Complex & operator*=(const Complex &);
Complex & operator/=(const Complex &);
friend Complex operator+(const Complex &, const Complex &);
friend Complex operator-(const Complex &, const Complex &);
friend Complex operator*(const Complex &, const Complex &);
friend Complex operator/(const Complex &, const Complex &);
friend bool operator==(const Complex &, const Complex &);
friend bool operator!=(const Complex &, const Complex &);
friend ostream & operator<<(ostream &, const Complex &);
friend istream & operator>>(istream &, Complex &);
};
通过以下主函数测试:
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1 = " << c1 << "\n" << "c2 = " << c2 << endl;
cout << "c1+c2 = " << c1 + c2 << endl;
cout << "c1-c2 = " << c1 - c2 << endl;
cout << "c1*c2 = " << c1 * c2 << endl;
cout << "c1/c2 = " << c1 / c2 << endl;
cout << (c1 += c2) << endl;
cout << (c1 -= c2) << endl;
cout << (c1 *= c2) << endl;
cout << (c1 /= c2) << endl;
cout << (c1 == c2) << " " << (c1 != c2) << endl;
return 0;
}
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0);
Complex & operator+=(const Complex &);
Complex & operator-=(const Complex &);
Complex & operator*=(const Complex &);
Complex & operator/=(const Complex &);
friend Complex operator+(const Complex &, const Complex &);
friend Complex operator-(const Complex &, const Complex &);
friend Complex operator*(const Complex &, const Complex &);
friend Complex operator/(const Complex &, const Complex &);
friend bool operator==(const Complex &, const Complex &);
friend bool operator!=(const Complex &, const Complex &);
friend ostream & operator<<(ostream &, const Complex &);
friend istream & operator>>(istream &, Complex &);
};
使用以下main函数进行测试:
int main()
{
String s;
s += "hello";
cout< String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}
输入
s3的值
输出
详见主函数和输出样例
输入样例 1
String3
输出样例 1
hello
String1
copy of String1
String3
1
0
End of my string.
#include<iostream>
#include<cstring>
using namespace std;
class String {
private:
char* s;
public:
String();
String(const char*);
String(const String&);
~String();
String& operator=(const char*);
String& operator=(const String&);
String operator+(const char*);
String operator+(const String&);
String& operator+=(const char*);
String& operator+=(const String&);
friend istream& operator>>(istream&, String&);
friend ostream& operator<<(ostream&, const String&);
friend bool operator==(const String&, const char*);
friend bool operator==(const String&, const String&);
friend bool operator!=(const String&, const char*);
friend bool operator!=(const String&, const String&);
};
String::String()
{
s = new char[100];
}
String::String(const char* c)
{
s = new char[strlen(c) + 1];
strcpy(s, c);
}
String::String(const String& c)
{
s = new char[strlen(c.s) + 1];
strcpy(s, c.s);
}
String::~String()
{
delete[]s;
}
String& String::operator=(const char* c)
{
return operator=(String(c));
}
String& String::operator=(const String& c)
{
s = new char[strlen(c.s) + 1];
strcpy(s, c.s);
return *this;
}
String String::operator+(const char* c)
{
return String(s) + String(c);
}
String String::operator+(const String& c)
{
return String(s) + c;
}
String& String::operator+=(const char* c)
{
return operator+=(String(c));
}
String& String::operator+=(const String& c)
{
char* r = new char[strlen(s) + strlen(c.s) + 1];
r = this->s;
strcat(r, c.s);
this->s = r;
return *this;
}
istream& operator>>(istream& stream, String& c)
{
stream >> c.s;
return stream;
}
ostream& operator<<(ostream& stream, const String& c)
{
stream << c.s;
return stream;
}
bool operator==(const String& c1, const char* c2)
{
if (strcmp(c1.s, c2) == 0)
return true;
else
return false;
}
bool operator==(const String& c1, const String& c2)
{
if (strcmp(c1.s, c2.s) == 0)
return true;
else
return false;
}
bool operator!=(const String& c1, const char* c2)
{
return(strcmp(c1.s, c2) != 0);
}
bool operator!=(const String& c1, const String& c2)
{
return(strcmp(c1.s, c2.s) != 0);
}
int main() {
String x;
x += "hello";
cout << x << endl;
String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}
描述
自增(++)和自减(--)操作符经常由诸如迭代器这样的类实现,这样的类提供类似于指针的行为访问序列中的元素。例如,定义以下类CheckedPtr,该类指向一个int数组并为该数组中的元素提供访问检查。
class CheckedPtr
{
public:
CheckedPtr(int * b, int * e) : beg(b), end(e), curr(b) { }
CheckedPtr & operator ++(); // prefix ++
CheckedPtr & operator --(); // prefix --
CheckedPtr operator ++(int); // postfix ++
CheckedPtr operator --(int); // postfix --
int * GetBeg();
int * GetEnd();
int * GetCurr();
private:
int * beg; // pointer to beginning of the array
int * end; // one past the end of the array
int * curr; // current position within the array
};
实现CheckedPtr类并通过以下main函数测试。
int main(){
int n;
cin>>n;
int * array = new int[n];
for(int i=0;i
cin>>array[i];
CheckedPtr cp(array, array+n);
for(;cp.GetCurr()
cout<<*cp.GetCurr()<<" ";
cout<
for(--cp;cp.GetCurr()>cp.GetBeg();cp--)
cout<<*cp.GetCurr()<<" ";
cout<<*cp.GetCurr()<
delete [] array;
return 0;
}
输入
输入为两行,第一行表示数组的长度n,第二行表示这n个数
输出
输出为两行,第一行依次输出数组的第0个到最后一个元素,第二行反向输出数组的所有元素(参考输出样例)
输入样例 1
5
1 2 3 4 5
输出样例 1
1 2 3 4 5
5 4 3 2 1
#include<iostream>
#include<string>
using namespace std;
class CheckedPtr
{
public:
CheckedPtr(int* a, int* e) : beg(a), end(e), curr(a) {}
CheckedPtr& operator ++();
CheckedPtr& operator --();
CheckedPtr operator ++(int);
CheckedPtr operator --(int);
int* GetBeg();
int* GetEnd();
int* GetCurr();
private:
int* beg;
int* end;
int* curr;
};
CheckedPtr& CheckedPtr::operator ++()
{
++curr;
return *this;
}
CheckedPtr& CheckedPtr::operator--()
{
--curr;
return *this;
}
CheckedPtr CheckedPtr::operator++(int)
{
CheckedPtr s(*this);
++* this;
return s;
}
CheckedPtr CheckedPtr::operator--(int)
{
CheckedPtr s(*this);
--* this;
return s;
}
int* CheckedPtr::GetBeg() {
return beg;
}
int* CheckedPtr::GetEnd()
{
return end;
}
int* CheckedPtr::GetCurr()
{
return curr;
}
int main() {
int n;
cin >> n;
int* array = new int[n];
for (int i = 0; i < n; i++)
cin >> array[i];
CheckedPtr cp(array, array + n);
for (; cp.GetCurr() < cp.GetEnd(); cp++)
cout << *cp.GetCurr() << " ";
cout << endl;
for (--cp; cp.GetCurr() > cp.GetBeg(); cp--)
cout << *cp.GetCurr() << " ";
cout << *cp.GetCurr() << endl;
delete[] array;
return 0;
}