第2题
#include<array>#include<cstdlib>#include<iostream>constint SIZE =10;intmain(){usingnamespace std;
cout <<"You can input the value of donation(Up to 10 numbers):\n";
array<double, SIZE> Dona;int i =0;double sum =0;while(cin >> Dona[i])//cin>>double返回的是读取数据是否成功,成功则进入循环{
sum += Dona[i];++i;
cout <<"Input the next value(non-number to quit):";}double Average = sum / i;
cout <<"Average: "<< Average << endl;int count =0;for(int j =0; j < i; j++){if(Dona[j]> Average){++count;
cout << Dona[j]<<"\t";}}
cout << endl;
cout << count <<" value(s) are bigger than average.\n";return0;}
第3题
#include<cctype>#include<iostream>usingnamespace std;voidshowmenu();intmain(){
cout <<"Please enter one of the following choice: \n";showmenu();char ch;
cin >> ch;
ch =tolower(ch);while(ch !='c'&& ch !='p'&& ch !='t'&& ch !='g'&& ch !='f'){
cout <<"Please enter a c,p,t, or g:";
cin >> ch;}switch(ch){case'c':
cout <<"Your choice is carnivore.\n";break;case'p':
cout <<"Your choice is pianist.\n";break;case't':
cout <<"Your choice is tree.\n";break;case'g':
cout <<"Your choice is game.\n";break;default:break;}// cout << "You can input your next choices:\n";return0;}voidshowmenu(){
cout <<"c) carnivore p) pianist\n"<<"t) tree g) game\n"<<"f) quit\n";}
第4题
#include<iostream>usingnamespace std;constint strsize =40;struct bop
{char fullname[strsize];char title[strsize];char bopname[strsize];int preference;};voidshowmenu();intmain(){//info of bop
bop arr_member[5]={{"Wimp Macho","Professor","Kate",0},{"Raki Rhodes","Academician","Junior Programmer",2},{"Celia Laiter","Professor","MIPS",2},{"Hoppy Hipman","Analyst Trainee","LUCKY boy",1},{"Pat Hand","Student","LOOPY",2}};
cout <<"Benevolent Order of Programmers Report\n";showmenu();
cout <<"Enter your chioces: ";//creat a val of choicechar choice;
cin >> choice;// corresponding to eace choicewhile(cin.good()&& choice !='q'){switch(choice){case'a':for(int i =0; i <5; i++){
cout << arr_member[i].fullname << endl;}break;case'b':for(int i =0; i <5; i++){
cout << arr_member[i].title << endl;}break;case'c':for(int i =0; i <5; i++){
cout << arr_member[i].bopname << endl;}break;case'd':for(int i =0; i <5; i++){switch(arr_member[i].preference){case0:
cout << arr_member[i].fullname << endl;break;case1:
cout << arr_member[i].title << endl;break;case2:
cout << arr_member[i].bopname << endl;break;}}break;default:break;}
cout <<"Next choice: ";
cin >> choice;}
cout <<"Bye!\n";return0;}voidshowmenu(){
cout <<"a.display by name b.display by title\n"<<"c.display by bopname d.display by preference\n"<<"q.quit\n";}
第5题
#include<iostream>intmain(){usingnamespace std;double tax;double income;
cout <<"Enter your income (unit in tvarps): ";
cin >> income;while(cin.good()&& income >=0){if(income <=5000){
tax =0.0;}elseif(income >5000&& income <=15000){
tax =(income -5000)*0.1;}elseif(income >15000&& income <=35000){
tax =(income -15000)*0.15+10000*0.1;}elseif(income >35000){
tax =(income -35000)*0.2+20000*0.15+10000*0.1;}
cout <<"The tax = "<< tax <<" tvarps.\n";
cout <<"You can enter next income(unit in tvarps): ";
cin >> income;}
cout <<"Byebye!\n";return0;}
第6题
#include<iostream>// #include <string>#include<vector>usingnamespace std;struct Member
{char name[50];double money;};intmain(){
cout <<"Please enter the number of donors:\n";int num;
cin >> num;
cin.get();
vector<Member>Mem_lib(num);
cout <<"Please enter the first donor's name:\n";int i =0;
cin.getline(Mem_lib[i].name,50);// cin >> Mem_lib[i].name;while(i < num && cin.good()&& Mem_lib[i].name[0]!='\n')//empty line to quit{//cin.get();
cout <<"Enter the money:\n";
cin >> Mem_lib[i].money;
cin.get();//在此处插入cin.get()的原因是为了消耗掉末尾的换行符'\n',因为cin读取数据和scanf类似是以空白符作为结尾的,并且会把//换行符留在输入队列中,进而影响cin.getline()读取字符串;但是并不影响cin读取数据,因为cin在读取数据时,会忽略遇到有效字符前的空白字符
i++;if(i < num){
cout <<"Please enter the next donor 's name: \n";
cin.getline(Mem_lib[i].name,50);//读取数据,遇到换行符就停止读取,所以上一次读取数据后如果把换行符留在输入队列中,将会影响本次读取}}
cout <<"Grand Patrons\n";int count =0;for(int j =0; j < num; j++){if(Mem_lib[j].money >=10000){
cout << Mem_lib[j].name <<"\t"<< Mem_lib[j].money << endl;++count;}}if(count ==0){
cout <<"None!"<< endl;}
cout <<"Patrons\n";int count2 =0;for(int j =0; j < num; j++){if(Mem_lib[j].money <10000){
cout << Mem_lib[j].name <<"\t"<< Mem_lib[j].money << endl;++count2;}}if(count2 ==0){
cout <<"None!"<< endl;}return0;}
第7题
#include<cctype>#include<iostream>#include<string>usingnamespace std;intmain(){
cout <<"Enter words(q to quit):\n";
string word;
cin >> word;int v_count =0;int c_count =0;int others =0;//(1 == word.size()) &&while((word.size()!=1)|| word[0]!='q'){if(isalpha(word[0])){
word[0]=tolower(word[0]);switch(word[0]){case'a':case'e':case'i':case'o':case'u':++v_count;break;default:++c_count;break;}}else{++others;}//cin.get(); //每次读取下一个单词前,读走一个字符,用于排除'\n'
cin >> word;}
cout << v_count <<" words beginning with vowels\n";
cout << c_count <<" words beginning with consonants\n";
cout << others <<" others\n";return0;}
第8题
#include<fstream>#include<iostream>intmain(){usingnamespace std;
ifstream fin;
fin.open("char.txt");if(!fin.is_open()){
cout <<"Wrong!Open file failed.\n";}char ch;
fin.get(ch);int count =0;while(fin.good()){++count;
fin.get(ch);}if(fin.eof()){
cout <<"End of file reached!\n";}elseif(fin.fail()){
cout <<"Type is mismatch\n";}else{
cout <<"Wrong! But I don't konw the reason\n";}
cout <<"The text has "<< count <<" characters.\n";return0;}
第9题
#include<fstream>#include<iostream>#include<vector>usingnamespace std;char filename[50];struct Member
{char name[50];double money;};intmain(){
ifstream fin;
cout <<"Please input the name of data file: \n";
cin >> filename;
fin.open(filename);int num;
fin >> num;
fin.get();
vector<Member>Mem_lib(num);int i =0;
fin.getline(Mem_lib[i].name,50);while(i < num && cin.good()&& Mem_lib[i].name[0]!='\n')//empty line to quit{
fin >> Mem_lib[i].money;
fin.get();//在此处插入cin.get()的原因是为了消耗掉末尾的换行符'\n',因为cin读取数据和scanf类似是以空白符作为结尾的,并且会把//换行符留在输入队列中,进而影响cin.getline()读取字符串;但是并不影响cin读取数据,因为cin在读取数据时,会忽略遇到有效字符前的空白字符
i++;if(i < num){//读取数据,遇到换行符就停止读取,所以上一次读取数据后如果把换行符留在输入队列中,将会影响本次读取
fin.getline(Mem_lib[i].name,50);}}
cout <<"\nGrand Patrons\n";int count =0;for(int j =0; j < num; j++){if(Mem_lib[j].money >=10000){
cout << Mem_lib[j].name <<"\t"<< Mem_lib[j].money << endl;++count;}}if(count ==0){
cout <<"None!"<< endl;}
cout <<"\nPatrons\n";int count2 =0;for(int j =0; j < num; j++){if(Mem_lib[j].money <10000){
cout << Mem_lib[j].name <<"\t"<< Mem_lib[j].money << endl;++count2;}}if(count2 ==0){
cout <<"None!"<< endl;}return0;}