Team LiB
Previous Section Next Section

1.6. The C++ Program

1.6. C++ 程序

Now we are 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 revenue, average sales price, and the number of copies sold.

现在我们已经做好准备,可以着手解决最初的书店问题了:我们需要读入销售交易文件,并产生报告显示每本书的总销售收入、平均销售价格和销售册数。

We'll assume that all of the transactions for a given ISBN appear together. Our program will combine the data for each ISBN in a Sales_item object named total. Each transaction we read from the standard input will be stored in a second Sales_item object named trans. Each time we read a new transaction we'll compare it to the Sales_item object in total. If the objects refer to the same ISBN, we'll update total. Otherwise we'll print the value in total and reset it using the transaction we just read.

假定给定ISBN的所有交易出现在一起。程序将把每个 ISBN 的数据组合至命名为 totalSales_item 对象中。从标准输入中读取的每一笔交易将被存储到命名为 trans 的第二个 Sales_item 对象中。每读取一笔新的交易,就将它与 total 中的 Sales_item 对象相比较,如果对象含有相同的 ISBN,就更新 total ;否则就输出 total 的值,并使用刚读入的交易重置 total

    #include <iostream>
    #include "Sales_item.h"
    int main()
    {
        //  declare variables to hold running sum and data for the next record
        Sales_item total, trans;
        //  is there data to process?
        if (std::cin >> total) {
            // if so, read the transaction records
            while (std::cin >> trans)
                if  (total.same_isbn(trans))
                   //  match: update the running total
                   total = total + trans;
                else {
                   //  no match: print & assign to total
                   std::cout << total << std::endl;
                   total = trans;
                }
            //  remember to print last record
            std::cout << total << std::endl;
         } else {
            //  no input!, warn the user
            std::cout << "No data?!" << std::endl;
            return -1;  //  indicate failure
         }
         return 0;
    }

This program is the most complicated one we've seen so far, but it uses only facilities that we have already encountered. As usual, we begin by including the headers that we use: iostream from the library and Sales_item.h, which is our own header.

这个程序是到目前我们见到的程序中最为复杂的一个,但它仅使用了我们已遇到过的工具。和平常一样,我们从包含所使用的头文件开始:标准库中的 iostream 和自定义的头文件 Sales_item.h

Inside main we define the objects we need: total, which we'll use to sum the data for a given ISBN, and trans, which will hold our transactions as we read them. We start by reading a 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 prints a message to warn the user that there was no input.

main 中我们定义了所需要的对象 total 用来计算给定的 ISBN 的交易的总数,trans 用来存储读取的交易。我们首先将交易读入 total 并测试是否读取成功;如果读取失败,表示没有记录,程序进入最外层的 else 分支,输出信息警告用户没有输入。

Assuming we have successfully read a record, we execute the code in the if branch. The first statement is a while that will loop through all the remaining records. Just as we did in the program on page 18, our while condition reads a value from the standard input and then tests that valid data was actually read. In this case, we read a Sales_item object into TRans. As long as the read succeeds, we execute the body of the while.

假如我们成功读取了一个记录,则执行 if 分支里的代码。首先执行 while 语句,循环遍历剩余的所有记录。就像第 1.4.3 节的程序一样,while 循环的条件从标准输入中读取值并测试实际读取的是否是合法数据。本例中,我们将一个 Sales_item 对象读至 trans。只要读取成功,就执行 while 循环体。

The body of the while is a single if statement. We test whether the ISBNs are equal, and if so we add the two objects and store the result in total. If the ISBNs are not equal, we print the value stored in total and reset total by assigning trans to it. After execution of the if, we return to the condition in the while, reading the next transaction and so on until we run out of records.

while 循环体只是一条 if 语句。我们测试 ISBN 是否相等。如果相等,我们将这两个对象相加并将结果存储到 total 中。否则,我们就输出存储在 total 中的值,并将 trans 赋值给 total 来重置 total。执行完 if 语句之后,将返回到 while 语句中的条件,读入下一个交易,直到执行完所有记录。

Once the while completes, we still must write the data associated with the last ISBN. When the while terminates, total contains the data for the last ISBN in the file, but we had no chance to print it. We do so in the last statement of the block that concludes the outermost if statement.

一旦 while 完成,我们仍须写出与最后一个 ISBN 相关联的数据。当 while 语句结束时,total 包含文件中最后一条 ISBN 数据,但是我们没有机会输出这条数据。我们在结束最外层 if 语句的语句块的最后一条语句中进行输出。

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.

使用源自本书配套网站的 Sales_item.h 头文件,编译并执行本节给出的书店程序。

Exercise 1.26:

In the bookstore program we used the addition operator and not the compound assignment operator to add trans to total. Why didn't we use the compound assignment operator?

在书店程序中,我们使用了加法操作符而不是复合赋值操作符将 trans 加到 total 中,为什么我们不使用复合赋值操作符?


Team LiB
Previous Section Next Section