我是靠谱客的博主 醉熏香菇,这篇文章主要介绍c++primer第五版课后练习答案(第六章),现在分享给大家,希望可以做个参考。

chapter6_6.3

复制代码
1
2
3
4
5
6
7
int fact(int val) { int ret = 1; while (val > 1) ret *= val--; return ret; }

chapter6_6.4

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h" #include <iostream> using namespace std; int fact(int val) { int ret = 1; while (val > 1) ret *= val--; return ret; } int _tmain(int argc, _TCHAR* argv[]) { int i; cout << "请输入一个数:" << endl << "i="; cin >> i; cout << i<<"的阶乘="<<fact(i) << endl; return 0; }


chapter6_6.5

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h" #include <iostream> using namespace std; template <class T> T fabs(T x) { if (x < 0) return -x; else return x; } int _tmain(int argc, _TCHAR* argv[]) { cout << fabs(-3.14) << endl; return 0; }


chapter6_6.7
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h" #include <iostream> using namespace std; int call() { static int i = 0; return i++; } int _tmain(int argc, _TCHAR* argv[]) { for (int i = 0; i < 10;i++) cout<<call()<<endl; return 0; }


chapter6_6.8

head.h头文件

复制代码
1
int fact(int val);


chapter6_6.9

head.h头文件

复制代码
1
int fact(int val);

fact.cpp

复制代码
1
2
3
4
5
6
7
8
9
#include "stdafx.h" #include "head.h" int fact(int val) { int ret = 1; while (val > 1) ret *= val--; return ret; }

factMain.cc

复制代码
1
2
3
4
5
6
7
8
9
#include "stdafx.h" #include "head.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout << fact(3) << endl; return 0; }

chapter6_6.10

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h" #include <iostream> using namespace std; void change(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int _tmain(int argc, _TCHAR* argv[]) { int a = 1; int b = 2; change(&a, &b); cout << a << endl << b << endl; return 0; }

chapter6_6.12

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h" #include <iostream> using namespace std; void change(int &x, int &y) { int temp; temp = x; x = y; y = temp; } int _tmain(int argc, _TCHAR* argv[]) { int a = 1; int b = 2; change(a, b); cout << a << endl << b << endl; return 0; }


chapter6_6.17

复制代码
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
#include "stdafx.h" #include <string> #include <iostream> using namespace std; bool isSupper(const string &s) { int flag = 0; for (auto c : s) { if (c > 'A'&&c < 'Z') { flag = 1; return true; } else { continue; } } if (flag) return true; else return false; } string toupper1(string &s) { for (auto &c:s) { c=toupper(c); } return s; } string toupper2(string &s) { int i = 0; for (; i < s.size()-1;i++) if (s[i]>='a'&&s[i]<='z') { s[i] = s[i] - 32; } return s; } int _tmain(int argc, _TCHAR* argv[]) { string str = "my name is Xj"; cout << isSupper(str) << endl; cout<<toupper2(str)<<endl; return 0; }

chapter6_6.21

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h" #include <iostream> using namespace std; int compare(int i, int *j) { if (i > (*j)) return i; else return *j; } int _tmain(int argc, _TCHAR* argv[]) { int x = 10; int y = 20; int *pt = &y; cout << compare(x, pt)<<endl; return 0; }


chapter6_6.25

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h" #include <string> #include <iostream> using namespace std; int main(int argc, char* argv[]) { if (argc > 2) { int i = 2; string str = argv[1]; while (i <argc) { str = str+argv[i]; i++; } cout << str << endl; } else { cout << "error" << endl; } return 0; }
将编译后Debug文件中的ConsoleApplication2_6.25.exe文件拷贝到C:UsersAdministrator目录下,在dos下执行程序,输入第二参数为hello word,结果显示如下:

最后

以上就是醉熏香菇最近收集整理的关于c++primer第五版课后练习答案(第六章)的全部内容,更多相关c++primer第五版课后练习答案(第六章)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部