C++ Primer(第五版) 第五章练习答案
目录
- C++ Primer(第五版) 第五章练习答案
-
-
- 5.1
- 5.2
- 5.3
- 5.4
- 5.5
- 5.6
- 5.7
- 5.8
- 5.9
- 5.10
- 5.11
- 5.12
- 5.13
- 5.14
- 5.15
- 5.16
- 5.17
- 5.18
- 5.19
- 5.20
- 5.21
- 5.22
- 5.23
- 5.24
- 5.25
5.1
5.2
5.3
#include
#include
#include
using namespace std;
int main()
{
int sum = 0, val = 1;
while (val <= 10)
sum += val, ++val;
cout << "Sum of 1 to 10 inclusive is "
<< sum << endl;
return 0;
}
5.4
5.5
#include
#include
#include
using namespace std;
int main()
{
vector<string> vec{
"E", "D", "C", "B", "A", "A++"};
int ch;
while (cin >> ch && ch >= 0 && ch <= 100)
if (ch == 100)
cout << vec[5] << endl;
else if (ch >= 90)
cout << vec[4] << endl;
else if (ch >= 80)
cout << vec[3] << endl;
else if (ch >= 70)
cout << vec[2] << endl;
else if (ch >= 60)
cout << vec[1] << endl;
else
cout << vec[0] << endl;
return 0;
}
5.6
#include
#include
#include
using namespace std;
int main()
{
vector<string> vec{
"E", "D", "C", "B", "A", "A++"};
int ch;
while (cin >> ch && ch >= 0 && ch <= 100)
cout << (ch == 100 ? vec[5] :
ch >= 90 ? vec[4] :
ch >= 80 ? vec[3] :
ch >= 70 ? vec[2] :
ch >= 60 ? vec[1] : vec[0]) << endl;
return 0;
}
5.7
5.8
5.9
#include
#include
#include
using namespace std;
int main()
{
string str;
size_t sum = 0;
while (getline(cin, str))
for (auto &ch : str)
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++sum;
cout << sum << endl;
return 0;
}
5.10
#include
#include
#include
#include
using namespace std;
int main()
{
string str;
size_t sum = 0;
while (getline(cin, str))
for (auto ch : str)
{
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++sum;
}
cout << sum << endl;
return 0;
}
5.11
#include
#include
#include
#include
using namespace std;
int main()
{
string str;
size_t sum = 0;
size_t space = 0;
while (getline(cin, str))
for (auto ch : str)
{
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++sum;
if (isspace(ch))
++space;
}
cout << sum << " " << space << endl;
return 0;
}
5.12
#include
#include
#include
#include
using namespace std;
int main()
{
string str;
size_t sum = 0;
size_t space = 0;
size_t count = 0;
char upper = '\0';
while (getline(cin, str))
for (auto ch : str)
{
if (upper != '\0')
{
string s = string() + upper + ch;
if (s == "ff" || s == "fl" || s == "fi")
++count;
}
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++sum;
if (isspace(ch))
++space;
upper = ch;
}
cout << sum << " " << space << " " << count << endl;
return 0;
}
5.13
5.14
#include
#include
#include
using namespace std;
int main()
{
pair<string, int> pa(string(), 0);
string word;
string upper;
int sum = 1;
while (cin >> word)
{
if (word == upper)
{
++sum;
}
else if (sum == 1)
{
upper = word;
}
else
{
if (pa.second < sum)
pa = make_pair(upper, sum);
sum = 1;
upper = word;
}
}
if (sum != 1 && pa.second < sum)
pa = make_pair(word, sum);
if (pa.second == 0)
cout << "null" << endl;
else
cout << pa.first << " " << pa.second;
return 0;
}
5.15
5.16
5.17
#include
#include
using namespace std;
bool test(vector<int> &vec1, vector<int> &vec2, size_t size);
int main()
{
vector<int> vec1{
0, 1, 1, 2};
vector<int> vec2{
0, 1, 1, 2, 3, 5, 8};
if (vec1.size() <= vec2.size())
cout << boolalpha << test(vec1, vec2, vec1.size()) << noboolalpha << endl;
else
cout << boolalpha << test(vec1, vec2, vec2.size()) << noboolalpha << endl;
return 0;
}
bool test(vector<int> &vec1, vector<int> &vec2, size_t size)
{
for (size_t i = 0; i != size; ++i)
if (vec1[i] != vec2[i])
return false;
return true;
}
5.18
5.19
#include
#include
using namespace std;
int main()
{
string str1, str2;
cout << "请输入两个string: " << endl;
do
{
if (str1.empty())
continue;
else if (str1.size() <= str2.size())
cout << "最短的: " + str1 << endl;
else
cout << "最短的: " + str2 << endl;
cout << "请输入两个string: " << endl;
} while (cin >> str1 >> str2);
return 0;
}
5.20
#include
#include
using namespace std;
int main()
{
string word;
string upper;
bool bl = false;
while (cin >> word)
{
if (word == upper)
{
bl = true;
break;
}
upper = word;
}
if (bl)
cout << word << endl;
else
cout << "null" << endl;
return 0;
}
5.21
#include
#include
#include
using namespace std;
int main()
{
string word;
string upper;
bool bl = false;
while (cin >> word)
{
if (!upper.empty() && isupper(upper[0]) && word == upper)
{
bl = true;
break;
}
upper = word;
}
if (bl)
cout << word << endl;
else
cout << "null" << endl;
return 0;
}
5.22
#include
#include
#include
using namespace std;
int get_size() {
int i; cin >> i; return i; }
int main()
{
begin:
int sz = get_size();
if (sz <= 0)
goto begin;
for (int sz = get_size(); sz <= 0; sz = get_size())
;
return 0;
}
5.23
#include
#include
#include
#include
using namespace std;
int main()
{
int i, j;
while (cin >> i >> j)
cout << i / j << endl;
return 0;
}
5.24
#include
#include
#include
#include
using namespace std;
int main()
{
int i, j;
while (cin >> i >> j)
{
if (j == 0)
throw runtime_error("除数为0");
cout << i / j << endl;
}
return 0;
}
5.25
#include
#include
#include
#include
using namespace std;
int main()
{
int i, j;
while (cin >> i >> j)
{
try
{
if (j == 0)
throw runtime_error("除数为0");
cout << i / j << endl;
}
catch(runtime_error e)
{
char ch;
cerr << e.what() << endl
<< "是否从新输入 y / n" << endl;
cin >> ch;
if (ch == 'n')
break;
}
}
return 0;
}