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

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':
			cout << "A map is a tree" << endl;
			break;
		case 'g':
			cout << "game" << endl;
			break;
		default:
			cout << "Please enter a c, p, t or g:";
			break;
		}
	}
	return 0;
}

4.BOP

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

using namespace std;

const int strsize = 64;



struct bop{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};

int main()
{
	char choice;
	bop bopArray[5]= {
		{"000","AAA","BBB",0},
		{"CCC","DDD","EEE",0},
		{"FFF","GGG","HHH",1},
		{"III","JJJ","KKK",1},
		{"LLL","MMM","NNN",2},
	};
	cout<<"Benevolent Order of Programmers Report\n"
		"a.display by name     b.display by title\n"
		"c.display by bopname  d.display by preference\n"
		"q.quit\n";
	cout<<"Enter your choice:";
	while(cin>>choice)
	{
		if(choice=='q')
		{
			break;
		}
		switch(choice)
		{
			case 'a':
				for(int i=0;i<5;i++)
					cout<<bopArray[i].fullname<<endl;
				break;
			case 'b':
				for(int i=0;i<5;i++)
					cout<<bopArray[i].title<<endl;
				break;
			case 'c':
				for(int i=0;i<5;i++)
					cout<<bopArray[i].bopname<<endl;
				break;
			case 'd':
				for(int i=0;i<5;i++)
                    switch(bopArray[i].preference)
                    {
                        case 0:   cout<<bopArray[i].fullname<<endl;   break;
                        case 1:   cout<<bopArray[i].title<<endl;      break;
                        case 2:   cout<<bopArray[i].bopname<<endl;    break;
                    }
				break;
			default:
				break;
		}
		cout<<"Next choice:";
	}
	cout<<"Bye!"<<endl;
	return 0;
}

5.征税


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

using namespace std;

int main()
{
    cout<<"Enter your income: ";
    int income;
    double tax = 0;
    while( cin>>income )
    {
        if(income >= 0 && income <= 5000)   tax = 0.00;
        else if(income >= 5001 && income <= 15000)   
			tax = (income - 5000) * 0.10;
        else if(income >= 15001 && income <= 35000)  
			tax = (income - 15000) * 0.15 + 10000 * 0.10;
        else if(income >= 35001)    
			tax = 10000 * 0.10 + 20000 * 0.15 + (income - 35000) * 0.20;
        else  
            break;

        cout<<"Your tax is "<<tax<<endl;
        cout<<"Enter your income: ";
    }
	return 0;
}

6.捐助


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

using namespace std;

struct patron{
	string name;
	double money;
};

int main()
{
	int patrons_num;
	int grand_num = 0;
    int ordinary_num = 0;

    cout<<"Enter the number of patrons: ";
    cin>>patrons_num;
    patron *patrons = new patron [patrons_num];
    for(int i=0;i<patrons_num;i++)
    {
        cout<<"Please enter the name of the patron"<<"["<<i+1<<"]  : ";
		getline(cin,patrons[i].name);

        cout<<"Please enter the money of the patron"<<"["<<i+1<<"] : ";
        cin>>patrons[i].money;
    }
   
    cout<<"Grand Patrons: "<<endl;
    for(int i=0;i<patrons_num;i++)
    {
        if(patrons[i].money > 10000)
        {
            grand_num++;
            cout<<"name: \t"<<patrons[i].name<<"\tmoney: "<<patrons[i].money<<endl;
        }
    }
    if(grand_num == 0)       
		cout<<"none"<<endl;

    cout<<"Patrons: "<<endl;
    for(int i=0;i<patrons_num;i++)
    {
        if(patrons[i].money <= 10000)
        {
            ordinary_num++;
            cout<<"name: \t"<<patrons[i].name<<"\tmoney: "<<patrons[i].money<<endl;
        }
    }
    if(ordinary_num == 0)    cout<<"none"<<endl;
    return 0;
}

7.读取单词

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

using namespace std;

int main()
{
    char word[20];
    int vowels = 0;
    int consonants = 0;
    int others = 0;
	cout<<"Enter words( q to quit ): "<<endl;
    while( cin>>word )
    {
        if( word[0] == 'q' && strlen(word) == 1)
            break;
        if( isalpha(word[0]) )
        {
            switch(word[0])
            {
                case 'a':   case 'A':   case 'e':   case 'E':   case 'i':
                case 'I':   case 'o':   case 'O':   case 'u':   case 'U':
                    vowels++;    break;
                default:
                    consonants++;
            }
        }
        else
            others++;
    }
    cout<<vowels<<" words beginning with vowels."<<endl
        <<consonants<<" words beginning with consonants."<<endl
        <<others<<" others."<<endl;
    return 0;
}

8.读文件字符


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

using namespace std;

int main()
{
    string FileName;
    ifstream inFile;
    unsigned int num = 0;
    char ch = 0;

    cout << "Enter the file name:";
    getline(cin, FileName);

    inFile.open(FileName.c_str());

    while ((ch = inFile.get()) != EOF)
    {
        num++;
    }

    cout << "There are " << num << " characters in " << FileName << " file." << endl;
    return 0;
}

9.读文件信息

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

using namespace std;

struct patron
{
    string name;
    double money;
};

int main()
{
    fstream infile;
    infile.open("test.txt", ios::in);
    if( !infile.is_open() )
    {
        cout<<"Open the file error!!!"<<endl;
        exit(EXIT_FAILURE);
    }
    int patrons_number;
    (infile>>patrons_number).get();
    patron *patrons = new patron [patrons_number];
    int i=0;
    while( infile.good() )
    {
        getline(infile, patrons[i].name);
        (infile>>patrons[i].money).get();
        i++;
    }
    unsigned int grand_number = 0;
    unsigned int ordinary_number = 0;
    cout<<"Grand Patrons: "<<endl;
    for(int i=0;i<patrons_number;i++)
    {
        if(patrons[i].money > 10000)
        {
            grand_number++;
            cout<<"name: \t"<<patrons[i].name<<"\tmoney: "<<patrons[i].money<<endl;
        }
    }
    if(grand_number == 0)       cout<<"none"<<endl;
    cout<<"Patrons: "<<endl;
    for(int i=0;i<patrons_number;i++)
    {
        if(patrons[i].money <= 10000)
        {
            ordinary_number++;
            cout<<"name: \t"<<patrons[i].name<<"\tmoney: "<<patrons[i].money<<endl;
        }
    }
    if(ordinary_number == 0)    cout<<"none"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zcysilent/article/details/81778997
今日推荐