C + + Production program for the Enlightenment

Source: Internet
Author: User
Tags header

Now we've solved the original bookstore problem: We have to read the sales record file and generate a report showing the total revenue, average selling price, and sales volume for each book.

Exercise 24:

Write a program to read several transaction records. For each newly read transaction record, check that its ISBN is the same as before, and calculate how many transaction records each ISBN has. Test the program by giving a few transaction records. These transactions should have more than one ISBN, but each ISBN record should be assembled together.

We assume that all transaction records for a given ISBN are present together. Our program will combine each ISBN data into the Sales_item object total. Each transaction that we read from the standard input is saved in the second Sales_item object trans. Every time we read a new transaction, we compare it to the Sales_item object in total. If the ISBN for two objects is the same, we update total. Otherwise, we print the value in total and set it to the value of the transaction record that you just read.

#include <iostream>
#include "Sales_item.h"
int main()
{
 // 定义变量来保存进行中的和与下一个记录
 Sales_item total, trans;
 // 需要处理数据吗?
 if (std::cin >> total) {
  // 是的,读取事务记录
  while (std::cin >> trans)
   if (total.same_isbn(trans))
    // 匹配,更新total
    total = total + trans;
   else {
    // 不匹配,打印和赋值
    std::cout << total << std::endl;
    total = trans;
   }
   // 记得打印最后的记录
   std::cout << total << std::endl;
 } else {
  // 没有输入!提示用户
  std::cout << "No data?!" << std::endl;
  return -1; // 表示失败
 }
 return 0;
}

This program is the most complex we've seen so far, but it only uses some of the tools we've already encountered. As usual, we started to include the iostream in the standard class library and the sales_item.h header files in our own classes.

In main we define the desired object: Total is used to count the given ISBN data and trans will hold the transaction records we read. We first read a transaction into the total and test the success of the read. If the read failed, then there is no record, we jump to the outermost else branch, print a message warning the user did not enter information.

Assuming that a record is successfully read, we will execute the code in the If branch. The first statement is a while, and it loops through all the remaining records. Like the operation in the previous example, our while condition reads the value from the standard input and tests whether the valid data was read. In the example, we read the Sales_item object into the trans. As long as the read succeeds, we execute the while body.

The body of the while is an if statement. We tested two ISBN for the same, and if the same, we added two objects and stored the results in total. If ISBN is different, we print out the value stored in total and assign trans to total. After the if is executed, we return to the while condition, reading the next transaction record until the record is read.

Once the while is complete, we still have to write the data associated with the last ISBN. When the while ends, Total contains the last ISBN in the file, but we have no chance to print it. We implement this function in the final statement code block, which contains the outermost if statement.

Exercise 25:

Compile and execute the bookstore program in this section using the Sales_item.h header file in the Web site.

Exercise 26:

In the bookstore program we used the addition operator instead of using the assignment operator to add total to the trans. Why don't we use the match assignment operator?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.