C++ primer习题记录——第五章

5.3:

#include <iostream>
using namespace std;

int main()
{
	int sum = 0, val = 1;
	while (val <= 10)sum += val, val++;
	cout << sum << endl;
	system("pause");
	return 0;
}

5.5:

#include <iostream>
using namespace std;
#include<vector>
int main()
{
	vector<string> scores = { "F","D","C","B","A","A++" };
	cout << "请输入你的成绩: " << endl;
	int m_score = 0;
	cin >> m_score;
	string level;
	if (m_score < 60)
	{
		level = scores[0];
	}
	else
	{
		level = scores[(m_score - 50) / 10];
		if (m_score != 100)
		{
			if (m_score % 10 <= 3)
			{
				level += "-";
			}
			else if (m_score % 10 >= 7)
			{
				level += "+";
			}
		}	
	}
	cout << level << endl;
	system("pause");
	return 0;
}

5.6:

第一次修改:

#include <iostream>
using namespace std;
#include<vector>
int main()
{
	vector<string> scores = { "F","D","C","B","A","A++" };
	cout << "请输入你的成绩: " << endl;
	int m_score = 0;
	cin >> m_score;
	string level;
	if (m_score < 60)
	{
		level = scores[0];
	}
	else
	{
		level = scores[(m_score - 50) / 10];
		if (m_score != 100)
		{
			level += m_score % 10 <= 3 ? "-" : m_score % 10 >= 7 ? "+" : "";
		}	
	}
	cout << level << endl;
	system("pause");
	return 0;
}

第二次修改:

#include <iostream>
using namespace std;
#include<vector>
int main()
{
	vector<string> scores = { "F","D","C","B","A","A++" };
	cout << "请输入你的成绩: " << endl;
	int m_score = 0;
	cin >> m_score;
	string level;
	level = (m_score < 60 ? scores[0] : scores[(m_score - 50) / 10]);
	level += (m_score == 100 || m_score < 60) ? "" : m_score % 10 <= 3 ? "-" : m_score % 10 >= 7 ? "+" : "";
	cout << level << endl;
	system("pause");
	return 0;
}

5.9:

#include <iostream>
using namespace std;

int main()
{
	char c1;
	cout << " 请输入一段英文字母: " << endl;
	int num = 0;
	while (cin >> c1)
	{
		if (c1 == 'a' || c1 == 'e' || c1 == 'i' || c1 == 'o' || c1 == 'u')
		{
			++num;
		}
	}
	cout << "元音字母个数: " << num << endl;
	system("pause");
	return 0;
}

5.10:

#include <iostream>
using namespace std;

