-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path7.3.cpp
51 lines (47 loc) · 1.03 KB
/
7.3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<string>
struct Sales_data
{
std::string ISBN;
double totalPrice;
int amount;
std::string isbn() const
{
return ISBN;
}
Sales_data & combine(const Sales_data & sa);
};
Sales_data & Sales_data::combine(const Sales_data & sa)
{
amount += sa.amount;
totalPrice += sa.totalPrice;
return *this;
}
int main()
{
Sales_data total;
double price;
if(std::cin >> total.ISBN >> total.amount >> price)
{
total.totalPrice = total.amount * price;
Sales_data trans;
while(std::cin >> trans.ISBN >> trans.amount >> price)
{
trans.totalPrice = trans.amount * price;
if(total.isbn() == trans.isbn())
total.combine(trans);
else
{
std::cout << total.isbn() << " " << total.amount << " " << total.totalPrice << " " << total.totalPrice/total.amount << std::endl;
total = trans;
}
}
std::cout << total.isbn() << " " << total.amount << " " << total.totalPrice << " " << total.totalPrice/total.amount << std::endl;
}
else
{
std::cerr << "No data?!" <<std::endl;
return -1;
}
return 0;
}