2020-10-31待解决问题

《C++Primer第五版》第二章遗留问题

  • 结尾题目目前知识点不够完整写出最后一题,问题在于暂时没学到重载流操作符
    目前只能输入两个值。
#include<iostream>
#include<string>
using namespace std;

struct Sales_data
{
	//分别定义书本单价、销量、收入、编号
	string book_no;
	double price = 0.0;
	unsigned units_sold = 0;
	double revenue = 0.0;

};
int main()
{
	//定义类的对象
	Sales_data trans, currtrans;
	
	//输入对象成员值
	cin >> trans.book_no >> trans.units_sold >> trans.price;
	while (cin >> currtrans.book_no >> currtrans.units_sold >> currtrans.price)
	{
		double totalAvg = 0;
		if (trans.book_no == currtrans.book_no)
		{
			trans.revenue = trans.price * trans.units_sold;
			trans.units_sold += currtrans.units_sold;
			currtrans.revenue = currtrans.price * currtrans.units_sold;
			trans.revenue += currtrans.revenue;
			totalAvg = trans.revenue / trans.units_sold;
			cout << trans.book_no << "   " << trans.units_sold << "   " << trans.revenue << "   " << totalAvg << endl;
		}
		else
		{
			//cout << trans.book_no << "   " << trans.units_sold << "   " << trans.revenue << "   " << totalAvg << endl;
			//更新trans值
			trans.book_no = currtrans.book_no, trans.price = currtrans.price, trans.revenue = currtrans.revenue, trans.units_sold = currtrans.units_sold;

		}
		return 0;

	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值