[C++ Primer Plus] 第6章、分支语句和逻辑运算符(二)课后习题
一、复习题
3.
#include<iostream>
using namespace std; void main()
{
char ch;
int c1, c2;
c1 = c2 = ; while ((ch=cin.get())!='$')
{
cout << ch;
c1++;
if (ch = '$') //注意是=,不是==
c2++;
cout << ch;
}
cout << "c1=" << c1 << ",c2=" << c2 << endl; system("pause");
}

二、编程练习
1. 编写一个小程序,读取键盘输入,直到遇到@符号为止,并回显输入(除数字外),同时将大写字符转换为小写字符,将小写字符转换为大写(别忘了cctype函数系列)
#include<iostream>
#include<cctype>
using namespace std; void main()
{
char ch;
while ((ch = cin.get()) && ch != '@')
{
if (!isdigit(ch)) {
if (islower(ch)) //如果是小写字符
ch = toupper(ch);//转化为大写
else if (isupper(ch))
ch = tolower(ch);
cout << ch;
}
} system("pause");
}

2. 编写一个程序,最多将10个donation值读到一个double数组中。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include<iostream>
using namespace std; const int SIZE = ;
void main()
{
double donation[], sum = 0.0,average;
int i,count=;
for (i = ; i < SIZE; i++)
{
if (cin >> donation[i])
sum += donation[i];
else
break;//退出最近的循环,即退出for循环
}
if (i == )
return;
else
average = sum / i;
for (int j = ; j < i; j++)
{
if (donation[j] > average)
count++;
}
cout << "average=" << average << "," << count << " numbers bigger than average." << endl; system("pause");
}


3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。
#include<iostream>
using namespace std; void showA() {
cout << "Please enter one of the following choices:\n"
<< "a) carnivore\t" << "b) pianist\n"
<< "c) tree\t\t" << "d) game\n";
} void main()
{
showA();
char ch;
cin >> ch;
int flag = ;
while ()
{
switch (ch)
{
case 'a':cout << "A maple is a carnivore." << endl;
flag = ;
break;
case 'b':cout << "A maple is a pianist." << endl;
flag = ;
break;
case 'c':cout << "A maple is a tree." << endl;
flag = ;
break;
case 'd':cout << "A maple is a game." << endl;
flag = ;
break;
default:cout << "Please enter a,b,c,d :";
cin >> ch;
}
if (flag)
break;
}
system("pause");
}

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; const int strsize = ,num=;
struct bop
{
char fullname[strsize]; //真实姓名
char title[strsize]; //头衔
char bopname[strsize]; //秘密姓名
int preference; //偏好,当为0时为fullname,1为title,2为bopname
}; void show() {
cout<< "Benevolent Order of Programmers Report\n"
<< "a. display by name\t" << "b. display by title\n"
<< "c. display by bopname\t"<< "d. display by preference\n"
<< "q. quit\n";
} void main()
{
bop p[num] = {
{ "王一","程序员","first", },
{ "张二", "老师", "2nd", },
{ "李三", "公务员", "3rd", },
{ "赵四", "咨询师", "4th", },
{ "南宫五", "理发师", "5th", },
{ "龙六", "玩家", "6th", }
};
char ch;
show();
int flag = ;
cout << "Enter your choice:";
int l = sizeof(p)/sizeof(p[]);
while (cin>>ch)
{
switch (ch)
{
case 'a':
for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
cout << p[i].fullname << endl;
break;
case 'b':
for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
cout << p[i].title << endl;
break;
case 'c':
for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
cout << p[i].bopname << endl;
break;
case 'd':
for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
{
switch (p[i].preference)
{
case :cout << p[i].fullname << endl;
break;
case :cout << p[i].title << endl;
break;
case :cout << p[i].bopname << endl;
break;
}
}
break;
case 'q':
flag = ;
cout << "Bye!\n";
break;
default:
cout << "Enter error!";
break;
}
if (flag)
break;
cout << "Next choice:";
}
system("pause");
}

5.在Ncutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~15000 tvarps: 10%
15001~35000 tvarps: 15%
35000 tvarps以上: 20%
例如,收入为38000 tvarps时,所得税为5000x0.00 + 10000x0.10 + 20000x0.15+3000x0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
#include<iostream>
using namespace std; void main()
{
double money;
cout << "请输入您的收入:";
while (cin>>money)
{
if (money < )
break;
else if (money <= )
cout << "不交税" << endl;
else if (money <= )
cout << (money - )*0.1 << endl;
else if (money <= )
cout <<*0.1+(money - )*0.15 << endl;
else
cout << * 0.1 +*0.15+ (money - )*0.2 << endl;
cout << "请输入您的收入:";
} system("pause");
}

