第一题
题目描述
编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符 转换为小写,将小写字符转换为大写(别忘了cctype 函数系列)。
代码片段
#include
#include
using namespace std;
void printconver(char);
int main()
{
char ch;
cin >> ch;
printconver(ch);
while (cin.get() != EOF)
;
return 0;
}
void printconver(char ch)
{
if (ch == '@')
return;
else
{
char m;
cin >> m;
printconver(m);
if (islower(ch))
cout << char(toupper(ch));
else if (isupper(ch))
cout << char(tolower(ch));
else if (isdigit(ch))
;
else
cout << ch;
}
}
第二题
题目描述
编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板 类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于 平均值。
代码片段
#include
#include
using namespace std;
int main()
{
array a;
int i = 0;
double sum = 0;
while (cin >> a[i])
{
sum += a[i];
i++;
}
sum /= i;
cout << sum << " is the average number.na";
int count = 0;
for (int j = 0; j < i; j++)
if (a[j] > sum)
count++;
cout<<"There are "<
第三题
题目描述
编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单---每个选项用一个字母标记。 如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。 然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore
p) pianist
t) tree
g) game
f
PleaseenteraC,P,t,org: q
PleaseenteraC,p,t,org: t
A maple is a tree.
代码片段
#include
using namespace std;
int freal(void);
int main()
{
cout << "Please enter one of the following choices:nc) carnivore p) pianistnt) tree g) gamen";
while (freal());
while (cin.get() != EOF);
return 0;
}
int freal(void)
{
char ch;
cin.get(ch).get();
switch (ch)
{
case 'c':
cout << "I don't konw what is carnivore.n";
break;
case 'p':
cout << "I also don't what the meaning of pianist.n";
break;
case 't':
cout << "A maple is a tree.n";
break;
case 'g':
cout << "LOL is kind of game.n";
break;
default:
cout<<"Pleaseenter a c,p,t,or g: ";
return 1;
}
return 0;
}
第四题
题目描述
加入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 Progr ammer
MIPs
Analyst Trainee
LOOPY
Next choice: q
Bye !
代码片段
#include
using namespace std;
#define size 5
int freal(void);
void byname(void);
void bytitle(void);
void bybop(void);
void bypre(void);
// Benevolent Order of Programmers name structure
struct bop
{
string fullname; // real name
string title; // job title
string bopname; // secret BOP name
int preference; // 0 = fullname, 1 = title, 2 = bopname
};
bop a[size] = {{"name1", "title1", "bop1", 0}, {"name2", "title2", "bop2", 1}, {"name3", "title3", "bop3", 2}, {"name4", "title4", "bop4", 1}, {"name5", "title5", "bop5", 0}};
int main()
{
cout << "Benevolent Order of Programmers Reportna. display by name b. display by titlenC. display by bopname d. display by preferencenq. quitnEnter your choice: ";
while (freal())
cout << "Next choice:";
while (cin.get() != EOF)
;
return 0;
}
int freal(void)
{
char ch;
cin.get(ch).get();
switch (ch)
{
case 'a':
byname();
break;
case 'b':
bytitle();
break;
case 'c':
bybop();
break;
case 'd':
bypre();
break;
case 'q':
cout << "Bye!n";
return 0;
default:
cout << "Invalid input! Please enter again!n";
}
return 1;
}
void byname(void)
{
for (int i = 0; i < size; i++)
cout << a[i].fullname << endl;
}
void bytitle(void)
{
for (int i = 0; i < size; i++)
cout << a[i].title << endl;
}
void bybop(void)
{
for (int i = 0; i < size; i++)
cout << a[i].bopname << endl;
}
void bypre(void)
{
for (int i = 0; i < size; i++)
{
if (a[i].preference == 0)
cout << a[i].fullname << endl;
if (a[i].preference == 1)
cout << a[i].title << endl;
if (a[i].preference == 2)
cout << a[i].bopname << endl;
}
}
第五题
题目描述
在Ncutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~ 15000 tvarps: 10%
15001 ~ 35000 tvarps: 15%
35000 tvarps以上: 20%
例如,收入为38000 tvarps时,所得税为
5000 x 0.00 + 10000x 0.10 + 20000 x0.15 + 3000x 0.20,
即 4600 tvarps。 请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字 时,循环将结束。
代码片段
#include
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
using namespace std;
int tva(void);
int main()
{
cout << "Please enter your income(invalid input to quit eg(x<0,x!=digital)):n";
while (tva())
cout << "Next input: ";
cout<<"Done!n";
cin.clear();
while (cin.get() != EOF)
;
return 0;
}
int tva(void)
{
double in;
if ((cin >> in)&&(in>=0))
{
double result = ((min(5000, max(in, 0)) - 0) * 0 + (min(15000, max(in, 5000)) - 5000) * 0.1 + (min(35000, max(in, 15000)) - 15000) * 0.15 + (max(in, 35000) - 35000) * 0.2);
cout << "Income tax is " << result << endl;
return 1;
}
else
return 0;
}
第六题
题目描述
编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后 要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两 个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后, 程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题, 指出下面的捐 款者是重要捐款人(Grand Patrons)。 然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某 种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
代码片段
#include
#include
using namespace std;
struct list
{
string name;
double money;
} * a;
int main()
{
int n;
cout << "How many persons will donate?:";
(cin >> n).get();
a = new list[n];
cout << "Plese enter the name and donation:n";
for (int i = 0; i < n; i++)
{
cout << "#" << i + 1 << " The name:";
getline(cin,a[i].name);
cout<<"#"<> a[i].money).get();
}
int flag = 0;
cout << " Grand Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money >= 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
flag = 0;
cout << " Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money < 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
while (cin.get() != EOF)
;
return 0;
}
第七题
题目描述
编写一个程序,它每次读取一个单词,直到用户只输入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
#include
#include
using namespace std;
int main()
{
int v, c, o;
v = c = o = 0;
string word;
cout << "Enter words (q to quit) :n";
while (1)
{
cin >> word;
if (word == "q")
break;
else if (isalpha(word[0]))
{
if ((tolower(word[0]) == 'a') || (tolower(word[0]) == 'e') || (tolower(word[0]) == 'i') || (tolower(word[0]) == 'o') || (tolower(word[0]) == 'u'))
v++;
else
c++;
}
else
o++;
}
cout << v << " words beginning with vowels.n";
cout << c << " words beginning with consonants.n";
cout << o << " othersn";
while (cin.get() != EOF)
;
return 0;
}
第八题
题目描述
编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文 件中包含多少个字符。
代码片段
#include
#include
#include
using namespace std;
int main()
{
ifstream fin;
fin.open("6.doc");
if (!fin.is_open())
{
cout << "Error!";
while (cin.get() != EOF)
;
return 0;
}
char ch;
long long count = 0;
while (fin.get() != EOF)
count++;
cout << fixed;
cout << "There is " << count << " chars in this file.n";
while (cin.get() != EOF)
;
fin.close();
return 0;
}
第九题
题目描述
完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成 对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面(此文件命名为6.doc):
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
代码片段
#include
#include
#include
#include
using namespace std;
struct list
{
string name;
double money;
} * a;
int main()
{
ifstream fin;
fin.open("6.doc");
if (!fin.is_open())
{
cout << "Error!";
while (cin.get() != EOF);
return 0;
}
int n;
(fin >> n).get();
a = new list[n];
for (int i = 0; i < n; i++)
{
getline(fin, a[i].name);
(fin >> a[i].money).get();
}
int flag = 0;
cout << " Grand Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money >= 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
flag = 0;
cout << " Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money < 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
fin.close();
while (cin.get() != EOF);
return 0;
}
编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符 转换为小写,将小写字符转换为大写(别忘了cctype 函数系列)。
代码片段
#include
#include
using namespace std;
void printconver(char);
int main()
{
char ch;
cin >> ch;
printconver(ch);
while (cin.get() != EOF)
;
return 0;
}
void printconver(char ch)
{
if (ch == '@')
return;
else
{
char m;
cin >> m;
printconver(m);
if (islower(ch))
cout << char(toupper(ch));
else if (isupper(ch))
cout << char(tolower(ch));
else if (isdigit(ch))
;
else
cout << ch;
}
}
第二题
题目描述
编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板 类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于 平均值。
代码片段
#include
#include
using namespace std;
int main()
{
array a;
int i = 0;
double sum = 0;
while (cin >> a[i])
{
sum += a[i];
i++;
}
sum /= i;
cout << sum << " is the average number.na";
int count = 0;
for (int j = 0; j < i; j++)
if (a[j] > sum)
count++;
cout<<"There are "<
第三题
题目描述
编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单---每个选项用一个字母标记。 如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。 然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore
p) pianist
t) tree
g) game
f
PleaseenteraC,P,t,org: q
PleaseenteraC,p,t,org: t
A maple is a tree.
代码片段
#include
using namespace std;
int freal(void);
int main()
{
cout << "Please enter one of the following choices:nc) carnivore p) pianistnt) tree g) gamen";
while (freal());
while (cin.get() != EOF);
return 0;
}
int freal(void)
{
char ch;
cin.get(ch).get();
switch (ch)
{
case 'c':
cout << "I don't konw what is carnivore.n";
break;
case 'p':
cout << "I also don't what the meaning of pianist.n";
break;
case 't':
cout << "A maple is a tree.n";
break;
case 'g':
cout << "LOL is kind of game.n";
break;
default:
cout<<"Pleaseenter a c,p,t,or g: ";
return 1;
}
return 0;
}
第四题
题目描述
加入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 Progr ammer
MIPs
Analyst Trainee
LOOPY
Next choice: q
Bye !
代码片段
#include
using namespace std;
#define size 5
int freal(void);
void byname(void);
void bytitle(void);
void bybop(void);
void bypre(void);
// Benevolent Order of Programmers name structure
struct bop
{
string fullname; // real name
string title; // job title
string bopname; // secret BOP name
int preference; // 0 = fullname, 1 = title, 2 = bopname
};
bop a[size] = {{"name1", "title1", "bop1", 0}, {"name2", "title2", "bop2", 1}, {"name3", "title3", "bop3", 2}, {"name4", "title4", "bop4", 1}, {"name5", "title5", "bop5", 0}};
int main()
{
cout << "Benevolent Order of Programmers Reportna. display by name b. display by titlenC. display by bopname d. display by preferencenq. quitnEnter your choice: ";
while (freal())
cout << "Next choice:";
while (cin.get() != EOF)
;
return 0;
}
int freal(void)
{
char ch;
cin.get(ch).get();
switch (ch)
{
case 'a':
byname();
break;
case 'b':
bytitle();
break;
case 'c':
bybop();
break;
case 'd':
bypre();
break;
case 'q':
cout << "Bye!n";
return 0;
default:
cout << "Invalid input! Please enter again!n";
}
return 1;
}
void byname(void)
{
for (int i = 0; i < size; i++)
cout << a[i].fullname << endl;
}
void bytitle(void)
{
for (int i = 0; i < size; i++)
cout << a[i].title << endl;
}
void bybop(void)
{
for (int i = 0; i < size; i++)
cout << a[i].bopname << endl;
}
void bypre(void)
{
for (int i = 0; i < size; i++)
{
if (a[i].preference == 0)
cout << a[i].fullname << endl;
if (a[i].preference == 1)
cout << a[i].title << endl;
if (a[i].preference == 2)
cout << a[i].bopname << endl;
}
}
第五题
题目描述
在Ncutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~ 15000 tvarps: 10%
15001 ~ 35000 tvarps: 15%
35000 tvarps以上: 20%
例如,收入为38000 tvarps时,所得税为
5000 x 0.00 + 10000x 0.10 + 20000 x0.15 + 3000x 0.20,
即 4600 tvarps。 请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字 时,循环将结束。
代码片段
#include
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
using namespace std;
int tva(void);
int main()
{
cout << "Please enter your income(invalid input to quit eg(x<0,x!=digital)):n";
while (tva())
cout << "Next input: ";
cout<<"Done!n";
cin.clear();
while (cin.get() != EOF)
;
return 0;
}
int tva(void)
{
double in;
if ((cin >> in)&&(in>=0))
{
double result = ((min(5000, max(in, 0)) - 0) * 0 + (min(15000, max(in, 5000)) - 5000) * 0.1 + (min(35000, max(in, 15000)) - 15000) * 0.15 + (max(in, 35000) - 35000) * 0.2);
cout << "Income tax is " << result << endl;
return 1;
}
else
return 0;
}
第六题
题目描述
编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后 要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两 个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后, 程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题, 指出下面的捐 款者是重要捐款人(Grand Patrons)。 然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某 种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
代码片段
#include
#include
using namespace std;
struct list
{
string name;
double money;
} * a;
int main()
{
int n;
cout << "How many persons will donate?:";
(cin >> n).get();
a = new list[n];
cout << "Plese enter the name and donation:n";
for (int i = 0; i < n; i++)
{
cout << "#" << i + 1 << " The name:";
getline(cin,a[i].name);
cout<<"#"<> a[i].money).get();
}
int flag = 0;
cout << " Grand Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money >= 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
flag = 0;
cout << " Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money < 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
while (cin.get() != EOF)
;
return 0;
}
第七题
题目描述
编写一个程序,它每次读取一个单词,直到用户只输入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
#include
#include
using namespace std;
int main()
{
int v, c, o;
v = c = o = 0;
string word;
cout << "Enter words (q to quit) :n";
while (1)
{
cin >> word;
if (word == "q")
break;
else if (isalpha(word[0]))
{
if ((tolower(word[0]) == 'a') || (tolower(word[0]) == 'e') || (tolower(word[0]) == 'i') || (tolower(word[0]) == 'o') || (tolower(word[0]) == 'u'))
v++;
else
c++;
}
else
o++;
}
cout << v << " words beginning with vowels.n";
cout << c << " words beginning with consonants.n";
cout << o << " othersn";
while (cin.get() != EOF)
;
return 0;
}
第八题
题目描述
编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文 件中包含多少个字符。
代码片段
#include
#include
#include
using namespace std;
int main()
{
ifstream fin;
fin.open("6.doc");
if (!fin.is_open())
{
cout << "Error!";
while (cin.get() != EOF)
;
return 0;
}
char ch;
long long count = 0;
while (fin.get() != EOF)
count++;
cout << fixed;
cout << "There is " << count << " chars in this file.n";
while (cin.get() != EOF)
;
fin.close();
return 0;
}
第九题
题目描述
完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成 对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面(此文件命名为6.doc):
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
代码片段
#include
#include
#include
#include
using namespace std;
struct list
{
string name;
double money;
} * a;
int main()
{
ifstream fin;
fin.open("6.doc");
if (!fin.is_open())
{
cout << "Error!";
while (cin.get() != EOF);
return 0;
}
int n;
(fin >> n).get();
a = new list[n];
for (int i = 0; i < n; i++)
{
getline(fin, a[i].name);
(fin >> a[i].money).get();
}
int flag = 0;
cout << " Grand Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money >= 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
flag = 0;
cout << " Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money < 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
fin.close();
while (cin.get() != EOF);
return 0;
}
题目描述
编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板 类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于 平均值。
代码片段
#include
#include
using namespace std;
int main()
{
array a;
int i = 0;
double sum = 0;
while (cin >> a[i])
{
sum += a[i];
i++;
}
sum /= i;
cout << sum << " is the average number.na";
int count = 0;
for (int j = 0; j < i; j++)
if (a[j] > sum)
count++;
cout<<"There are "<
第三题
题目描述
编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单---每个选项用一个字母标记。 如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。 然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore
p) pianist
t) tree
g) game
f
PleaseenteraC,P,t,org: q
PleaseenteraC,p,t,org: t
A maple is a tree.
代码片段
#include
using namespace std;
int freal(void);
int main()
{
cout << "Please enter one of the following choices:nc) carnivore p) pianistnt) tree g) gamen";
while (freal());
while (cin.get() != EOF);
return 0;
}
int freal(void)
{
char ch;
cin.get(ch).get();
switch (ch)
{
case 'c':
cout << "I don't konw what is carnivore.n";
break;
case 'p':
cout << "I also don't what the meaning of pianist.n";
break;
case 't':
cout << "A maple is a tree.n";
break;
case 'g':
cout << "LOL is kind of game.n";
break;
default:
cout<<"Pleaseenter a c,p,t,or g: ";
return 1;
}
return 0;
}
第四题
题目描述
加入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 Progr ammer
MIPs
Analyst Trainee
LOOPY
Next choice: q
Bye !
代码片段
#include
using namespace std;
#define size 5
int freal(void);
void byname(void);
void bytitle(void);
void bybop(void);
void bypre(void);
// Benevolent Order of Programmers name structure
struct bop
{
string fullname; // real name
string title; // job title
string bopname; // secret BOP name
int preference; // 0 = fullname, 1 = title, 2 = bopname
};
bop a[size] = {{"name1", "title1", "bop1", 0}, {"name2", "title2", "bop2", 1}, {"name3", "title3", "bop3", 2}, {"name4", "title4", "bop4", 1}, {"name5", "title5", "bop5", 0}};
int main()
{
cout << "Benevolent Order of Programmers Reportna. display by name b. display by titlenC. display by bopname d. display by preferencenq. quitnEnter your choice: ";
while (freal())
cout << "Next choice:";
while (cin.get() != EOF)
;
return 0;
}
int freal(void)
{
char ch;
cin.get(ch).get();
switch (ch)
{
case 'a':
byname();
break;
case 'b':
bytitle();
break;
case 'c':
bybop();
break;
case 'd':
bypre();
break;
case 'q':
cout << "Bye!n";
return 0;
default:
cout << "Invalid input! Please enter again!n";
}
return 1;
}
void byname(void)
{
for (int i = 0; i < size; i++)
cout << a[i].fullname << endl;
}
void bytitle(void)
{
for (int i = 0; i < size; i++)
cout << a[i].title << endl;
}
void bybop(void)
{
for (int i = 0; i < size; i++)
cout << a[i].bopname << endl;
}
void bypre(void)
{
for (int i = 0; i < size; i++)
{
if (a[i].preference == 0)
cout << a[i].fullname << endl;
if (a[i].preference == 1)
cout << a[i].title << endl;
if (a[i].preference == 2)
cout << a[i].bopname << endl;
}
}
第五题
题目描述
在Ncutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~ 15000 tvarps: 10%
15001 ~ 35000 tvarps: 15%
35000 tvarps以上: 20%
例如,收入为38000 tvarps时,所得税为
5000 x 0.00 + 10000x 0.10 + 20000 x0.15 + 3000x 0.20,
即 4600 tvarps。 请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字 时,循环将结束。
代码片段
#include
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
using namespace std;
int tva(void);
int main()
{
cout << "Please enter your income(invalid input to quit eg(x<0,x!=digital)):n";
while (tva())
cout << "Next input: ";
cout<<"Done!n";
cin.clear();
while (cin.get() != EOF)
;
return 0;
}
int tva(void)
{
double in;
if ((cin >> in)&&(in>=0))
{
double result = ((min(5000, max(in, 0)) - 0) * 0 + (min(15000, max(in, 5000)) - 5000) * 0.1 + (min(35000, max(in, 15000)) - 15000) * 0.15 + (max(in, 35000) - 35000) * 0.2);
cout << "Income tax is " << result << endl;
return 1;
}
else
return 0;
}
第六题
题目描述
编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后 要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两 个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后, 程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题, 指出下面的捐 款者是重要捐款人(Grand Patrons)。 然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某 种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
代码片段
#include
#include
using namespace std;
struct list
{
string name;
double money;
} * a;
int main()
{
int n;
cout << "How many persons will donate?:";
(cin >> n).get();
a = new list[n];
cout << "Plese enter the name and donation:n";
for (int i = 0; i < n; i++)
{
cout << "#" << i + 1 << " The name:";
getline(cin,a[i].name);
cout<<"#"<> a[i].money).get();
}
int flag = 0;
cout << " Grand Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money >= 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
flag = 0;
cout << " Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money < 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
while (cin.get() != EOF)
;
return 0;
}
第七题
题目描述
编写一个程序,它每次读取一个单词,直到用户只输入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
#include
#include
using namespace std;
int main()
{
int v, c, o;
v = c = o = 0;
string word;
cout << "Enter words (q to quit) :n";
while (1)
{
cin >> word;
if (word == "q")
break;
else if (isalpha(word[0]))
{
if ((tolower(word[0]) == 'a') || (tolower(word[0]) == 'e') || (tolower(word[0]) == 'i') || (tolower(word[0]) == 'o') || (tolower(word[0]) == 'u'))
v++;
else
c++;
}
else
o++;
}
cout << v << " words beginning with vowels.n";
cout << c << " words beginning with consonants.n";
cout << o << " othersn";
while (cin.get() != EOF)
;
return 0;
}
第八题
题目描述
编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文 件中包含多少个字符。
代码片段
#include
#include
#include
using namespace std;
int main()
{
ifstream fin;
fin.open("6.doc");
if (!fin.is_open())
{
cout << "Error!";
while (cin.get() != EOF)
;
return 0;
}
char ch;
long long count = 0;
while (fin.get() != EOF)
count++;
cout << fixed;
cout << "There is " << count << " chars in this file.n";
while (cin.get() != EOF)
;
fin.close();
return 0;
}
第九题
题目描述
完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成 对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面(此文件命名为6.doc):
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
代码片段
#include
#include
#include
#include
using namespace std;
struct list
{
string name;
double money;
} * a;
int main()
{
ifstream fin;
fin.open("6.doc");
if (!fin.is_open())
{
cout << "Error!";
while (cin.get() != EOF);
return 0;
}
int n;
(fin >> n).get();
a = new list[n];
for (int i = 0; i < n; i++)
{
getline(fin, a[i].name);
(fin >> a[i].money).get();
}
int flag = 0;
cout << " Grand Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money >= 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
flag = 0;
cout << " Patrons " << endl;
for (int i = 0; i < n; i++)
if (a[i].money < 10000)
{
cout << a[i].name << " " << a[i].money << endl;
flag = 1;
}
if (!flag)
cout << "none!" << endl;
fin.close();
while (cin.get() != EOF);
return 0;
}
编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板 类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于 平均值。
#include#include using namespace std; int main() { array a; int i = 0; double sum = 0; while (cin >> a[i]) { sum += a[i]; i++; } sum /= i; cout << sum << " is the average number.na"; int count = 0; for (int j = 0; j < i; j++) if (a[j] > sum) count++; cout<<"There are "<
第三题
题目描述
编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单---每个选项用一个字母标记。 如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。 然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices: c) carnivore p) pianist t) tree g) game f PleaseenteraC,P,t,org: q PleaseenteraC,p,t,org: t A maple is a tree.代码片段
#includeusing namespace std; int freal(void); int main() { cout << "Please enter one of the following choices:nc) carnivore p) pianistnt) tree g) gamen"; while (freal()); while (cin.get() != EOF); return 0; } int freal(void) { char ch; cin.get(ch).get(); switch (ch) { case 'c': cout << "I don't konw what is carnivore.n"; break; case 'p': cout << "I also don't what the meaning of pianist.n"; break; case 't': cout << "A maple is a tree.n"; break; case 'g': cout << "LOL is kind of game.n"; break; default: cout<<"Pleaseenter a c,p,t,or g: "; return 1; } return 0; }
第四题
题目描述
加入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 Progr ammer MIPs Analyst Trainee LOOPY Next choice: q Bye !代码片段
#includeusing namespace std; #define size 5 int freal(void); void byname(void); void bytitle(void); void bybop(void); void bypre(void); // Benevolent Order of Programmers name structure struct bop { string fullname; // real name string title; // job title string bopname; // secret BOP name int preference; // 0 = fullname, 1 = title, 2 = bopname }; bop a[size] = {{"name1", "title1", "bop1", 0}, {"name2", "title2", "bop2", 1}, {"name3", "title3", "bop3", 2}, {"name4", "title4", "bop4", 1}, {"name5", "title5", "bop5", 0}}; int main() { cout << "Benevolent Order of Programmers Reportna. display by name b. display by titlenC. display by bopname d. display by preferencenq. quitnEnter your choice: "; while (freal()) cout << "Next choice:"; while (cin.get() != EOF) ; return 0; } int freal(void) { char ch; cin.get(ch).get(); switch (ch) { case 'a': byname(); break; case 'b': bytitle(); break; case 'c': bybop(); break; case 'd': bypre(); break; case 'q': cout << "Bye!n"; return 0; default: cout << "Invalid input! Please enter again!n"; } return 1; } void byname(void) { for (int i = 0; i < size; i++) cout << a[i].fullname << endl; } void bytitle(void) { for (int i = 0; i < size; i++) cout << a[i].title << endl; } void bybop(void) { for (int i = 0; i < size; i++) cout << a[i].bopname << endl; } void bypre(void) { for (int i = 0; i < size; i++) { if (a[i].preference == 0) cout << a[i].fullname << endl; if (a[i].preference == 1) cout << a[i].title << endl; if (a[i].preference == 2) cout << a[i].bopname << endl; } }
第五题
题目描述
在Ncutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~ 15000 tvarps: 10%
15001 ~ 35000 tvarps: 15%
35000 tvarps以上: 20%
例如,收入为38000 tvarps时,所得税为
5000 x 0.00 + 10000x 0.10 + 20000 x0.15 + 3000x 0.20,
即 4600 tvarps。 请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字 时,循环将结束。代码片段
#include#define min(x, y) (x < y ? x : y) #define max(x, y) (x > y ? x : y) using namespace std; int tva(void); int main() { cout << "Please enter your income(invalid input to quit eg(x<0,x!=digital)):n"; while (tva()) cout << "Next input: "; cout<<"Done!n"; cin.clear(); while (cin.get() != EOF) ; return 0; } int tva(void) { double in; if ((cin >> in)&&(in>=0)) { double result = ((min(5000, max(in, 0)) - 0) * 0 + (min(15000, max(in, 5000)) - 5000) * 0.1 + (min(35000, max(in, 15000)) - 15000) * 0.15 + (max(in, 35000) - 35000) * 0.2); cout << "Income tax is " << result << endl; return 1; } else return 0; }
第六题
题目描述
编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后 要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两 个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后, 程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题, 指出下面的捐 款者是重要捐款人(Grand Patrons)。 然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某 种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
代码片段
#include#include using namespace std; struct list { string name; double money; } * a; int main() { int n; cout << "How many persons will donate?:"; (cin >> n).get(); a = new list[n]; cout << "Plese enter the name and donation:n"; for (int i = 0; i < n; i++) { cout << "#" << i + 1 << " The name:"; getline(cin,a[i].name); cout<<"#"<> a[i].money).get(); } int flag = 0; cout << " Grand Patrons " << endl; for (int i = 0; i < n; i++) if (a[i].money >= 10000) { cout << a[i].name << " " << a[i].money << endl; flag = 1; } if (!flag) cout << "none!" << endl; flag = 0; cout << " Patrons " << endl; for (int i = 0; i < n; i++) if (a[i].money < 10000) { cout << a[i].name << " " << a[i].money << endl; flag = 1; } if (!flag) cout << "none!" << endl; while (cin.get() != EOF) ; return 0; }
第七题
题目描述
编写一个程序,它每次读取一个单词,直到用户只输入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#include #include using namespace std; int main() { int v, c, o; v = c = o = 0; string word; cout << "Enter words (q to quit) :n"; while (1) { cin >> word; if (word == "q") break; else if (isalpha(word[0])) { if ((tolower(word[0]) == 'a') || (tolower(word[0]) == 'e') || (tolower(word[0]) == 'i') || (tolower(word[0]) == 'o') || (tolower(word[0]) == 'u')) v++; else c++; } else o++; } cout << v << " words beginning with vowels.n"; cout << c << " words beginning with consonants.n"; cout << o << " othersn"; while (cin.get() != EOF) ; return 0; }
第八题
题目描述
编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文 件中包含多少个字符。
代码片段
#include#include #include using namespace std; int main() { ifstream fin; fin.open("6.doc"); if (!fin.is_open()) { cout << "Error!"; while (cin.get() != EOF) ; return 0; } char ch; long long count = 0; while (fin.get() != EOF) count++; cout << fixed; cout << "There is " << count << " chars in this file.n"; while (cin.get() != EOF) ; fin.close(); return 0; }
第九题
题目描述
完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成 对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面(此文件命名为6.doc):
Sam Stone 2000 Freida Flass 100500 Tammy Tubbs 5000 Rich Raptor 55000代码片段
#include#include #include #include using namespace std; struct list { string name; double money; } * a; int main() { ifstream fin; fin.open("6.doc"); if (!fin.is_open()) { cout << "Error!"; while (cin.get() != EOF); return 0; } int n; (fin >> n).get(); a = new list[n]; for (int i = 0; i < n; i++) { getline(fin, a[i].name); (fin >> a[i].money).get(); } int flag = 0; cout << " Grand Patrons " << endl; for (int i = 0; i < n; i++) if (a[i].money >= 10000) { cout << a[i].name << " " << a[i].money << endl; flag = 1; } if (!flag) cout << "none!" << endl; flag = 0; cout << " Patrons " << endl; for (int i = 0; i < n; i++) if (a[i].money < 10000) { cout << a[i].name << " " << a[i].money << endl; flag = 1; } if (!flag) cout << "none!" << endl; fin.close(); while (cin.get() != EOF); return 0; }