int main()
{
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
    char ch;
    while (cin >> ch)
    {
        switch (ch)
        {
        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: \t" << aCnt << '\n'
        << "Number of vowel e: \t" << eCnt << '\n'
        << "Number of vowel i: \t" << iCnt << '\n'
        << "Number of vowel o: \t" << oCnt << '\n'
        << "Number of vowel u: \t" << uCnt << endl;
	system("pause");
	return 0;
}

5.11:

#include <iostream>
using namespace std;

int main()
{
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, 
        spaceCnt = 0, tabCnt = 0, newlineCnt = 0;
    char ch;
    while (cin >> std::noskipws >> ch)
    {
        switch (ch)
        {
        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 ' ':
            ++spaceCnt;
            break;
        case '\t':
            ++tabCnt;
            break;
        case '\n':
            ++newlineCnt;
            break;
        default:
            break;
        }
    }
    cout << "Number of vowel a: \t" << aCnt << '\n'
        << "Number of vowel e: \t" << eCnt << '\n'
        << "Number of vowel i: \t" << iCnt << '\n'
        << "Number of vowel o: \t" << oCnt << '\n'
        << "Number of vowel u: \t" << uCnt << '\n'
        << "Number of vowel space: \t" << spaceCnt << '\n'
        << "Number of vowel table: \t" << tabCnt << '\n'
        << "Number of vowel enter: \t" << newlineCnt << endl;
	system("pause");
	return 0;
}

5.12:

#include <iostream>
using namespace std;

int main()
{
    int ffCnt = 0, flCnt = 0, fiCnt = 0;
    char ch, prech = '\0';
    while (cin >> std::noskipws >> ch)
    {
        switch (ch)
        {
        case 'f':
            if (prech == 'f')
            {
                ++ffCnt;
            }
            break;
        case 'i':
            if (prech == 'f')
            {
                ++fiCnt;
            }
            break;
        case 'l':
            if (prech == 'f')
            {
                ++fiCnt;
            }
            break;
        default:
            break;
        }
        prech = ch;
    }
    cout << "Number of vowel ff: \t" << ffCnt << '\n'
        << "Number of vowel fl: \t" << flCnt << '\n'
        << "Number of vowel fi: \t" << fiCnt << endl;

    system("pause");
	return 0;
}

5.14:

第一稿,再看看题目我应该漏掉了一些细节QAQ

#include <iostream>
using namespace std;
int main()
{
	cout << "请输入一组单词,并以Ctrl+z结束:" << endl;
	string currVal, val;
	if (cin >> currVal)
	{
		int cnt = 1;
		while (cin >> val)
		{
			if (val == currVal)
			{
				cnt++;
			}
			else
			{
				cout << currVal << "重复了" << cnt << "次" << endl;
				currVal = val;
				cnt = 1;
			}
		}
		cout << currVal << "重复了" << cnt << "次" << endl;
	}
	return 0;
}

5.17:

#include <iostream>
using namespace std;
#include<vector>
int main()
{
	vector<int>v1 = { 0,1,1,2 ,3,5,8,9};
	vector<int>v2 = { 0,1,1,2,3,5,8 };
	if (v1.size() < v2.size())
	{
		auto it1 = v1.begin(), it2 = v2.begin();
		for (; it1 != v1.end() && (*it1) == (*it2); ++it1, ++it2);
		if (it1 == v1.end())
		{
			cout << "是前缀" << endl;
		}
		else
		{
			cout << "不是" << endl;
		}
	}
	else
	{
		auto it1 = v2.begin(), it2 = v1.begin();
		for (; it1 != v2.end() && (*it1) == (*it2); ++it1, ++it2);
		if (it1 == v2.end())
		{
			cout << "是前缀" << endl;
		}
		else
		{
			cout << "不是" << endl;
		}
	}
	return 0;
}

5.19:

#include <iostream>
using namespace std;

int main()
{
	string str1, str2, str3, rsp;
	do
	{
		cout << "请输入两个string对象:" << endl;
		cin >> str1 >> str2;
		str3 = (str1.size() > str2.size() ? str2 : str1);
		cout << "较短的那个对象是: " << str3 << endl;
		cout << "More? Enter yes or no: " << endl;
		cin >> rsp;
	} while (!rsp.empty() && rsp[0] != 'n');
	return 0;
}

5.20:

#include <iostream>
using namespace std;

int main()
{
	
	string str1, str2;
	cin >> str1;
	while (cin >> str2)
	{
		if (str2 == str1)
		{
			break;
		}
		else
		{
			str1 = str2;
		}
	}
	if (cin.eof())//如果输入文件结束符
	{
		cout << "没有重复的" << endl;
	}
	else
	{
		cout << "重复的词语是: " << str1 << endl;
	}
	return 0;
}

5.21:

#include <iostream>
using namespace std;

int main()
{
	
	string str1, str2;
	cin >> str1;
	while (cin >> str2)
	{
		if (str2 == str1)
		{
			if (!isupper(str2[0]))
			{
				continue;
			}
			break;
		}
		else
		{
			str1 = str2;
		}
	}
	if (cin.eof())//如果输入文件结束符
	{
		cout << "没有重复的" << endl;
	}
	else
	{
		cout << "重复的词语是: " << str1 << endl;
	}
	return 0;
}

5.22:

for (int sz = get_size(); sz <=0; sz = get_size())

5.25:

#include <iostream>
using namespace std;

int main()
{
	double a = 0, b = 0;
	while (cin >> a >> b)
	{
		try{
			if (b == 0)
			{
				throw runtime_error("除数不能为0!");
			}
			cout << "a / b = " << a / b << endl;
		}
		catch (runtime_error err)
		{
			cout << err.what() << "\nTry again? Enter y or n" << endl;
			char c;
			cin >> c;
			if (!cin || c == 'n')
			{
				break;
			}
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值