#include<iostream>#include<array>usingnamespace std;intmain(){
array<double,10> donation;double input;int count =0;double sum =0.0;double average;
cout <<"Please enter the double numerial: ";while((cin >> input)&&(count <10)){
donation[count++]= input;
cout <<"Please enter the double numerial: ";}for(int i =0; i < count; i++){
sum += donation[i];}
average = sum / count;int i =0;while(count--){if(donation[count]> average)
i++;}
cout <<"Average is: "<< average << endl;
cout << i <<" numbers are bigger than average."<< endl;return0;}
#include<iostream>usingnamespace std;intmain(){char ch;
cout <<"Please enter one of the following choices: "<< endl;
cout <<"c) carnivore p) pianist"<< endl;
cout <<"t) tree g) game"<< endl;while(cin >> ch){if(ch !='c'&& ch !='p'&& ch !='t'&& ch !='g'){
cout <<"Please enter a c,p,t or g: ";continue;}switch(ch){case'c': cout <<"A car."<< endl;break;case'p': cout <<"A pepple."<< endl;break;case't': cout <<"A maple is a tree."<< endl;break;case'g': cout <<"A google."<< endl;break;}
cout <<"Please enter a c,p,t or g: ";}return0;}
#include<iostream>usingnamespace std;constint strsize =40;structbop{char fullname[strsize];char title[strsize];char bopname[strsize];int preference;};intmain(){char choice;
bop people[5]={{"Wimp macho","Level_A","ww",0},{"Raki Rhodes","Junior Programmer","RR",1},{"Celia Laiter","Level_C","MIPS",2},{"Hoppy Hipman","Analyst trainee","HH",1},{"Pat Hand","Level_D","LOOPY",2}};
cout <<"Benevolent Order of Programmers Report "<< endl;
cout <<"a. display by name b. display by title"<< endl;
cout <<"c. display by bopname d. display by preference"<< endl;
cout <<"q. quit"<< endl;
cout <<"Enter your choice: ";while(cin >> choice){switch(choice){case'a':for(int i =0; i <5; i++)
cout << people[i].fullname << endl;break;case'b':for(int i =0; i <5; i++)
cout << people[i].title << endl;break;case'c':for(int i =0; i <5; i++)
cout << people[i].bopname << endl;break;case'd':for(int i =0; i <5; i++){switch(people[i].preference){case0: cout << people[i].fullname << endl;break;case1: cout << people[i].title << endl;break;case2: cout << people[i].bopname << endl;break;}}break;case'q':
cout <<"Bye!"<< endl;exit(0);}
cout <<"Next choice: ";}return0;}
#include<iostream>usingnamespace std;intmain(){int income;double tax;
cout <<"Please enter your income: ";while((cin >> income)&&(income >=0)){if(income <=5000){
tax = income *0.0;}elseif(income <=15000){
tax =(income -5000)*0.1;}elseif(income <=35000){
tax =10000*0.1+(income -15000)*0.15;}else{
tax =10000*0.1+20000*0.15+(income -35000)*0.2;}
cout <<"your tax = "<< tax << endl;
cout <<"Please enter your income: ";}return0;}
#include<iostream>#include<string>usingnamespace std;structPatrons{
string name;double money;};intmain(){int num;
cout <<"Please enter the number of patrons: ";
cin >> num;
cin.get();
Patrons* patron =new Patrons[num];
cout <<"Enter the information of patrons: "<< endl;
Patrons* grand_patron =new Patrons[num];
Patrons* common_patron =new Patrons[num];int k =0, j =0;for(int i =0; i < num; i++){
cout <<"Please enter name #"<< i +1<<": ";getline(cin, patron[i].name);
cout <<"Please enter the amount of donation #"<< i +1<<": ";while(!(cin >> patron[i].money))//处理错误输入;{
cin.clear();while(cin.get()!='\n')continue;
cout <<"Please enter a number: ";}
cin.get();//吸收正确输入时的换行符;if(patron[i].money >10000)
grand_patron[k++]= patron[i];else
common_patron[j++]= patron[i];}
cout <<"Grand Patrons"<< endl;if(k ==0)
cout <<"none"<< endl;for(int i =0; i < k; i++){
cout << grand_patron[i].name <<" "<< grand_patron[i].money << endl;}
cout <<"Patrons"<< endl;if(j ==0)
cout <<"none"<< endl;for(int i =0; i < j; i++){
cout << common_patron[i].name <<" "<< common_patron[i].money << endl;}delete[]common_patron;delete[]grand_patron;delete[]patron;return0;}
#include<iostream>#include<string>#include<cctype>usingnamespace std;intmain(){
string word;
cout <<"Enter words (q to quit): "<< endl;int vowels =0, consonants =0, others =0;while((cin >> word)&&(word !="q")){if(isalpha(word[0])){switch(word[0]){case'a':case'e':case'i':case'o':case'u':case'A':case'E':case'I':case'O':case'U':
vowels++;break;default:
consonants++;}}else{
others++;}}
cout << vowels <<" words beginning with vowels"<< endl;
cout << consonants <<" words beginning with consonants"<< endl;
cout << others <<" others"<< endl;return0;}
#include<iostream>#include<fstream>#include<string>#include<cstdlib>usingnamespace std;intmain(){
ifstream inFile;
string file_name;
cout <<"Enter the file name: ";getline(cin, file_name);
inFile.open(file_name);if(!inFile.is_open()){
cout <<"Failed to open the file!"<< endl;exit(EXIT_FAILURE);}char ch;int count =0;while(!inFile.eof()){
inFile >> ch;
count++;}
cout << file_name <<" has "<< count <<" characters."<< endl;
inFile.close();return0;}
#include<iostream>#include<string>#include<fstream>#include<cstdlib>usingnamespace std;structPatrons{
string name;double money;};intmain(){
ifstream inFile;
string file_name;
cout <<"Enter the file name: ";getline(cin, file_name);
inFile.open(file_name);if(!inFile.is_open()){
cout <<"Failed to open the file!"<< endl;exit(EXIT_FAILURE);}int num;
inFile >> num;
inFile.get();
Patrons* patron =new Patrons[num];
Patrons* grand_patron =new Patrons[num];
Patrons* common_patron =new Patrons[num];int k =0, j =0;for(int i =0; i < num; i++){getline(inFile, patron[i].name);
inFile >> patron[i].money;
inFile.get();//吸收正确输入时的换行符;if(patron[i].money >10000)
grand_patron[k++]= patron[i];else
common_patron[j++]= patron[i];}
cout <<"Grand Patrons"<< endl;if(k ==0)
cout <<"none"<< endl;for(int i =0; i < k; i++){
cout << grand_patron[i].name <<" "<< grand_patron[i].money << endl;}
cout <<"Patrons"<< endl;if(j ==0)
cout <<"none"<< endl;for(int i =0; i < j; i++){
cout << common_patron[i].name <<" "<< common_patron[i].money << endl;}delete[]common_patron;delete[]grand_patron;delete[]patron;
inFile.close();return0;}