设为首页 加入收藏

TOP

1.6 The Bookstore Program (1)
2013-10-07 16:14:32 来源: 作者: 【 】 浏览:80
Tags:1.6 The Bookstore Program

We are now ready to solve our original bookstore problem. We need to read a file of sales transactions and produce a report that shows, for each book, the total number of copies sold, the total revenue, and the average sales price. We’ll assume that all the transactions for each ISBN are grouped together in the input.

Our program will combine the data for each ISBN in a variable named total. We’ll use a second variable named trans to hold each transaction we read. If trans and total refer to the same ISBN, we’ll update total. Otherwise we’ll print total and reset it using the transaction we just read:

  1. #include    
  2. #include "Sales_item.h"   
  3. int 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. Sales_item trans; // variable to hold the running sum   
  9. // read and process the remaining transactions   
  10. while (std::cin >> trans) {   
  11. // if we’re still processing the same book   
  12. if (total.isbn() == trans.isbn())   
  13. total += trans; // update the running total   
  14. else {   
  15. // print results for the previous book   
  16. std::cout << total << std::endl;   
  17. // no input! warn the user   
  18. std::cerr << "No data !" << std::endl;   
  19. return -1; // indicate failure   
  20. }  
  21. return 0;   
  22. }  

This program is the most complicated one we’ve seen so far, but it uses only facilities that we have already seen.

As usual, we begin by including the headers that we use, iostream from the library and our own Sales_item.h. Inside main we define an object named total, which we’ll use to sum the data for a given ISBN. We start by reading the first transaction into total and testing whether the read was successful. If the read fails, then there are no records and we fall through to the outermost else branch, which tells the user that there was no input.

Assuming we have successfully read a record, we execute the block following the outermost if. That block starts by defining the object named trans, which will hold our transactions as we read them. The while statement will read all the remaining records. As in our earlier programs, the while condition reads a value from the standard input. In this case, we read a Sales_item object into trans. As long as the read succeeds, we execute the body of the while.

The body of the while is a single if statement. The if checks whether the ISBNs are equal. If so, we use the compound assignment operator to add trans to total. If the ISBNs are not equal, we print the value stored in total and reset total by assigning trans to it. After executing the if, we return to the condition in the while, reading the next transaction, and so on until we run out of records.

When the while terminates, total contains the data for the last ISBN in the file. We write the data for the last ISBN in the last statement of the block that concludes the outermost if statement.

EXERCISES SECTION 1.6

Exercise 1.25: Using the Sales_item.h header from the Web site, compile and execute the bookstore program presented in this section.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.5 Introducing Classes (4) 下一篇1.5 Introducing Classes (6)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)