C++ Primer Plus (6th)第六章 编程练习题答案

本文提供了C++ Primer Plus第六版中第六章的编程练习解答,包括键盘输入处理、存储数值、菜单驱动程序实现、运算符重载、税务计算、捐助计算、单词读取、文件字符读取及文件信息读取等实战练习。
摘要由CSDN通过智能技术生成

1.读取键盘输入

#include "stdafx.h"
#include<iostream>
#include<string>

using namespace std;

int main()
{
    char ch=0;
    cout<<"Please enter:";
    while((ch=cin.get())!='@')
    {
        if(isdigit(ch))
        {
            continue;
        } 
        else if (islower(ch))
        {
            cout<<(char)toupper(ch);
        }
        else if (isupper(ch))
        {
            cout << (char)tolower(ch)<<endl;;
        }
    }

    cout << "Ending" << endl;
    return 0;
}

2.存十个数值


#include "stdafx.h"
#include<iostream>
#include<string>
#include<array>

using namespace std;

const unsigned int SIZE=10;

int main()
{
	array<double,SIZE> donation;
	int enter=0;
	double total_value=0.0;
	double avg=0.0;
	int over_avg=0;

	cout<<"Please enter ten doulbe value(Non-numeric to exit):";
	while(enter<10&&(cin>>donation[enter]))
	{
		total_value+=donation[enter];
		enter++;
	}
	avg=total_value/enter;

	for(int i=0;i<enter;i++)
	{
		if(donation[i]>avg)
		{
			over_avg++;
		}
	}

	cout<<"The average value is "<<avg<<",and there are "<<over_avg
		<<" double value more than average !"<<endl;

	return 0;
}

3.菜单驱动程序


#include "stdafx.h"
#include<iostream>
#include<string>

using namespace std;

int main()
{
	char ch=0;
	cout<<"Please enter one of the following choices:\n"
		"c) carnivore     p)pianist \n"
		"t) tree          g)game\n";
	
	while(cin>>ch)
	{
		switch(ch)
		{
		case 'c':
			cout<<"carnivore"<<endl;
			break;
		case 'p':
			cout << "pianist" << endl;
			break;
		case 't':
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值