Skip to content

Commit df2c2e4

Browse files
committed
add ch06
1 parent 548499b commit df2c2e4

9 files changed

Lines changed: 152 additions & 2 deletions

File tree

primer/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
1+
include_directories(${PROJECT_SOURCE_DIR}/primer/include)
22
add_subdirectory(ch01)
33
add_subdirectory(ch02)
44
add_subdirectory(ch03)
5-
include_directories(${PROJECT_SOURCE_DIR}/primer/include)
5+
add_subdirectory(ch06)

primer/ch06/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_executable(exercise6.3 exercise6.3.cpp)
2+
add_executable(exercise6.4 exercise6.4.cpp)
3+
add_executable(exercise6.5 exercise6.5.cpp)
4+
add_executable(exercise6.7 exercise6.7.cpp)
5+
add_executable(exercise6.8 exercise6.8.cpp)
6+
add_executable(exercise6.10 exercise6.10.cpp)

primer/ch06/exercise6.10.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by zing on 5/18/2020.
3+
//
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
void swap(int *a, int *b) {
9+
int c = *a;
10+
*a = *b;
11+
*b = c;
12+
}
13+
14+
int main(int argc, char *argv[]) {
15+
int a = 10;
16+
int b = 11;
17+
cout << "a = " << a << ",b = " << b << endl;
18+
swap(&a, &b);
19+
cout << "a = " << a << ",b = " << b << endl;
20+
return 0;
21+
}

primer/ch06/exercise6.3.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by zing on 5/18/2020.
3+
//
4+
5+
#include <iostream>
6+
using namespace std;
7+
8+
int fact(int val){
9+
int ret = 1;
10+
while (val > 1){
11+
ret *= val--;
12+
}
13+
return ret;
14+
}
15+
16+
int main(int argc, char *argv[]) {
17+
cout << fact(1) << endl;
18+
cout << fact(2) << endl;
19+
cout << fact(3) << endl;
20+
cout << fact(4) << endl;
21+
return 0;
22+
}

primer/ch06/exercise6.4.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Created by zing on 5/18/2020.
3+
//
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int fact(int val) {
10+
int ret = 1;
11+
while (val > 1) {
12+
ret = ret * val;
13+
val--;
14+
}
15+
return ret;
16+
}
17+
18+
int main(int argc, char *argv[]) {
19+
int val = 0;
20+
cout << "please in put a number" << endl;
21+
cin >> val;
22+
cout << fact(val) << endl;
23+
return 0;
24+
}

primer/ch06/exercise6.5.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Created by zing on 5/18/2020.
3+
//
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int abs(int val){
10+
if(val < 0)
11+
return -1 *val;
12+
return val;
13+
}
14+
15+
int main(int argc, char *argv[]) {
16+
cout << abs(1) << endl;
17+
cout << abs(0) << endl;
18+
cout << abs(-1) << endl;
19+
return 0;
20+
}

primer/ch06/exercise6.7.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by zing on 5/18/2020.
3+
//
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
int increasing() {
10+
static int val = 0;
11+
return val++;
12+
}
13+
14+
int main(int argc, char *argv[]) {
15+
16+
cout << increasing() << endl;
17+
cout << increasing() << endl;
18+
cout << increasing() << endl;
19+
cout << increasing() << endl;
20+
cout << increasing() << endl;
21+
return 0;
22+
}

primer/ch06/exercise6.8.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by zing on 5/18/2020.
3+
//
4+
5+
#include <iostream>
6+
#include "exercise6.8.h"
7+
using namespace std;
8+
int main(int argc, char *argv[]) {
9+
print();
10+
print();
11+
cout << fact(5) << endl;
12+
cout << fact(6) << endl;
13+
return 0;
14+
}

primer/ch06/exercise6.8.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by zing on 5/18/2020.
3+
//
4+
5+
#ifndef LEARN_CPP_EXERCISE6_8_H
6+
#define LEARN_CPP_EXERCISE6_8_H
7+
8+
#include <iostream>
9+
using namespace std;
10+
void print(){
11+
cout << "hello world"<< endl;
12+
}
13+
int fact(int val){
14+
int ret = 1;
15+
while (val > 1){
16+
ret *= val--;
17+
}
18+
return ret;
19+
}
20+
21+
#endif //LEARN_CPP_EXERCISE6_8_H

0 commit comments

Comments
 (0)