forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.1.cpp
148 lines (137 loc) · 3.46 KB
/
3.1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;
using std::cerr;
void ex1_9() {
int sum = 0, val = 50;
while (val <= 100) {
sum += val;
++val;
}
cout << "Sum of 50 to 100 inclusive is " << sum << endl;
}
void ex1_10() {
int val = 10;
while (val >= 0)
cout << val-- << " ";
cout << endl;
}
void ex1_11() {
int bg = 0, ed = 0;
cout << "Enter the number of begin and end: ";
cin >> bg >> ed;
while (bg <= ed)
cout << bg++ << " ";
cout << endl;
}
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
void ex1_20() {
Sales_data item;
double price = 0;
while(cin >> item.bookNo >> item.units_sold >> price) {
item.revenue = item.units_sold * price;
cout << item.bookNo << " " << item.units_sold << " " << item.revenue << endl;
}
}
int ex1_21(){
Sales_data d1, d2;
double price = 0;
if (cin >> d1.bookNo >> d1.units_sold >> price) {
d1.revenue = d1.units_sold * price;
if (cin >> d2.bookNo >> d2.units_sold >> price) {
d2.revenue = d2.units_sold * price;
if (d1.bookNo == d2.bookNo) {
unsigned totalSold = d1.units_sold + d2.units_sold;
double totalRevenue = d1.revenue + d2.revenue;
cout << d1.bookNo << " " << totalSold << " " << totalRevenue << " ";
if (totalSold != 0)
cout << totalRevenue / totalSold << endl;
else
cout << "(no sales)" << endl;
} else {
cerr << "Data must refer to the same ISBN!" << endl;
return -1;
}
} else {
cerr << "No data for item two!" << endl;
return -1;
}
} else {
cerr << "No data for item one!" << endl;
return -1;
}
return 0;
}
int ex1_22(){
Sales_data total, d;
double price = 0;
if (cin >> total.bookNo >> total.units_sold >> price) {
total.revenue = total.units_sold * price;
while (cin >> d.bookNo >> d.units_sold >> price) {
d.revenue = d.units_sold * price;
if (total.bookNo == d.bookNo) {
total.units_sold += d.units_sold;
total.revenue += d.revenue;
} else {
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
cout << total.revenue / total.units_sold << endl;
else
cout << "(no sales)" << endl;
total.bookNo = d.bookNo;
total.units_sold = d.units_sold;
total.revenue = d.revenue;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
if (total.units_sold != 0)
cout << total.revenue / total.units_sold << endl;
else
cout << "(no sales)" << endl;
} else {
cerr << "No data for item!" << endl;
return -1;
}
return 0;
}
int ex1_23(){
Sales_data total, d;
double price = 0;
if (cin >> total.bookNo >> total.units_sold >> price) {
int cnt = 1;
while (cin >> d.bookNo >> d.units_sold >> price) {
if (total.bookNo == d.bookNo) {
++cnt;
} else {
cout << total.bookNo << " " << cnt << endl;
total.bookNo = d.bookNo;
total.units_sold = d.units_sold;
cnt = 1;
}
}
cout << total.bookNo << " " << cnt << endl;
} else {
cerr << "No data for item!" << endl;
return -1;
}
return 0;
}
void ex2_41() {
//ex1_20();
//ex1_21();
//ex1_22(); // Also for ex1.25
//ex1_23(); // Also for ex1.24
}
int main() {
//ex1_9();
//ex1_10();
//ex1_11();
//ex2_41();
return 0;
}