C++ Primer(第5版) 练习 7.1
练习 7.1 使用2.6.1节练习定义的Sales_data类为1.6节(第21页)的交易处理程序编写一个新版本。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************> File Name: ex7.1.cpp> Author: > Mail: > Created Time: Wed 14 Feb 2024 10:16:23 PM CST************************************************************************/#include<iostream>
using namespace std;class Sales_data{public:bool read();void print();string isbn;int unit = 0;double price = 0.0;double revenue = 0.0;
};bool Sales_data::read(){if(cin>>isbn>>unit>>price){revenue = unit * price;return true;}return false;
}void Sales_data::print(){cout<<isbn<<" "<<unit<<" "<<price<<" "<<revenue<<endl;
}int main(){Sales_data total;if(total.read()){Sales_data trans;while(trans.read()){if(total.isbn == trans.isbn){total.unit += trans.unit;total.revenue += trans.revenue;}else{total.print();total = trans;}}total.print();}else{cout<<"No data?"<<endl;}return 0;
}