c++ primer 学习笔记-第五章

习题5.5:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>    
using std::cin; using std::cout;
using std::endl;
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	vector<string> grades{ "F", "D", "C", "B", "A", "A++" };
	string grade = "";
	unsigned int score = 0;
	while (cin >> score && score<100)
	{
		if (score < 60)
			grade = grades[0];
		else
		{
			grade = grades[(score - 50) / 10];
			if (score != 100)
			{
				if (score % 10 < 3)
					grade += "-";
				if (score % 10 > 7)
					grade += "+";
			}
		}
		cout << grade << endl;
	}
	getchar();
	getchar();
	return 0;
}

习题5.6:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>    
using std::cin; using std::cout;
using std::endl;
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	vector<string> grades{ "F", "D", "C", "B", "A", "A++" };
	string grade = "";
	unsigned int score = 0;
	while (cin >> score && score<=100)
	{
		grade = (score < 60) ? grades[0] : grades[(score - 50) / 10];
		grade += (score == 100 || score < 60) ? "" : (score % 10 < 3) ? "-" : (score % 10 > 7) ? "+" : "";
		cout << grade << endl;
	}
	getchar();
	getchar();
	return 0;
}


习题5.9:
#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>    
using std::cin; using std::cout;
using std::endl;
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	char c;
	unsigned int cnt = 0;
	while (cin >> c)
	{
		if ((c == 'a') || (c == 'o') || (c == 'e') || (c == 'i') || (c == 'u'))
			++cnt;
	}
	cout << cnt << endl;
	getchar();
	getchar();
	return 0;
}

习题5.10:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>    
using std::cin; using std::cout;
using std::endl;
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	char ch;
	unsigned int aCnt = 0, oCnt = 0, eCnt = 0, iCnt = 0, uCnt = 0;
	while (cin >> ch)
	{
		switch (tolower(ch)){
			case 'a':++aCnt; break;
			case 'o':++oCnt; break;
			case 'e':++eCnt; break;
			case 'i':++iCnt; break;
			case 'u':++uCnt; break;
			default:break;
		}
	}
	cout << "'a'&'A' occurs " << aCnt << "times." << endl;
	cout << "'o'&'O' occurs " << oCnt << "times." << endl;
	cout << "'e'&'E' occurs " << eCnt << "times." << endl;
	cout << "'i'&'I' occurs " << iCnt << "times." << endl;
	cout << "'u'&'u' occurs " << uCnt << "times." << endl;
	getchar();
	getchar();
	return 0;
}

习题5.11:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	char c;
	unsigned int aCnt = 0, oCnt = 0, eCnt = 0, iCnt = 0, uCnt = 0;
	unsigned int spaceCnt = 0, tabCnt = 0, crCnt = 0;
	while (cin>>std::noskipws>>c)
	{
		switch (tolower(c))
		{
			case 'a':++aCnt; break;
			case 'o':++oCnt; break;
			case 'e':++eCnt; break;
			case 'i':++iCnt; break;
			case 'u':++uCnt; break;
			case ' ':++spaceCnt; break;
			case '\t':++tabCnt; break;
			case '\n':++crCnt; break;
			default:break;
		}
	}
	cout << "'a'&'A' occurs " << aCnt << "times." << endl;
	cout << "'o'&'O' occurs " << oCnt << "times." << endl;
	cout << "'e'&'E' occurs " << eCnt << "times." << endl;
	cout << "'i'&'I' occurs " << iCnt << "times." << endl;
	cout << "'u'&'U' occurs " << uCnt << "times." << endl;
	cout << "space occurs " << spaceCnt << "times." << endl;
	cout << "tab occurs " << tabCnt << "times." << endl;
	cout << "cr occurs " << crCnt << "times." << endl;
	//aCnt = 0, oCnt = 0, eCnt = 0, iCnt = 0, uCnt = 0;
	//spaceCnt = 0, tabCnt = 0, crCnt = 0;
	getchar();
	getchar();
	return 0;
}

习题5.12:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	char c; 
	bool flag = false;
	unsigned int aCnt = 0, oCnt = 0, eCnt = 0, iCnt = 0, uCnt = 0;
	unsigned int spaceCnt = 0, tabCnt = 0, crCnt = 0, fCnt = 0;
	while (cin>>std::noskipws>>c)
	{
		if (!flag && c=='f')
		{
			flag = true;
			continue;
		}
		if (flag && (c == 'f' || c == 'l' || c == 'i'))
		{
			++fCnt;
			flag = false;
		}
		else
		{
			flag = false;
			switch (tolower(c))
			{
			case 'a':++aCnt; break;
			case 'o':++oCnt; break;
			case 'e':++eCnt; break;
			case 'i':++iCnt; break;
			case 'u':++uCnt; break;
			case ' ':++spaceCnt; break;
			case '\t':++tabCnt; break;
			case '\n':++crCnt; break;
			default:break;
			}
		}
		
	}
	cout << "'a'&'A' occurs " << aCnt << "times." << endl;
	cout << "'o'&'O' occurs " << oCnt << "times." << endl;
	cout << "'e'&'E' occurs " << eCnt << "times." << endl;
	cout << "'i'&'I' occurs " << iCnt << "times." << endl;
	cout << "'u'&'U' occurs " << uCnt << "times." << endl;
	cout << "space occurs " << spaceCnt << "times." << endl;
	cout << "tab occurs " << tabCnt << "times." << endl;
	cout << "cr occurs " << crCnt << "times." << endl;
	cout << "f* occurs " << fCnt << "times." << endl;
	//aCnt = 0, oCnt = 0, eCnt = 0, iCnt = 0, uCnt = 0;
	//spaceCnt = 0, tabCnt = 0, crCnt = 0;
	getchar();
	getchar();
	return 0;
}

