习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第5章 语句


练习5.9

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t;
int cnt = 0;
while (cin >> t) {
if (t == 'a' || t == 'e' || t == 'i' || t == 'o' || t == 'u') {
cnt++;
}
}
cout << cnt;
return 0;
}

练习5.10

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
while (cin >> t) {
switch (t) {
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 << "The number of vowel a(A):" << aCnt << endl;
cout << "The number of vowel e(E):" << eCnt << endl;
cout << "The number of vowel i(I):" << iCnt << endl;
cout << "The number of vowel o(O):" << oCnt << endl;
cout << "The number of vowel u(U):" << uCnt << endl;
system("pause");
return 0;
}

练习5.11

noskipws:no skip whitespace

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,newlineCnt=0,tabCnt=0;
while (cin >> noskipws >> t) {
switch (t) {
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'\n':
++newlineCnt;
break;
case'\t':
case'\v':
++tabCnt;
break;
} }
cout << "The number of vowel a(A):" << aCnt << endl;
cout << "The number of vowel e(E):" << eCnt << endl;
cout << "The number of vowel i(I):" << iCnt << endl;
cout << "The number of vowel o(O):" << oCnt << endl;
cout << "The number of vowel u(U):" << uCnt << endl;
cout << "The number of newline:" << newlineCnt << endl;
cout << "The number of tab:" << tabCnt << endl;
system("pause");
return 0;
}

练习5.12

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
char t,p;
int ffCnt=0,fiCnt=0,flCnt=0;
while (cin >> t) {
if (t == 'f') {
cin >> p;
switch (p) {
case'f':
++ffCnt;
break;
case'l':
++flCnt;
break;
case'i':
++fiCnt;
break;
}
}
}
cout << "The number of ff:" << ffCnt << endl;
cout << "The number of fl:" << flCnt << endl;
cout << "The number of fi:" << fiCnt << endl;
system("pause");
return 0;
}

练习5.13

a.没有break;

b.case1中包含变量的定义,如果越过case1会出错

c.应该改为case1: case3: case5格式

d.case标签必须是常量表达式,改为const unsigned ival.....

练习5.14

#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main() {
string nowString, lastString, resString;
int nowNum = 1, resNum = -1;
while (cin >> nowString) {
if (nowString == lastString) {
nowNum++;
if (nowNum > resNum) {
resString = nowString;
resNum = nowNum;
}
}
else {
nowNum = 1;
lastString = nowString;
}
}
if(resNum > 1) cout << resString << " " << resNum;
else cout << "None";
system("pause");
return 0;
}

练习5.17

#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool compare(vector<int> a, vector<int> b) {
for (decltype(a.size()) i = 0, Sa = a.size(), Sb = b.size();i != Sa && i!=Sb;++i) {
if (a[i] != b[i]) {
cout << "false" << endl;
return false;
}
}
cout << "true" << endl;
return true; }
int main() {
vector<int> a = { 0,1,1,2 };
vector<int> b = { 0,1,1,2,3,5,8 };
vector<int> c = { 0,1,2,5,5 };
compare(a, b);
compare(a, c); system("pause");
return 0;
}

练习5.18

a.少了花括号

b.变量申明放在了do的条件部分

c.变量申明必须定义在循环体外

练习5.19

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string a, b;
do {
cout << "Please input two strings:"<<endl;
cin >> a >> b;
cout << (a.size() < b.size() ? a : b) << endl;
} while (cin);
system("pause");
return 0;
}

练习5.20

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string now, pre;
bool flag = false;
while (cin >> now) {
if (now == pre) {
cout << now <<endl;
flag = true;
break;
}
else {
pre = now;
}
}
if (!flag) cout << "None" << endl;
system("pause");
return 0;
}

练习5.21

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string now, pre;
bool flag = false;
while (cin >> now) {
if (now == pre) {
if (!isupper(now[0])) continue;
cout << now <<endl;
flag = true;
break;
}
else {
pre = now;
}
}
if (!flag) cout << "None" << endl;
system("pause");
return 0;
}

练习5.22

do{
int sz=get_size();
}while(sz<=0);

练习5.25

#include<iostream>
#include<string>
#include<vector>
#include<stdexcept>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) {
try {
if (b == 0) {
throw runtime_error("除数不能为0\n");
}
else {
cout << a / b << endl;
}
}
catch (runtime_error err) {
cout << err.what() << "Try Again? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n') {
break;
}
}
}
system("pause");
return 0;
}
05-11 10:53