1.编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),
同时将大写字符转换为小写字符,将小写字符转换为大写(别忘了cctype函数系列)
cpp
#include <iostream>
int main()
{
using namespace std;
char ch;
cout << "Enter characters:(enter @ to stop)" << endl;
while ((ch = cin.get()) != '@') {
if (isupper(ch))
ch = tolower(ch);
else if(islower(ch))
{
ch = toupper(ch);
}
if(!isdigit(ch))
{
cout << ch << endl;
}
}
return 0;
}
2.编写一个程序,最多将10个donation值读入一个double数组中
(如果你愿意,也可以使用模板类array)。
程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
cpp
#include <iostream>
const int num = 10;
int main() {
using namespace std;
double donation[num];
double sum = 0;
double avg;
cout << "Enter a number(Stop when entering an unnumber):" << endl;
for (int i = 0; i < num; i++) {
cin >> donation[i];
/*if(!isdigit(donation[i])){
break;
}*/
sum += donation[i];
}
avg = sum / num;
cout << "The sum of all numbers is:" << avg << endl;
cout << "The number that is larger than avg is:" << endl;
for (int i = 0; i < num; i++) {
if (donation[i] > avg) {
cout << donation[i] << endl;
}
}
return 0;
}
3.编写一个菜单驱动程序的雏形。
该程序显示一个提供四个选项的菜单--每个选项用一个字母表标记。
如果用户使用有效选项之外的字母进行响应,程序将提示用户输入有效的字母,
直到用户这样选择为止。然后,该程序使用一条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.
cpp
#include <iostream>
int main() {
using namespace std;
char ch;
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g)game" << endl;
cin >> ch;
while (isalpha(ch)) {
switch (ch)
{
case 'c':
cout << "A maple is a carnivore." << endl;
break;
case 'p':
cout << "A maple is a pianist." << endl;
break;
case 't':
cout << "A maple is a tree." << endl;
break;
case 'g':
cout << "A maple is a game." << endl;
break;
}
cout << "Please enter a c,p,t or g:";
cin >> ch;
}
return 0;
}
4.加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
//Benevolent Order of Programmers name structure
struct bop{
char fullname[strsize]; //real name
char title[strzie]; //job title
char bopname[strsize]; //secret BOP name
int preference; //0=fullname,1=title,2=bopname
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a.display by name b.diaplay 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!
cpp
#include <iostream>
const int strsize = 20;
int main() {
using namespace std;
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
};
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;
bop programmers[5] = { {"Wimp Macho", "Junior Programmer", "WM",0},
{"Raki Rhodes", "Junior Programmer", "RR", 1},
{"Celia Laiter", "Junior Programmer", "MIPS", 2},
{"Hoppy Hipman", "Analyst Trainee", "HH", 1},
{"Pat Hand", "Junior Programmer", "LOOPY", 2} };
cout << "Enter your choice:" << endl;
char choice;
cin >> choice;
while (choice == 'a' || choice == 'b' || choice == 'c' || choice == 'd' || choice == 'q') {
if (choice == 'a') {
for (int i = 0; i < 5; i++) {
cout << programmers[i].fullname << endl;
}
}
else if (choice == 'b') {
for (int i = 0; i < 5; i++) {
cout << programmers[i].title << endl;
}
}
else if (choice == 'c') {
for (int i = 0; i < 5; i++) {
cout << programmers[i].bopname << endl;
}
}
else if (choice == 'd') {
for (int i = 0; i < 5; i++) {
cout << programmers[i].preference << endl;
}
}
else if (choice == 'q') {
cout << "Bye!" << endl;
break;
}
cout << "Next choice:" << endl;
cin >> choice;
}
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+2000×0.15+3000×0.20,即 4600tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税,当用户输入负数或非数字时,循环将结束。
cpp
#include <iostream>
int main() {
using namespace std;
double tax, income;
while (true) {
cout << "Enter your income (negative number or non-numeric to exit): ";
if (!(cin >> income)) {
// 如果输入不是数字,则结束循环
break;
}
if (income < 0) {
// 如果收入为负数,则结束循环
break;
}
// 计算所得税
if (income <= 5000) {
tax = 0;
}
else if (income <= 15000) {
tax = (income - 5000) * 0.1;
}
else if (income <= 35000) {
tax = 10000 * 0.1 + (income - 15000) * 0.15;
}
else {
tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.2;
}
// 输出所得税
cout << "Your tax is: " << tax << " tvarps." << endl;
}
cout << "Thank you for using the tax calculator." << endl;
}
6.编写一个程序,记录捐助给"维护合法权利团体"的资金。该程序要求用户输入捐赠者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被存储在一个动态分配的结构数组中.每个数据结构有两个成员:用来存储姓名的字符串数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款着姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Partons开头。如果某种类别没有捐款者,则程序将打印单词"none"。该程序只显示这两种类别,而不进行排序。
cpp
#include <iostream>
#include <string>
struct Donor {
string name;
double money;
};
void getKind(Donor donor[], int num, bool isGrand) {
int count = 0;
for (int i = 0; i < num; i++) {
if (isGrand) {
if (donor[i].money > 10000) {
count++;
cout << donor[i].name << ":" << donor[i].money <<endl;
}
}
else {
if (donor[i].money <= 10000) {
count++;
cout << donor[i].name;
}
}
}
if (count == 0) {
cout << "none" << endl;
}
}
int main() {
using namespace std;
int num;
cout << "Enter the number of donator:" << endl;
cin >> num;
Donor* donor = new Donor[num];
for (int i = 0; i < num; i++) {
cout << "Please enter the donor's name:";
cin >> donor[i].name;
cout << "Please enter the donation amount:";
cin >> donor[i].money;
}
cout << "Grand Patrons:" << endl;
getKind(donor, num, true);
cout << "Patrons:" << endl;
getKind(donor, num, false);
return 0;
}
7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字母打头的单词,然后对于通过isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words (q to quit):
The 12 awesome oxem ambled
quiety across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others
cpp
#include <iostream>
#include <string>
int main() {
using namespace std;
string word;
int vowelNum = 0;
int consonantNum = 0;
int othersNum = 0;
cout << "Enter words (q to quit):" << endl;
while (cin >> word) {
if (word == "q") {
break;
}
else {
if (isalpha(word[0])){
switch (word[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowelNum++;
break;
default:
consonantNum++;
break;
}
}
else {
othersNum++;
}
}
}
cout << vowelNum << " words beginning with vowels." << endl;
cout << consonantNum << " words beginning with consonant." << endl;
cout << othersNum << " words beginning with others." << endl;
return 0;
}
8.编写一个程序,他打开一个文件,逐个字地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
cpp
#include <iostream>
#include <string>
#include <fstream>
int main() {
using namespace std;
string filename = "1.txt";
char c;
fstream file;
int count = 0;
file.open(filename);
if (!file.is_open()) {
cout << "Can't open!" << endl;
}
while (file.get(c)) {
if (isalpha(c)) {
count++;
}
}
cout << "The number of characters is " << count << endl;
file.close();
return 0;
}
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
cpp
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Donor {
string name;
double money;
};
void getKind(Donor donor[], int num, bool isGrand) {
int count = 0;
for (int i = 0; i < num; i++) {
if (isGrand) {
if (donor[i].money > 10000) {
count++;
cout << donor[i].name << ":" << donor[i].money <<endl;
}
}
else {
if (donor[i].money <= 10000) {
count++;
cout << donor[i].name;
}
}
}
if (count == 0) {
cout << "none" << endl;
}
}
int main() {
string fileName = "1.txt";
fstream file(fileName);
if (!file.is_open()) {
cout << "Can't open!" << endl;
}
while (file.get()) {
}
int num = 0;
(file >> num).get();
Donor* donor = new Donor[num];
for (int i = 0; i < num; i++) {
file >> donor[i].money;
file >> donor[i].name;
}
cout << "Grand Patrons:" << endl;
getKind(donor, num, true);
cout << "Patrons:" << endl;
getKind(donor, num, false);
return 0;
}