习题5.14:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

void sort(vector<string> svec, vector<int> &iCnt, vector<string> &ordered_svec)
{
	int cnt = 1;
	string pre = *(svec.begin());
	for (auto iter = svec.begin() + 1; iter != svec.end(); ++iter)
	{
		if ((*iter) == pre)
			++cnt;
		else
		{
			ordered_svec.push_back(pre);
			iCnt.push_back(cnt);
			pre = *iter;
			cnt = 1;
		}
	}
	ordered_svec.push_back(pre);
	iCnt.push_back(cnt);
}

void display(vector<int> iCnt, vector<string> ordered_svec)
{
	int index = 0;
	for (int i = 1; i != iCnt.size() ; ++i)
		if (iCnt[i] > iCnt[index])
			index = i;
	cout << ordered_svec[index] << " occurs " << iCnt[index] << " times." << endl;
}
int main()//看github有pair容器的方法,暂时还没看到pair,留着再做
{
	vector<string> svec, ordered_svec;
	vector<int> iCnt;
	string s;
	while (cin >> s)
		svec.push_back(s);
	sort(svec, iCnt, ordered_svec);
	display(iCnt, ordered_svec);
	getchar();
	getchar();
	return 0;
}

习题5.17:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

bool prefix(vector<int> ivec1, vector<int> ivec2)
{
	if (ivec1.size() > ivec2.size())
		return prefix(ivec2, ivec1);
	else
	{
		bool flag = true;
		for (unsigned int i = 0; i != ivec1.size(); ++i)
			if (ivec1[i] != ivec2[i])
				flag = false;
		return flag;
	}
}
int main()
{
	vector<int> ivec1{ 1, 2, 1, 3 }, ivec2{ 1, 2, 1, 3, 5 };
	if (prefix(ivec1, ivec2))
		cout << "true" << endl;
	else
		cout << "false" << endl;
	getchar();
	getchar();
	return 0;
}

习题5.19:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	string rsp;
	do
	{
		string s1, s2;
		cout << "Please input two strings and will return the shorter one for u:" << endl;
		cin >> s1 >> s2;
		cout << ((s1.size() < s2.size()) ? s1 : s2) << endl;
		cout << "More inputs?(Y/N)" << endl;
		cin >> rsp;
	} while (!rsp.empty() && tolower(rsp[0]) != 'n');
	getchar();
	getchar();
	return 0;
}

习题5.20:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	string s, pre("");
	bool flag = true;
	while (cin >> s && !s.empty())
	{
		if (s == pre)
		{
			cout << "Word '" << s << "' shows 2 times." << endl;
			flag = false;
			break;
		}
		else
		{
			pre = s;
		}
	}
	if (flag)
		cout << "No duplicate." << endl;
	getchar();
	getchar();
	return 0;
}

习题5.21:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	string s, pre("");
	bool flag = true;
	while (cin >> s && !s.empty())
	{
		if (!isupper(s[0]))continue;
		if (s == pre)
		{
			cout << "Word '" << s << "' shows 2 times." << endl;
			flag = false;
			break;
		}
		else
		{
			pre = s;
		}
	}
	if (flag)
		cout << "No duplicate." << endl;
	getchar();
	getchar();
	return 0;
}


习题5.25:

#include <iostream>    
#include <string>    
#include <vector>    
#include <iterator>    
#include <cstring>  
#include <exception>
#include <stdexcept>

using std::cin; using std::cout;
using std::endl; 
using std::string; using std::vector;
using std::begin; using std::end;
using std::strcat; using std::strcpy;
using std::exception; using std::runtime_error;

int main()
{
	int dividend = 1, divisor = 1;
	cout << "input dividend and divisor:" << endl;
	while (cin >> dividend >> divisor)
	{
		try{
			if (!divisor)
				throw runtime_error("divisor is 0");
			cout << dividend / divisor << endl;
		}
		catch(runtime_error err){
			cout << err.what() << endl << "Do you want to try again?(Y/N)" << endl;
			char c;
			cin >> c;
			if (!cin || tolower(c) == 'n')
				break;
		}
	}
	getchar();
	getchar();
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值