Singer类
描述
实现一个Singer类,通过以下测试:
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;
}
输入
输入包含两行,第一行为歌手s1的信息,第二行为歌手s2的信息,每位歌手的信息包括姓名(不包含空格)、性别、年龄 和 分数;姓名、性别、年龄和分数之间用空格分隔
输出
输出为三行,前两行分别是歌手s1和s2的信息,第三行根据s1和s2比较结果输出(s1和s2的比较结果和他们的分数的比较结果一致),具体参见主函数
输入样例 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.
提示
后台已有main函数,提交时去掉main函数
// 注意:无需提交main函数
#include<iostream>
using namespace std;
class Singer {
public:
string m_name;
char m_sex;
int m_age;
double m_score;
public:
string getName();
friend istream& operator>>(istream& in, Singer &s);
friend ostream& operator<<(ostream& out, const Singer &s);
friend bool operator>(const Singer s1, const Singer s2);
friend bool operator==(const Singer s1, const Singer s2);
};
bool operator>(const Singer s1, const Singer s2) {
if (s1.m_score > s2.m_score)
return true;
else
return false;
}
bool operator==(const Singer s1, const Singer s2) {
if (s1.m_score == s2.m_score)
return true;
else
return false;
}
istream& operator>>(istream& in, Singer &s) {
in >> s.m_name >> s.m_sex >> s.m_age >> s.m_score;
return in;
}
ostream& operator<<(ostream& out, const Singer &s) {
out << s.m_name <<" " << s.m_sex <<" "<<s.m_age << " " << s.m_score ;
return out;
}
string Singer::getName() {
return m_name;
}
Sales_data类
描述
实现以下Sales_data类(包括它的友元函数):
class Sales_data {
//依次输入书号、销量和收入
friend istream & operator>>(istream&, Sales_data &);
//依次输出书号、销量、收入和均价
friend ostream & operator<<(ostream &, const Sales_data &);
// 判两个对象==的条件是它们的3个属性(书号、销量和收入)都一样
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<<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;
}
输入
输入多组数据,每组数据两行,每行表示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
提示
后台已有main函数,提交时去掉main函数
// 注意:无需提交main函数
#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&);
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;
Sales_data& operator+=(const Sales_data& s);
private:
double avg_price() const;
string bookNo; //书号
unsigned units_sold; //销量
double revenue; //收入
};
istream& operator>>(istream&in, Sales_data&s) {
in >> s.bookNo >> s.units_sold >> s.revenue;
return in;
}
ostream& operator<<(ostream& out, const Sales_data& s) {
out << s.bookNo <<" "<< s.units_sold << " " << s.revenue << " " << s.avg_price();
return out;
}
bool operator==(const Sales_data&s1, const Sales_data&s2) {
if (s1.bookNo == s2.bookNo && s1.units_sold == s2.units_sold && s1.revenue == s2.revenue)
return true;
else
return false;
}
bool operator!=(const Sales_data&s1, const Sales_data&s2) {
if (s1.bookNo != s2.bookNo || s1.units_sold != s2.units_sold || s1.revenue != s2.revenue)
return true;
else
return false;
}
Sales_data operator+(const Sales_data&s1, const Sales_data&s2) {
Sales_data temp;
temp.bookNo = s1.bookNo;
temp.units_sold = s1.units_sold + s2.units_sold;
temp.revenue = s1.revenue + s2.revenue;
return temp;
}
Sales_data&Sales_data::operator+=(const Sales_data& s2)
{
units_sold += s2.units_sold;
revenue += s2.revenue;
return *this;
}
string Sales_data::get_bookNo() const {
return bookNo;
}
double Sales_data::avg_price() const {
return revenue / units_sold;
}
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
提示
后台已有main函数,提交时去掉main函数
复数加法公式:(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
// 注意:无需提交main函数
#include<iostream>
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& Complex::operator+=(const Complex&c) {
this->x += c.x;
this->y += c.y;
return *this;
}
Complex& Complex::operator-=(const Complex&c) {
this->x -= c.x;
this->y -= c.y;
return *this;
}
Complex& Complex::operator*=(const Complex&c) {
*this = *this * c;
return *this;
}
Complex& Complex::operator/=(const Complex&c) {
*this = *this / c;
return *this;
}
Complex operator+(const Complex&c1, const Complex&c2) {
Complex temp;
temp.x = c1.x + c2.x;
temp.y = c1.y + c2.y;
return temp;
}
Complex operator-(const Complex&c1, const Complex&c2) {
Complex temp;
temp.x = c1.x - c2.x;
temp.y = c1.y - c2.y;
return temp;
}
Complex operator*(const Complex&c1, const Complex&c2) {
Complex temp;
temp.x = c1.x * c2.x-c1.y*c2.y;
temp.y = c1.x * c2.y+c1.y*c2.x;
return temp;
}
Complex operator/(const Complex&c1, const Complex&c2) {
Complex temp;
temp.x = (c1.x * c2.x + c1.y * c2.y) / (c2.x * c2.x + c2.y * c2.y);
temp.y = (c1.y * c2.x - c1.x * c2.y) / (c2.x * c2.x + c2.y * c2.y);
return temp;
}
bool operator==(const Complex&c1, const Complex&c2) {
if (c1.x == c2.y && c1.y == c2.y)
return true;
else
return false;
}
bool operator!=(const Complex&c1, const Complex&c2) {
if (c1.x != c2.y || c1.y != c2.y)
return true;
else
return false;
}
ostream& operator<<(ostream&out, const Complex&c) {
out << c.x << " + " << c.y << "i";
return out;
}
istream& operator>>(istream&in, Complex&c) {
in >> c.x >> c.y;
return in;
}
String类
描述
实现以下String类:
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 &);
};
使用以下main函数进行测试:
int main()
{
String s;
s += "hello";
cout<<s<<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;
}
输入
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];
s[0] = '\0';
}
String::String(const char*str) {
s = new char[strlen(str) + 1];
strcpy(s, str);
}
String::String(const String&str) {
s = new char[strlen(str.s) + 1];
strcpy(s, str.s);
}
String::~String() {
if (s != nullptr)
delete []s;
}
String& String::operator=(const char*str) {
s = new char[strlen(str) + 1];
strcpy(s, str);
return *this;
}
String& String::operator=(const String&str) {
s = new char[strlen(str.s) + 1];
strcpy(s, str.s);
return *this;
}
String String::operator+(const char*str) {
return String(s) + String(str);
}
String String::operator+(const String&str) {
return String(s) + str;
}
String& String::operator+=(const char*str) {
return operator+=(String(str));
}
String& String::operator+=(const String& str) {
char* temp = new char[strlen(s) + strlen(str.s) + 1];
temp = this->s;
strcat(temp, str.s);
this->s = temp;
return *this;
}
istream& operator>>(istream&in, String&str) {
in >> str.s;
return in;
}
ostream& operator<<(ostream& out, const String& str) {
out << str.s;
return out;
}
bool operator==(const String&str1, const char*str2) {
if (strcmp(str1.s, str2) == 0)
return true;
else
return false;
}
bool operator==(const String&str1, const String&str2) {
if (strcmp(str1.s, str2.s) == 0)
return true;
else
return false;
}
bool operator!=(const String&str1, const char*str2) {
if (strcmp(str1.s, str2) != 0)
return true;
else
return false;
}
bool operator!=(const String&str1, const String&str2) {
if (strcmp(str1.s, str2.s) != 0)
return true;
else
return false;
}
CheckedPtr
描述
自增(++)和自减(--)操作符经常由诸如迭代器这样的类实现,这样的类提供类似于指针的行为访问序列中的元素。例如,定义以下类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<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;
}
输入
输入为两行,第一行表示数组的长度n,第二行表示这n个数
输出
输出为两行,第一行依次输出数组的第0个到最后一个元素,第二行反向输出数组的所有元素(参考输出样例)
输入样例 1
5
1 2 3 4 5
输出样例 1
1 2 3 4 5
5 4 3 2 1
提示
后台已有main函数,提交时去掉main函数
// 注意:无需提交main函数
#include <iostream>
using namespace std;
class CheckedPtr {
public:
CheckedPtr(int* b, int* e) : beg(b), end(e), curr(b) {}
CheckedPtr& operator++() { // prefix ++
++curr;
return *this;
}
CheckedPtr& operator--() { // prefix --
--curr;
return *this;
}
CheckedPtr operator++(int) { // postfix ++
CheckedPtr ret = *this;
++* this;
return ret;
}
CheckedPtr operator--(int) { // postfix --
CheckedPtr ret = *this;
--* this;
return ret;
}
int* GetBeg() { return beg; }
int* GetEnd() { return end; }
int* GetCurr() { return curr; }
private:
int* beg;
int* end;
int* curr;
};