设为首页 加入收藏

TOP

2.6 Defining Our Own Data Structures (2)
2013-10-07 16:16:54 来源: 作者: 【 】 浏览:76
Tags:2.6 Defining Our Own Data Structures

2.6.2 Using the Sales_data Class

Unlike the Sales_item class, our Sales_data class does not provide any operations. Users of Sales_data have to write whatever operations they need. As an example, we’ll write a version of the program from § 1.5.2 (p. 23) that printed the sum of two transactions. The input to our program will be transactions such as

0-201-78345-X 3 20.00
0-201-78345-X 2 25.00

Each transaction holds an ISBN, the count of how many books were sold, and the
price at which each book was sold.

Adding Two Sales_data Objects

Because Sales_data provides no operations, we will have to write our own code to do the input, output, and addition operations. We’ll assume that our Sales_data class is defined inside Sales_data.h. We’ll see how to define this header in § 2.6.3 (p. 76).

Because this programwill be longer than any we’ve written so far, we’ll explain it in separate parts. Overall, our program will have the following structure:

  1. #include <iostream>   
  2. #include <string>   
  3. #include "Sales_data.h"   
  4. int main()   
  5. {   
  6. Sales_data data1, data2;   
  7. // code to read into data1 and data2   
  8. // code to check whether data1 and data2 have the same ISBN   
  9. // and if so print the sum of data1 and data2   
  10. }  

As in our original program, we begin by including the headers we’ll need and define variables to hold the input. Note that unlike the Sales_item version, our new program includes the string header. We need that header because our code will have to manage the bookNo member, which has type string.
Reading Data into a Sales_data Object

Although we won’t describe the library string type in detail until Chapters 3 and 10, we need to know only a little bit about strings in order to define and use our ISBN member. The string type holds a sequence of characters. Its operations include the >>, <<, and == operators to read,write, and compare strings, respectively. With this knowledge we can write the code to read the first transaction:

  1. double price = 0; // price per book, used to calculate total revenue   
  2. // read the first transactions: ISBN, number of books sold, price per book   
  3. std::cin >> data1.bookNo >> data1.units_sold >> price;   
  4. // calculate total revenue from price and units_sold   
  5. data1.revenue = data1.units_sold * price;  

Our transactions contain the price at which each book was sold but our data structure stores the total revenue. We’ll read the transaction data into a double named price, from which we’ll calculate the revenue member. The input statement

  1. std::cin >> data1.bookNo >> data1.units_sold >> price; 

uses the dot operator (§ 1.5.2, p. 23) to read into the bookNo and units_sold members of the object named data1.

The last statement assigns the product of data1.units_sold and price into
the revenue member of data1.

Our program will next repeat the same code to read data into data2:

  1. // read the second transaction   
  2. std::cin >> data2.bookNo >> data2.units_sold >> price;   
  3. data2.revenue = data2.units_sold * price;  

Printing the Sum of Two Sales_data Objects

Our other task is to check that the transactions are for the same ISBN. If so, we’ll print their sum, otherwise, we’ll print an error message:

  1. if (data1.bookNo == data2.bookNo) {   
  2. unsigned totalCnt = data1.units_sold + data2.units_sold;   
  3. double totalRevenue = data1.revenue + data2.revenue;   
  4. // print: ISBN, total sold, total revenue, average price per book   
  5. std::cout << data1.bookNo << " " << totalCnt   
  6. << " " << totalRevenue << " ";   
  7. if (totalCnt != 0)   
  8. std::cout << totalRevenue/totalCnt << std::endl;   
  9. else 
  10. std::cout << "(no sales)" << std::endl;   
  11. return 0; // indicate success   
  12. else { // transactions weren’t for the same ISBN   
  13. std::cerr << "Data must refer to the same ISBN"   
  14. << std::endl;   
  15. return -1; // indicate failure   
  16. }  

In the first if we compare the bookNo members of data1 and data2. If those members are the same ISBN, we execute the code inside the curly braces. That code adds the components of our two variables. Because we’ll need to print the average price, we start by computing the total of units_sold and revenue and store those in totalCnt and totalRevenue, respectively. We print those values. Next we check that there were books sold and, if so, print the computed average price per book. If there were no sales, we print a message noting that fact.

EXERCISES SECTION 2.6.2

Exercise 2.41: Use your Sales_data class to rewrite the exercises in § 1.5.1 (p. 22), § 1.5.2 (p. 24), and § 1.6 (p. 25). For now, you should define your Sales_data class in the same file as your main function.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.5 Dealing with Types (3) 下一篇2.2 Variables (8)

评论

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

·数据库:推荐几款 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)