5.9
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 6 unsigned vowelCnt = 0, otherCnt = 0; 7 char ch; 8 while (cin >> ch) { 9 if (ch == 'a') { 10 ++vowelCnt; 11 } 12 else if (ch == 'e') { 13 ++vowelCnt; 14 } 15 else if (ch == 'i') { 16 ++vowelCnt; 17 } 18 else if (ch == 'o') { 19 ++vowelCnt; 20 } 21 else if (ch == 'u') { 22 ++vowelCnt; 23 } 24 else { 25 ++otherCnt; 26 } 27 28 } 29 cout<<"Number of vowel: "<<vowelCnt<<"\n" 30 <<"Number of other: "<<otherCnt<<"\n"<<endl; 31 system("pause"); 32 return 0; 33 }
5.10
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 6 unsigned vowelCnt = 0, otherCnt = 0; 7 char ch; 8 while (cin >> ch) { 9 switch (ch) { 10 case 'a':case 'A':case 'e':case 'E':case 'i':case 'I':case 'o':case 'O':case 'u':case 'U': 11 ++vowelCnt; 12 break; 13 default: 14 ++otherCnt; 15 break; 16 } 17 } 18 cout<<"Number of vowel: "<<vowelCnt<<"\n" 19 <<"Number of other: "<<otherCnt<<"\n"<<endl; 20 system("pause"); 21 return 0; 22 }
5.11
if 语句
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 6 unsigned vowelCnt = 0, otherCnt = 0, spaceCnt = 0, tableCnt = 0, lineCnt = 0; 7 char ch; 8 while (cin>>ch) 9 { 10 ch = getchar(); 11 if ((ch == 'a' || ch == 'A') || (ch == 'e' || ch == 'E') || (ch == 'i' || ch == 'I') || (ch == 'o' || ch == 'O') || (ch == 'u' || ch == 'U')) { 12 ++vowelCnt; 13 } 14 else if (ch == ' ') { 15 ++spaceCnt; 16 } 17 else if (ch == '\t') { 18 ++tableCnt; 19 } 20 else if (ch == '\n') { 21 ++lineCnt; 22 } 23 else { 24 ++otherCnt; 25 } 26 } 27 28 29 cout << "Number of vowel: " << vowelCnt << "\n" 30 << "Number of other: " << otherCnt << "\n" 31 << "Number of space: " << spaceCnt << "\n" 32 << "Number of table: " << tableCnt << "\n" 33 << "Number of line: " << lineCnt << "\n" 34 << "Number of other: " << otherCnt << "\n" << endl; 35 36 37 38 system("pause"); 39 return 0; 40 }
小结:第一个输入必须是字母,不能是空格、制表符等等,否则不计入。
switch 语句
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 6 unsigned vowelCnt = 0, otherCnt = 0, spaceCnt = 0, tableCnt = 0, lineCnt = 0; 7 char ch; 8 while (cin>>noskipws>>ch) 9 { 10 switch (ch) { 11 case 'a':case 'A':case 'e':case 'E':case 'i':case 'I':case 'o':case 'O':case 'u':case 'U': 12 ++vowelCnt; 13 break; 14 case ' ': 15 ++spaceCnt; 16 break; 17 case '\t': 18 ++tableCnt; 19 break; 20 case '\n': 21 ++lineCnt; 22 break; 23 } 24 } 25 26 27 cout << "Number of vowel: " << vowelCnt << "\n" 28 << "Number of other: " << otherCnt << "\n" 29 << "Number of space: " << spaceCnt << "\n" 30 << "Number of table: " << tableCnt << "\n" 31 << "Number of line: " << lineCnt << "\n" 32 << "Number of other: " << otherCnt << "\n" << endl; 33 34 35 36 system("pause"); 37 return 0; 38 }
cin后面写noskipws告诉cin不要忽略空白符。
5.12
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 6 unsigned vowelCnt = 0, otherCnt = 0, spaceCnt = 0, tableCnt = 0, lineCnt = 0, ffCnt = 0, flCnt = 0, fiCnt = 0; 7 char ch, ch_before=' '; 8 while (cin>>noskipws>>ch) 9 { 10 switch (ch) { 11 case 'a':case 'A':case 'e':case 'E':case 'i':case 'I':case 'o':case 'O':case 'u':case 'U': 12 ++vowelCnt; 13 if (ch_before == 'f') { 14 ++fiCnt; 15 } 16 break; 17 case ' ': 18 ++spaceCnt; 19 break; 20 case '\t': 21 ++tableCnt; 22 break; 23 case '\n': 24 ++lineCnt; 25 break; 26 case 'f': 27 if (ch_before == 'f') { 28 ++ffCnt; 29 } 30 break; 31 case 'l': 32 if (ch_before == 'f') { 33 ++fiCnt; 34 } 35 break; 36 } 37 ch_before=ch; 38 39 } 40 41 42 cout << "Number of vowel: " << vowelCnt << "\n" 43 << "Number of other: " << otherCnt << "\n" 44 << "Number of space: " << spaceCnt << "\n" 45 << "Number of table: " << tableCnt << "\n" 46 << "Number of line: " << lineCnt << "\n" 47 << "Number of other: " << otherCnt << "\n" 48 << "Number of ff: " << ffCnt << "\n" 49 << "Number of fl: " << flCnt << "\n" 50 << "Number of fi: " << fiCnt << "\n" << endl; 51 52 53 54 system("pause"); 55 return 0; 56 }
思路:在switch后面将ch的值赋给ch_before
5.14
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 string str, str_before, str_max; 8 unsigned int maxCnt = 0, m = 0; 9 vector<string> strs; 10 vector<int> nums; 11 while (cin >> str) { 12 if (str == str_before) { 13 ++maxCnt; 14 ++m; 15 } 16 else { 17 18 nums.push_back(maxCnt); 19 maxCnt = 1; 20 str_before = str; 21 strs.push_back(str); 22 m = 0; 23 } 24 } 25 nums.push_back(m + 1); 26 int a = 1; 27 vector<int>::iterator it = nums.begin(); 28 for (it; it != nums.end(); ++it) { 29 if (*it > a) { 30 a = *it; 31 } 32 } 33 for (int i = 0; i < nums.size(); ++i) { 34 if (nums[i] == a) { 35 cout << "单词" << strs[i-1] << "出现了" << nums[i] << "次" << endl; 36 } 37 } 38 system("pause"); 39 return 0; 40 }