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 以内的孪生素数还有 ( ...
随机推荐
- JSTL语法及参数
转:http://blog.csdn.net/hakunamatata2008/article/details/3942812 JSTL语法及参数 JSTL包含以下的标签: 常用的标签:如&l ...
- Delphi Webservice 杂谈
用WebService来实现B2B集成的最大好处在于可以轻易实现互操作性 WebService可用基于XML的SOAP来表示数据和调用请求,并且通过HTTP协议来传输这些XML格式的数据,因为此时的调 ...
- checkbox操作
小小示例:自己备份顺便粘出来共享. 引入头部文件:<script src="../js/jQuery1.7.2.js"></script> HTML代码: ...
- 你不一定能做对的JavaScript闭包面试题
由工作中演变而来的面试题 这是一个我工作当中的遇到的一个问题,似乎很有趣,就当做了一道题去面试,发现几乎没人能全部答对并说出原因,遂拿出来聊一聊吧. 先看题目代码: function fun(n,o) ...
- 用js判断一个复选框是否被选中
<html> <head> <title> 复选框全选.全不选.反选.必选一个 </title> ...
- C# WinForm 透明控件 PictureBox透明 分类: WinForm 2014-07-30 13:27 591人阅读 评论(0) 收藏
1.要实现C# WinForm中的控件与背景的透明,可以通过设置控件的BackColor属性为Transparent,同时设置其父控件.因为在C#中,控件的透明指对父窗体透明.如果不设置Parent属 ...
- VS2012的自动生成测试的插件 Unit Test Generator
Unit Test Generator extension是一个VS2012的插件,可以为C#的public方法很方便的自动生成unit test.安装这个插件后点击TEST菜单可以配置,如下所示: ...
- ASP.NET datable导出excel
本文转载:http://www.cnblogs.com/chwkai/archive/2005/10/08/250426.html 不错的文章:http://www.cnblogs.com/lzhp/ ...
- HDU 3065 AC自动机 裸题
中文题题意不再赘述 注意 失配数组 f 初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...
- java文件处理工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...