本文程序为自己学习C++过程中编写,文章给出需要自己编写程序的课后习题解答,如有问题或错误欢迎交流指正,共同学习。
编译软件为 vs2015。
第五章
练习5.1:
什么是空语句?什么时候会用到空语句?
解答:
只含有一个单独的分号的语句就是空语句。如果在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该使用空语句。
练习5.2:
什么是块?什么时候会用到块?
解答:
复合语句也被称作块,是指用花括号括起来的(可能为空的)语句和声明的序列。如果在程序的某个地方,语法上需要一条语句,但是逻辑上需要多条语句,就会用到块。
练习5.3:
使用逗号运算符(参见 4.10 节,第 140 页)重写 1.4.1 节(第 10 页)的 while 循环,使它不再需要块,观察改写之后的代码的可读性提高了还是降低了。
解答:
#include "stdafx.h"
#include<iostream>
using std::cout;
using std::endl;
int main()
{
int sum = 0, val = 1;
//只要 val 的值小于 1, while 循环的值就会持续执行
while (val <= 10)
sum += val, ++val;
cout << "Sum of 1 to 10 inclusive is " << sum << endl;
return 0;
}
用逗号运算符分隔开块内的各条语句,以不再需要块。但这种改写方式降低了代码的可读性,同时逗号运算符总是输出右侧的运算结果,在本程序中虽然没有影响,但不保证这种方式会在其他程序中有丢失数据的风险。
练习5.4:
说明下列例子的含义,如果存在问题,试着修改它。
(a)while (string::iterator iter != s.end){ /*......*/ }
(b)while (bool status = find(word)){ /*......*/ }
if ( !status ){ /*......*/ }
解答:
(a)迭代器 iter 没有初始化,修改:while (string::iterator iter = s.begin(), iter != s.end()){ /*......*/ };
(b)定义在控制结构当中的变量旨在相应语句的内部可见,一旦语句结束,变量就超出作用范围。此时 status 在 if 语句中相当于未定义的变量,修改:while (bool status = find(word)){ /*......*/ ; if ( !status ){ /*......*/ } }//将 if 语句放在 while 循环中。
练习5.5:
写一段自己的程序,使用 if else 语句实现把数字成绩转换成字母成绩的要求。
解答:
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::endl;
int main()
{
int grade = 0;
const vector<string> vec = { "F","D","C","B","A","A++" };
cout << " please input your grade: " << endl;
while (cin >> grade) {
string final;
if (grade < 60)
final = vec[0];
else {
final = vec[(grade - 50) / 10];
if (grade != 100) { //注意100除以10余数为0但要避免在其后面添加减号
if (grade % 10 >= 3) {
if (grade % 10 > 7)
final += "+";
}
else
final += "-";
}
}
cout << final << endl;
cout << " please input your grade: " << endl;
}
return 0;
}
检验结果:
练习5.6:
改写上一题的程序,使用条件运算符(参见第 4.7 节,第 134 页)代替 if else 语句。
解答:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
int grade = 0;
const vector<string> vec = { "F","D","C","B","A","A++" };
string final;
cout << " please input your grade: " << endl;
while (cin >> grade) {
final = grade < 60 ? vec[0] ://判断分数段是否小于 60
(grade == 100) ? vec[(grade - 50) / 10] :
(grade % 10 > 7) ? (vec[(grade - 50) / 10] + "+"):
(grade % 10 < 3) ? (vec[(grade - 50) / 10] + "-"):
vec[(grade - 50) / 10];
cout << final << endl;
cout << " please input your grade: " << endl;
}
return 0;
}
验证结果:
练习5.7:
改正下列代码段中的错误。
(a)if ( ival1 != ival2 )
ival1 = ival2
else ival1 = ival2 = 0;
(b)if ( ival < minval )
minval = ival;
occurs = 1;
(c)if ( int ival ) = get_value() )
cout << "ival = " << ival << endl;
if ( !ival )
cout << "ival = 0\n";
(d)if ( ival = 0)
ival = get_value( );
解答:
(a)第二行没有加分号。
修改:if ( ival1 != ival2 )
ival1 = ival2;
else ival1 = ival2 = 0;
(b)if 中包含多条语句,需要用花括号括起来,容易产生歧义。
修改: if ( ival < minval ) {
minval = ival;
occurs = 1;}
(c)在第二条 if 语句中,变量 ival 是指未定义的,需要在条件语句外定义 ival,使其作用范围为整个程序。
修改:int ival;
if ( int ival ) = get_value() )
cout << "ival = " << ival << endl;
if ( !ival )
cout << "ival = 0\n";
(d)判断是否相等运算符为 "==",此处使用的是赋值运算符,条件语句的执行结果永远是 false。
修改:if ( ival == 0)
ival = get_value( );
练习5.8:
什么是“悬垂 else”?C++ 语言是如何处理 else 子句的?
解答:
当 if 语句内部嵌套多个 if 语句时,很可能 if 分支多于 else 分支,如何确定给定的 else 与某个 if 匹配成为悬垂 else。C++ 中规定 else 与其相邻最近的未匹配的 if 匹配,消除了程序的二义性。
练习5.9:
编写一段程序,使用一系列 if 语句统计从 cin 读入的文本中有多少元音字母。
解答:
#include "stdafx.h"
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char ch;
unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0;
cout << "Please input words: " << endl;
while (cin >> ch) {
if (ch == 'a')
++acnt;
if (ch == 'e')
++ecnt;
if (ch == 'i')
++icnt;
if (ch == 'o')
++ocnt;
if (ch == 'u')
++ucnt;
}
cout << "Number of vowel a: \t" << acnt << endl;
cout << "Number of vowel e: \t" << ecnt << endl;
cout << "Number of vowel i: \t" << icnt << endl;
cout << "Number of vowel o: \t" << ocnt << endl;
cout << "Number of vowel u: \t" << ucnt << endl;
return 0;
}
练习5.10:
我们之前实现的统计元音字母的程序存在一个问题:如果元音字母以大写形式出现,不会被统计在内。编写一段程序,统计元音字母的小写形式,也统计大写形式,也就是说,新程序遇到 'a' 和 'A' 都应该递增 acnt 的值,以此类推。
解答:
#include "stdafx.h"
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char ch;
unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0;
cout << "please input your words: " << endl;
while (cin >> ch) {
switch (ch) {
case 'a':
case 'A':
++acnt;
break;
case 'e':
case 'E':
++ecnt;
break;
case 'i':
case 'I':
++icnt;
break;
case 'o':
case 'O':
++ocnt;
break;
case 'u':
case 'U':
++ucnt;
break;
}
}
cout << "Number of vowel a or A: \t" << acnt << endl;
cout << "Number of vowel e or E: \t" << ecnt << endl;
cout << "Number of vowel i or I: \t" << icnt << endl;
cout << "Number of vowel o or O: \t" << ocnt << endl;
cout << "Number of vowel u or U: \t" << ucnt << endl;
return 0;
}