1、请看下面两个计算空格和换行符数目的代码片段:
//Version 1
while (cin.get(ch))
{
if(ch == ' ')
spaces++;
if(ch == '\n')
newlines++;
}
//Version 2
while (cin.get(ch))
{
if(ch == ' ')
spaces++;
else if(ch == '\n')
newlines++;
}
第二种格式比第一种好在哪里呢?
答:这两个版本将给出相同的答案,但是ifelse效率更高。因为如果第一个判断成立的话。第二种格式不会执行第二个判断,而第一种格式会执行第二个判断。
2、在下图程序中,用ch+1替换++ch将发生什么情况呢?
//ifelse.cpp -- using the if else statement
#include <iostream>
int main()
{
char ch;
std::cout <<"Typt,and I shall repeat.\n";
std::cin.get(ch);
while(ch != '.')
{
if(ch == '\n')
std::cout << ch;
else
std::cout << ++ch;
std::cin.get(ch);
}
std::cout << "\nPlease excuse the slight confusion.\n";
return 0;
}
答:++ch 和ch+1得到的数值相同。但++ch的类型为char,将作为字符打印,而ch+1是int类型(因为char和1相加),将作为数字打印。
3、请认真考虑下面的程序
#include <iostream>
using namespace std;
int main()
{
char ch;
int ct1,ct2;
ch1 = ct2 = 0;
while((ch = cin.get()) != '$')
{
cout << ch;
ct1++;
if(ch = '$')
ct2++;
cout << ch;
}
cout <<"ct1 = " << ", ct2 = " << ct2 << ""\n;
return0;
}
假设输入如下(请在每行末尾按回车键)
Hi!
Send $10or $20 now!
则输出将是什么?(输入被缓冲)
答:由于程序使用=而不是==,所以执行的是赋值操作,而不是判断操作。
Hi!
H$i$!$
$Send $10 or $20 now!
S$e$n$d$ $ct1 = 9, ct2 = 9
4、创建表示下述条件的逻辑运算符
weight 大于或等于115,但小于125
ch为q或Q
x为偶数,但不是26
x为偶数,但不是26的倍数
donation 为1000-2000或guest为1
ch是小写字母或大写字母(假设小写字母是依次编码的,大写字母也是依次编码的,但在大小写字母间编码不是连续的)
答:
a. weight >= 115 && weight < 125
b. ch == 'q'||ch == 'Q'
c. x%2 ==0&& x!= 26
d. x%2 ==0 && !(x%26 == 0)
e. donation >= 1000 && donation < = 2000 || guest == 1
f. (ch >='a' && ch<='z')||(ch>='A' && ch<= 'Z')
5、在英语中,“I will not not speak (我不会不说)”的意思与“I will speak(我要说)”相同。在C++中,!!x是否与x下相同呢?
答:
不一定。bool和整数值还是有区别的。
6、创建一个条件表达式,其值为变量的绝对值。也就是说,如果变量x为正,则表达式的值为x;但如果x为负,则表达式的值为-x——这是一个正值。
答:
(x >=0) ? x : -x;
7、用Switch改写下面的代码片段:
if(ch == 'A')
a_grade++;
else if (ch == 'B')
b_grade++;
else if (ch == 'C')
c_grade++;
else if (ch == 'D')
d_grade++;
else
f_grade++;
答:
switch(ch)
{
case 'A':
a_grade++;
break;
case 'B':
a_grade++;
break;
case 'C':
a_grade++;
break;
case 'D':
a_grade++;
break;
default:
f_grade++;
break;
}
8、对于下面所示的程序,与使用数字相比,使用字符(如a和c)表示菜单选项和casse标签有何优点呢?
#include <iostream>
using namespace std;
void showmenu();
void report();
void comfort();
int main()
{
showmenu();
int choice;
cin >> choice;
while(choice != 5)
{
switch(choice)
{
case 1 : cout << "\a\n";
break;
case 2 : report();
break;
case 3 : cout << "The boss was in all day.\n";
break;
case 4 : comfort();
break;
default : cout << "That's not a choice .\n";
}
showmenu();
cin >> choice;
}
cout << "Bye!\n";
return 0 ;
}
void showmenu()
{
cout << "Please enter 1,2,3,4,or 5:\n"
"1) alarm 2) reprot\n"
"3) alibi 4) comfort\n"
"5) quit\n";
}
void report()
{
cout << "It's been an excellent week for business.\n"
"Sales are up 120$. Expenses are down 35$.\n";
}
void comfort()
{
cout << "Your employees think you are the finest CEO\n"
"int the industry. The board of directors think\n"
"you are the finest CEO in the industry.\n";
}
答:如果使用整数标签,且用户输入了非整数(如q),则程序将因为整数输入不能处理字符而挂起。但是,如果使用字符标签,而用户输入了整数(如5),则字符输入将5作为字符处理。然后,switch语句的default部分将提示输入另一个字符。
9、请重写该代码片段,不要使用break和continue语句。
int line = 0;
char ch;
while (cin.get(ch))
{
if(ch == 'Q')
break;
if(ch != '\n')
continue;
line++;
}
答:
int line = 0;
char ch;
while (cin.get (ch)&& ch != 'Q')
{
if (ch == '\n ')
line++;
}