C++Primer Plus第六章编程练习.doc
第六章编程练习1、#include <iostream>#include <cctype>int main() using namespace std; char ch; cin.get(ch); while(ch!='') if(isdigit(ch) cin.get(ch); else if(islower(ch) ch=toupper(ch); else ch=tolower(ch); cout<<ch; cin.get(ch); system("pause"); return 0; 2、#include <iostream>int main() using namespace std; double sum=0; double average=0; double num10; int i=0,total=0; double tmp; while(cin>>tmp&&i<10) numi=tmp; sum+=numi; +i; if(i!=0) average=sum/i; for(int j=0;j<i;+j) if(numj>average) +total; cout<<"这些数字的平均值为"<<average<<endl; cout<<"并且共有"<<total<<"个数字大于平均值。n" system("pause"); return 0; 3、#include <iostream>int main() using namespace std; cout<<"Please enter one of the following choices:n" <<"c)carnivore p)pianistn" <<"t)tree g)gamenfn" cout<<"Please enter a c, p, t, or g: " char ch; cin>>ch; while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g') cout<<"Please enter a c, p, t, or g: " cin>>ch; switch(ch) case 'c': cout<<"A maple is a tree.n" break; case 'p': cout<<"A maple is a pianist.n" break; case 't': cout<<"A maple is a tree.n" break; case 'g': cout<<"A maple is a game.n" system("pause"); return 0; 4、#include <iostream>const int strsize=20;struct bop char fullnamestrsize; char titlestrsize; char bopnamestrsize; int preference;int main() std:cout<<"Benevolent Order of Programmers Reportn" "a. display by name b. display by titlen" "c. display by bopname d. diplay by preferencen" "q. quit" char ch; bop member5="Wimp Macho","English Teacher","DEMON",0, "Raki Rhodes","Junior Programmer","BOOM",1, "Celia Laiter","Super Star","MIPS",2, "Hoppy Hipman","Analyst Trainee","WATEE",1, "Pat Hand","Police","LOOPY",2; std:cout<<"Enter your choice:" while(std:cin>>ch&&ch!='q') switch(ch) case 'a': for(int i=0;i<5;i+) std:cout<<memberi.fullname<<std:endl; break; case 'b': for(int i=0;i<5;i+) std:cout<<memberi.title<<std:endl; break; case 'c': for(int i=0;i<5;i+) std:cout<<memberi.bopname<<std:endl; break; case 'd': for(int i=0;i<5;i+) if(memberi.preference=0) std:cout<<memberi.fullname<<std:endl; else if(memberi.preference=1) std:cout<<memberi.title<<std:endl; else if(memberi.preference=2) std:cout<<memberi.bopname<<std:endl; std:cout<<"Next choice: " std:cout<<"Bye!n" system("pause"); return 0;5、#include <iostream>int main() using namespace std; double income,revenue; cout<<"请输入你的收入:" while(cin>>income&&income>=0) if(income<=5000) revenue=0.0; else if(income>5000&&income<=15000) revenue=0.1*(income-5000); else if(income>15000&&income<=35000) revenue=0.1*(15000-5000)+0.15*(income-15000); else revenue=0.1*(15000-5000)+0.15*(35000-15000)+0.2*(income-35000); cout<<"你的所得税为"<<revenue<<endl; cout<<"请输入你的收入:" system("pause"); return 0;6、#include <iostream>#include <string>struct patron std:string name; double money;int main() using namespace std; int num,tmp=0; cout<<"请输入捐款的人数:" cin>>num; cin.get(); patron *ps=new patronnum; for(int i=0;i<num;+i) cout<<"请输入第"<<i+1<<"位捐款人的名字:" getline(cin,psi.name); cout<<"请输入第"<<i+1<<"位捐款人捐款的数目:" cin>>psi.money; cin.get(); cout<<"Grand Patrons:n" for(int i=0;i<num;+i) if(psi.money>10000) cout<<psi.name<<endl; +tmp; if(tmp=0) cout<<"nonen" cout<<"Patrons:n" for(int i=0;i<num;+i) if(psi.money<=10000) cout<<psi.name<<endl; +tmp; if(tmp=0) cout<<"nonen" delete ps; system("pause"); return 0; 7、#include <iostream>#include <cctype>int main() using namespace std; int vowel,consonant,other; vowel=consonant=other=0; char word15; cout<<"Enter words (q to quit):n" while(cin>>word) if(isalpha(word0) if(word0='q'&&strlen(word)=1) break; else if(word0='a'|word0='i'| word0='u'|word0='e'|word0='o') +vowel; else +consonant; else +other; cout<<vowel<<" words beginning with vowelsn" cout<<consonant<<" words beginning with consonantsn" cout<<other<<" othersn" system("pause"); return 0; 8、#include <iostream>#include <fstream>#include <cstdlib>int main() using namespace std; char ch; int sum=0; ifstream inFile; inFile.open("abc.txt"); if(!inFile.is_open() cout<<"Could not open the file n" cout<<"Program terminating.n" exit(EXIT_FAILURE); inFile>>ch; while(inFile.good() +sum; inFile>>ch; 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 unkonwn reason.n" cout<<"总共有"<<sum<<"个字符在这个文件中。"<<endl; system("pause"); return 0; 9、#include <iostream>#include <fstream>#include <cstdlib>struct member char name20; double donation;int main() using namespace std; int num; int count1=0; int count2=0; ifstream fin; char file20; cout<<"Enter name of data file: " cin.getline(file,20); fin.open(file); if(!fin.is_open() cout<<"Could not open the file-"<<file<<endl; cout<<"Program terminating.n" exit(EXIT_FAILURE); fin>>num; fin.get(); member *pd=new membernum; for(int i=0;i<num;i+) fin.getline(pdi.name,20); fin>>pdi.donation; fin.get(); cout<<"Grand Patrons:n" for(int i=0;i<num;i+) if(pdi.donation>=10000) cout<<pdi.name<<" "<<pdi.donation<<endl; count1+; if(count1=0) cout<<"nonen" cout<<"Patrons:n" for(int i=0;i<num;i+) if(pdi.donation<10000) cout<<pdi.name<<" "<<pdi.donation<<endl; count2+; if(count2=0) cout<<"nonen" delete pd; system("pause"); return 0;