C++ Primer Plus第六版编程练习---第6章 分支语句和逻辑运算符
1、
1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 5 int main() 6 { 7 std::string inputStr; 8 std::cout<<"Enter your character list. enter @ to end." << std::endl; 9 char ch; 10 std::string srcStr; 11 std::string dstStr; 12 while(std::cin.get(ch)) 13 { 14 if(ch == '@') 15 { 16 std::cout << "input befor exchane: " << srcStr << std::endl; 17 std::cout << "input after exchane: " << dstStr << std::endl; 18 break; 19 } 20 srcStr = srcStr + ch; 21 if(islower(ch)) 22 { 23 dstStr = dstStr + char(toupper(ch)); 24 continue; //必须有continue,否则全转为小写了 25 } 26 if(isupper(ch)) 27 { 28 dstStr = dstStr + char(tolower(ch)); 29 continue; 30 } 31 dstStr = dstStr + ch; //其他字符原样显示 32 } 33 return 0; 34 }
2、
1 #include <iostream> 2 #include <array> 3 4 int main() 5 { 6 const int size = 10; 7 std::array<double,size> donation; 8 double sum = 0.0; 9 double avrgValue = 0.0; 10 int countLager = 0; 11 int enterNum = 0; 12 std::cout<<"Pleas enter up 10 double value, Non-digital to exit."<<std::endl; 13 while((enterNum < size) && (std::cin>>donation[enterNum])) 14 { 15 sum += donation[enterNum]; 16 enterNum++; 17 } 18 avrgValue = sum/enterNum; 19 for(int i=0; i<enterNum;i++) 20 { 21 if(donation[i] > avrgValue) 22 { 23 countLager++; 24 } 25 } 26 27 std::cout<<"There have " << countLager << " larger than everage." << std::endl; 28 29 return 0; 30 }
3、
1 #include <iostream> 2 #include <string> 3 4 void useage() 5 { 6 std::cout<<"Please enter one of the following choices:"<<std::endl; 7 std::cout<<"c) carnivore\t"<<"p) pianist"<<std::endl; 8 std::cout<<"t) tree\t\t"<<"g) game"<<std::endl; 9 } 10 11 void tip() 12 { 13 std::cout<<"Please enter a c, p, t or g: "; 14 } 15 int main() 16 { 17 useage(); 18 char ch; 19 while(true) 20 { 21 std::cin>>ch; 22 switch(ch) 23 { 24 case 'c': 25 std::cout<<"A tager is a carnivore." << std::endl; 26 continue; 27 case 'p': 28 std::cout<<"Beethoven is a pianist." << std::endl; 29 continue; 30 case 't': 31 std::cout<<"A maple is a tree." << std::endl; 32 continue; 33 case 'g': 34 std::cout<<"Chess is a game." << std::endl; 35 continue; 36 default: 37 tip(); 38 continue; 39 } 40 } 41 return 0; 42 }
4、
1 #include <iostream> 2 #include <string> 3 4 #define strsize 100 5 //Benevolent Order of Programmers name structure 6 struct bop{ 7 char fullname[strsize]; //real name 8 char title[strsize]; //job title 9 char bopname[strsize]; //secret BOP name 10 int preference; //0 = fullname, 1 = title, 2 = bopname 11 }; 12 13 void useage() 14 { 15 std::cout<<"a) display by name\t"<<"b) display by title"<<std::endl; 16 std::cout<<"c) display by bopname\t"<<"d) display by preference"<<std::endl; 17 std::cout<<"q) quit"<<std::endl; 18 } 19 20 void nextChoice() 21 { 22 std::cout<<"Next choice: "; 23 } 24 25 int main() 26 { 27 bop userInfo[3]={{"Wimp Macho","title of Wimp Macho","bopname of Wimp Macho",0}, 28 {"Raki Rhodes","title of Raki Rhodes","bopname of Raki Rhodes",1}, 29 {"Celia Laiter","title of Celia Laiter","bopname of Celia Laiter",2}, 30 }; 31 useage(); 32 char ch; 33 std::cout<<"Enter your choice:"; 34 while(true) 35 { 36 std::cin>>ch; 37 if(ch != 'a' && ch != 'b' && ch != 'c' && ch != 'd' && ch != 'q') 38 { 39 std::cout<<"Enter your choice:"; 40 continue; 41 } 42 if(ch == 'a') 43 { 44 for(unsigned int i=0;i<3;i++) 45 { 46 std::cout<< userInfo[i].fullname << std::endl; 47 } 48 nextChoice(); 49 continue; 50 } 51 if(ch == 'b') 52 { 53 for(unsigned int i=0;i<3;i++) 54 { 55 std::cout<< userInfo[i].title << std::endl; 56 } 57 nextChoice(); 58 continue; 59 } 60 if(ch == 'c') 61 { 62 for(unsigned int i=0;i<3;i++) 63 { 64 std::cout<< userInfo[i].bopname << std::endl; 65 } 66 nextChoice(); 67 continue; 68 } 69 if(ch == 'd') 70 { 71 for(unsigned int i=0;i<3;i++) 72 { 73 if(userInfo[i].preference == 0) 74 { 75 std::cout<< userInfo[i].fullname << std::endl; 76 } 77 else if(userInfo[i].preference == 1) 78 { 79 std::cout<< userInfo[i].title << std::endl; 80 } 81 else if(userInfo[i].preference == 2) 82 { 83 std::cout<< userInfo[i].bopname << std::endl; 84 } 85 } 86 nextChoice(); 87 continue; 88 } 89 if(ch == 'q') 90 { 91 std::cout<<"Bye!" << std::endl; 92 break; 93 } 94 } 95 return 0; 96 }
5、
1 #include <iostream> 2 #include <string> 3 4 int main() 5 { 6 double income = 0; 7 std::cout<<"Please enter your income."<<std::endl; 8 while(std::cin>>income) //非数字的输入将使得cin方法调用返回false 9 { 10 if(income<0) 11 { 12 break; 13 } 14 if(income<=5000) 15 { 16 std::cout<<"Your income tax is: 0.00 tvarps." << std::endl; 17 } 18 else if(income<=15000) 19 { 20 std::cout<<"Your income tax is: " << 5000*0.00+(income-5000)*0.10 <<" tvarps."<< std::endl; 21 } 22 else if(income<=35000) 23 { 24 std::cout<<"Your income tax is: " << 5000*0.00+10000*0.10+(income-15000)*0.15 <<" tvarps."<< std::endl; 25 } 26 else 27 { 28 std::cout<<"Your income tax is: " << 5000*0.00+10000*0.10+20000*0.15+(income-35000)*0.20 <<" tvarps."<< std::endl; 29 } 30 } 31 return 0; 32 }
6、
1 #include <iostream> 2 #include <string> 3 4 struct patron{ 5 std::string name; 6 double amount; 7 }; 8 9 int main() 10 { 11 int countOfPatrons = 0; 12 std::cout<<"Please enter count of patrons: "; 13 std::cin>>countOfPatrons; 14 std::cin.get(); 15 patron * pointPatrons = new patron[countOfPatrons]; 16 for(int i=0; i<countOfPatrons; i++) 17 { 18 std::cout<<"Please enter the name of patron."<<std::endl; 19 getline(std::cin, pointPatrons[i].name); 20 std::cout<<"Please enter the amount of patron."<<std::endl; 21 std::cin>>pointPatrons[i].amount; 22 std::cin.get(); 23 } 24 std::cout<<"Grand Patrons"<<std::endl; 25 for(int i=0; i<countOfPatrons; i++) //有没有什么办法可以不使用这么多for循环 26 { 27 if(pointPatrons[i].amount > 10000) 28 { 29 if(pointPatrons[i].name.empty()) 30 { 31 std::cout << "none\t"<<pointPatrons[i].amount<<std::endl; 32 } 33 std::cout << pointPatrons[i].name<<"\t"<<pointPatrons[i].amount<<std::endl; 34 } 35 } 36 37 std::cout<<"Patrons"<<std::endl; 38 for(int i=0; i<countOfPatrons; i++) 39 { 40 if(pointPatrons[i].amount <= 10000) 41 { 42 if(pointPatrons[i].name.empty()) 43 { 44 std::cout << "none\t"<<pointPatrons[i].amount<<std::endl; 45 } 46 std::cout << pointPatrons[i].name<<"\t"<<pointPatrons[i].amount<<std::endl; 47 } 48 } 49 delete[] pointPatrons; 50 return 0; 51 }
7、
1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 5 int main() 6 { 7 char word[100]={'\0'}; 8 int wordsStartVow = 0; 9 int wordsStartCons = 0; 10 int otherCount = 0; 11 std::cout<<"Enter words (q to quit):" <<std::endl; 12 while(std::cin>>word) 13 { 14 bool quit = true; 15 for(int i=0;word[i] != '\0';i++) 16 { 17 if(word[i] != 'q') 18 { 19 quit = false; 20 } 21 } 22 if(quit) 23 { 24 break; 25 } 26 if(!isalpha(word[0])) 27 { 28 otherCount++; 29 continue; 30 } 31 32 switch(word[0]) 33 { 34 case 'a': 35 case 'e': 36 case 'i': 37 case 'o': 38 case 'u': 39 wordsStartVow++; 40 continue; 41 default: 42 wordsStartCons++; 43 } 44 } 45 std::cout<<wordsStartVow<<" words beginning with vowels."<<std::endl; 46 std::cout<<wordsStartCons<<" words beginning with consonants."<<std::endl; 47 std::cout<<otherCount<<" others."<<std::endl; 48 return 0; 49 }
8、
1 #include <iostream> 2 #include <fstream> 3 4 int main() 5 { 6 std::ifstream file; 7 file.open("E:\\test.txt"); 8 if(!file.is_open()) 9 { 10 std::cout<<"Could not open the file E:\\test.txt"<<std::endl; 11 return -1; 12 } 13 int countCharacter = 0; 14 char ch; 15 while(file.get(ch) && file.good()) 16 { 17 countCharacter++; 18 std::cout<<ch; 19 } 20 std::cout<<std::endl<<"file E:\\test.txt contain "<< countCharacter << " Characteres."<<std::endl; 21 file.close(); 22 return 0; 23 }
9、
1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 5 struct patron{ 6 char name[100]; 7 double amount; 8 }; 9 10 int main() 11 { 12 std::ifstream pfile; 13 pfile.open("E:\\test.txt"); 14 int countOfPatrons = 0; 15 pfile>>countOfPatrons; 16 pfile.get(); 17 patron * pointPatrons = new patron[countOfPatrons]; 18 for(int i=0; i<countOfPatrons; i++) 19 { 20 pfile.getline(pointPatrons[i].name, 100); 21 pfile>>pointPatrons[i].amount; 22 pfile.get(); 23 } 24 std::cout<<"Grand Patrons"<<std::endl; 25 for(int i=0; i<countOfPatrons; i++) //有没有什么办法可以不使用这么多for循环 26 { 27 if(pointPatrons[i].amount > 10000) 28 { 29 if(pointPatrons[i].name[0] == '\0') 30 { 31 std::cout << "none\t"<<pointPatrons[i].amount<<std::endl; 32 } 33 std::cout << pointPatrons[i].name<<"\t"<<pointPatrons[i].amount<<std::endl; 34 } 35 } 36 37 std::cout<<"Patrons"<<std::endl; 38 for(int i=0; i<countOfPatrons; i++) 39 { 40 if(pointPatrons[i].amount <= 10000) 41 { 42 if(pointPatrons[i].name[0] == '\0') 43 { 44 std::cout << "none\t"<<pointPatrons[i].amount<<std::endl; 45 } 46 std::cout << pointPatrons[i].name<<"\t"<<pointPatrons[i].amount<<std::endl; 47 } 48 } 49 50 delete[] pointPatrons; 51 pfile.close(); 52 return 0; 53 }
浙公网安备 33010602011771号