#include<iostream>#include<stdlib.h>#include<string>usingnamespace std;#include<cctype>constint ArrSize =10;intmain(){double arr[ArrSize];int donation;int n =0;int average =0;int sum =0;int count =0;
cout <<"Enter donation:";while(n < ArrSize &&(cin >> donation)){
arr[n]= donation;++n;
sum += donation;if(n<ArrSize)
cout <<"Next : ";}
average = sum / n;for(int i =0; i < n; i++){if(arr[i]> average)++count;}
cout <<"average = "<< average <<" count = "<< count << endl;system("pause");return0;}
3
#include<iostream>#include<stdlib.h>#include<string>usingnamespace std;voidShowMenu();intmain(){
cout <<"Please enter a choice :\n";char choice;ShowMenu();while(cin>>choice &&true){//cin >> choice;switch(choice){case'c':
cout <<"carnivore"<< endl;break;case'p':
cout <<"pianist"<< endl;break;case't':
cout <<"tree"<< endl;break;case'g':
cout <<"game"<< endl;break;case'@':
cout <<"欢迎下次使用!"<< endl;system("pause");return0;break;default:
cin.clear();while(cin.get()!='\n')continue;
cout <<"Please enter c, p, t or g (@ to quit): ";}}/*char ch;
string c = "carnivore";
string p = "pianist";
string t = "tree";
string g = "game";
cout << "Please enter one of the following choices:\n";
cout << "c) " << c << " " << "p) " << p << "\n";
cout << "t) " << t << " " << "g) " << g << "\n";
while (cin >> ch && ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') {
cout << "Please enter a c, p, t or g: ";
}
switch (ch) {
case 'c':cout << "A maple is a " << c << endl;
break;
case 'p':cout << "A maple is a " << p << endl;
break;
case 't':cout << "A maple is a " << t << endl;
break;
case 'g':cout << "A maple is a " << g << endl;
break;
}*/system("pause");return0;}voidShowMenu(){
cout <<"c) carnivore p) pianist"<< endl
<<"t) tree g) game"<< endl;}
4
#include<iostream>#include<stdlib.h>#include<string>usingnamespace std;constint ArrSize =20;// Benevolent Order of Programmers name structurestructbop{char fullname[ArrSize];// real name char title[ArrSize];// job title char bopname[ArrSize];// secret BOP name int preference;// 0 = fullname , 1 = title , 2 = bopname };voidShowMenu(){
cout <<"Benevolent Order of Programmers Report"<< endl
<<" a. display by name b. display by title"<< endl
<<" c. display by bopname d. display by preference"<< endl
<<" q. quit"<< endl;}intmain(){
bop arr[]={{"Tom","professor","T",1},{"Jerry","doctor","J",2},{"Jack","graduate","J",3},{"Mary","college","T",1}};ShowMenu();char choice;int count =0;int len =sizeof(arr)/sizeof(structbop);//结构体中元素个数
cout <<"Enter your choice:";//cin >> choice;while(cin>>choice){switch(choice){case'a':for(int i =0; i < len; i++){
cout << arr[i].fullname << endl;}
cout <<"Next choice:";break;case'b':for(int i =0; i < len; i++){
cout << arr[i].title << endl;}
cout <<"Next choice:";break;case'c':for(int i =0; i < len; i++){
cout << arr[i].bopname << endl;}
cout <<"Next choice:";break;case'd':
cout <<"Enter preference:";int pref;
cin >> pref;for(int i =0; i < len; i++){if(arr[i].preference == pref){
cout << arr[i].title << endl;++count;}}if(count ==0)
cout <<"No person's preference is "<< pref << endl;/*for (int i = 0; i < len; i++) {
if (arr[i].preference == 0)
cout << arr[i].fullname << endl;
else if (arr[i].preference == 1)
cout << arr[i].title << endl;
else
cout << arr[i].bopname << endl;
}*/
cout <<"Next choice:";break;case'q':
cout <<"欢迎下次使用! Bye!"<< endl;system("pause");return0;break;default:
cout <<"That's not a choice.\n";
cout <<"Enter your choice:";//cin >> choice;}}system("pause");return0;}