练习1 #include <iostream> #include <cctype> using namespace std; int main() { char ch; int count=0; while((cin >> ch)&&(ch!='@')) { if (!isdigit(ch)) if (islower(ch)) cout << (char)toupper(ch); else if (isupper(ch)) cout << (char)tolower(ch); // cout << islower(ch)?toupper(ch):(isupper(ch)?tolower(ch):ch); ++count; } cout << "/ntotal " << count << " charactors read" << endl; return 0; } 练习2 #include <iostream> using namespace std; int main() { const unsigned int MAX=10; int i=0,count=0; double donation[MAX]; cout << "Enter ten donation values,(non-number to stop)/n"; double sum=0; while(i<MAX&&(( cout << "input donation " << i+1 << ": "),cin >>donation[i])) sum+=donation[i++]; sum/=i; for (int j=0;j<i;j++) { if (donation[j]>sum) ++count; } cout << "Average of your input of " << i << " donation is " << sum; cout << "/nand there're " << count << " of them larger than the average."; return 0; } 练习3 #include <iostream> using namespace std; int main() { char ch; cout << "Please enter one of the following choices:" << endl; cout << "c) carbuvire/t/tp) pianist/n"; cout << "t) tree/t/t/tg) game/n"; while(cin >> ch) { switch (ch) { case 'c': cout << "this is a carbuvire."; return 0; case 'p': cout << "he is a pianist."; return 0; case 't': cout << "A maple is a tree."; return 0; case 'g': cout << "let's play game"; return 0; default: cout << "Please enter a c, p, t, or g: "; } } return 0; } 练习4 #include <iostream> const unsigned strsize=20; struct bop { char fullname[strsize]; char title[strsize]; char bopname[strsize]; int preference; }; int main() { using namespace std; bop bops[]={ {"Wimp Macho","Dr.","BOP",0}, {"Raki Rhodes","Junior Programmer","BIP",1}, {"Celia Laiter","Senior Programmer","MIPS",2}, {"Hoppy Hipman","Analyst Trainee","LOOP",1}, {"Pat Hand","Deluxe Programmer","LOOPY",2} }; cout << "Benevolent Order of Programmers Report/n" "a. display by name/tb. display by title/n" "c. display by bopname/td.display by preference/n" "q. quit/n"; cout << "Enter your choice: "; char ch; while (cin>>ch) { switch (ch) { case 'a':for (unsigned i=0;i<sizeof(bops)/sizeof(bops[0]);i++) cout << bops[i].fullname << endl; break; case 'b':for (unsigned i=0;i<sizeof(bops)/sizeof(bops[0]);i++) cout << bops[i].title << endl; break; case 'c':for (unsigned i=0;i<sizeof(bops)/sizeof(bops[0]);i++) cout << bops[i].bopname << endl; break; case 'd':for (unsigned i=0;i<sizeof(bops)/sizeof(bops[0]);i++) cout << (bops[i].preference==0?bops[i].fullname:bops[i].preference==1?bops[i].title:bops[i].bopname) << endl; break; case 'q':cout << "Bye!"; return 0; } cout << "Next choice: "; } return 0; } 练习5 #include <iostream> using namespace std; int main() { char ch; int m=0; float tax=0; cout << "Input your income: Native number or other char to exit/n"; while(cin.get(ch)&&isdigit(ch)) m=m*10+ch-48; while(m>0) { cout << "Your income is " << m << " tvarp/n"; if (m-35000>0) { tax+=(m-35000)*0.2;; } if (m-15000>0) { tax+=(m-15000<20000?m-15000:20000)*0.15;; } if (m-5000>0) { tax+=(m-5000<10000?m-5000:10000)*0.1;; } cout << "you should pay income tax " << tax << " tvarp/n"; tax=m=0; cout << "Input your income: Native number or other char to exit/n"; while(cin.get(ch)&&isdigit(ch)) m=m*10+ch-48; } return 0; } 练习6 #include <iostream> using namespace std; struct donor{ std::string name; unsigned donation; }; int main() { cout << "input the number of donors:"; int num; (cin >> num).get(); donor *donors=new donor[num]; for (int i=0;i<num;i++) { cout << "input donor's name " << i+1 << ":"; getline(cin,donors[i].name); cout << "input donation :"; (cin >> donors[i].donation).get(); } cout << "output the donors list" << endl; cout << "****************** Grand Patrons *****************/n"; int j=0; for (int i=0;i<num;i++) { if (donors[i].donation>10000) { ++j; cout << "/t" << donors[i].name << "/t/t" << donors[i].donation << endl; } } if (!j) cout << "/t/t/tnone/n"; cout << "********************* Patrons ********************/n"; j=0; for (int i=0;i<num;i++) { if (donors[i].donation<=10000) { ++j; cout << "/t" << donors[i].name << "/t/t" << donors[i].donation << endl; } } if (!j) cout << "/t/t/tnone/n"; return 0; } 练习7 #include <iostream> #include <cctype> int main() { using namespace std; cout << "Enter words (q to quit):" << endl; char ch; string str="",s=""; while (cin.get(ch)&&(str+=ch,ch=='q'?(cin.get(ch),isalpha(ch)):true)) if (str[str.size()-1]=='q') str+=ch; str.resize(str.size()-1); int i,j,p,q,r; i=j=p=q=r=0; for (unsigned k=0;k<str.size();k++) { if (str[k]!=' '&&str[k]!='.'&&str[k]!='/n') { if (!isdigit(str[k])) { if (j++==0) switch (str[k]) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': p++; break; default : q++; } } else { if (j++==0) r++; } } else { j=0; } } cout << p << " words beginning with vowels/n"; cout << q << " words beginning with consonants/n"; cout << r << " others" << endl; return 0; } 练习8 #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; cout << "Enter the input file name:"; char filename[20],ch; unsigned i; cin >> filename; fin.open(filename); if (!fin.is_open()) exit(EXIT_FAILURE); while(fin.get(ch),fin.good()) i++; cout << "Total characters : " << i << endl; return 0; } 练习9 #include <iostream> #include <fstream> using namespace std; struct donor{ std::string name; unsigned donation; }; int main() { ifstream fin; fin.open("donors.txt"); //cout << "input the number of donors:"; int num; (fin >> num).get(); donor *donors=new donor[num]; for (int i=0;i<num;i++) { //cout << "input donor's name " << i+1 << ":"; getline(fin,donors[i].name); //cout << "input donation :"; (fin >> donors[i].donation).get(); } cout << "output the donors list" << endl; cout << "****************** Grand Patrons *****************/n"; int j=0; for (int i=0;i<num;i++) { if (donors[i].donation>10000) { ++j; cout << "/t" << donors[i].name << "/t/t" << donors[i].donation << endl; } } if (!j) cout << "/t/t/tnone/n"; cout << "********************* Patrons ********************/n"; j=0; for (int i=0;i<num;i++) { if (donors[i].donation<=10000) { ++j; cout << "/t" << donors[i].name << "/t/t" << donors[i].donation << endl; } } if (!j) cout << "/t/t/tnone/n"; return 0; }