赞
踩
描述
实现一个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类(包括它的友元函数):
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 operato
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。