Skip to content

Commit 72db7d8

Browse files
committed
add ch03
1 parent 5746ccb commit 72db7d8

7 files changed

Lines changed: 177 additions & 0 deletions

File tree

‎primer/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

22
add_subdirectory(ch01)
33
add_subdirectory(ch02)
4+
add_subdirectory(ch03)
45
include_directories(${PROJECT_SOURCE_DIR}/primer/include)

‎primer/ch03/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include_directories(${PROJECT_SOURCE_DIR}/primer/include)
2+
add_executable(exercise_3_1_a exercise_3_1_a.cpp)
3+
add_executable(exercise_3_1_b exercise_3_1_b.cpp)
4+
add_executable(exercise3_2_a exercise3_2_a.cpp)
5+
add_executable(exercise3_2_b exercise3_2_b.cpp)

‎primer/ch03/exercise3_2_a.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Created by zing on 4/28/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <string>
7+
8+
using std::string;
9+
using std::cin;
10+
using std::cout;
11+
using std::endl;
12+
using std::getline;
13+
14+
int main()
15+
{
16+
string s;
17+
while (getline(cin,s))
18+
{
19+
if (s == "q"){
20+
break;
21+
}
22+
cout << s << endl;
23+
}
24+
return 0;
25+
}

‎primer/ch03/exercise3_2_b.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Created by zing on 4/28/2020.
3+
//
4+
5+
6+
#include <iostream>
7+
#include <string>
8+
9+
using std::string;
10+
using std::cin;
11+
using std::cout;
12+
using std::endl;
13+
14+
int main() {
15+
string s;
16+
while (cin >> s) {
17+
if(s == "q"){
18+
break;
19+
}
20+
cout << s << endl;
21+
}
22+
return 0;
23+
}

‎primer/ch03/exercise_3_1_a.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Created by zing on 4/28/2020.
3+
//
4+
5+
#include <iostream>
6+
7+
using std::cin;
8+
using std::cout;
9+
using std::endl;
10+
11+
int main()
12+
{
13+
int sum = 0;
14+
for (int val = 1; val <= 10; ++val) sum += val;
15+
16+
cout << "Sum of 1 to 10 inclusive is " << sum << endl;
17+
18+
return 0;
19+
}

‎primer/ch03/exercise_3_1_b.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Created by zing on 4/28/2020.
3+
//
4+
5+
6+
#include <iostream>
7+
#include <string>
8+
#include "exercise2_42.h"
9+
10+
using std::cin;
11+
using std::cout;
12+
using std::endl;
13+
using std::cerr;
14+
15+
int main() {
16+
Sales_data data1, data2;
17+
18+
double price = 0;
19+
20+
cin >> data1.bookNo >> data1.units_sold >> price;
21+
22+
data1.revenue = data1.units_sold * price;
23+
cout << " >1 "<< data1.bookNo << data1.units_sold << endl;
24+
25+
cin >> data2.bookNo >> data2.units_sold >> price;
26+
cout << " >2 " << data2.bookNo << data2.units_sold << endl;
27+
28+
data2.revenue = data2.units_sold * price;
29+
if (data1.bookNo == data2.bookNo) {
30+
unsigned totalCnt = data1.units_sold + data2.units_sold;
31+
double totalRevenue = data1.revenue + data2.revenue;
32+
33+
cout << data1.bookNo << " " << totalCnt
34+
<< " " << totalRevenue << " ";
35+
if (totalCnt != 0)
36+
cout << totalRevenue / totalCnt << endl;
37+
else
38+
cout << "(no sales)" << endl;
39+
40+
return 0;
41+
} else {
42+
cerr << "Data must refer to the same ISBN" << endl;
43+
return -1;
44+
}
45+
}

‎primer/include/exercise2_42.h‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Created by zing on 4/28/2020.
3+
//
4+
5+
#ifndef LEARN_CPP_EXERCISE2_42_H
6+
#define LEARN_CPP_EXERCISE2_42_H
7+
#include <string>
8+
#include <iostream>
9+
10+
struct Sales_data
11+
{
12+
std::string bookNo;
13+
unsigned units_sold = 0;
14+
double revenue = 0.0;
15+
16+
void CalcRevenue(double price);
17+
double CalcAveragePrice();
18+
void SetData(Sales_data data);
19+
void AddData(Sales_data data);
20+
void Print();
21+
};
22+
23+
void Sales_data::CalcRevenue(double price)
24+
{
25+
revenue = units_sold * price;
26+
}
27+
28+
void Sales_data::SetData(Sales_data data)
29+
{
30+
bookNo = data.bookNo;
31+
units_sold = data.units_sold;
32+
revenue = data.revenue;
33+
}
34+
35+
void Sales_data::AddData(Sales_data data)
36+
{
37+
if (bookNo != data.bookNo) return;
38+
units_sold += data.units_sold;
39+
revenue += data.revenue;
40+
}
41+
42+
double Sales_data::CalcAveragePrice()
43+
{
44+
if (units_sold != 0)
45+
return revenue / units_sold;
46+
else
47+
return 0.0;
48+
}
49+
50+
void Sales_data::Print()
51+
{
52+
std::cout << bookNo << " " << units_sold << " " << revenue << " ";
53+
double averagePrice = CalcAveragePrice();
54+
if (averagePrice != 0.0)
55+
std::cout << averagePrice << std::endl;
56+
else
57+
std::cout << "(no sales)" << std::endl;
58+
}
59+
#endif //LEARN_CPP_EXERCISE2_42_H

0 commit comments

Comments
 (0)