1 请编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。
#include <iostream>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char ch;
while( cin.get(ch) && ch != '@' )
{
if( isdigit(ch) )
continue;
else if( islower(ch) )
cout<<(char)toupper(ch);
else if( isupper(ch) )
cout<<(char)tolower(ch);
else
cout<<ch;
}
return 0;
}
char ch 每次只读取一个字符,cin 遇到空格,回车等实际中常见字符是就会自动停止读取,而cin.get()则可以读取这些信息,此处(char)是什么意思,是强制声明转换为char类型??
2 编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include <iostream>
#include <string>
#include <cctype>
#include <array>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
double donations[10];
double total = 0, avg = 0;
int i = 0, j = 0,large = 0;
for ( i = 0; i < 10; i++)
{
cout << i+1 <<"enter the donations,no more than 10,alpha to quit.\n";
if (!(cin >> donations[i]))
{
break;
}
total += donations[i];
j++;
}
avg = total / j;
cout << total << "total donations.\n";
cout << avg << "average donations.\n";
for (i = 0; i < j; i++)
{
if (donations[i]>avg)
{
large++;
}
}
cout << large << "larger than average.\n";
while (1)
{
}
return 0;
}
最初考虑使用cctype的函数isdigit,后来发现cout出来都是ASCII码,查了一下原因是因为isdigit函数智能判别0-9的个位数,参见175页的程序6.8,该程序中使用的就是char ch; cin.get(ch),每次接受输入的一个字符,然后使用isdigit,isalpha等函数判定输入内容为数字、字母、标点等字符。
3 编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单,每个选项用一个字母标记。若用户使用有效选项之外的字母进行响应,程序提示用户输入一个有效的字母,直到输入正确为止。然后程序使用一条switch语句,根据用户选择执行操作。
int main()
{
char ch;
bool quit = true;
cout << "please enter 'c, p, t, g', other word no accepted!" << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
while (cin>>ch && quit)
{
switch (ch)
{
case 'c': cout << "tigers are carnivore." << endl;
break;
case 'p': cout << "Richard is a pianist." << endl;
break;
case 't': cout << "Apple is born from tree." << endl;
break;
case 'g': cout << "I like video game." << endl;
break;
default: cout << "please enter 'c, p, t, g', other word no accepted!" << endl;
break;
quit = false;
}
continue;
}
return 0;
}
每个case执行完之后,如果不想执行后续的case,就需要增加break,否则后续语句将继续执行,default默认执行除去上述case案例之后所有案例。在这里犯了一个重点错误,就是错把if当做了循环来使用,if,switch+case,是判断,while,do++while,for 是循环,配合continue来使用。
4 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
/ Benevolent Order of Programmers name structure
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
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
注意,“display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
Benevolent Order of Programmers Report
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
Enter your choice: a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
Wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice: q
Bye!
#include <iostream>
#include <string>
#include <cctype>
#include <array>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
char ch;
struct bop {
char fullname[50]; //real name
char title[50]; //job title
char bopname[50]; //secret bop name
int perference; //0:fullname 1:title 2:bopname
};
int i = 0;
bop Wimp = { "Wimp Macho", "CEO", "E", 1 };
bop Raki = { "Raki Rhodes", "CTO", "F", 0 };
bop Celia = { "Celia Laiter", "CFO", "F", 2 };
bop Hoppy = { "Hoppy Hipaman", "COO", "O", 2 };
bop Pat = { "Pat Hand", "CIO", "I", 0 };
bop Name[5] = { Wimp,Raki,Celia,Hoppy,Pat };
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;
while (cin >> ch)
{
if (ch == 'q')
{
cout << "Bye!!" << endl;
break;
}
switch (ch)
{
case 'a':
cout << "these are the fullname." << endl;
for (i = 0; i < 5; i++)
{
cout << Name[i].fullname << endl;
}
break;
case 'b':
cout << "these are the title." << endl;
for (i = 0; i < 5; i++)
{
cout << Name[i].title << endl;
}
break;
case 'c':
cout << "these are the bopname." << endl;
for (i = 0; i < 5; i++)
{
cout << Name[i].bopname << endl;
}
break;
case 'd':
cout << "As preference." << endl;
for (i = 0; i < 5; i++)
{
int preference = Name[i].perference;
switch (preference)
{
case 0:cout << Name[i].fullname << endl;
break;
case 1:cout << Name[i].title << endl;
break;
case 2:cout << Name[i].bopname << endl;
break;
}
}
break;
default:
break;
}
cout << "enter a, b, c, d." << endl;
}
return 0;
}
知识点: struct、struct数组、while循环、switch循环。重点if不是循环,是判断,在if判断{}里面的break,就是针对while循环的break,执行该语句之后就直接结束循环。需要重点学习while循环。
5 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps: 不收税
5001~15000 tvarps: 10%
15001~35000 tvarps: 15%
35000 tvarps 以上: 20%
例如,收入为38000 tvarps时,所得税为5000 * 0.00 + 10000 * 0.10 + 20000 * 0.15 + 3000 * 0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
double income = 0, tax = 0;
bool flag = 0;
while (1)
{
cout << "enter the income.\n";
if (!(cin >> income) )
{
cout << "输入有错!请重新输入数字" << endl;
cin.clear();
cin.ignore(80,'\n');
continue;
}
if (cin)
{
if (income <= 5000)
{
tax = 0;
cout << "the income is"<< income <<" and the tax is " << tax << ".\n";
continue;
}
else if (income > 5000 && income <= 15000)
{
tax = (income - 5000) * 0.1;
cout << "the income is" << income << " and the tax is " << tax << ".\n";
continue;
}
else if (income > 15000 && income <= 35000) {
tax = (income - 15000) * 0.15 + 10000 * 0.1;
cout << "the income is" << income << " and the tax is " << tax << ".\n";
continue;
}
else if (income > 35000) {
tax = (income - 15000)*0.2 + 20000 * 0.15 + 10000 * 0.1;
cout << "the income is" << income << " and the tax is " << tax << ".\n";
continue;
}
}
}
return 0;
}
本来想能够实现数值分阶计算就完事儿的,后想要做到非数字识别,引导用户重新输入,所以就考虑加入错误输入判定也就是if(!(cin>> income))这个判断,执行之后引导回到while循环重新开始接受输入,这里是重点,需要配合cin.clear和cin.ignore(清空的位数,结束符),两个参数以先到为准。不然就会陷入死循环,不停重复错误输入判定模块。
6 编写程序,每次读取一个单词,知道用户只输入q结束。该程序支出多少单词以元音字母开头,多少以辅音字母开头,还有多少不属于此两类。