6.编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用于储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者姓名及其捐款数额。该列表前包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include<iostream>
#include<string>
using namespace std; struct donation
{
string name;
double money;
}; void main()
{
cout << "请输入捐献者数量:";
int num;
cin >> num;
int s[] = { };
donation *p = new donation[num]; for (int i = ; i < num; i++)
{
cout << "请输入第" << (i + ) << "个捐献者的姓名:";
cin >> p[i].name;
cout << "请输入第" << (i + ) << "个捐献者的捐款数额:";
cin >> p[i].money;
}
int flag = ;//标示,判断是否打印“none”
cout << "重要捐款人如下:" << endl;
for (int i = ; i < num; i++)
{
if (p[i].money > )
{
cout << p[i].name << "捐款" << p[i].money << "元" << endl;
flag = ;
}
}
if (flag == )
cout << "none" << endl;
flag = ;
cout << "其他捐款人如下:" << endl;
for (int i = ; i < num; i++)
{
if (p[i].money <= )
{
cout << p[i].name << "捐款" << p[i].money << "元" << endl;
flag = ;
}
}
if (flag == )
cout << "none" << endl;
delete p[];//用完new一定要记得delete
system("pause");
}


7.编写一个程序,他每次读取一个单词,直到用户只输入q。然后,该程序指出有多少单词以元音打头,有多少个单词以辅音打头,还有多少单词不属于这两类。为此,方法之一是使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过salpha()测试的单词,使用if或switch语句来确定哪些是以元音打头。
#include<iostream>
#include<cctype>
using namespace std; void main()
{
cout << "Enter words (q to quit):" << endl;
char ch;
cin.get(ch);
int vowel = , consonant = , other = , flag = ;
while (ch != 'q')
{
if (isalpha(ch)) {//如果参数是字母,返回true
if (flag)
{
switch (ch) {
case 'a':
case 'A':
vowel++;
flag = ; break;
case 'e':
case 'E':
vowel++;
flag = ; break;
case 'i':
case 'I':
vowel++;
flag = ; break;
case 'o':
case 'O':
vowel++;
flag = ; break;
case 'u':
case 'U':
vowel++;
flag = ; break;
default:
consonant++;
flag = ; break;
}
}
}
else if (isspace(ch) || ispunct(ch))//如果参数是空格、tab、换行等空白字符,或者是标点符号,返回true
flag = ;
else {
if (flag)
{
other++;
flag = ;
}
}
cin.get(ch);
}
cout << vowel << " words beginning with vowels " << endl;
cout << consonant << " words beginning with consonants " << endl;
cout << other << " others" << endl;
system("pause");
}

这是一串有问题的代码,找了半天实在不知道错在哪,先贴在这儿,看以后能发现错误不
8.编写一个程序,它打开一个文件夹,逐个字符的读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include<iostream>
#include<fstream>//文件IO
using namespace std; void main()
{
ifstream inFile;//声明ifstream对象
inFile.open("1.txt");//关联文件 if (!inFile.is_open())//文件打开失败
{
cout << "Could not open the file " << endl;
exit(EXIT_FAILURE);
}
char ch;
int count = ; inFile >> ch;
while (inFile.good())//输入正确
{
++count;
cout << ch;
inFile >> ch;
}
cout << endl; if (inFile.eof())
cout << "End of file reached." << endl;
else if (inFile.fail())
cout << "Input terminated by data misamatch." << endl;
else
cout << "Input terminated for unknown reason." << endl; cout << "文件包含" << count<<"个字符" << endl;
inFile.close(); system("pause");
}

9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
#include<iostream>
#include<fstream>//文件IO
#include<string>
using namespace std; struct donation
{
string name;
double money;
};
void main()
{
ifstream inFile;//声明ifstream对象
inFile.open("1.txt");//关联文件
if (!inFile.is_open())//文件打开失败
{
cout << "Could not open the file " << endl;
exit(EXIT_FAILURE);
} int num, i = ;
inFile >> num;
inFile.get();//抵消换行
donation *p = new donation[num];
for (int i = ; i < num; i++)
{
getline(inFile, p[i].name);
inFile >> p[i].money;
inFile.get();//抵消换行
} int flag = ;//标示,判断是否打印“none”
cout << "重要捐款人如下:" << endl;
for (int i = ; i < num; i++)
{
if (p[i].money > )
{
cout << p[i].name << "捐款" << p[i].money << "元" << endl;
flag = ;
}
}
if (flag == )
cout << "none" << endl;
flag = ;
cout << "其他捐款人如下:" << endl;
for (int i = ; i < num; i++)
{
if (p[i].money <= )
{
cout << p[i].name << "捐款" << p[i].money << "元" << endl;
flag = ;
}
}
if (flag == )
cout << "none" << endl; if (inFile.eof())
cout << "End of file reached." << endl;
else if (inFile.fail())
cout << "Input terminated by data misamatch." << endl;
else
cout << "Input terminated for unknown reason." << endl; inFile.close();
delete [] p;//用完new一定要记得delete
system("pause");
}

