#include<iostream>intmain(){usingnamespace std;double Total[10];cout <<"Please enter numbers.(stop with no-numbers)"<< endl;int i =0;double donation;double sum =0;while(cin >> donation && i <10){sum += donation;Total[i]= donation;i++;}double average = sum / i;int j =0;for(int k =0; k < i; k++){if(Total[k]> average)j++;}cout <<"Average is "<< average <<" and there are "<< j <<" numbers above the average.\n";return0;}
6-3
switch本身沒有循環
#include<iostream>intmain(){usingnamespace std;char ch;cout <<"Please enter one of the following choice: \n";cout <<"c) carnivore p)pianist"<< endl;cout <<"t) tree g) game"<< endl;cin >> ch;while(ch !='c'&& ch !='p'&& ch !='t'&& ch !='g'){cout <<"Please enter a c, p, t, of g: ";cin >> ch;}cout << endl;switch(ch){case'c':cout <<"A maple is a carnivore.";break;case'p':cout <<"A maple is a pianist.";break;case't':cout <<"A maple is a tree.";break;case'g':cout <<"A maple is a game.";break;default:break;}return0;}
6-4
#include<iostream>constint strsize =20;usingnamespace std;struct bop
{char fullname[strsize];char title[strsize];char bopname[strsize];int preference;};intmain(){char PreferType;bop programmers[5]={{"Wimp Macho","Analyst Trainee","WM",0},{"Raki Rhodes","Junior Programmer","RR",1},{"Celia Laiter","Analyst Trainee","MIPS",2},{"Hoppy Hipman","Analyst Trainee","HH",1},{"Pat Hand","Junior Programmer","LOOPY",2}};cout <<"Benevolent Order of Programmers Report\n";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 >> PreferType && PreferType !='q'){switch(PreferType){case'a':for(int i =0; i <5; i++)cout << programmers[i].fullname << endl;cout <<"Next choice: ";continue;case'b':for(int i =0; i <5; i++)cout << programmers[i].title << endl;cout <<"Next choice: ";continue;case'c':for(int i =0; i <5; i++)cout << programmers[i].bopname << endl;cout <<"Next choice: ";continue;case'd':for(int i =0; i <5; i++){if(programmers[i].preference ==0)cout << programmers[i].fullname << endl;elseif(programmers[i].preference ==1)cout << programmers[i].title << endl;elsecout << programmers[i].bopname << endl;}cout <<"Next choice: ";continue;default:break;}}cout <<"Bye!"<< endl;return0;}
6-5 累進稅率
#include<iostream>constint strsize =20;usingnamespace std;intmain(){cout <<"Enter the income: ";double income;while(cin >> income && income >0){double tax =0.0;if(income <5000.0){cout <<"tax = 0"<< endl;cout <<"Enter the income: ";}elseif(income <15000.0){tax +=(income -5000.0)*0.1;cout <<"tax = "<< tax << endl;cout <<"Enter the income: ";}elseif(income <35000.0){tax +=(income -15000.0)*0.15;tax +=(15000.0-5000.0)*0.1;cout <<"tax = "<< tax << endl;cout <<"Enter the income: ";}else{tax +=(income -35000)*0.2;tax +=(35000.0-15000.0)*0.15;tax +=(15000.0-5000.0)*0.1;cout <<"tax = "<< tax << endl;cout <<"Enter the income: ";}}return0;}
6-6 還是那個問題,用getline之前需要清掉cin數字時留下的空
#include<iostream>#include<string>usingnamespace std;struct PatronsInfo
{string name;double amount;};intmain(){int NumOfPatrons;cout <<"Enter the number of patrons: ";cin >> NumOfPatrons;cin.get();PatronsInfo* PInfo =new PatronsInfo[NumOfPatrons];for(int i =0; i < NumOfPatrons;++i){cout <<"\nEnter the name of patrons: ";getline(cin, PInfo[i].name);cout <<"\nEnter the amout of patrons: ";cin >> PInfo[i].amount;cin.get();}cout <<"Grand Patrons\n";int k =0;for(int i =0; i < NumOfPatrons; i++){if(PInfo[i].amount >10000.0){cout << PInfo[i].name << endl;k++;}}if(k ==0){cout <<"None!"<< endl;}cout <<"Patrons\n";k =0;for(int i =0; i < NumOfPatrons; i++){if(PInfo[i].amount <=10000.0){cout << PInfo[i].name << endl; k++;}}if(k ==0){cout <<"None!"<< endl;}return0;}
#include<iostream>#include<string>usingnamespace std;intmain(){string WordStr;int NumOfVowels, NumOfConsonants, other;NumOfConsonants = NumOfVowels = other =0;cout <<"Enter words (q to quit): \n";while(cin >> WordStr && WordStr !="q"){if(!isalpha(WordStr[0]))other +=1;elseif(WordStr[0]=='A'or WordStr[0]=='E'or WordStr[0]=='I'or WordStr[0]=='O'or WordStr[0]=='U'or WordStr[0]=='a'or WordStr[0]=='e'or WordStr[0]=='i'or WordStr[0]=='o'or WordStr[0]=='u')NumOfVowels +=1;elseif(isalpha)NumOfConsonants +=1;}cout << NumOfVowels <<" words begining with vowels\n";cout << NumOfConsonants <<" words begining with consonants\n";cout << other <<" others\n";return0;}
6-8
#include<iostream>#include<fstream>usingnamespace std;intmain(){ifstream txtfile;txtfile.open("D://test.txt");if(!txtfile.is_open()){cout <<"Failed to open file!"<< endl;exit(EXIT_FAILURE);}char ch;int count =0;while(txtfile >> ch)count++;if(txtfile.eof())cout <<"End of file reached.\n";elseif(txtfile.fail())cout <<"Input termiated by data mismatch.\n";elsecout <<"Input terminated for unknown reason.\n";if(count ==0)cout <<"No data processed.\n";elsecout <<"There are "<< count <<" characters in this file."<< endl;return0;}