1. 编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。
#include<iostream> #include<cctype>//prototypes for character functions using namespace std; int main() { char ch;//定义一个char类型变量 cout << "Enter the character,and type @"<<endl; cin.get(ch); while (ch != '@ ') { if (isdigit(ch))//当输入的是数字时 { cin.get(ch);//输出数字 continue; } else if (islower(ch))//当输入的是小写字母时 { ch = toupper(ch);//小写 -> 大写 } else if (isupper(ch))//当输入的是大写字母时 { ch = tolower(ch);//大写 -> 小写 } cout << ch ; cin.get(ch); } system("pause"); return 0; } //Enter the character, and type @ //@ //@ //china //CHINA // // //CHINA //china
2. 编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include<iostream> #include<array> using namespace std; int main() { array<double, 10> donation;//使用array模板定义数组长度为10 double input; int counter = 0; double average, sum = 0; int bigger = 0; //sum ->和 average ->平均值 counter ->元素数 bigger->大于平均值的个数 cout << "Enter the double numerical:"; cin >> input; while (input != 0 && counter < 10) { donation[counter++] = input; cout << "No." << counter << " Data input to Array." << endl; cout << "Enter the double numerical."; cin >> input; }//通过while循环输入数据,当输入非数字时或超过10个元素时推出循环 for (int i = 0; i < counter; i++) { sum += donation[i];//十个数的总和 } average = sum / counter; for (int i = 0; i < counter; i++) { if (donation[i] > average) bigger++; }//通过遍历计算大于平均值的元素个数 cout << "The Average is " << average << " and " << bigger; cout << " data bigger than average." << endl; system("pause"); return 0; } //Enter the double numerical : 10 //No.1 Data input to Array. //Enter the double numerical.20 //No.2 Data input to Array. //Enter the double numerical.30 //No.3 Data input to Array. //Enter the double numerical.40 //No.4 Data input to Array. //Enter the double numerical.50 //No.5 Data input to Array. //Enter the double numerical.60 //No.6 Data input to Array. //Enter the double numerical.70 //No.7 Data input to Array. //Enter the double numerical.80 //No.8 Data input to Array. //Enter the double numerical.90 //No.9 Data input to Array. //Enter the double numerical.100 //No.10 Data input to Array. //Enter the double numerical.110 //The Average is 55 and 5 data bigger than average. //请按任意键继续. . .
3. 编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
f
Please enter a c, p, t, or g: q
Please enter a c, p, t, or g: t
A maple is a tree.
#include<iostream> using namespace std; void showmenu();//showmenu()函数声明 int main() { char choice; showmenu(); cin.get(choice);//显示菜单,并读取用户输入,保存至choice变量中 while (choice != 'c' && choice != 'p' && choice != 't' && choice != 'g') { cin.get(); cout << "Please enter a,c,p,t,or g "; cin.get(choice); } switch (choice) { case'c': break; case'p': break; case't': cout << "Asample is a tree." << endl; case'g': break; } system("pause"); return 0; } void showmenu() { cout << "Please enter one of the following choice:\n"; cout << "c) carnivore \t\t\t p) pianist\n"; cout << "t) tree\t\t\t\t g)game\n"; }//showmenu()函数只负责菜单传递信息的输出 //Please enter one of the following choice : //c) carnivore p) pianist //t) tree g)game //f //Please enter a, c, p, t, or g q //Please enter a, c, p, t, or g t //Asample is a tree. //请按任意键继续. . .
4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
// Benevolent Order of Programmer 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 Programmer 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<cstring> using namespace std; const int strsize = 40; const int usersize = 5; //Benevolent Order of Programmer 姓名结构体 struct bop { char fullname[strsize];//真实姓名 char title[strsize];//头衔 char bopname[strsize];//秘密BOP姓名 int preference;//0 = fullname,1 = title;2 = bopname }; bop bop_user[usersize] = { {"Wimp Macho","Programmer","MIPS",0}, {"Raki Rhodes","Junior Programmer",1}, {"Celia Laiter","","MIPS",2}, {"Hoppy Hipman","Analyst Trainee","",1}, {"Pat Hand","","LOOPY",2}, };//定义常量,定义结构体初始化bop数组信息 void showmenu();//showmenu()函数声明 void print_by_name();//打印姓名函数声明 void print_by_pref();//打印偏好函数声明 void print_by_title();//打印头衔函数声明 void print_by_bopname();//打印秘密姓名函数声明 void create_info(); int main() { char choice; //create_info(); showmenu(); cout << "Enter your choice:"; cin.get(choice);//显示菜单读取用户输入 while (choice != 'q') { switch (choice) { case 'a': print_by_name(); break; case 'b': print_by_title(); break; case 'c': print_by_bopname(); break; case 'd': print_by_pref(); break; default: cout << "Please enter character a ,b ,c ,d , or q:" << endl; } //通过switch语句对应的函数进行显示 cout << "Next choice:"; cin.get(choice); } //将switch语句放在while循环中,可以反复循环给下那个功能 cout << "Bye!" << endl; system("pause"); return 0; } void showmenu() { cout << "a. display by name\t\t b. display by title" << endl; cout << "c. display by bopname\t\t d. display by preference" << endl; cout << "q. quit" << endl; }//显示菜单 void print_by_name() { for (int i = 0; i < usersize; i++) { if (bop_user[i].fullname == 0) break; else cout << bop_user[i].fullname << endl; } } void print_by_pref() { for (int i = 0; i < usersize; i++) { if (bop_user[i].fullname == 0) break; else { switch (bop_user[i].preference) { case 0: cout << bop_user[i].fullname << endl; break; case 1: cout << bop_user[i].title << endl; break; case 2: cout << bop_user[i].bopname << endl; break; } } } } void print_by_title() { for (int i = 0; i < usersize; i++) { if (bop_user[i].fullname == 0) break; else { cout << bop_user[i].title << endl; break; } } } void print_by_bopname() { for (int i = 0; i < usersize; i++) { if (bop_user[i].fullname == 0) break; else { cout << bop_user[i].bopname << endl; break; } } } void create_info() { for (int i = 0; i < usersize; i++) { cout << "Enter the user's full name:"; cin.getline(bop_user[i].fullname, strsize); cout << "Enter the user's title:"; cin.getline(bop_user[i].title, strsize); cout << "Enter the user's bopname:"; cin.getline(bop_user[i].bopname, strsize); cout << "Enter the user's preference:"; cin >> bop_user[i].preference; cout << "Next...(f for finished):"; cin.get(); if (cin.get() == 'f') break; } } //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 : Please enter character a, b, c, d, or q : //Next choice : d //Wimp Macho //Raki Rhodes //MIPS //Analyst Trainee //LOOPY //Next choice : Please enter character a, b, c, d, or q : //Next choice : q //Bye! //请按任意键继续. . .
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> using namespace std; int main() { float tax, salary; cout << "Please enter your salary:"; cin >> salary; while (salary > 0) { if (salary < 5000) { tax = 0; } else if (salary <= 15000) { tax = (salary - 5000) * 0.10; } else if (salary <= 35000) { tax = 10000 * 0.10 + (salary - 15000) * 0.15; } else if (salary > 35000) { tax = 10000 * 0.10 + 20000 * 0.15 + (salary - 30000) * 0.20; } cout << "Your salary is " << salary << " trarps,and you should pay "; cout << tax << " trarps of tax." << endl; cout << "Enter your salary:"; cin >> salary; } cout << "Bye!" << endl; system("pause"); return 0; } //Please enter your salary : 4000 //Your salary is 4000 trarps, and you should pay 0 trarps of tax. //Enter your salary : 15000 //Your salary is 15000 trarps, and you should pay 1000 trarps of tax. //Enter your salary : 25000 //Your salary is 25000 trarps, and you should pay 2500 trarps of tax. //Enter your salary : 40000 //Your salary is 40000 trarps, and you should pay 6000 trarps of tax. //Enter your salary : 0 //Bye! //请按任意键继续...
6. 编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐赠者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名和字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐献者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类型没有捐献者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include<iostream> #include<string> using namespace std; struct patrons { string full_name; double fund; };//捐款人基本信息的结构体 int main() { int patrons_number; patrons *ppatrons; cout << "How many patrons ? "; cin >> patrons_number; cin.get(); ppatrons = new patrons[patrons_number];//建立动态数组 int id = 0; bool empty = true; cout << "Starting to input patron's info:"; while (id < patrons_number) { cout << "Enter the full name of patrons: "; getline(cin, ppatrons[id].full_name); cout << "Enter the fuud of " << ppatrons[id].full_name << " : "; cin >> ppatrons[id].fund; cin.get(); id++; cout << "Continue to input,or press (f) to finished."; if (cin.get() == 'f') break; }//建立捐款人名单 cout << "Grand Patrons" << endl; for (int i = 0; i < patrons_number; i++) { if (ppatrons[i].fund >= 1000) { cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl; empty = false; } } //查询Grand Patrons名单,如果empty为true,输出NONE if (empty) cout << "NONE" << endl; empty = false; cout << "Patrons" << endl; for (int i = 0; i < patrons_number; i++) { if (ppatrons[i].fund < 1000) { cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl; } } if (empty) cout << "NONE" << endl; system("pause"); return 0; } //How many patrons ? 2 //Starting to input patron's info:Enter the full name of patrons: LI MING //Enter the fuud of LI MING : 5000 //Continue to input, or press(f) to finished. //Enter the full name of patrons : LI HUA //Enter the fuud of LI HUA : 1000 //Continue to input, or press(f) to finished.f //Grand Patrons //LI MING : 5000 //LI HUA : 1000 //Patrons //请按任意键继续. . .
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 consotants
2 others
#include<iostream> #include<cctype> using namespace std; int main() { char words[40]; int vowel, consonant, others; vowel = consonant = others = 0; cout << "Enter words(q to quit):" << endl; cin >> words; while (strcmp(words, "q") != 0) { if (!isalpha(words[0])) { others++; }//统计非字母开头的单词 else { switch (words[0]) { case 'a': case 'e': case 'i': case 'o': case 'u': vowel++;//统计以元音字母开头的单词 break; default: consonant++;//统计非元音字母开头的单词 } } cin >> words; } cout << vowel << " words beginning with vowel." << endl; cout << consonant << " words beginning with consonants." << endl; system("pause"); return 0; } //Enter words(q to quit) : //The 12 awesome oxen ambled //quitely across 15 meters of lawn //q //5 words beginning with vowel. //4 words beginning with consonants. //请按任意键继续. . .
8. 编写一个程序,它打开一个文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include<iostream> #include<fstream> using namespace std; int main() { ifstream fin;//定义文件输入流 string file_name; cout << "Enter the file name:"; getline(cin, file_name); fin.open(file_name); if (!fin.is_open()) { cout << "Error to open file." << endl; exit(EXIT_FAILURE); } char read_char; int char_counter = 0; while (!fin.eof()) { fin >> read_char; char_counter++; } cout << "The file " << file_name << " contains " << char_counter << " characters." << endl; fin.close();//关闭文件 system("pause"); return 0; }
建立txt文件如下所示:
9. 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
Tammy Tubbs
5000
Rich Raptor
55000
#include <iostream> #include <fstream> #include<string> using namespace std; struct patrons { string full_name; double fund; }; //表示捐款人信息的结构体 int main() { ifstream fin; string file_name; cout << "Enter the file name:"; getline(cin, file_name); fin.open(file_name); if (!fin.is_open()) { cout << "Error to open file:" << endl; exit(EXIT_FAILURE); } int patrons_number; patrons *ppatrons; int id = 0; bool empty = true; fin >> patrons_number; if (patrons_number <= 0) { exit(EXIT_FAILURE); } ppatrons = new patrons[patrons_number]; fin.get(); //读取人数,创建动态数组 while (!fin.eof() && id < patrons_number) { getline(fin, ppatrons[id].full_name); cout << "Read Name: " << ppatrons[id].full_name << endl; fin >> ppatrons[id].fund; cout << "Read fund: " << ppatrons[id].fund << endl; fin.get(); id++; } //循环读取捐款人信息,也可以使用for循环 fin.close();//关闭文件 cout << "Grand Patrons" << endl; for (int i = 0; i < patrons_number; i++) { if (ppatrons[i].fund >= 10000) { cout << ppatrons[i].full_name << " ; " << ppatrons[i].fund << endl; empty = false; } } if (empty) cout << "NONE" << endl; empty = false; cout << "Patrons" << endl; for (int i = 0; i < patrons_number; i++) { if (ppatrons[i].fund < 10000) { cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl; } } if (empty) cout << "NONE" << endl; system("pause"); return 0; }
建立txt文件如下所示: