题目
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。请编写 一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
6.编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来储存款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand> Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons 开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
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
8.编写一个程序,它打开一个文件,逐个字符的读取该文件,直到到达文件末尾,然后指出该文件包含多少个字符 。
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
程序
6.1
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
char ch;
cin.get(ch);
while(ch!='@')
{
if(!isdigit(ch))
{
if(islower(ch))
ch=toupper(ch);
else if(isupper(ch))
ch=tolower(ch);
cout<<ch;
}
cin.get(ch);
}
return 0;
}
6.2
#include <iostream>
const int Max=10;
int main()
{
using namespace std;
double donation[Max];
cout<<"donation #1: ";
int i=0;
while(i<Max&&cin>>donation[i])
{
if(++i<Max)
cout<<"donation #"<<i+1<<": ";
}
double sum=0.0,aver;
for(int j=0;j<i;j++)
{
sum+=donation[j];
}
aver=sum/i;
cout<<"The "<<i<<" donation average is: "<<aver<<endl;
int count=0;
for(int j=0;j<i;j++)
{
if(donation[j]>aver)
count++;
}
cout<<count<<" over average."<<endl;
return 0;
}
6.3
#include <iostream>
void showmenu();
using namespace std;
int main()
{
showmenu();
char ch;
cin>>ch;
while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g')
{
cout<<"Please enter a c, p, t, or g: ";
cin>>ch;
}
cout<<"A maple is a ";
switch(ch)
{
case 'c': cout<<"carnivore."<<endl;
break;
case 'p': cout<<"pianist."<<endl;
break;
case 't': cout<<"tree."<<endl;
break;
case 'g': cout<<"game."<<endl;
break;
default: cout<<"That is not a choice.\n";
}
return 0;
}
void showmenu()
{
cout<<"Please enter one of the following choices:\n"
"c) carnivore p) pianist\n"
"t) tree g) game\n";
}
6.4
#include <iostream>
void choice();
void fullname();
void title();
void bopname();
void preference();
using namespace std;
const int strsize=20;
const int num=5;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop people[num]=
{
{"Wimp Macho","Siop Xhui","Biop Nuyg",0},
{"Raki Rhodes","Junior Programmer","Grin Herk",1},
{"Celia Laiteer","Buit Njhu","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","Neat Baiu",1},
{"Pat Hand","Njht Bhuy","LOOP",2}
};
int main()
{
char ch;
choice();
cout<<"Enter your choice: ";
cin>>ch;
while(ch!='q')
{
while(!(ch>='a'&&ch<='d'||ch=='q'))
{
cout<<"Please enter a a, b, c, d, or q: ";
cin>>ch;
}
if(ch=='q')
break;
switch(ch)
{
case 'a': fullname();
break;
case 'b': title();
break;
case 'c': bopname();
break;
case 'd': preference();
break;
default: break;
}
cout<<"Next choice: ";
cin>>ch;
}
cout<<"Bye!\n";
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";
}
void fullname()
{
for(int i=0;i<num;i++)
{
cout<<people[i].fullname<<endl;
}
}
void title()
{
for(int i=0;i<num;i++)
{
cout<<people[i].title<<endl;
}
}
void bopname()
{
for(int i=0;i<num;i++)
{
cout<<people[i].bopname<<endl;
}
}
void preference()
{
for(int i=0;i<num;i++)
{
int j=people[i].preference;
if(j==0)
cout<<people[i].fullname<<endl;
else if(j==1)
cout<<people[i].title<<endl;
else if(j==2)
cout<<people[i].bopname<<endl;
}
}
6.5
#include <iostream>
int main()
{
using namespace std;
cout<<"Enter the income: ";
int m;
while(cin>>m&&m>=0)
{
double t=0.0;
if(m>5000&&m<=15000)
t=(m-5000)*0.1;
else if(m>15000&&m<=35000)
t=10000*0.1+(m-15000)*0.15;
else if(m>35000)
t=10000*0.1+20000*0.15+(m-35000)*0.2;
cout<<"The income tax is: "<<t<<endl;
cout<<"The next: ";
}
return 0;
}
6.6
#include <iostream>
struct Patrons
{
char name[20];
double money;
};
int main()
{
using namespace std;
cout<<"The number of patrons: ";
int num;
cin>>num;
cin.get();
Patrons * p=new Patrons[num];
for(int i=0;i<num;i++)
{
cout<<i+1<<"patron's name: ";
cin.get(p[i].name,20);
cout<<"Money: ";
cin>>p[i].money;
cin.get();
}
cout<<"Grand Patrons\n";
int count1=0;
for(int i=0;i<num;i++)
{
if(p[i].money>1000)
{
cout<<p[i].name<<":\t"<<p[i].money<<endl;
count1++;
}
}
if (count1==0)
cout<<"none\n";
cout<<"Patrons\n";
int count2=0;
for(int i=0;i<num;i++)
{
if(p[i].money<=1000)
{
cout<<p[i].name<<":\t"<<p[i].money<<endl;
count2++;
}
}
if (count2==0)
cout<<"none\n";
delete [] p;
return 0;
}
6.7
#include <iostream>
#include <string>
int main()
{
using namespace std;
string word;
int consonants=0,vowels=0,others=0;
cout<<"Enter words (q to quit):\n";
cin>>word;
while(word!="q")
{
if(isalpha(word[0]))
{
char ch=tolower(word[0]);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vowels++;
break;
default: consonants++;
break;
}
}
else
others++;
cin>>word;
}
cout<<vowels<<" beginning with vowels\n";
cout<<consonants<<" beginning with consonants\n";
cout<<others<<" others\n";
return 0;
}
6.8
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE=60;
int main()
{
using namespace std;
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 count=0;
inFile>>ch;
while(inFile.good())
{
++count;
inFile>>ch;
}
cout<<"The count is: "<<count<<endl;
inFile.close();
return 0;
}
6.9
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE=60;
struct Patrons
{
char name[20];
double money;
};
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;
cout<<"Please enter filename: ";
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 num;
inFile>>num;
inFile.get();
Patrons * p=new Patrons[num];
for(int i=0;i<num&&inFile.good();i++)
{
inFile.getline(p[i].name,20);
inFile>>p[i].money;
inFile.get();
}
cout<<"Grand Patrons\n";
int count1=0;
for(int i=0;i<num;i++)
{
if(p[i].money>1000)
{
cout<<p[i].name<<":\t"<<p[i].money<<endl;
count1++;
}
}
if (count1==0)
cout<<"none\n";
cout<<"Patrons\n";
int count2=0;
for(int i=0;i<num;i++)
{
if(p[i].money<=1000)
{
cout<<p[i].name<<":\t"<<p[i].money<<endl;
count2++;
}
}
if (count2==0)
cout<<"none\n";
delete [] p;
inFile.close();
return 0;
}