1.请编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)
#include
#include
using namespace std;
int main() {
char ch;
cout << "Please enter string: " << endl;
while (cin >> ch && ch != '@') {
if (!isdigit(ch)) {
if (isupper(ch)) {
ch = ch + 32;
cout << ch;
}
else if (islower(ch)) {
ch = ch - 32;
cout << ch;
}
}
}
cout << endl;
system("pause");
return 0;
}
2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include
using namespace std;
const int MAX = 10;
int main() {
double arr[MAX];
double number;
double total = 0;
int i = 0;
cout << "Please enter a number: ";
while (i < MAX && cin >> number) {
arr[i] = number;
total += number;
i++;
if (i average)
high++;
}
cout << "The number of higher than average is " << high << endl;
system("pause");
return 0;
}
3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提供用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
Please enter a c , p , t , or g: q
Please enter a c , p , t , or g: t
A maple is a tree.
#include
#include
using namespace std;
int main() {
char ch;
string c = "carnivore";
string p = "pianist";
string t = "tree";
string g = "game";
//cout << "Please enter one of the following choices:\n";
//cout << "c) " << c << " " << "p) " << p << "\n";
//cout << "t) " << t << " " << "g) " << g << "\n";
//while (cin >> ch) {
// switch (ch) {
// case 'c':cout << "A maple is a " << c << endl;
// break;
// case 'p':cout << "A maple is a " << p << endl;
// break;
// case 't':cout << "A maple is a " << t << endl;
// break;
// case 'g':cout << "A maple is a " << g << endl;
// break;
// default:cout << "Please enter a c, p, t or g: ";
// }
//}
cout << "Please enter one of the following choices:\n";
cout << "c) " << c << " " << "p) " << p << "\n";
cout << "t) " << t << " " << "g) " << g << "\n";
while (cin >> ch && ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') {
cout << "Please enter a c, p, t or g: ";
}
switch (ch) {
case 'c':cout << "A maple is a " << c << endl;
break;
case 'p':cout << "A maple is a " << p << endl;
break;
case 't':cout << "A maple is a " << t << endl;
break;
case 'g':cout << "A maple is a " << g << endl;
break;
}
system("pause");
return 0;
}
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
using namespace std;
const int strsize = 20;
const int human_num = 5;
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main() {
bop human[human_num] = { {"Wimp Macho", "Teacher","Captain America", 0},
{"Raki Rhides", "Junior Programmer", "Raytheon", 1},
{"Celia Laiter", "Doctor","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","Black widow", 1},
{"Pat Hand","Student", "LOOPY", 2} };
cout << "Benevolent Order of Programmers Report\n";
cout << "a. display by name b.display by title\n";
cout << "c. display by bopname d. display by preference\n";
cout << "q. quit\n";
char ch;
cout << "Enter your choice: ";
while (cin >> ch && ch != 'q') {
switch (ch) {
case 'a':
{
for (int i = 0; i < human_num; i++) {
cout << human[i].fullname << endl;
}
break;
}
case 'b':
{
for (int i = 0; i < human_num; i++)
cout << human[i].title << endl;
break;
}
case 'c':
{
for (int i = 0; i < human_num; i++)
cout << human[i].bopname << endl;
break;
}
case 'd':
{
for (int i = 0; i < human_num; i++) {
if (human[i].preference == 0)
cout << human[i].fullname << endl;
else if (human[i].preference == 1)
cout << human[i].title << endl;
else
cout << human[i].bopname << endl;
}
break;
}
}
cout << "Next choice: ";
}
cout << "Bye!" << endl;
system("pause");
return 0;
}
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
using namespace std;
const float Rate0 = 0;
const float Rate1 = 0.1;
const float Rate2 = 0.15;
const float Rate3 = 0.2;
int main() {
float income, tax;
cout << "Please enter your income(enter postive number and not number to quit):";
while (cin >> income && income > 0)
{
if (income < 5000)
tax = 0;
else if (income < 15000)
tax = 5000 * Rate0 + (income - 5000) * Rate1;
else if (income < 35000)
tax = 5000 * Rate0 + 10000 * Rate1 + (income - 15000)*Rate2;
else
tax = 5000 * Rate0 + 10000 * Rate1 + 20000 * Rate2 + (income - 35000) * Rate3;
cout << "The tax that you should pay is " << tax << endl;
cout << "Please enter your income(enter postive number and not number to quit):";
}
system("pause");
return 0;
}
6. 编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来储存款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include
#include
using namespace std;
struct information {
string name;
double money;
};
void show_grand(information * info, int number);
void show_other(information * info, int number);
int main() {
int number;
cout << "Please enter the number of people: ";
cin >> number;
cin.get();
information * info = new information[number];
for (int i = 0; i < number; i++)
{
cout << "Please enter the " << i + 1 << "-th name: ";
getline(cin, info[i].name);
cout << "Please enter the " << i + 1 << "-th money:";
cin >> info[i].money;
cin.get();
}
cout << "Patrons" << endl;
show_grand(info, number);
show_other(info, number);
system("pause");
return 0;
}
void show_grand(information * info, int number)
{
cout << "Grand Patrons:" << endl;
int count_grand = 0;
for (int i = 0; i < number; i++)
{
if (info[i].money > 10000)
{
cout << info[i].name << "\t" << info[i].money << endl;
count_grand++;
}
}
if (count_grand == 0)
cout << "none" << endl;
}
void show_other(information * info, int number)
{
cout << "other Patrons:" << endl;
int count_other = 0;
for (int i = 0; i < number; i++)
{
if (info[i].money <= 10000)
{
cout << info[i].name << "\t" << info[i].money << endl;
count_other++;
}
}
if (count_other == 0)
cout << "none" << endl;
}
7. 编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words (q to quit) :
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others
#include
using namespace std;
int main()
{
char str[20];
int number_vowel = 0;
int number_consonants = 0;
int number_others = 0;
cout << "Enter words (q to quit):" << endl;
while (cin >> str)
{
if (strcmp(str, "q") == 0)
break;
char ch = str[0];
if (isalpha(ch))
{
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
number_vowel++;
break;
default:
number_consonants++;
}
}
else
number_others++;
}
cout << number_vowel << " words beginning with vowels\n";
cout << number_consonants << " words beginning with consonants\n";
cout << number_others << " others\n";
system("pause");
return 0;
}
8. 编写一个程序,它打开一个文件,逐个字符的读取该文件,直到到达文件末尾,然后指出该文件包含多少个字符。
9. 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000