本节主要的内容只有一份代码,就是标题所说的书店程序,
1#include <iostream>
2#include "Sales_item.h"
3int main()
4{
5 Sales_item total; // variable to hold data for the next transaction
6 // read the first transaction and ensure that there are data to process
7 if (std::cin >> total)
8 {
9 Sales_item trans; // variable to hold the running sum
10 // read and process the remaining transactions
11 while (std::cin >> trans)
12 {
13 // if we’re still processing the same book
14 if (total.isbn() == trans.isbn())
15 total += trans; // update the running total
16 else
17 {
18 // print results for the previous book
19 std::cout << total << std::endl;
20 total = trans; // total now refers to the next book
21 }
22 }
23 std::cout << total << std::endl; // print the last transaction
24 }
25 else
26 {
27 // no input! warn the user
28 std::cerr << "No data?!" << std::endl;
29 return -1; // indicate failure
30 }
31 return 0;
32}
33
34/* input:
350-201-70353-X 4 24.99
360-201-82470-1 4 45.39
370-201-88954-4 2 15.00
380-201-88954-4 5 12.00
390-201-88954-4 7 12.00
400-201-88954-4 2 12.00
410-399-82477-1 2 45.39
420-399-82477-1 3 45.39
430-201-78345-X 3 20.00
440-201-78345-X 2 25.00
45*/
可能有同志会注意到这个程序在读取输入的时候,最后可能需要先回车一下,然后按下 Ctrl + Z 然后再回车,才能使程序接收到 EOF(end of file) 信号,这个问题我们只需要知道如何去规避即可,如果想刨根究底,那么,也没有问题,推荐去看以下两个链接,主要是第二个链接,