编写一个程序,最多将10个donation值读入到一个double数组中(如果
您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,
并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include<iostream>#include<array>usingnamespace std;intmain(void){constunsignedint SIZE =10;
array<double, SIZE> donation;unsignedint enter =0;double total_value =0.0;double avg =0.0;unsignedint large_avg =0;
cout<<"Please enter up to ten double value, Non-digital to exit: "<< endl;while(enter <10&&(cin >> donation[enter])){
total_value += donation[enter];
enter++;}
avg = total_value / enter;for(unsignedint i =0; i < enter; i++){if(donation[i]> avg){
large_avg++;}}
cout <<"The average value is "<< avg <<", and there are "<< large_avg <<" double value large than agerage value!"<< endl;return0;}
或
#include<iostream>usingnamespace std;intmain(){double sum=0.0,ary[10],average;int count_1=0,count_2=0;for(int i =0; i <10;++i){if(cin>>ary[i]){
count_1++;
sum +=ary[i];}else{break;}}
average=sum/count_1;
cout<<"average: "<<average<<endl;for(int j =0; j <count_1 ;++j){
cout<<ary[j]<<endl;if(ary[j]>average){
count_2 ++;}}
cout<<count_2<<endl;}
编写一个菜单驱动程序的雏形。该程序显示一个提供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>usingnamespace std;voidreport();intmain(){char ch;report();while(cin>>ch && ch !='q'){switch(ch){case'c':
cout<<" tiger is acarnivore"<<endl;break;case'p':
cout<<"Langlang is a famous pianist"<<endl;break;case't':
cout<<"A maple is a tree"<<endl;break;case'g':
cout<<"My favorite game is Kenshi"<<endl;break;default:report();
cout<<"please enter a c, p, t, or g"<<endl;break;}}
cout<<"Bye-bye"<<endl;return0;}voidreport(){
cout<<"please enter one of the following choices:\n""c) carnivore p) pianist\n""t) tree g) game\n""q) quit"<<endl;}
加人Benevolent Order Programmer后,在BOP大会上,人们便可以通过
可以通过加入者的真实姓名、头衔或者秘密BOP姓名来了解他(她)。请
编写一个程序,可以使用真实姓名、头衔、秘密姓名或者成员偏好来列出
成员偏好来列出成员。编写该程序时,请使用下面的结构:
//Benevolent Order of Programmers name structurestruct bop{char fullname[strsize]//real namechar title[strszie]//job titlechar bopname[strsize];//secret BOP nameint 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 prefenren”并不意味着显示成员的偏好,而是意味
着根据成员的偏好来列出成员。例如,如果偏好为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!#include<iostream>usingnamespace std;struct bop{char fullname[50];char title[50];char bopname[50];int preference;};struct bop bopArray[5]={{"Wimp Macho","YYY","Y----",0},{"XXXXXXXX","2XXXX","3XXXXX",1},{"AAAAAAAA","2AAAA","3AAAAA",2},{"BBBBBBBB","2BBBB","3BBBBB",0},{"CCCCCCCC","4CCCC","3CCCCC",1}};voiddisplay_by_name();voiddisplay_by_title();voiddisplay_by_bopname();voiddisplay_by_preference();intmain(){char ch;
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"<<endl;
cout<<"Enter your choice: "<<endl;while(cin>>ch && ch !='q'){switch(ch){case'a':display_by_name();break;case'b':display_by_title();break;case'c':display_by_bopname();break;case'd':display_by_preference();break;default:
cout<<"your entered char is errored"<<endl;break;}
cout<<"Next choice: "<<endl;}}voiddisplay_by_name(){for(int i =0;i<5;i++){
cout<<bopArray[i].fullname<<endl;}}voiddisplay_by_title(){for(int i =0;i<5;i++){
cout<<bopArray[i].title<<endl;}}voiddisplay_by_bopname(){for(int i =0;i<5;i++){
cout<<bopArray[i].bopname<<endl;}}voiddisplay_by_preference(){for(int i =0;i<5;i++){
cout<<bopArray[i].preference<<endl;}}
在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>usingnamespace std;int main (){double income,tax;
cout<<"pleace enter your income: ";while((cin>>income)&&(income>0.0)){if(5001.0>income){
tax = income*0.0;}elseif((5001.0<=income)&&(income<15000.0)){
tax =(income-5000.0)*0.1+5000.0*0.0;}elseif((15001.0<=income)&&(income<35000.0)){
tax =(income-15000.0)*0.15+5000.0*0.0+10000*0.10;}else{
tax =(income-35000.0)*0.2+5000.0*0.0+10000*0.10+20000.0*0.15;}
cout<<"your tax is "<<tax<<endl;}return0;}
编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户
输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信
息被储存在一个动态分配的结构数组中。每个结构由两个成员:用来储存
姓名的字符组(或string对象)和用来储存款项的double成员。读取所
有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数
额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人
(Crand Patrons).然后,程序将列出其他的捐款者,该列表要以
Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。
该程序只显示这两种类别,而不进行排序。
#include<iostream>#include<cstring>usingnamespace std;struct patrons{
string name;double donation;};intmain(){int patronsNumber,flag=0;
cout <<"How many patrons do you wish to push?"<< endl;
cin >> patronsNumber;
patrons* List =new patrons[patronsNumber];for(int i =0;i<patronsNumber;i++){
cout <<"patron #"<< i +1<< endl;
cout <<"Please enter the name:"<< endl;
cin>>List[i].name;
cout <<"Please enter the donation:"<< endl;(cin >> List[i].donation).get();}for(int i =0;i<patronsNumber;i++){if(List[i].donation>1000){
cout <<"Crand Patrons name: "<< List[i].name << endl;
cout <<"Crand Patrons amount: "<<List[i].donation << endl;
flag++;}if(flag ==0){
cout <<"none"<< endl;}}
flag =0;for(int i =0;i<patronsNumber;i++){if(List[i].donation<=1000){
cout <<"Patrons name: "<< List[i].name << endl;
cout <<"Patrons amount: "<<List[i].donation << endl;
flag++;}if(flag ==0){
cout <<"none"<< endl;}}delete[] List;return0;}
编写一个程序,他每次读取一个单词,直到用户输入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 vowls
4 words beginning with consonants
2 others
#include<iostream>#include<cstring>usingnamespace std;intmain(){unsignedint vowels =0;unsignedint consonants =0;unsignedint others =0;
string input;
cout <<"Enter words (q to quit): "<< endl;while(cin>>input){if(input.length()==1&& input[0]=='q'){break;}if(isalpha(input[0])){if(input[0]=='a'|| input[0]=='e'|| input[0]=='i'|| input[0]=='o'|| input[0]=='u'){
vowels++;}else
consonants++;}else{
others ++;}}
cout << vowels <<" words beginning with vowels"<< endl;
cout << consonants <<" words beginning with consonants"<< endl;
cout << others <<" otners"<< endl;return0;}
编写一个程序,他打开一个文件,逐个字符地读取该文件,知道到达文件
末尾,然后指出该文件包含多少个字符。
#include<iostream>#include<fstream>#include<cstdlib>usingnamespace std;intmain(){char ch;int num=0;
ifstream inFile;
inFile.open("D:\\ClionProject\\abc.txt");if(!inFile.is_open()){
cout<<"Could not open the file"<<endl;
cout<<"Program terminating.\n";exit(EXIT_FAILURE);}while((ch = inFile.get())!=EOF){
num++;}
cout <<"There are "<< num <<" characters in "<<" file."<< endl;}
完成编程练习6,但从文件中读取所需信息。该文件的第一项应为捐款人
数,剩余的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二
行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000