1 void ch6_1() {
2 using namespace std;
3 char ch;
4 while ((ch = cin.get()) != '@') {
5 if (isdigit(ch))
6 continue;
7 else if (islower(ch))
8 ch = toupper(ch);
9 else if (isupper(ch))
10 ch = tolower(ch);
11 cout << ch;
12 }
13 }
14
15 void ch6_2() {
16 using namespace std;
17 const unsigned int ArrSize = 10;
18 double donations[ArrSize]{0};
19 double average{0};
20 unsigned int overcount{0};
21 cout << "enter 10 donations in double: " << endl;
22 for (int i = 0; i < ArrSize; ++ i) {
23 while (!(cin >> donations[i])) {
24 cin.clear();
25 while (cin.get() != '\n')
26 continue;
27 cout << "must enter a double: ";
28 }
29 }
30 for (int i = 0; i < ArrSize; ++ i)
31 average += donations[i];
32 average /= ArrSize;
33 for (int i = 0; i < ArrSize; ++ i)
34 if (donations[i] > average)
35 ++ overcount;
36 cout << "average donation: " << average << endl;
37 cout << overcount << " donations above average" << endl;
38 }
39
40 void ch6_3() {
41 using namespace std;
42 char ch;
43 cout << "Please enter one of the following choices: " << endl;
44 cout << "c) carnivore \t p) pianist" << endl;
45 cout << "t) tree \t g) game" << endl;
46 bool loop = true;
47 while (loop) {
48 cin >> ch;
49 switch (ch) {
50 case 'c':
51 cout << "carnivore" << endl;
52 loop = false;
53 break;
54 case 'p':
55 cout << "pianist" << endl;
56 loop = false;
57 break;
58 case 't':
59 cout << "tree" << endl;
60 loop = false;
61 break;
62 case 'g':
63 cout << "game" << endl;
64 loop = false;
65 break;
66 default:
67 cout << "Please enter a c, p, t, or g: ";
68 break;
69 }
70 }
71 }
72
73 void ch6_4() {
74 using namespace std;
75 const int strsize = 100;
76 struct bop {
77 char fullname[strsize];
78 char title[strsize];
79 char bopname[strsize];
80 int preference; // 0 = fullname, 1 = title, 2 = bopname
81 };
82 const int ArSize = 3;
83 bop member[ArSize] = {
84 {"fullname1", "title1", "bopname1", 3},
85 {"fullname2", "title2", "bopname2", 2},
86 {"fullname3", "title3", "bopname3", 1}
87 };
88 bool loop = true;
89 char ch;
90 cout << "Benevolent Order of Programmers Report" << endl
91 << "a. display by name \t b. display by title" << endl
92 << "c. display by bopname \t b. display by preference" << endl
93 << "q. quit" << endl;
94 while (loop) {
95 cout << "Enter your choice: ";
96 cin >> ch;
97 switch (ch) {
98 case 'a':
99 for (int i = 0; i < ArSize; ++ i)
100 cout << member[i].fullname << endl;
101 break;
102 case 'b':
103 for (int i = 0; i < ArSize; ++ i)
104 cout << member[i].title << endl;
105 break;
106 case 'c':
107 for (int i = 0; i < ArSize; ++ i)
108 cout << member[i].bopname << endl;
109 break;
110 case 'd':
111 for (int i = 0; i < ArSize; ++ i) {
112 if (member[i].preference == 1)
113 cout << member[i].fullname << endl;
114 else if (member[i].preference == 2)
115 cout << member[i].title << endl;
116 else
117 cout << member[i].bopname << endl;
118 }
119 break;
120 case 'q':
121 loop = false;
122 default:
123 cout << "enter a, b, c, d, or q: ";
124 break;
125 }
126 }
127 }
128
129 void ch6_5() {
130 using namespace std;
131 unsigned int salary{0};
132 double tax{0};
133 while (true) {
134 cout << "enter your salary: ";
135 if (!(cin >> salary)) {
136 cout << "invalid input!" << endl;
137 break;
138 }
139 if (salary <= 5000)
140 tax = 0;
141 else if (salary > 5000 && salary <= 15000)
142 tax = (salary - 5000) * 0.1;
143 else if (salary > 15000 && salary <= 35000)
144 tax = 10000 * 0.1 + (salary - 15000) * 0.15;
145 else
146 tax = 10000 * 0.1 + 20000 * 0.15 + (salary - 35000) * 0.2;
147 cout << "tax: " << tax << endl;
148 }
149
150 }
151
152 void ch6_6() {
153 using namespace std;
154 struct donator {
155 string name;
156 double amount;
157 };
158 unsigned int donum{0};
159 cout << "enter number of donators: ";
160 while (!(cin >> donum)) {
161 cin.clear();
162 while (cin.get() != '\n')
163 continue;
164 cout << "enter a number: ";
165 }
166 cin.get();
167 donator * donator_arr = new donator[donum];
168 cout << "enter name and amount for each donator" << endl;
169 for (int i = 0; i < donum; ++ i) {
170 cout << "#" << i + 1 << " name: ";
171 getline(cin, donator_arr[i].name);
172 cout << "#" << i + 1 << " amount: ";
173 while (!(cin >> donator_arr[i].amount)) {
174 cin.clear();
175 while (cin.get() != '\n')
176 continue;
177 cout << "enter a number: ";
178 }
179 cin.get();
180 }
181 bool grand{false}, normal{false};
182 cout << endl << "Grand Patrons: " << endl;
183 for (int i = 0; i < donum; ++ i)
184 if (donator_arr[i].amount >= 10000) {
185 grand = true;
186 cout << donator_arr[i].name << ": " << donator_arr[i].amount << endl;
187 }
188 if (!grand)
189 cout << "None!" << endl;
190 cout << endl << "Other Patrons: " << endl;
191 for (int i = 0; i < donum; ++ i)
192 if (donator_arr[i].amount < 10000) {
193 normal = true;
194 cout << donator_arr[i].name << ": " << donator_arr[i].amount << endl;
195 }
196 if (!normal)
197 cout << "None!" << endl;
198 }
199
200 void ch6_7() {
201 using namespace std;
202 string word;
203 int vowels{0}, consonant{0}, other{0};
204 cout << "Enter words (q to quit): " << endl;
205 cin >> word;
206 while (word != "q") {
207 if (isalpha(word[0])) {
208 switch (word[0]) {
209 case 'a':
210 case 'e':
211 case 'i':
212 case 'o':
213 case 'u':
214 ++ vowels;
215 break;
216 default:
217 ++ consonant;
218 break;
219 }
220 }
221 else
222 ++ other;
223 cin >> word;
224 }
225 cout << vowels << " words beginning with vowels" << endl
226 << consonant << " words beginning with consonants" << endl
227 << other << " others" << endl;
228 }
229
230 void ch6_8() {
231 using namespace std;
232 const string FILENAME = "../C++PrimerPlus/testfiles/test.txt";
233 ifstream InFile;
234 InFile.open(FILENAME);
235 if (!InFile.is_open()) {
236 cout << "file not found" << endl;
237 return;
238 }
239 unsigned int count{0};
240 char ch;
241 while ((ch = InFile.get()) != EOF)
242 ++ count;
243 InFile.close();
244 cout << count << " chars in this file" << endl;
245 }
246
247 void ch6_9() {
248 using namespace std;
249 struct donator {
250 string name;
251 double amount;
252 };
253 const string FILENAME = "../C++PrimerPlus/testfiles/patrons.txt";
254 ifstream InFile;
255 InFile.open(FILENAME);
256 if (!InFile.is_open()) {
257 cout << "file not found" << endl;
258 return;
259 }
260 unsigned int donum{0};
261 InFile >> donum; InFile.get();
262 donator * donator_arr = new donator[donum];
263 for (int i = 0; i < donum; ++ i) {
264 getline(InFile, donator_arr[i].name);
265 InFile >> donator_arr[i].amount;
266 InFile.get();
267 }
268 InFile.close();
269 bool grand{false}, normal{false};
270 cout << endl << "Grand Patrons: " << endl;
271 for (int i = 0; i < donum; ++ i)
272 if (donator_arr[i].amount >= 10000) {
273 grand = true;
274 cout << donator_arr[i].name << ": " << donator_arr[i].amount << endl;
275 }
276 if (!grand)
277 cout << "None!" << endl;
278 cout << endl << "Other Patrons: " << endl;
279 for (int i = 0; i < donum; ++ i)
280 if (donator_arr[i].amount < 10000) {
281 normal = true;
282 cout << donator_arr[i].name << ": " << donator_arr[i].amount << endl;
283 }
284 if (!normal)
285 cout << "None!" << endl;
286 }

