第六章 编程练习
1.编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。
//编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写。
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
cout<<"Enter text for analysis,and type @ to terminate input.\n";
char ch;
cin.get(ch);
while(ch !='@')
{
if (!isdigit(ch))
if (isupper(ch))
cout<<char(tolower(ch));
else
cout<<char(toupper(ch));
cin.get(ch);
}
cin.get();
cin.get();
return 0;
}
2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。 程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。(参考程序清单6.13)
//编写一个程序,最多将10个donation值读入到一个double数组中。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
const int ArSize = 10;
double donation[ArSize];
cout<<"Enter the number:\n";
cout<<"You may enter up to"<<ArSize<<"num.\n";
cout<<"dona#1:";
int i=0;
while(i<ArSize && cin>>donation[i])
{ if (++i<ArSize)
cout<<"dona#"<<i+1<<":";
}
double total=0.0;
double average;
for (int j=0;j<i;j++)
{
total +=donation[j];
average = total/i;
}
int count =0;
for (int j=0;j<i;j++)
{
if (donation[j]>average)
++count;
}
if (i==0)
cout<<"No number.\n";
else
cout<<"average is "<<average<<" and there are "<<count<<" number more than average.";
if (!cin)
{
cin.clear();
cin.get();
}
cin.get();
cin.get();
return 0;
}
3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单————每一个选项用一个字母标记。 如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
//编写一个菜单选项的雏形,该程序显示一个提供四个选项的菜单---每个选项用一个字母标记。使用switch语句...
#include<iostream>
using namespace std;
void showmenu();
int main()
{
showmenu();
cout<<"Please enter a c,p, t, or g: ";
char choice;
while (cin>>choice)
{
switch (choice)
{
case 'c' : cout<<"A maple is a carnivore.";
break;
case 'p' : cout<<"A maple is a pianist.";
break;
case 't' : cout<<"A maple is a tree.";
break;
case 'g' : cout<<"A maple is a game.";
break;
default : cout<<"Please enter a c, p, t, or g: ";
}
}
return 0;
}
void showmenu()
{
cout<<"Please enter one of the following choices: \n"
"c) carnivore p) pianist \n"
"t) tree g) game \n";
}
4.加入Benevolent Order of Programmer后,在BOP大会上,人们可通过加入者的真实姓名、头衔或秘密BOP来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员preference(书中翻译为偏好,个人认为应该翻译为优先权)来 列出成员。编写该程序时,请使用下面的结构:
//Benevolent Order of Programmers name structure
struct bop{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
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”,并不意味着显示成员的preference,而是意味着根据成员的preference来列出成员。
例如:如果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!
//编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。
//Benevolent Order of Programmers name structure
#include<iostream>
using namespace std;
void choice();
const int strsize=50;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main()
{
//Structure initialization
bop data[5]={
{"Wimp Macho","Student","Macho",0},
{"Raki Rhodes","Junior Programmer","Rhodes",1},
{"Celia Laiter","Studet","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","Hipman",1},
{"Pat Hand","Student","LOOPY",2}
};
choice();
cout<<"Enter your choice: ";
char selet;
cin>>selet;
while (selet !='q')
{
switch (selet)
{
case 'a' :
for (int i=0;i<5;i++)
cout<<data[i].fullname<<endl;
break;
case 'b' :
for (int i=0;i<5;i++)
cout<<data[i].title<<endl;
break;
case 'c' :
for (int i=0;i<5;i++)
cout<<data[i].bopname<<endl;
break;
case 'd' :
for (int i=0;i<5;i++)
{
if (data[i].preference ==0)
cout<<data[i].fullname<<endl;
else if (data[i].preference==1)
cout<<data[i].title<<endl;
else
cout<<data[i].bopname<<endl;
}
break;
default : cout<<"Pleaes enter a,b,c,d,or q: ";
break;
}
cout<<"Next choice: ";
cin>>selet;
}
cout<<"Bye!\n";
cin.get();
cin.get();
return 0;
}
void choice()
{
cout<<"Benevolent Order of Programmers Report: \n"
"a. display by name b. display by title\n"
"c. display by bopname d. display by preference\n"
"q. quit\n";
}
5.在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarp:不收税
5001~15000 tvarps: 10%
15001~35000 tvarps: 15%
35000 tvarps以上: 20%
//在Neutronia王国,货币单位是tvarp,收入所得税的计算方式为:......
//编写一个程序,使用循环来要求用户输入收入,并报告所得税。
//当用户输入负数或非数字时,结束循环。
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"输入你的收入:";
while ((cin>>n)&&n>=0)
{
if (n<=5000)
cout<<"不收税\n";
else if (n>5000 && n<=15000)
cout<<"你需要交税:"<<(n-5000)*0.1<<endl;
else if (n>15000 && n<=35000)
cout<<"你需要交税:"<<10000*0.1+(n-15000)*0.15<<endl;
else
cout<<"你需要交税:"<<10000*0.1+20000*0.15+(n-35000)*0.2<<endl;
cout<<"输入另一个收入(非数字或负数):";
}
cout<<"程序结束!"<<endl;
cin.get();
cin.get();
return 0;
}
6.编写一个程序,记录捐助给“维护合法权益团体”的资金。该程序要求用户输入捐献者的数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被存储在一个动态分配的数组里。每一个结构有两个成员:用来存储姓名的字符数组(或string 对象)和用来存储款项的double成员。 读取所有数据后,程序将显示所有捐款超过10000的捐款者的姓名即捐款数额。 该列表应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐献者,则程序打印单词“none”。给程序只显示这两种类别,而不进行排序。
//编写程序记录捐助给“维护合法权利团体”的资金。程序要求如下:
//第一,用户输入捐献者数目,然后要求用户输入每个捐献者的姓名和款项。这些信息被存储在一个动态数组中。
//第二,每个结构有两个成员,姓名用string,款项用double。
//第三,读取数据后,程序将显示所有捐款超过10000的捐款者姓名和捐款数额。
#include<iostream>
#include<string>
using namespace std;
struct data
{
string name;
double amount;
};
int main()
{
int n;
cout<<"请输入捐赠者数目:";
while (!(cin>>n)||n<0)
{
cin.clear();
while (cin.get() !='\n')
continue;
cout<<"输入错误,请重新输入:";
}
if (n==0)
{
cout<<"没有捐献者,程序结束!"<<endl;
return 0;
}
cin.get();
struct data *pf =new struct data[n];
for (int i=0;i<n;++i)
{
cout<<"输入第"<<i+1<<"个捐献者的姓名:";
getline(cin,pf[i].name);
cout<<"输入第"<<i+1<<"个捐献者捐献的金额:";
while (!(cin>>pf[i].amount)||n<0)
{
cin.clear();
while (cin.get() !='\n')
continue;
cout<<"输入错误,请重新输入:";
}
cin.get();
}
cout<<"重要捐赠人(Grand Patrons):\n";
int temp = 0;
for (int i=0;i<n;++i)
if (pf[i].amount > 10000)
{
cout<<"姓名:"<<pf[i].name<<"; "<<"金额:"<<pf[i].amount<<endl;
++temp;
}
if (temp == 0)
cout << "none" << endl;
temp = 0;
cout << "其他捐献人(Patrons):\n";
for (int i = 0; i < n; ++i)
if (pf[i].amount <= 10000)
{
cout << pf[i].name <<endl;
++temp;
}
if (temp == 0)
cout << "none" << endl;
delete []pf;
cin.get();
return 0;
}
7.编写一个程序,每次读取一个单词,直到用户只输入q。然后,给程序指出有多少个单词以元音打头。有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序运行情况如下:
Enter words (q to quit):
The 12 awesome oxen ambles
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others
//编写一个程序,它每次读取一个单词,直到用户只输入q.
//使用isalpha()和if或者switch。
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main()
{
string s;
int vowels = 0;
int consonants = 0;
int others = 0;
cout<<"Enter words (q to quit):\n";
while (cin>>s && (s != "q"))
{
if (isalpha(s[0]))
{
if (s[0] == 'a'||s[0] == 'e'||s[0] == 'i'||s[0] == 'o'||s[0] == 'u')
++vowels;
else
++consonants;
}
else
++others;
}
cout<<vowels<<" words beginning with vowels\n";
cout<<consonants<<" words beginning with consonants\n";
cout<<others<<" others."<<endl;
cin.get();
cin.get();
return 0;
}
8.编写一个程序,它打开一个文件,逐字符地读取该文件,直到达到文件末尾,然后指出该文件中包含多少字符。
//编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE=60;
using namespace std;
int main()
{
char filename[SIZE];
ifstream inFile;
cout<<"Enter name of data file: ";
cin.getline(filename,SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout<<"Could not open the file"<<filename<<endl;
cout<<"Program terminating.\n";
exit(EXIT_FAILURE);
}
char ch;
int num=0;
while (inFile>>ch)
++num;
cout<<"there are "<<num<<" character.\n";
if (inFile.eof())
cout<<"End of file reached.\n";
else if (inFile.fail())
cout<<"Input terminated by data mismatch.\n";
else
cout<<"Input terminated for unknown reason.\n";
inFile.close();
cin.get();
cin.get();
return 0;
}
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名。第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubba
5000
Rich Raptor
55000
//修改编程练习6,从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数目。
//编写程序记录捐助给“维护合法权利团体”的资金。程序要求如下:
//第一,用户输入捐献者数目,然后要求用户输入每个捐献者的姓名和款项。这些信息被存储在一个动态数组中。
//第二,每个结构有两个成员,姓名用string,款项用double。
//第三,读取数据后,程序将显示所有捐款超过10000的捐款者姓名和捐款数额。
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
const int SIZE=60;
struct data
{
string name;
double amount;
};
int main()
{
char filename[SIZE];
ifstream inFile;
cout<<"Enter name of data file: ";
cin.getline(filename,SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout<<"Could not open the file"<<filename<<endl;
cout<<"Program terminating.\n";
exit(EXIT_FAILURE);
}
int n;
cout<<"请输入捐赠者数目:";
inFile>>n;
inFile.get();
cout<<n<<endl;
struct data *pf =new struct data[n];
for (int i=0;i<n;++i)
{
cout<<"输入第"<<i+1<<"个捐献者的姓名:";
getline(inFile,pf[i].name);
cout<<pf[i].name<<endl;
cout<<"输入第"<<i+1<<"个捐献者捐献的金额:";
inFile >> pf[i].amount;
inFile.get();
cout << pf[i].amount << endl;
}
cout<<"重要捐赠人(Grand Patrons):\n";
int temp = 0;
for (int i=0;i<n;++i)
if (pf[i].amount > 10000)
{
cout<<"姓名:"<<pf[i].name<<"; "<<"金额:"<<pf[i].amount<<endl;
++temp;
}
if (temp == 0)
cout << "none" << endl;
temp = 0;
cout << "其他捐献人(Patrons):\n";
for (int i = 0; i < n; ++i)
if (pf[i].amount <= 10000)
{
cout << pf[i].name <<endl;
++temp;
}
if (temp == 0)
cout << "none" << endl;
delete []pf;
cin.get();
return 0;
}