【C++ Primer Plus】编程练习答案——第5章
1 void ch5_1() {
2 using namespace std;
3 int small, big, sum{0};
4 cout << "enter small and big: " << endl;
5 cout << "small: "; cin >> small;
6 cout << "big: "; cin >> big;
7 for (int i = small; i <= big; ++ i)
8 sum += i;
9 cout << "sum between " << small << "and " << big << ": " << sum << endl;
10 }
11
12 void ch5_2() {
13 using namespace std;
14 const int ArrSize = 101;
15 array<long double, ArrSize> factorials;
16 factorials[0] = factorials[1] = 1;
17 for (int i = 2; i < ArrSize; ++ i)
18 factorials[i] = factorials[i-1] * i;
19 for (int i = 0; i < ArrSize; ++ i)
20 cout << i << "! = " << factorials[i] << endl;
21 }
22
23 void ch5_3() {
24 using namespace std;
25 int num, sum{0};
26 cout << "enter a num (quit by 0): ";
27 cin >> num;
28 while (num) {
29 sum += num;
30 cout << "sum == " << sum << ", next num: ";
31 cin >> num;
32 }
33 }
34
35 void ch5_4() {
36 using namespace std;
37 double D_money{100}, C_money{100}, sin_factor{0.1}, mul_factor{0.05};
38 int year_count = 1;
39 while (C_money <= D_money) {
40 D_money += 100 * sin_factor;
41 C_money += C_money * mul_factor;
42 cout << "year " << year_count << ": C: " << C_money << " D:" << D_money << endl;
43 ++ year_count;
44 }
45 }
46
47 void ch5_5() {
48 using namespace std;
49 const char * MONTHSNAME[12] = {
50 "January", "February", "March",
51 "April", "May", "June",
52 "July", "August", "September",
53 "October", "November", "December"
54 };
55 int sales[12]{0}, sum{0};
56 for (int i = 0; i < 12; ++ i) {
57 cout << "enter sales in " << MONTHSNAME[i] << ":";
58 cin >> sales[i];
59 sum += sales[i];
60 }
61 cout << "all sales: " << sum;
62 }
63
64 void ch5_6() {
65 using namespace std;
66 const char * MONTHSNAME[12] = {
67 "January", "February", "March",
68 "April", "May", "June",
69 "July", "August", "September",
70 "October", "November", "December"
71 };
72 int sales[3][12]{0}, sum{0};
73 for (int i = 0; i < 3; ++ i) {
74 cout << "enter sales in year " << i + 1 << endl;
75 for (int j = 0; j < 12; ++ j) {
76 cout << MONTHSNAME[j] << ":";
77 cin >> sales[i][j];
78 sum += sales[i][j];
79 }
80 }
81 cout << "all sales: " << sum;
82 }
83
84 void ch5_7() {
85 using namespace std;
86 struct Car{
87 string brand;
88 unsigned int year;
89 };
90 Car * car_arr;
91 unsigned int num{0};
92 cout << "how many cars do you wish to catalog? ";
93 cin >> num; cin.get();
94 car_arr = new Car[num];
95 for (int i = 0; i < num; ++ i) {
96 cout << "Car# " << i + 1 << ":" << endl;
97 cout << "enter brand: ";
98 getline(cin, car_arr[i].brand);
99 cout << "enter year: ";
100 cin >> car_arr[i].year; cin.get();
101 }
102 cout << "here's your collection: " << endl;
103 for (int i = 0; i < num ;++ i)
104 cout << car_arr[i].year << " " << car_arr[i].brand << endl;
105 }
106
107 void ch5_8() {
108 using namespace std;
109 char word[100];
110 unsigned int count{0};
111 cout << "Enter words (to stop, type the word done):" << endl;
112 cin >> word;
113 while (strcmp(word, "done") != 0) {
114 ++ count;
115 cin.get();
116 cin >> word;
117 }
118 cout << "You entered a total of " << count << " words.";
119 }
120
121 void ch5_9() {
122 using namespace std;
123 string word;
124 unsigned int count{0};
125 cout << "Enter words (to stop, type the word done):" << endl;
126 cin >> word;
127 while (word != "done") {
128 ++ count;
129 cin.get();
130 cin >> word;
131 }
132 cout << "You entered a total of " << count << " words.";
133 }
134
135 void ch5_10() {
136 using namespace std;
137 unsigned int rows{0};
138 cout << "enter number of rows: ";
139 cin >> rows;
140 for (int i = 0; i < rows; ++ i) {
141 for (int j = 0; j < rows - i - 1; ++ j)
142 cout << '.';
143 for (int k = 0; k < i + 1; ++ k)
144 cout << '*';
145 cout << endl;
146 }
147 }
【C++ Primer Plus】编程练习答案——第5章的更多相关文章
- 【C++ Primer Plus】编程练习答案——第12章
1 // chapter12_1_cow.h 2 3 4 #ifndef LEARN_CPP_CHAPTER12_1_COW_H 5 #define LEARN_CPP_CHAPTER12_1_COW ...
- 【C++ Primer Plus】编程练习答案——第11章 (待更新)
最近开学,事情较多,过两天更新...
- 【C++ Primer Plus】编程练习答案——第10章
1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10 ...
- 【C++ Primer Plus】编程练习答案——第9章
1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 5 ...
- 【C++ Primer Plus】编程练习答案——第8章
1 void ch8_1_print(const std::string & str, int n = 0 ) { 2 using namespace std; 3 static int fl ...
- 【C++ Primer Plus】编程练习答案——第7章
1 double ch7_1_harmonicaverage(double a, double b) { 2 return 2 / (1 / a + 1 / b); 3 } 4 5 void ch7_ ...
- 【C++ Primer Plus】编程练习答案——第6章
1 void ch6_1() { 2 using namespace std; 3 char ch; 4 while ((ch = cin.get()) != '@') { 5 if (isdigit ...
- 【C++ Primer Plus】编程练习答案——第4章
1 void ch4_1() { 2 using namespace std; 3 string fname, lname; 4 char grade; 5 unsigned int age; 6 c ...
- 【C++ Primer Plus】编程练习答案——第3章
1 void ch3_1() { 2 using namespace std; 3 unsigned int factor = 12; 4 unsigned int inch, feet; 5 cou ...
随机推荐
- 【ArcGIS】 设置管段的流向
在排水管网或者燃气管网中对管段进行几何网络分析,常常用到设置管段流向,一般有三种方法: 1,有流向字段的,直接进行唯一值渲染, 2,没有流向字段的需要建立几何网络, 2.1 在几何网络存在的情况下,设 ...
- jquery validate 验证插件 解决多个相同的Name 只验证第一个的方案
方案一:如果 项目里不是只是个别页面 有多个name 验证, 那么利用 prototype 来写,把这段代码加在你所要使用多个name的页面 的js初始化里 即可 if ($.validator) ...
- Flink 运行时架构
参考链接:https://blog.csdn.net/dajiangtai007/article/details/88575553 1.Flink 运行时架构 Flink 运行时架构主要包含几个部分: ...
- tf.app.run() 运行结束时,报错:SystemExit exception: no description
环境:Python3.6.6 + tensorflow-gpu 源码如下: import tensorflow as tf def main(): print("hello tf.app.r ...
- 并发编程之:Lock
大家好,我是小黑,一个在互联网苟且偷生的农民工. 在之前的文章中,为了保证在并发情况下多线程共享数据的线程安全,我们会使用synchronized关键字来修饰方法或者代码块,以及在生产者消费者模式中同 ...
- [bug]spring项目通过反射测试私有方法时,注入对象异常
背景 遇到问题:在进行Spring单元测试编写时,发现被测方法是一个私有方法,无法直接通过注入对象调用 解决思路:首先想到通过反射获取该私有方法的访问权限,并传入注入对象,最终调用对象的私有方法. 出 ...
- multipass指定virualbox搭建k8s集群(选择docker作为默认容器)
目录 前言 步骤 初始化三台虚拟机 统一安装docker 修改docker镜像源 查看masterIP 安装master节点(重点设置) 查看master的token 安装worker节点 测试 部署 ...
- [考试总结]noip模拟43
这个题目出的还是很偷懒.... 第一题...第二题...第三题...四.... 好吧... 这几次考得都有些问题,似乎可能是有些疲惫,脑袋也是转不太动,考完总觉得自己是能力的问题,但是改一分钟之后会发 ...
- pluto中监听各个网口的500端口处理逻辑
1. pluto中监听各个网口的500端口处理逻辑 whack_handle() find_ifaces() find_raw_ifaces4() socket.setsockopt.bind.ioc ...
- 异步处理方式之信号(三):kill、raise、alarm、pause函数简介
文章目录 6. 函数kill和raise 7. 函数alarm和pause 7.1 alarm() 7.2 pause() 6. 函数kill和raise kill函数用来将信号发送给进程或者进程组. ...