[C++ Primer Plus] 第6章、分支语句和逻辑运算符(二)课后习题的更多相关文章
- C++ primer plus读书笔记——第6章 分支语句和逻辑运算符
第6章 分支语句和逻辑运算符 1. 逻辑运算符的优先级比关系运算符的优先级低. 2. &&的优先级高于||. 3. cctype中的函数P179. 4. switch(integer- ...
- C++ Primer Plus 6th 读书笔记 - 第6章 分支语句和逻辑运算符
1. cin读取错误时对换行符的处理 #include <iostream> using namespace std; int main() { double d; char c; cin ...
- [C++ Primer Plus] 第6章、分支语句和逻辑运算符(一)程序清单
程序清单6.2 #include<iostream> using namespace std; void main() { char ch; cout << "Typ ...
- c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- C++ Primer Plus读书笔记(六)分支语句和逻辑运算符
1. 以上均包含在cctype中 1 #include<cctype> 2 //#include<ctype.h> 2.文件操作 (1)头文件 1 #include<fs ...
- [C++ Primer Plus] 第8章、函数探幽(二)课后习题
1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数.不过,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第 ...
- [C++ Primer Plus] 第4章、复合类型(二)课后习题
1.编写一个 c++ 程序,如下述输出示例所示的那样请求并显示信息 : What is your first name? Betty SueWhat is your last name? YeweWh ...
- [C++ Primer Plus] 第3章、处理数据(二)课后习题
1 . 编写一个小程序,要求用户使用一个整数输出自己的身高(单位为厘米),然后将身高转换为米和厘米.该程序使用下划线字符来指示输入位置.另外,使用一个 const 符号常量来表示转换因子. #incl ...
- 重拾c++第三天(6):分支语句与逻辑运算符
1.逻辑运算符 && || ! 2.关系运算符优先级高于逻辑运算符 3.cctype库中好用的判断 4. ?:符号用法: 状态1?结果1:结果2 5.switch用法: switch ...
随机推荐
- Kylin如何进行JDBC方式访问或者调用
Kylin提供了标准的ODBC和JDBC接口,能够和传统BI工具进行很好的集成.分析师们可以用他们最熟悉的工具来享受Kylin带来的快速.我们也可以对它进行定制开发报表等,把kylin当做数据库服务器 ...
- 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误
我调这个bug调了一天多,在网上搜索的检查namespace,package等,都没有错.错误提示是没有找到xml文件,我就纳闷了,为什么找不到呢?后来才发现,原来是resource中奇怪的目录为题, ...
- 《Linux.Shell编程从入门到精通》读书笔记
第一章 第一个Shell程序 以 #!解析器名称 开头,表示选择哪个解释器解释shell脚本 source命令 export命令 env命令 unset命令 第二章 shell编程基础 函数传递 标准 ...
- Nestjs 身份验证
文档 yarn add @nestjs/passport passport passport-http-bearer @nestjs/jwt passport-jwt auth.service.ts ...
- web端MSF搭建
去购买一个廉价VPS 阿里X/tx学生服务器然后选择Ubuntu系统http://jingyan.baidu.com/article/2c8c281dabacad0008252aa6.html安装M ...
- missing python bz2 module
import bz2 ImportError: No module named bz2 一般是手动编译python时,编译的机器上环境不全面导致的依赖库不完整. 需要安装bzip库,使用如下命令: s ...
- Angel - 模拟Kafka数据流调试FTRL的方法
Angel - 模拟Kafka数据流调试FTRL的方法 Mac或者Linux版本(Win10的Linux子系统同样适用) 创建一个目录用来安装kafka以及zookeeper等相关软件,比如,新建一个 ...
- Javascript实现MD5加密
/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...
- intput/output 文件的复制练习
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- 客户续费模型 逻辑回归 分类器 AdaBoost
客户续费模型 逻辑回归 分类器 AdaBoost