练习5.9:编写一段程序,使用一系列if语句统计从cin读入的文本中有多少个元音字母。
练习5.10:我们之前实现的统计元音字母的程序存在一个问题:如果元音字母以大写字母形式出现,不会被统计在内。编写一段程序,既统计元音字母的小写形式,也统计大写形式,也就是说,新程序遇到‘a’和‘A’都应该递增aCnt的值,以此类推。
练习5.11:修改统计元音字母的程序,使其也能统计空格,制表符和换行符的数量。
练习5.12:修改统计元音字母的程序,使其能统计以下含有两个字符的字符序列的数量:ff,fl和fi。
答:练习5.9-5.12见云盘程序。
练习5.13:下面显示的每个程序都含有一个常见的编程错误,指出错误在哪里,然后修改它们。
(a).
unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
char ch = next_text();
switch (ch) {
case ‘a’ : aCnt++;//缺少break
//break;
case ‘e’ : eCnt++;//缺少break
//break;
default: iouCnt++;//缺少break
//break;
}
(b).
unsigned index = some_value();
Switch(index) {
case 1:
//int ix = get_value(); // 错误:控制流绕过一个显式初始化的变量
//ivec[ ix ] = index; // 错误:控制流绕过一个显式初始化的变量
break;
//break;
default:
ix = ivec.size() – 1;
ivec [ ix ] = index;
}
(c).
unsigned evenCnt = 0, oddCnt = 0;
int digit = get_num() % 10;
switch (digit)
{
Case 1,3,5,7,9://写法错误 case 1: case 3: case 5: case 7: case 9:
oddCnt++;
case 2,4,6,8,10://case 2: case 4: case 6: case 8: case 10:
evenCnt++;
break;
}
(d). unsigned ival = 512, jval = 1024, kval = 4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt)
{
case ival://错误:case标签必须是整型常量表达式。
bufsize = ival * sizeof(int);
break;
case jval: //错误:case标签必须是整型常量表达式。
bufsize = javl * sizeof(int);
case kval: //错误:case标签必须是整型常量表达式。
bufsize = kval * sizeof(int);
break;
}
练习5.9
#include
using namespace std;
int main()
{
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, totalCnt = 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;
totalCnt = aCnt + eCnt + iCnt + oCnt + uCnt ;
}
cout << "a:" << aCnt << endl;
cout << "e:" << eCnt << endl;
cout << "i:" << iCnt << endl;
cout << "o:" << oCnt << endl;
cout << "u:" << uCnt << endl;
cout << "total:" << totalCnt << endl;
return 0;
}
练习5.10
#include
using namespace std;
int main()
{
unsigned aCnt =0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, other = 0, vowelCnt = 0,total = 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;
default:
++other;
break;
}
}
vowelCnt = aCnt + eCnt + iCnt + oCnt + uCnt;
total = vowelCnt + other;
cout << "a:" << aCnt << endl;
cout << "e:" << eCnt << endl;
cout << "i:" << iCnt << endl;
cout << "o:" << oCnt << endl;
cout << "other:" << other << endl;
cout << "vowelCnt:" << vowelCnt << endl;
cout << "total:" << total << endl;
return 0;
}
练习5.11
#include
#include
#include
using namespace std;
int main()
{
unsigned aCnt =0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, other = 0, vowelCnt = 0,total = 0, space = 0, tableSign = 0, Enter = 0;
vector<char> charVector;
char ch;
//while (cin >> ch && ch != '#')
//输入流会掠过,空格,制表符,以及回车
ifstream in("练习5-11.txt");
//这边参考网上的例子,定义一个文件流,在文件同目录下,相应建一个new.txt文件,用于读取文件里的东西
if (!in)
{
cerr << "oh , shit!" << endl;
return -1;
}
while(in.get(ch))
{
cout << ch ;
charVector.push_back(ch);
}
cout << endl;//以上打印文件里面的字符
for ( auto i = 0; i != charVector.size(); ++i)
{
switch (charVector[i])
{
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;
case ' ':
++space;
break;
case '\t':
++tableSign;
break;
case '\n':
++Enter;
break;
default:
++other;
break;
}
}
vowelCnt = aCnt + eCnt + iCnt + oCnt + uCnt;
total = vowelCnt + other;
cout << "a:" << aCnt << endl;
cout << "e:" << eCnt << endl;
cout << "i:" << iCnt << endl;
cout << "o:" << oCnt << endl;
cout << "other:" << other << endl;
cout << "vowelCnt:" << vowelCnt << endl;
cout << "space:" << space << endl;
cout << "tableSign:" << tableSign << endl;
cout << "Enter:" << Enter << endl;
cout << "total:" << total << endl;
return 0;
}
练习5-11.txt
a b c d e f g h i j k l m n
o p q
练习5.12
#include
#include
#include
using namespace std;
int main()
{
unsigned aCnt =0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, other = 0, vowelCnt = 0,total = 0, space = 0, tableSign = 0, Enter = 0, ffCnt = 0, flCnt = 0, fiCnt = 0;
vector<char> charVector;
char ch;
//while (cin >> ch && ch != '#') 输入流会掠过,空格,制表符,以及回车
//这边参考网上的例子,定义一个文件流,在文件同目录下,相应建一个new.txt文件,用于读取文件里的东西
if (!in)
{
cerr << "oh , shit!" << endl;
return -1;
}
while(in.get(ch))
{
cout << ch ;
charVector.push_back(ch);
}
cout << endl;//以上打印文件里面的字符
for ( auto i = 0; i != charVector.size(); ++i)
{
switch (charVector[i])
{
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;
case ' ':
++space;
break;
case '\t':
++tableSign;
break;
case '\n':
++Enter;
break;
case 'f':
if (charVector[i+1] == 'f')
//前一个字符为f,紧接着为ff情况
++ffCnt;
else if (charVector[i+1] == 'l')
++flCnt;
else if (charVector[i+1] == 'i')
++fiCnt;
//else ;
//执行一个空语句,代表上述三种情况之外的,执行一个空语句,无任何结果
break;
default:
++other;
break;
}
}
vowelCnt = aCnt + eCnt + iCnt + oCnt + uCnt;
total = vowelCnt + other;
cout << "a:" << aCnt << endl;
cout << "e:" << eCnt << endl;
cout << "i:" << iCnt << endl;
cout << "o:" << oCnt << endl;
cout << "other:" << other << endl;
cout << "vowelCnt:" << vowelCnt << endl;
cout << "space:" << space << endl;
cout << "tableSign:" << tableSign << endl;
cout << "Enter:" << Enter << endl;
cout << "ffCnt:" << ffCnt << endl;
cout << "flCnt:" << flCnt << endl;
cout << "fiCnt:" << fiCnt << endl;
cout << "total:" << total << endl;
return 0;
}