《C++ Primer》 2.6 自定义数据结构
编写一个书店数据结构,统计输入的两个数据。#include <iostream>using namespace std;
struct Sales_date {
string bookNo;
unsigned units_sold = 0;
double price = 0;
double revenue = 0.0;
};
int main() {
Sales_date data1, data2;
cin >> data1.bookNo >> data1.units_sold >> data1.price;
data1.revenue = data1.price * data1.units_sold;
cin >> data2.bookNo >> data2.units_sold >> data2.price;
data2.revenue = data2.price * data2.units_sold;
if (data1.bookNo == data2.bookNo) {
double totalRevenue = data1.revenue + data2.revenue;
unsigned totalSold = data1.units_sold + data2.units_sold;
cout << data1.bookNo << " " << totalSold << " " << totalRevenue << endl;
}
else {
cout << "Data must refer to same ISBN" << endl;
return -1;
}
return 0;
} 感谢分享 看两个是不是同一本书了 刚刚开始学习这个
页:
[1]