C++ Primer Plus 第六版课后习题答案(第六章)

 C++ Primer Plus 第六版(第六章)的课后习题答案,在main函数中把题号换掉就可以跑不同的程序,第九题的用例在附件中:

#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
using namespace std;

void p1()
{
    char ch;
    while( ch = cin.get())
    {
        if (ch == '@')
            break;
        else if (isdigit(ch))
            continue;
        else if(islower(ch))
            ch = toupper(ch);
        else if(isupper(ch))
            ch = tolower(ch);
        cout << ch;
    }
}

bool is_double(string x)
{
    double y;
	try{
		y = stod(x);
	}
	catch (const std::exception&){
		return false;
	}
	return true;
}

void p2()
{
    double donation,sum,average;
    double Donation[10];
    int size,above = 0;
    string buf;
    cout << "Please input the donation:"<< endl;
    getline(cin, buf);
    while(is_double(buf) && size<10)
    {
        donation = stod(buf);
        Donation[size++] = donation;
        sum += donation;
        getline(cin,buf);
    }
    average = sum/size;
    cout << "The average is " << average <<endl;
    for(int i=0; i<=size; ++i)
    {
        if(Donation[i]>average)
            above++;
    }
    cout << "There are " << above << " numbers larger than average."<<endl;
}

void p3()
{
    char ch;
    cout << "Please enter one of the following choices:" << endl;
    cout << "c) carnivore                    p) pianist" << endl;
    cout << "t) tree                         g) game" << endl;
    while(true)
    {
        cout << "Please enter a c, p, t, or g: ";
        cin >> ch;
        if(ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
        {
            continue;
        }
        cout << "A maple is a";
        swi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值