c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。
(具体方式参见第3章模板)
1.编写一个小程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。
- #include <iostream>
- #include <cctype>
- using namespace std;
- void cprimerplus_exercise_6_1()
- {
- char letter;
- cout << "Please input letters:";
- cin >> letter;
- cin.get();
- while ( letter != '@')
- {
- if (isdigit(letter))
- {
- cin.get(letter);
- }
- else
- {
- if (islower(letter))
- {
- letter = toupper(letter);
- }else if (isupper(letter))
- {
- letter = tolower(letter);
- }
- cout << letter;
- cin >> letter;
- cin.get();
- }
- }
- }
2.编写一个程序,最多将10个donation值读入到一个double数组中(如果你愿意,也可以使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
- #include <iostream>
- #include <cctype>
- using namespace std;
- void cprimerplus_exercise_6_2()
- {
- double donation[10];
- double sum = 0.0;
- double average = 0.0;
- double tmp;
- int i = 0;
- int cnt = 0;
- while ( cin >> tmp && i < 10)
- {
- donation[i] = tmp;
- sum +=donation[i];
- ++i;
- }
- if ( i != 0)
- {
- average = sum / i;
- }
- for (int j = 0; j < i; j++)
- {
- if (donation[j] > average)
- {
- ++cnt;
- }
- }
- cout << "The average is:" << average << endl;
- cout << "There are " << cnt << " numbers are above the average!"<< endl;
- }
3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。
- #include <iostream>
- using namespace std;
- void cprimerplus_exercise_6_3()
- {
- cout << "Please enter one of the following choices:" << endl\
- <<"c) carnivore \tp) pianist \nt) tree \tg) game" << endl;
- cout << "Please enter a c, p, t, or g:";
- char letter;
- cin >> letter;
- while ( letter != 'c' && letter != 'p' && letter != 't' && letter != 'g')
- {
- cout << "Please enter a c, p, t, or g:";
- cin >> letter;
- }
- switch (letter)
- {
- case 'c':
- cout << "A maple is a carnivore";
- break;
- case 'p':
- cout << "A maple is a pianist";
- case 't':
- cout << "A maple is a tree";
- break;
- case 'g':
- cout << "A maple is a game";
- break;
- default:
- break;
- }
- }
4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他,请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面结构
//Benevolent Order of Programmers name structure
Struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值,另外,该程序使用一个循环让用户在下面的选项中进行选择:
A. Display by name B. Display by title C. Display by bopname
D. Display by preference Q. quit
- #include <iostream>
- using namespace std;
- void cprimerplus_exercise_6_4()
- {
- const int strsize = 20;
- const int mennum = 3;
- struct bop
- {
- char fullname[strsize]; //real name
- char title[strsize]; //job title
- char bopname[strsize]; //secret BOP name
- int preference; // 0 = fullname, 1 = title, 2 = bopname
- };
- bop member[mennum] = {
- { "thm", "leader", "sb", 0},
- { "cgf", "sb", "sb", 1},
- { "th", "dsb", "ssb", 2} };
- cout << "Enter your choice!";
- char ch;
- cin.get(ch);
- while (cin >> ch && ch != 'q')
- {
- switch (ch)
- {
- case 'a':
- for (int i = 0; i < mennum; i++)
- cout << member[i].fullname << endl;
- break;
- case 'b':
- for (int i = 0; i < mennum; i++)
- cout << member[i].title << endl;
- break;
- case 'c':
- for (int i = 0; i < mennum; i++)
- cout << member[i].bopname << endl;
- break;
- case 'd':
- for (int i = 0; i < mennum; i++)
- {
- if (member[i].preference == 0)
- {
- cout << member[i].fullname << endl;
- }else if (member[i].preference = 1)
- {
- cout << member[i].title << endl;
- }else if (member[i].preference = 2)
- {
- cout << member[i].bopname << endl;
- }
- }
- break;
- default:
- break;
- }
- cout << "Next choice:";
- }
- cout << "Bye!\n";
- }
5. 在neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~15000 tvarps:10%
15001~35000 tvarps:15%
35000 tvarps以上:10%
如:收入38000 tvarps,所得税:5000*0.0+10000*0.1+20000*0.15+3000*0.2;
- #include <iostream>
- using namespace std;
- void cprimerplus_exercise_6_5()
- {
- cout << "please input your income:";
- double income, revenue;
- while (cin >> income && income >= 0)
- {
- if (income <= 5000)
- {
- revenue = 0.0;
- }else if ( income > 5000 && income <= 15000)
- {
- revenue = (income - 5000) * 0.1;
- }else if( income > 15000 && income < 35000)
- {
- revenue = 5000 * 0.00 + 10000 * 0.10 + (income -20000) * 0.15;
- }else if( income > 35000)
- {
- revenue = 5000 * 0.00 + 10000 * 0.10 + + 20000 * 0.15 + (income -35000) * 0.15;
- }
- cout << "your revenue is:" << revenue << endl;
- cout << "please enter your income:";
- }
- }
6.编写一个程序,记录捐助给“维护合法权利团体”的资金,该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项,这些信息都被存储在一个动态分配的结构数组中,每个结构有两个成员;用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名以及捐款的数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某类别没有捐款人,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
- #include <iostream>
- #include <string>
- using namespace std;
- void cprimerplus_exercise_6_6()
- {
- int num;
- cout << "please input the donate num:";
- cin >> num;
- cin.get();
- struct patron
- {
- string name;
- double money;
- };
- patron *ps = new patron[num];
- for (int i = 0; i < num; ++i)
- {
- cout <<"please input the "<< i+1 <<"th patron name:";
- getline(std::cin, ps[i].name);
- cout << "please input the " << i+1 << "th patron money:";
- cin >> ps[i].money;
- cin.get();
- }
- int cnt = 0, snt = 0;
- cout << "Grand Patrons:" << endl ;
- for (int i = 0; i < num; i++)
- {
- if (ps[i].money >= 10000)
- {
- cout << ps[i].name << '\t' <<ps[i].money << endl;
- ++cnt;
- }
- }
- if ( cnt == 0)
- {
- cout << "none";
- }
- cout << "\nPatrons:" << endl;
- for (int i = 0; i < num; i++)
- {
- if (ps[i].money < 10000)
- {
- cout << ps[i].name << '\t' << ps[i].money << endl;
- ++snt;
- }
- }
- if ( snt == 0)
- {
- cout << "none";
- }
- delete []ps;
- }
7.编写一个程序,他每次读取一个单词,直到用户只输入q。然后,该程序指出有多少单词以元音打头,有多少个单词以辅音打头,还有多少单词不属于这两类。为此,方法之一是使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过salpha()测试的单词,使用if或switch语句来确定哪些是以元音打头。
- #include <iostream>
- #include <string>
- #include <ctype.h>
- using namespace std;
- void cprimerplus_exercise_6_7()
- {
- string word;
- cout << "Enter words (q to quit):";
- int cnt = 0;
- int vowel = 0, constant = 0, other = 0;
- while (cin >> word && !word.empty())
- {
- if(isalpha(word[0]))
- {
- if (word[0] == 'q' && word.length() == 1)
- break;
- else if( word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
- {
- ++vowel;
- }else
- {
- ++constant;
- }
- }else
- {
- ++other;
- }
- }
- cout << vowel << " words beginning with vowels "<< endl;
- cout << constant <<" words beginning with constants " << endl;
- cout << other <<" others" << endl;
- }
8.编写一个程序,它打开一个文件夹,逐个字符的读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- void cprimerplus_exercise_6_8()
- {
- char ch;
- int sum = 0;
- ifstream inFile;
- inFile.open("1.txt");
- if( !inFile.is_open())
- {
- cout << "Could not open the file!\n";
- cout << "Program terminating.\n";
- exit(EXIT_FAILURE);
- }
- inFile >> ch;
- while (inFile.good())
- {
- ++sum;
- inFile >> ch;
- }
- if (inFile.eof())
- {
- cout << "End of file reached.\n";
- }else if(inFile.fail())
- {
- cout << "Input terminated by data mismatch.\n";
- }else
- {
- cout << "Input terminated for unknown reason.\n";
- }
- cout << "there are " << sum << " chars in this file!\n";
- }
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。及该文件类似于下面:
4
Sam Stone
2000
Freida Flass
10050
Tammy Tubbs
5000
Rich Raptor
5500
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- const int SIZE = 60;
- void cprimerplus_exercise_6_9()
- {
- char filename[SIZE];
- ifstream inFile;
- cout << "Enter the name of the file: ";
- cin.getline(filename,SIZE);
- inFile.open(filename);
- if (!inFile.is_open())
- {
- cout << "Could not open the file" << filename << endl;
- cout << "Program terminating.\n";
- exit(EXIT_FAILURE);
- }
- struct patron
- {
- string name;
- double money;
- };
- int num, cnt = 0, snt = 0;
- inFile >> num;
- inFile.get();
- patron *ps = new patron[num];
- for (int i = 0; i < num; i++)
- {
- getline(inFile, ps[i].name);
- inFile >> ps[i].money;
- inFile.get();
- }
- cout << "Grand patrons:\n";
- for (int i = 0; i < num; i++)
- {
- if (ps[i].money >= 10000)
- {
- cout << ps[i].name << '\n' << ps[i].money << endl;
- ++cnt;
- }
- }
- if ( cnt == 0)
- {
- cout << "none";
- }
- cout << "\nPatrons:" << endl;
- for (int i = 0; i < num; i++)
- {
- if (ps[i].money < 10000)
- {
- cout << ps[i].name << '\t' << ps[i].money << endl;
- ++snt;
- }
- }
- if ( snt == 0)
- {
- cout << "none";
- }
- delete []ps;
- inFile.close();
- }
c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)的更多相关文章
- c++primerplus(第六版)编程题——第4章(复合类型)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- c++primerplus(第六版)编程题——第3章(数据类型)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. 工程命名和文件命名可以命名成易识 ...
- c++primerplus(第六版)编程题——第5章(循环和关系表达式)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)
第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...
- C程序设计(谭浩强)第五版课后题答案 第一章
大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...
- c primer plus(五版)编程练习-第七章编程练习
1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...
- C++ Primer Plus读书笔记(六)分支语句和逻辑运算符
1. 以上均包含在cctype中 1 #include<cctype> 2 //#include<ctype.h> 2.文件操作 (1)头文件 1 #include<fs ...
- C算法编程题(六)串的处理
前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...
- 某软件大赛C#版考题整理——【编程题】
三.编程题(4小题共40.0分)程序及结果写入对应文框内 1. 孪生素数查找程序. 所谓孪生素数指的是间隔为2 的相邻素数,就像孪生兄弟.最小的孪生素数是(3, 5),在100 以内的孪生素数还有 ( ...
随机推荐
- decodeURIComponent
var s = '%%' try { s = decodeURIComponent(s) } catch(e) { console.log(e) } console.log(s)
- 如何配置jdk和tomcat 转
一.配置JDK1.解压JDK至D:\JDK1.5目录下(楼主可以自由选取目录).2.设置环境变量(右键我得电脑->属性->高级->环境变量),在系统变量中添加一个叫JAVA_HOME ...
- 怎样打开64位 Ubuntu 的32位支持功能?
怎样打开64位 Ubuntu 的32位支持功能? 现在有一个让你可以在64位系统中使用32位软件的方法,就在你读了这篇文章然后照着做了之后就可以了.如果你有一个13.10或更高版本的Ubuntu/De ...
- mac下的改装人生——关于机械键盘
这几天好像弄了很多关于机械键盘的东西,我自己的这块键盘也已经慢慢熟悉了,感觉打字超级爽哈,然后看了很多网上关于机械键盘的帖子,也看了很多教程,在Amazon和Taobao看了很多键盘的价位,前几天还试 ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
- lightoj 1036 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1036 #include <cstdio> #include <cst ...
- linux mono
linux下.net环境; rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm &am ...
- Redis教程03——Redis 发布/订阅(Pub/Sub)
Pub/Sub 订阅,取消订阅和发布实现了发布/订阅消息范式(引自wikipedia),发送者(发布者)不是计划发送消息给特定的接收者(订阅者).而是发布的消息分到不同的频道,不需要知道什么样的订阅者 ...
- stickyListHeaders框架的使用
最近在Demo中使用到了stickyListHeaders框架实现悬停效果 比较好用 在xml中 <se.emilsjolander.stickylistheaders.StickyListHe ...
- 封装Unity3d的dll时的经验总结
部分时候,我们需要自己封装一些小工具来简化我们的工作. 实验时,偶然发现Unity3d的console在双击进行debug信息的输出定位时,只能跟进到dll的上一层,因此我们可以将unity3d自带的 ...