C++Primer部分练习参考答案 第四、五章
编译环境:Microsoft Visual Studio2019
备注:本文留作作者自用,如有错误敬请指出
4.21
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> a = {1,2,3,4,5,6,7,8,9,10};
for (auto& i : a)
i = (i % 2 == 0) ? i : 2*i;
for (auto i : a)
cout << i<<" ";
return 0;
}
4.22
#include<iostream>//版本一
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
int grade;
string finalgrade;
cout << "please enter your grade:";
cin >> grade;
finalgrade = (grade > 90) ? "high pass"
: (grade < 60) ? "fail"
: (grade > 75) ? "pass": "low pass";
cout << finalgrade;
return 0;
}
#include<iostream>//版本二
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
int grade;
string finalgrade;
cout << "please enter your grade:";
cin >> grade;
if (grade > 90)
finalgrade = "high pass";
else if (grade < 60)
finalgrade = "fail";
else if (grade > 75)
finalgrade = "pass";
else finalgrade="low pass";
cout << finalgrade;
return 0;
}
4.28
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(long long) << endl;
cout << sizeof(char) << endl;
cout << sizeof(unsigned long) << endl;
cout << sizeof(float) << endl;
return 0;
}
4.31
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> ivec(10);
vector<int>::size_type cnt = ivec.size();
for (vector<int>::size_type ix = 0; ix != ivec.size(); ix++, cnt--)
ivec[ix] = cnt;
for (auto i : ivec)
cout << i << " ";
return 0;
}
5.5
#include<iostream>
#include<vector>
using std::vector;
using std::string;
using std::cin;
using std::cout;
int main()
{
const vector<string> grade = { "F","D","C","B","A","A++" };
int score;
string lettergrade;
cin >> score;
if (score >= 60) {
lettergrade = grade[(score - 50) / 10];
}
else lettergrade = grade[0];
cout << lettergrade;
return 0;
}
5.6
#include<iostream>
#include<vector>
using std::vector;
using std::string;
using std::cin;
using std::cout;
int main()
{
const vector<string> grade = { "F","D","C","B","A","A++" };
int score;
string lettergrade;
cin >> score;
lettergrade = (score >= 60 ? grade[(score - 50) / 10]:grade[0]);
cout << lettergrade;
return 0;
}
5.9,5.10
#include<iostream>//小写大写都统计了
using std::cin;
using std::cout;
int main()
{
char c;
int cnt=0;
while (cin >> c) {
c = toupper(c);
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
cnt++;
}
cout << cnt;
return 0;
}
5.11
#include<iostream>
using std::cin;
using std::cout;
int main()
{
char c;
int acnt=0,scnt=0;
while (cin >> c) {
c = toupper(c);
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
acnt++;
else if (c == ' ' || c == '\t' || c == '\n')
scnt++;
}
cout << scnt;
return 0;
}
5.12
#include<iostream>
using std::cin;
using std::cout;
int main()
{
char c;
int acnt=0;
while (cin >> c) {
if (c == 'f') {
cin >> c;
if(c=='f'||c=='l'||c=='i')
acnt++;
}
}
cout << acnt;
return 0;
}
5.14
#include<iostream>
#include<vector>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string s1,s2;
int sum=1, maxsum=1;
vector<string> word;//存放且只连续存放一次所有出现过的单词
vector<int> time;//以上单词连续出现的个数(1次也算)
cin >> s1;
while (cin >> s2) {
if (s2 == s1) sum++;
else {
word.push_back(s1);
time.push_back(sum);
sum = 1;
}
s1=s2;
}
word.push_back(s2);//最后一个单词也放进去
time.push_back(sum);
for (vector<int>::iterator p = time.begin(); p != time.end(); ++p)
if (*p > maxsum)
maxsum = *p;
if (maxsum > 1) {
for (int i = 0; i < time.size(); ++i)
if (time[i] == maxsum)
cout << word[i] << "连续重复出现最多次,为" << maxsum << "次" << endl;
}
else cout << "任何单词都没有连续出现过" << endl;
return 0;
}
5.17
#include<iostream>
#include<vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
int main()
{
vector<int> v1 = { 0,1,1,2 }, v2 = {0,1,1,2,3,5,8},temp;
int flag = 0,ex=1;
if (v1.size() > v2.size()) {
temp = v1;
v1 = v2;
v2 = temp;
flag =1;
}
for (int i = 0; i < v1.size(); i++) {
if (v1[i] != v2[i]) {
ex = 0;
break;
}
}
if (ex) {
if (flag) cout << "v2是v1的前缀" << endl;
else cout << "v1是v2的前缀" << endl;
}
else cout << "v1和v2都不是对方的前缀" << endl;
return 0;
}
5.19
#include<iostream>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string s1, s2;
do{
cout << "请输入两个string对象:" << endl;
cin >>s1 >>s2;
if (s1.size() < s2.size())
cout << s1;
else if (s1.size() > s2.size())
cout << s2;
else cout << s1 << "和" << s2 << "一样长";
cout <<endl<< "输入yes以继续" << endl;
} while (cin>>s1 &&s1[0]=='y');
return 0;
}
5.20
#include<iostream>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string s1, s2;
int flag = 0;
cin >> s1;
while (cin >> s2 &&!s2.empty()) {
if (s1 == s2) {
flag = 1;
break;
}
else s1 = s2;
}
if (flag) cout << s2;
else cout << "没有任何单词是连续重复出现的";
return 0;
}
5.21
#include<iostream>//这个程序大小写敏感
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string s1, s2;
int flag = 0;
cin >> s1;
while (cin >> s2 &&!s2.empty()) {
if (s1 == s2&&isupper(s1[0])) {
flag = 1;
break;
}
else s1 = s2;
}
if (flag) cout << s2;
else cout << "没有任何单词是连续重复出现的";
return 0;
}
5.23
#include<iostream>//就不循环了
using std::cin;
using std::cout;
using std::endl;
int main()
{
int i1, i2;
cout << "请输入两个整数:";
cin >> i1 >> i2;
if (i2 == 0) cout << "第二个数不能为0!" << endl;
else cout << i1 / i2<<endl;
return 0;
}
5.24
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int i1, i2;
cout << "请输入两个整数:";
cin >> i1 >> i2;
cout << i1 / i2<<endl;
return 0;
}
5.25
#include<iostream>
#include<stdexcept>
using std::runtime_error;
using std::cin;
using std::cout;
using std::endl;
int main()
{
int i1, i2;
cout << "请输入两个整数:";
while (cin >> i1 >> i2) {
try {
if (i2 == 0) throw runtime_error("第二个数不能为0!");
cout << i1 / i2 << endl;
}
catch (runtime_error err) {
cout << "再试一次?按y以继续" << endl;
char c;
cin >> c;
if (c != 'y' || !c)
break;
}
}
return 0;
}