《C++ Primer》第5版 课后习题第五章(代码部分)

5.3 不使用块之后,代码的可读性降低。

#include <iostream>
using namespace std;
int main()
{
	int sum = 0, val = 1;
	//需要块
	while (val <= 10)
	{
		sum += val;
		++val;
	}
	//不需要块,使用逗号运算符
	while (val <= 10)
		sum += val, ++val;
	cout << "Sum of 1 to 10 inclusive is " << sum << endl;
	system("pause");
	return 0;
}

5.5

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	const vector<string> scores = { "F","D","C","B","A","A++" };
	string lettergrade;
	int grade;
	cin >> grade;
	if (grade < 60)
		lettergrade = scores[0];
	else if (grade<70)
		lettergrade = scores[1];
	else if (grade<80)
		lettergrade = scores[2];
	else if (grade<90)
		lettergrade = scores[3];
	else if (grade<100)
		lettergrade = scores[4];
	else if (grade=100)
		lettergrade = scores[5];
	cout << lettergrade << endl;
	system("pause");
	return 0;
}

5.6

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	const vector<string> scores = { "F","D","C","B","A","A++" };
	string lettergrade;
	int grade;
	cin >> grade;
	lettergrade = (grade < 60) ? "F" : (grade < 70) ? "D" : (grade < 80) ? "C" : (grade < 90) ? "B" : (grade < 100) ? "A" : "A++";
	cout << lettergrade << endl;
	system("pause");
	return 0;
}

5.9

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0;
	string text;
	cin >> text;
	for (auto i : text)
	{
		switch (i)
		{
		case 'a':++acnt;break;
		case 'e':++ecnt; break;
		case 'i':++icnt; break;
		case 'o':++ocnt; break;
		case 'u':++ucnt; break;
		default:break;
		}
	}
	cout << "Number of vowel a: " << acnt << endl;
	cout << "Number of vowel e: " << ecnt << endl;
	cout << "Number of vowel i: " << icnt << endl;
	cout << "Number of vowel o: " << ocnt << endl;
	cout << "Number of vowel u: " << ucnt << endl;
	system("pause");
	return 0;
}

5.10

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0;
	string text;
	cin >> text;
	for (auto i : text)
	{
		switch (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;
		default:break;
		}
	}
	cout << "Number of vowel a and A: " << acnt << endl;
	cout << "Number of vowel e and E: " << ecnt << endl;
	cout << "Number of vowel i and I: " << icnt << endl;
	cout << "Number of vowel o and O: " << ocnt << endl;
	cout << "Number of vowel u and U: " << ucnt << endl;
	system("pause");
	return 0;
}

5.11

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, a = 0, b = 0, c = 0;
	string text;
	getline(cin, text);
	for (auto i : text)
	{
		switch (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 ' ':++a; break;
		case '\t':++b; break;
		case '\n':++c; break;
		default:break;
		}
	}
	cout << "Number of vowel a and A: " << acnt << endl;
	cout << "Number of vowel e and E: " << ecnt << endl;
	cout << "Number of vowel i and I: " << icnt << endl;
	cout << "Number of vowel o and O: " << ocnt << endl;
	cout << "Number of vowel u and U: " << ucnt << endl;
	cout << "Number of Space: " << a << endl;
	cout << "Number of tab: " << b << endl;
	cout << "Number of line break: " << c << endl;
	system("pause");
	return 0;
}

5.12

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	unsigned ff_num=0,fl_num=0,fi_num=0;
	char char_before='\0';
	string text;
	getline(cin, text);
	for (auto i : text)
	{
		switch (i)
		{
		case 'i':if (char_before = 'f')
					++fi_num;
					break;
		case 'f':if (char_before=='f')
					++ff_num; 
					break;
		case 'l':if (char_before == 'f')
					++fl_num;
				break;
		default:break;
		}
		char_before = i;
	}
	cout << "Number of ff: " << ff_num << endl;
	cout << "Number of fl: " << fl_num << endl;
	cout << "Number of fi: " << fi_num << endl;
	system("pause");
	return 0;
}

5.14

5.17

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int> num0, num1, num_big, num_small;
	int a = 0, b = 0, flag = 0;
	//向两个vector对象中输入整数
	while (cin >> a)
	{
		num0.push_back(a);
		if (getchar() == '\n')
			break;
	}	
	while (cin >> b)
	{
		num1.push_back(b); 
		if (getchar() == '\n')
			break;
	}
	//挑出长度短的vector
	if (num0.size() < num1.size())
	{
		num_big.assign(num1.begin(), num1.end());
		num_small.assign(num0.begin(), num0.end());
	}
	else
	{
		num_big.assign(num0.begin(), num0.end());
		num_small.assign(num1.begin(), num1.end()); 
	}
	//以长度短的vector对象为脊椎,进行元素的逐个比较
	for (decltype(num_small.size()) i = 0; i != num_small.size(); ++i)
	{
		if (num_small[i] != num_big[i])
			break;
		else
			flag++;
	}
	//最终判断并输出结果
	if(flag==num_small.size())
		cout << "两个vector对象具有相同的前缀" << endl;
	else
		cout << "两个vector对象具有不相同的前缀" << endl;
	system("pause");
	return 0;
}

5.19

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string rsp;
	do
	{
		cout << "Please enter two string:" << endl;
		string str1, str2;
		cin >> str1 >> str2;
		if (str1.size() <= str2.size())
			cout << "The shorter string is: " << str1 << endl;
		else
			cout << "The shorter string is: " << str2 << endl;
		/*上述判断条件可以利用条件运算符
		cout << "The shorter string is:";
		(str1.size() <= str2.size()) ? (cout << str1 << endl) : (cout << str2 << endl);*/
		cout << "More?Enter yes or no:" << endl;
		cin >> rsp;
	} while (rsp == "yes");
	system("pause");
	return 0;
}

5.20

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	vector<string> text;
	int flag = 1;
	string str, str_before;
	while (cin >> str)
	{
		text.push_back(str);
		if (str == str_before)
		{
			cout << "连续重复出现的string对象:" << str << endl;
			flag = 0;
			break;
		}
		else
			str_before = str;
	}
	if(flag)
		cout << "没有任何单词是重复的" << endl;
	system("pause");
	return 0;
}

5.21

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	vector<string> text;
	int flag = 1;
	string str, str_before;
	while (cin >> str)
	{
		text.push_back(str);
		if (str == str_before)
		{
			if (isupper(str[0]))
			{
				cout << "连续重复出现的string对象:" << str << endl;
				flag = 0;
				break;
			}
			else
				continue;			
		}
		else
			str_before = str;
	}
	if (flag)
		cout << "没有任何单词是重复的" << endl;
	system("pause");
	return 0;
}

5.22

// 向后跳过一个带初始化的变量定义是合法的
begin:
    int sz = get_size();
    if (sz <= 0) {
        goto begin;
    }
//改成while循环
do
{
 int sz = get_size();
}while(sz <= 0);

5.23

#include <iostream>
using namespace std;
int main()
{
	int num1, num2;
	cin >> num1 >> num2;
	cout << num1 / num2 << endl;
	system("pause");
	return 0;
}

5.25

#include <iostream>
using namespace std;
int main()
{
	int num1, num2;
	while (cin >> num1 >> num2)
	{
		try
		{
			if (num2 == 0)
				throw runtime_error("Integer division by zero");
			cout << num1 / num2 << endl;
		}
		catch (runtime_error err)
		{
			cout << err.what() << "\nTry again? Enter y or n" << endl;
			char c;
			cin >> c;
			if (!cin || c == 'n')
				break;
		}
	}
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值