练习5.9
int main() {
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch) {
if (ch == 'a')
++aCnt;
if (ch == 'e')
++eCnt;
if (ch == 'i')
++iCnt;
if (ch == 'o')
++oCnt;
if (ch == 'u')
++uCnt;
}
cout << "Number of vowel a: \t" << aCnt << '\n';
cout << "Number of vowel e: \t" << eCnt << '\n';
cout << "Number of vowel i: \t" << iCnt << '\n';
cout << "Number of vowel o: \t" << oCnt << '\n';
cout << "Number of vowel u: \t" << uCnt << '\n';
}
练习5.10
int main() {
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
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;
}
}
cout << "Number of vowel a: \t" << aCnt << '\n'
<< "Number of vowel e: \t" << eCnt << '\n'
<< "Number of vowel i: \t" << iCnt << '\n'
<< "Number of vowel o: \t" << oCnt << '\n'
<< "Number of vowel u: \t" << uCnt << '\n';
}
练习5.11
int main() {
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,spaceCnt = 0,
nCnt = 0,tCnt = 0;
string line;
while (getline(cin,line)){ //每次读入一整行,直到到达文件末尾
++nCnt; //换行符增加1,因为getline()函数读取内容直到遇到换行符为止
for (auto c : line) {
switch (c) {
case 'a':
++aCnt;
break;
case 'e':
++eCnt;
break;
case 'i':
++iCnt;
break;
case 'o':
++oCnt;
break;
case 'u':
++uCnt;
break;
case ' ': //空格
++spaceCnt;
break;
case '\t': //我不知道怎么统计制表符
++tCnt;
break;
}
}
}
cout << "Number of vowel a: \t" << aCnt << '\n'
<< "Number of vowel e: \t" << eCnt << '\n'
<< "Number of vowel i: \t" << iCnt << '\n'
<< "Number of vowel o: \t" << oCnt << '\n'
<< "Number of vowel u: \t" << uCnt << '\n'
<< "Number of space: \t" << spaceCnt << '\n'
<< "Number of newline: \t" << nCnt << '\n'
<< "Number of tabs: \t" << tCnt;
}
练习5.12
int main() {
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,ffCnt = 0,
flCnt = 0,fiCnt = 0;
string line;
while (getline(cin,line)){ //每次读入一整行,直到到达文件末尾
for(decltype(line.size()) index = 0; index!= line.size();++index) { //通过下标的方法循环读取每一个字符
switch (line[index]) { //判断某一字符
case 'a':
++aCnt;
break;
case 'e':
++eCnt;
break;
case 'i':
++iCnt;
break;
case 'o':
++oCnt;
break;
case 'u':
++uCnt;
break;
case 'f': { //字符为f的情况下
switch (line[index+1]) { //判断下一个字符
case 'f':
++ffCnt;
break;
case 'l':
++flCnt;
break;
case 'i':
++fiCnt;
break;
}
}
}
}
}
cout << "Number of vowel a: \t" << aCnt << '\n'
<< "Number of vowel e: \t" << eCnt << '\n'
<< "Number of vowel i: \t" << iCnt << '\n'
<< "Number of vowel o: \t" << oCnt << '\n'
<< "Number of vowel u: \t" << uCnt << '\n'
<< "Number of ff: \t\t" << ffCnt << '\n'
<< "Number of fl: \t\t" << flCnt << '\n'
<< "Number of fi: \t\t" << fiCnt;
}
练习5.13
(a)没有添加break语句,统计结果错误。修改后如下所示:
int main() {
unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
char ch = next_text();
switch (ch) {
case 'a':
aCnt++;
break;
case 'e':
eCnt++;
default:
iouCnt++;
}
}
(b)需要定义ivec,在default中使用到ix变量,因此不能在case1中定义ix变量。修改后如下所示:
int main() {
unsigned index = some_value();
string ivec; //需要定义ivec
switch (index) {
int ix = get_value(); //在default中使用到ix变量,因此不能在case1中定义ix变量
case 1:
ivec[ix] = index;
break;
default:
ix = ivec.size() - 1;
ivec[ix] = index;
}
}
(c)case语句写法不对,oddCnt和oddcnt对应不上,evenCnt和evencnt对应不上,注意大小写。修改后如下所示:
int main() {
unsigned evenCnt = 0, oddCnt = 0;
int digit = get_num() % 10;
switch (digit) {
case 1: case 3: case 5: case 7: case 9:
oddCnt++;
break;
case 2: case 4: case 6: case 8: case 10:
evenCnt++;
break;
}
}
(d)case标签必须是整型常量表达式。修改后如下所示:
int main() {
//case标签必须是整型常量表达式
const unsigned ival = 512, jval = 1024, kval = 4096;
unsigned bufsize;
unsigned swt = get_bufsize();
switch (swt) {
case ival:
bufsize = ival * sizeof(int);
break;
case jval:
bufsize = jval * sizeof(int);
break;
case kval:
bufsize = kval * sizeof(int);
break;
}