switch, ex6.9

版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 4.0

Example

统计输入的一句话中元音出现的次数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int ()
{
char ch;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, otherCnt = 0;
while (cin >> ch)
{
switch (ch)
{
case 'a':
case 'A':
++aCnt;
break;
case 'e':
case 'E':
++eCnt;
break;
case 'i':
case 'I':
++iCnt;
break;
case 'o':
case 'O':
++oCnt;
break;
case 'u':
case 'U':
++uCnt;
break;
default:
++otherCnt;
break;
}
}
cout << "Number of vowel a/A:t" << aCnt << "n"
<< "Number of vowel e/E:t" << eCnt << "n"
<< "Number of vowel i/I:t" << iCnt << "n"
<< "Number of vowel o/O:t" << oCnt << "n"
<< "Number of vowel u/U:t" << uCnt << "n"
<< "Number of other characters:t" << otherCnt << endl;
return 0;
}

outputs

1
2
3
4
5
6
7
8
Sorry! Who Are You Again?↙
^Z↙
Number of vowel a/A: 3
Number of vowel e/E: 1
Number of vowel i/I: 1
Number of vowel o/O: 3
Number of vowel u/U: 1
Number of other characters: 12

Exercise 6.9

统计下列字符序列出现的次数:ff、fl以及fi。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int ()
{
char pre_ch = '