【C++ Primer Plus】编程练习答案——第6章的更多相关文章

  1. 【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 ...

  2. 【C++ Primer Plus】编程练习答案——第11章 (待更新)

    最近开学,事情较多,过两天更新...

  3. 【C++ Primer Plus】编程练习答案——第10章

    1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10 ...

  4. 【C++ Primer Plus】编程练习答案——第9章

    1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 5 ...

  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 ...

  6. 【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_ ...

  7. 【C++ Primer Plus】编程练习答案——第5章

    1 void ch5_1() { 2 using namespace std; 3 int small, big, sum{0}; 4 cout << "enter small ...

  8. 【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 ...

  9. 【C++ Primer Plus】编程练习答案——第3章

    1 void ch3_1() { 2 using namespace std; 3 unsigned int factor = 12; 4 unsigned int inch, feet; 5 cou ...

随机推荐

  1. spring boot集成pagehelper(两种方式)

    当spring boot集成好mybatis时候需要进行分页,我们首先添加maven支持 <dependency> <groupId>com.github.pagehelper ...

  2. JDBC中级篇——批处理和PreparedStatement对有sql缓冲区的数据库的友好,测试

    注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package a_batch; import util.JdbcUtil; import java.sql.Connection; ...

  3. Vue.JS快速上手(组件间的通信)

    前言 Vue采用的是组件化思想,那么这些组件间是如何通信的呢?下面详细介绍一下. 所谓组件间通信,不单单是我们字面上理解的相互传递数据,这里还包括一个组件访问另一个组件的实例方法等,如父组件通过ref ...

  4. linux安装mysql80

    打开网址:https://dev.mysql.com/downloads/repo/yum/,选择对应li 安装mysql源 yum -y localinstall mysql80-community ...

  5. springboot邮通知553错误和

    com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal authorized user ; nested excepti ...

  6. 云原生 AI 前沿:Kubeflow Training Operator 统一云上 AI 训练

    分布式训练与 Kubeflow 当开发者想要讲深度学习的分布式训练搬上 Kubernetes 集群时,首先想到的往往就是 Kubeflow 社区中形形色色的 operators,如 tf-operat ...

  7. Docker下制作一个容器镜像

    操作过程描述: (1)先基于centos的镜像启动一个centos容器 (2)在这个容器中安装nginx (3)然后把这个已经安装了nginx的容器制作成一个docker的镜像 操作:docker c ...

  8. Redis的读写分离

    1.概述 随着企业业务的不断扩大,请求的并发量不断增长,Redis可能终会出现无法负载的情况,此时我们就需要想办法去提升Redis的负载能力. 读写分离(主从复制)是一个比较简单的扩展方案,使用多台机 ...

  9. 【Python从入门到精通】(二十五)Python多进程的使用

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本篇重点介绍Python多进程的使用,读者朋友们可以将多进程和多线程两者做一个对比学习. 干货满满,建议收藏,需要用到时常看看. 小伙伴们如有问题 ...

  10. 异步处理方式之信号(一):基础知识和signal函数说明

    文章目录 1. 引言 2. 信号的概念 2.1 信号操作之忽略信号 2.2 信号操作之捕捉信号 2.3 信号操作之执行系统默认操作 2.4 常见的信号 3. 函数signal 3.1 signal函数 ...