-
-
-
-
-
-
-
-
-
-
-
-
-
struct sales_data {
字符串名称;
无符号的cnt;
双重价格;
};
int main()
{
sales_data total;
COUT << “请输入总” << ENDL;
如果(cin >> total.name >> total.cnt >> total.price)
{
cout << total.name <<“”<< total.cnt <<“”<< total.price <<“”<< endl;
sales_data trans;
COUT << “请输入反式” << ENDL;
while(cin >> trans.name >> trans.cnt >> trans.price)
{
cout << trans.name <<“”<< trans.cnt <<“”<< trans.price <<“”<< endl;
if(total.name == trans.name)
{
total.cnt + = trans.cnt;
}
其他
{
COUT << “total.cnt * total.price” << total.cnt * total.price << ENDL;
总=反;
}
}
}
其他
{
cerr <<“没有数据?”<< endl;
返回-1;
}
系统( “暂停”);
返回0;
}
中的结构和类基本是通用的,唯有几个细节不同: -
-
-
-
struct sales_data {
字符串名称;
无符号的cnt;
双重价格;
};
int main()
{
sales_data total;
COUT << “请输入总” << ENDL;
如果(cin >> total.name >> total.cnt >> total.price)
{
cout << total.name <<“”<< total.cnt <<“”<< total.price <<“”<< endl;
sales_data trans;
COUT << “请输入反式” << ENDL;
while(cin >> trans.name >> trans.cnt >> trans.price)
{
cout << trans.name <<“”<< trans.cnt <<“”<< trans.price <<“”<< endl;
if(total.name == trans.name)
{
total.cnt + = trans.cnt;
}
其他
{
COUT << “total.cnt * total.price” << total.cnt * total.price << ENDL;
总=反;
}
}
}
其他
{
cerr <<“没有数据?”<< endl;
返回-1;
}
系统( “暂停”);
返回0;
}
struct sales_data {
上市:
字符串名称;
无符号的cnt;
双倍总价;
string isbn()const {return name;}
sales_data&combine(const sales_data&rhs);
私人的:
保护:
};
sales_data&sales_data :: combine(const sales_data&rhs)
{
CNT + = rhs.cnt;
totalprice + = rhs.totalprice;
返回*这个;
}
int main()
{
sales_data total;
COUT << “请输入总” << ENDL;
如果(cin >> total.name >> total.cnt >> total.totalprice)
{
cout << total.name <<“”<< total.cnt <<“”<< total.totalprice <<“”<< endl;
sales_data trans;
COUT << “请输入反式” << ENDL;
sales_data总和;
总和=总;
无符号的CNT = total.cnt;
双TOTALPRICE = total.totalprice;
while(cin >> trans.name >> trans.cnt >> trans.totalprice)
{
cout << trans.name <<“”<< trans.cnt <<“”<< trans.totalprice <<“”<< endl;
if(total.isbn()== trans.isbn())
{
总和= total.combine(反式); //需要定义一个SALES_DATA对象来引用CNT和totalprice,用total.combine(反式)直接引用不对
CNT = sum.cnt;
TOTALPRICE = sum.totalprice;
}
其他
{
COUT << “CNT” << CNT << “TOTALPRICE” << TOTALPRICE << ENDL;
总=反;
}
}
}
其他
{
cerr <<“没有数据?”<< endl;
返回-1;
}
系统( “暂停”);
返回0;
}