-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSales_data_exercises.cpp
56 lines (51 loc) · 1.76 KB
/
Sales_data_exercises.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
#include <iostream>
#include <algorithm>
#include <vector>
#include "Sales_data.h"
void task20(), task21(), task22(), task23();
int main(){
// test functions
//task20();
//task21();
//task22();
task23();
}
// 1.20: reads a set of book sales, writing each transaction to the standard output
void task20(){
Sales_data currentSale;
if (read(currentSale))
print(currentSale);
}
// 1.21: reads two Sales_data objects that have the same ISBN and produce their sum
void task21(){
Sales_data data1, data2;
if (read(data1) && read(data2))
print(sum(data1, data2));
}
// 1.22: Reads several transactions for the same ISBN and prints the sum of the result
void task22(){
Sales_data totalSale, currentSale;
read(totalSale); // first transaction
while(read(currentSale) && totalSale.bookNo == currentSale.bookNo){
totalSale = sum(totalSale,currentSale);
}
std::cout << "End of current transaction" << std::endl;
print(totalSale);
}
//Reads several transactions, counts and outputs how many sales for each ISBN
void task23(){
// input Sales_data transactions
std::vector<Sales_data> transactions;
Sales_data currentSale;
while(read(currentSale))
transactions.push_back(currentSale);
// Counts how many transactions for each ISBN
for (auto it = transactions.begin(), it_end = transactions.end(); it < it_end; ++it){ // for each ISBN
unsigned currentAmount = it->units_sold;
it_end = remove_if(it+1, it_end, // shifts all elements with the same ISBN to end
[&it, ¤tAmount](Sales_data test){ // lambda: check ISBN, count sales
return (test.bookNo == it->bookNo) ? currentAmount += test.units_sold, true : false;
});
std::cout << it->bookNo << " "<< currentAmount << std::endl; // output sum for ISBN
}
}