目录
复习题:
1 .请看下面两个计算空格和换行符数目的代码片段:
2.在程序清单6.2中,用ch+1替换++ch将发生什么情况?
3.请认真考虑下面的程序:
4.创建表示下述条件的逻辑表达式:a.weight大于或等于115,但小于125。b.ch为q或Q。c.x为偶数,但不是26.d.x为偶数,但不是26。e.donation为1000-2000或guest为1。f.ch是小写字母或大写字母(假设小写字母是依次编码的,大写字母也是依次编码的,但在大小写字母间编码是不连续的)。
5.在英语中,"I will not not speak(我不会不说) "的意思与"I will speak(我要说)"相同。在c++中,!!x是否与x相同呢?
6.创建一个条件表达式,其值为变量的绝对值。也就是说,如果变量x为正,则表达式的值为x;但如果x为负,则表达式的值为-x–这是一个正值。
7.用switch改写下面的代码片段:
8.对于程序清单6.10,与使用字符(如a和c)表示菜单选项和case标签有何优点呢?(提示:想一想用户输入q和输入5的情况。)
9.重写下面代码片段,不要使用break和continue
编程练习(题号和test序号对应):
1.编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写字符,将小写字符转换为大写(别忘了cctype函数系列)
2.编写一个程序,最多将10个donation值读入一个double数组中(如果你愿意,也可以使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
3.编写一个菜单驱动程序的雏形。该程序显示一个提供四个选项的菜单–每个选项用一个字母表标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入有效的字母,直到用户这样选择为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。
4.加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。:
5.在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:5000 tvarps: 不收税5001~15000 tvarps: 10%15001~35000 tvarps: 15%35000 tvarps以上: 20%
例如,收入为38000 tvarps时,所得税为5000×0.00+10000×0.10+2000×0.15+3000×0.20,即 4600tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税,当用户输入负数或非数字时,循环将结束。
6.编写一个程序,记录捐助给"维护合法权利团体"的资金。该程序要求用户输入捐赠者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被存储在一个动态分配的结构数组中.每个数据结构有两个成员:用来存储姓名的字符串数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款着姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Partons开头。如果某种类别没有捐款者,则程序将打印单词"none"。该程序只显示这两种类别,而不进行排序。
7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字母打头的单词,然后对于通过isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。
8.编写一个程序,他打开一个文件,逐个字地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
//version 1
while(cin.get(ch)) //quit on eof
{
if(ch==' ')
spaces++;
if(ch=='\n')
newlines++;
}
//version 2
while(cin.get(ch)) //quit eof
{
if(ch==' ')
spacees++;
else if(ch=='\n')
newlines++;
}
第二个格式比第一个格式好在哪里?
第二个版本运行效率高,在第一个版本中每个字符都要判断两次,在第二个版本中,如果第一次判断为空格,则不会进行第二次判断
// ifelse.cpp -- using the if else statement
#include
int main()
{
char ch;
std::cout << "Type, and I shall repeat.\n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
std::cout << ch; // done if newline
else
std::cout << ++ch; // done otherwise
std::cin.get(ch);
}
// try ch + 1 instead of ++ch for interesting effect
std::cout << "\nPlease excuse the slight confusion.\n";
// std::cin.get();
// std::cin.get();
return 0;
}
++ch的数据类型依旧是char型,但char+1最终类型为int型
#include
using namespace std;
int main()
{
char ch;
int ct1,ct2;
ct1=ct2=0;
while((ch=cin.get())!='$')
{
cout<
假设输入如下(请在每行末尾按回车键):
Hi!
Send $10 or $20 now!
则输出将是什么(还记得吗,输入被缓冲)?
输出将是:
H$i$!$
S$e$n$d$ $ct1=9,ct2=9
因为程序中判断语句为:if(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<=20000 || guest==1
//f
(ch>='a' && ch<='z') || (ch<='Z' && ch>='A')
对于bool变量而言,!!x与x是相同的,但对于其他类型变量不一定相同。
x>=0 ? x : -x;
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' : b_grade++;
break;
case 'C' : c_grade++;
break;
case 'D' : d_grade++;
break;
default : f_grade++;
break;
}
使用数字作为菜单选项和case标签,限定了用户只有输入数字的时候才能有效,若用户错误的输入非整数类型,导致程序被挂起。而使用字符作为菜单选项和case标签,当用户输入错误类型,程序能正确通过default部分提示用户输入错误,用户体验更加友好,提高了程序的容错性和健壮性。
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++;
}
#include
using namespace std;
#include
#include
#include
#include
void test01()
{
cout << "读取键盘输入,直到遇到@符号为止:\n";
char ch;
cin.get(ch);
while (ch != '@')
{
if (islower(ch))
{
ch = toupper(ch);
}
else if (isupper(ch))
{
ch = tolower(ch);
}
if (isdigit(ch) == false)
{
cout << ch;
}
cin.get(ch);
}
cout << endl;
}
void test02()
{
double donation[10];
int len, i, j;
double sum = 0.0;
double average;
int num = 0;
double line;
cout << "输入 10个 donation:" << endl;
for (i = 0; i < 10; i++)
{
cin >> donation[i];
if (cin.fail())
{
cout << "不是数字!\n";
break;
}
sum += donation[i];
}
average = sum / i;
for (i = 0; i < 10; i++)
{
if (donation[i] > average)
{
num++;
}
}
cout << "平均数 = " << average << endl;
cout << "在这个数组中有 " << num << " 个值大于平均数" << endl;
}
void test03()
{
cout << "请在c,p,t,g中选择一个选项进行输入:" << endl;
cout << "c) carnicore\t" << "p) pianist" << endl;
cout << "t) tree\t\t" << "g) game" << endl;
char iput;
cin.get(iput);
while (isalpha(iput))
{
switch (iput)
{
case 'c': cout << "A maple is a carnivore." << endl;
break;
case 'p': cout << "A maple is a pianisit." << endl;
break;
case 't': cout << "A maple is a tree." << endl;
break;
case 'g': cout << "A maple is a game." << endl;
break;
default: cout << "请输入c, p, t, or g中的一个: ";
cin.ignore();
}
cin.get(iput);
}
}
void show_menu()
{
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name b. display by title" << endl;
cout << "c. display by bopname d. display by preference" << endl;
cout << "q. quit" << endl;
}
void test04()
{
const int NUM = 5;
const int strsize = 20;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void show_menu();
char ch;
bop people[NUM] =
{
{"Wimp Macho", "Teacher", "WMA", 0},
{"Raki Rhodes", "Junior Programmer", "RHES", 1},
{"Celia Laiter", "Professor", "MIPS", 2},
{"Hoppy Hipman", "Analyst Trainee", "HPAN", 1},
{"Pat Hand", "Animal Keeper", "LOOPY", 2}
};
show_menu();
cout << "Enter your choice: ";
while (cin >> ch && ch != 'q')
{
switch (ch)
{
case 'a':
{
for (int i = 0; i < NUM; i++)
{
cout << people[i].fullname << endl;
}
break;
}
case 'b':
{
for (int i = 0; i < NUM; i++)
{
cout << people[i].title << endl;
}
break;
}
case 'c':
{
for (int i = 0; i < NUM; i++)
{
cout << people[i].bopname << endl;
}
break;
}
case 'd':
{
for (int i = 0; i < NUM; i++)
{
switch (people[i].preference)
{
case 0:
{
cout << people[i].fullname << endl;
break;
}
case 1:
{
cout << people[i].title << endl;
break;
}
case 2:
{
cout << people[i].bopname << endl;
break;
}
}
}
break;
}
default:
{
cout << "Illegal input!" << endl;
break;
}
}
cout << "Next choice: ";
}
cout << "Bye!" << endl;
}
void test05()
{
const double TVARPS_5000 = 0.0;
const double TVARPS_5000_15000 = 0.1;
const double TVARPS_15001_35000 = 0.15;
const double TVARPS_35000 = 0.2;
double wage, tax;
cout << "Please enter your wage (q or <0 to quit): ";
while (cin >> wage && wage > 0)
{
cout << "Your wage: " << wage << " tvarps.\n";
if (wage < 5000)
{
tax = 0.0;
}
else if (wage < 15000)
{
tax = (wage - 5000) * TVARPS_5000_15000;
}
else if (wage < 35000)
{
tax = (wage - 15000) * TVARPS_15001_35000 + 10000 * TVARPS_5000_15000;
}
else
{
tax = (wage - 35000) * TVARPS_35000 + 20000 * TVARPS_15001_35000 + 10000 * TVARPS_5000_15000;
}
cout << "Your tax: " << tax << " tvarps.\n";
cout << "Next wage (q or <0 to quit): ";
}
cout << "Bye." << endl;
}
void test06()
{
struct donation
{
string name;
double money;
};
int num;
cout << "请输入捐献者数目:";
cin >> num;
donation* d = new donation[num];
int bigger = 0;
for (int i = 0; i < num; i++)
{
cout << "请输入捐献者的姓名和款项:"<> d[i].name;
cin >> d[i].money;
if (d[i].money > 10000)
{
bigger++;
}
}
if (bigger > 0 && bigger < num)
{
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money > 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
}
cout << "Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money <= 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
if (bigger == 0)
{
cout << "Grand Patrons" << endl;
cout << "none" << endl;
cout << "Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money <= 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
}
if (bigger == num)
{
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money > 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
cout << "Patrons" << endl;
cout << "none" << endl;
}
delete[]d;
}
void test07()
{
cout << "Enter words (q to quit):\n";
int type1 = 0;
int type2 = 0;
int type3 = 0;
string input;
cin >> input;
while (input != "q")
{
if (isalpha(input[0]))
{
if (input[0] == 'a' || input[0] == 'e' || input[0] == 'i' || input[0] == 'o' || input[0] == 'u')
type1++;
else
type2++;
}
else
type3++;
cin >> input;
}
cout << type1 << " words beginning with vowels" << endl;
cout << type2 << " words beginning with consonants" << endl;
cout << type3 << " others" << endl;
}
void test08()
{
char ch;
unsigned int count = 0;
ifstream infile;
string filename;
cout << "Please enter the file name: ";
getline(cin, filename);
infile.open(filename);
if (!infile.is_open()) {
cout << "Failed to open file " << filename << endl;
exit(EXIT_FAILURE);
}
while (infile.get(ch)) {
count++;
}
infile.close();
cout << "The file " << filename << " contains " << count << " characters." << endl;
}
void test09()
{
struct donation
{
char name[30];
double money;
};
ifstream inFile;
cout << "Please enter the name of your file: ";
char filename[30];
cin.getline(filename, 30);
inFile.open(filename);
int num;
inFile >> num;
char str[30];
inFile.getline(str, 1);
donation* d = new donation[num];
int bigger = 0;
for (int i = 0; i < num; i++)
{
inFile.getline(d[i].name, 30);
inFile >> d[i].money;
inFile.getline(str, 1);
if (d[i].money > 10000)
{
bigger++;
}
}
if (bigger > 0 && bigger < num)
{
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money > 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
cout << "Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money <= 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
}
if (bigger == 0)
{
cout << "Grand Patrons" << endl;
cout << "none" << endl;
cout << "Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money <= 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
}
if (bigger == num)
{
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (d[i].money > 10000)
{
cout << d[i].name << "\t" << d[i].money << endl;
}
}
cout << "Patrons" << endl;
cout << "none" << endl;
}
delete[]d;
inFile.close();
}
int main()
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
test09();
system("pause");
return 0;
}