File tree 5 files changed +130
-0
lines changed
5 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
1
+ // if(read(read(cin, data1), data2))//to judge whether read function get two input
Original file line number Diff line number Diff line change
1
+ #ifndef EX7_6_H
2
+ #define EX7_6_H
3
+
4
+ #include < string>
5
+ #include < iostream>
6
+
7
+ using std::string;
8
+
9
+ struct Sales_data
10
+ {
11
+ Sales_data &combine (const Sales_data &);
12
+ string isbn () const ;
13
+
14
+ string bookNo;
15
+ unsigned units_sold = 0 ;
16
+ double revenue = 0.0 ;
17
+ };
18
+
19
+ string Sales_data::isbn () const
20
+ {
21
+ return bookNo;
22
+ }
23
+
24
+ Sales_data &Sales_data::combine (const Sales_data &trans)
25
+ {
26
+ units_sold +=trans.units_sold ;
27
+ revenue += trans.revenue ;
28
+ return *this ;
29
+ }
30
+
31
+ Sales_data add (const Sales_data &lhs, const Sales_data &rhs)
32
+ {
33
+ Sales_data sum = lhs;
34
+ sum.combine (rhs);
35
+ return sum;
36
+ }
37
+
38
+ std::istream &read (std::istream &is, Sales_data &item)
39
+ {
40
+ double price = 0 ;
41
+ is >> item.bookNo >> item.units_sold >> price;
42
+ item.revenue = price * item.units_sold ;
43
+ return is;
44
+ }
45
+
46
+ std::ostream &print (std::ostream &os, const Sales_data &item)
47
+ {
48
+ os << item.isbn () << " " << item.units_sold << " " << item.revenue ;
49
+ return os;
50
+ }
51
+
52
+ #endif
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string>
3
+ #include " 7_6.h"
4
+
5
+ using std::string;
6
+ using std::cin; using std::cout; using std::endl; using std::cerr;
7
+
8
+ int main ()
9
+ {
10
+ Sales_data total;
11
+ if (read (cin, total))
12
+ {
13
+ Sales_data trans;
14
+ while (read (cin, trans))
15
+ {
16
+ if (total.isbn () == trans.isbn ())
17
+ {
18
+ total.combine (trans);
19
+ }
20
+ else
21
+ {
22
+ print (cout, total) << endl;
23
+ total = trans;
24
+ }
25
+ }
26
+ print (cout, total) << endl;
27
+ }
28
+ else
29
+ {
30
+ cerr << " No data?!" << endl;
31
+ }
32
+
33
+ return 0 ;
34
+ }
Original file line number Diff line number Diff line change
1
+ // std::istream &read(std::istream &is, Sales_data &item)//because read change the Sales_data object item
2
+ // {
3
+ // double price = 0;
4
+ // is >> item.bookNo >> item.units_sold >> price;
5
+ // item.revenue = price * item.units_sold;
6
+ // return is;
7
+ // }
8
+
9
+ // std::ostream &print(std::ostream &os, const Sales_data &item)//print does not change the Sales_data object item
10
+ // {
11
+ // os << item.isbn() << " " << item.units_sold << " " << item.revenue;
12
+ // return os;
13
+ // }
Original file line number Diff line number Diff line change
1
+ #ifndef EX7_9_H
2
+ #define EX7_9_H
3
+
4
+ #include < string>
5
+ #include < iostream>
6
+
7
+ using std::string;
8
+
9
+ struct Person
10
+ {
11
+ string getName () const { return name; }
12
+ string getAddress () const { return address; }
13
+
14
+ string name;
15
+ string address;
16
+ };
17
+
18
+ std::istream &read (std::istream &is, Person &item)
19
+ {
20
+ is >> item.name >> item.address ;
21
+ return is;
22
+ }
23
+
24
+ std::ostream &print (std::ostream &os, const Person &item)
25
+ {
26
+ os >> item.getName () >> item.getAddress ();
27
+ return os;
28
+ }
29
+
30
+ #endif
You can’t perform that action at this time.
0 commit comments