C++ Primer Plus (第六版)第六章編程練習參考答案

練習一:

#include<iostream>
#include<cctype>    //字符庫函數

using namespace std;

//const int SIZE=1000;  //最大容量

int main()
{
    char ch;

    cout<<"Please enter: ";

    while(cin.get(ch) && ch != '@') //判斷輸入是否結束
    {
        if(islower(ch)) //小變大
            ch=toupper(ch);
        else if(isupper(ch))    //大變小
            ch=tolower(ch);
        else if(isdigit(ch))    //忽略數字回到循環
            continue;
        cout<<ch;

    }

    cout<<endl;

    return 0;
}

練習二:

#include<iostream>

using namespace std;

const int SIZE=10;

int main()
{
    double donation[SIZE];
    int i=0;
    double sum=0;   //total value

    cout<<"Please enter donation value: ";

    while(cin>>donation[i] && i<SIZE)   //Determine whether to stop input
    {
        sum+=donation[i];
        ++i;
        /*if(i<SIZE)
            continue;
        else
            break;*/
    }

    double ave=(sum/i); //average value
    int j=0;
    int acount=0;

    while(j<i)  //caculate greater than the average numbers
    {
        if(donation[j]>ave)
            ++acount;
        ++j;
        continue;
    }

    cout<<"Average value is: "<<ave<<endl;
    cout<<"Is greater than the average number of: "<<acount<<endl;


    return 0;
}

練習三:

#include<iostream>
using namespace std;
void showmenu();

int main()
{
    showmenu(); 

    char ch;

    while(cin>>ch)
    {
        switch(ch)
        {
        case 'c' : cout<<"Lion is a canivore."<<endl;   //lion:獅子
            break;
        case 'p' : cout<<"Chopin is a pianist."<<endl;  //pianist:鋼琴家
            break;
        case 't' : cout<<"A maple is a tree."<<endl;
            break;
        case 'g' : cout<<"I like game."<<endl;
            break;
        default  : cout<<"Please enter a c, p, t, or g: ";
            continue;
        }
    }

    return 0;
}

void showmenu()
{
    cout<<"Please enter one of the following choices: \n"
        "c) carnivore            p) painist\n"      //c-食肉動物,p-鋼琴家
        "t) tree                 g)game\n";
}

練習四:

#include<iostream>

using namespace std;

const int strsize=20;

struct bop
{
    char fullname[strsize]; //real name
    char title[strsize];    //job title
    char bopname[strsize];  //secret BOP name
    int perference; //0 = fullname, 1 = title, 2 = bopname
};

bop everyone[4]=
{
    {"hanchengcheng","Porgrammer","haidao",1},
    {"suoxuyang","Accounting","nangyang",0},
    {"wuxubo","Chairman","daboluo",2},
    {"weijianhang","Mistress","laoer",2}
};

void name();
void title();
void bopname();
void perference();

int main()
{
    //輸出提示
    cout<<" Benevolent Order of Programmers Report\n"
        <<" a. display by name          b. display by title\n"
        <<" c. display by bopname       d. display by perference\n"
        <<" q. quit"<<endl;

    char ch;

    cout<<"Enter your choice: ";
    cin>>ch;

    while(ch!='q')  //判斷是否離開
    {
        switch(ch)
        {
            case 'a' : name();
                break;
            case 'b' : title();
                break;
            case 'c' : bopname();
                break;
            case 'd' : perference();
                break;
            default : cout<<"It's not a choice!!!"<<endl;
        }
        cout<<"Next choice: ";
        cin>>ch;
    }

    cout<<"Bye!!!"<<endl;

    return 0;
}

void name()
{
    for(int i=0; i<4; ++i)
    cout<<everyone[i].fullname<<endl; 
}

void title()
{
    for(int i=0; i<4; ++i)
    cout<<everyone[i].title<<endl; 
}

void bopname()
{
    for(int i=0; i<4; ++i)
    cout<<everyone[i].bopname<<endl; 
}

void perference()
{
    for(int n=0; n<4; ++n)  //根據個人愛好顯示
    {
        switch(everyone[n].perference)  
        {
        case 0 : cout<<everyone[n].fullname<<endl;
            break;
        case 1 : cout<<everyone[n].title<<endl;
            break;
        case 2 : cout<<everyone[n].bopname<<endl;
            break;
        }
    }
}

練習五:

#include<iostream>

using namespace std;

int main()
{
    double income;  //收入
    double tax;     //所得稅

    cout<<"Please enter your incom: ";

    while(cin>>income && income>=0) //判斷是否正確輸入且輸入不小於零
    {
        if(income<=5000)
        {
            cout<<"Icome tax payable to zero tvarps."<<endl;
        }
        else if(income>5000 && income<=15000)
        {
            tax=(income-5000)*0.10;
            cout<<"Income tax payable is "<<tax<<" tvarps."<<endl;
        }
        else if(income>15000 && income<=35000)
        {
            tax=10000*0.10+(income-15000)*0.15;
            cout<<"Income tax payable is "<<tax<<" tvarps."<<endl;
        }
        else
        {
            tax=10000*0.10+20000*0.15+(income-35000)*0.20;
            cout<<"Income tax payable is "<<tax<<" tvarps"<<endl;
        }
        cout<<"Please enter your income: ";
        continue;   //回到循環
    }


    return 0;
}

練習六:

#include<iostream>

using namespace std;

struct patron
{
    char name[20];
    double money;
};

int main()
{
    int i;                      //捐款者人數
    int j=0;

    cout<<"Please enter the number of patrons: ";
    cin>>i;
    cin.get();  //讀取換行符

    patron * pa=new patron[i];  //創建動態結構數組

    while(j<i)                      //循環輸入
    {
        cout<<"Please enter name: ";
        cin.getline(pa[j].name,20);
        cout<<"Please enter the amount of money $ : ";
        cin>>(pa[j].money);
        cin.get();      //讀取換行符
        ++j;
        continue;
    }

    //顯示Grand Patrons
    int t=0;    
    cout<<"Grand Patrons:"<<endl;
    for(int n=0; n<i; ++n)
    {
        if(pa[n].money>10000)
        {
            cout<<pa[n].name<<"     "<<endl;
            ++t;
        }
    }
    if(t==0)    //判斷有沒有這種類別捐款者
        cout<<"none\n";

    //顯示Patrons
    int f=0;
    cout<<"Patrons:\n";
    for(int m=0; m<i; ++m)
    {
        if(pa[m].money<=10000)
        {
            cout<<pa[m].name<<"     "<<endl;
            ++f;
        }
    }
    if(f==0)    //判斷有沒有這種類別捐款者
        cout<<"none\n";


    return 0;
}

練習七:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string word;
    int i=0;
    int j=0;
    int others=0;
    //char ch;

    cout<<"Enter words (q to quit): \n";
    cin>>word;

    while( word != "q") //word字符串,所以用雙引號
    {
        if(isalpha(word[0]))
        {

            switch(word[0])
            {
                case 'a' : ++i;
                       break;
                case 'A' : ++i;
                       break;
                case 'e' : ++i;
                       break;
                case 'E' : ++i;
                       break;
                case 'i' : ++i;
                       break;
                case 'I' : ++i;
                       break;
                case 'o' : ++i;
                       break;
                case 'O' : ++i;
                       break;
                case 'u' : ++i;
                       break;
                case 'U' : ++i;
                       break;
                default  : ++j;
            }
        }
        else
            ++others;
        cin>>word;

    }

    cout<<i<<" words beginning with vowels\n";
    cout<<j<<" words beginning with consonants\n";
    cout<<others<<" others"<<endl;

    return 0;
}

練習八:

#include<iostream>
#include<fstream>   //file I/O support
#include<cstdlib>   //sport for exit()

using namespace std;

const int SIZE=60;

int main()
{
    char filename[SIZE];
    ifstream inFile;

    cout<<"Enter name od data file: ";
    cin.getline(filename,SIZE);

    inFile.open(filename);      //associate inFile with a file

    if(!inFile.is_open())       //failed to open file
    {
        cout<<"Could not open the file\n"
            <<"Program terminating.\n";
        exit(EXIT_FAILURE);     //函數exit()終止程序
    }

    char ch;
    int i=0;

    inFile>>ch;
    while(inFile.good())    //while input good and not at EOF
    {
        ++i;
        inFile>>ch;
    }

    cout<<"一共包含"<<i<<"個字符"<<endl;

    inFile.close();

    return 0;
}

練習九:

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>   //sport for exit()

using namespace std;

struct patron
{
    string name;
    int money;
};

int main()
{
    char filename[20];
    ifstream inFile;

    cout<<"Enter the fileanme: ";
    cin.getline(filename,20);
    inFile.open(filename);

    if(!inFile.is_open())   //failed to open file
    {
        cout<<"FAIL"<<endl;
        exit(EXIT_FAILURE);
    }


    int num;    //捐款人數
    inFile>>num;

    patron * pa=new patron[num];

    while(inFile.good())    //while input good and not at EOF
    {
        for(int i=0; i<num; i++)    //讀入
        {
            inFile.get();
            getline(inFile,pa[i].name);
            inFile>>pa[i].money;
        }

    }

    if(inFile.eof())
        cout<<"End of file reached.\n";
    else if(inFile.fail())
        cout<<"Input terminated by data mismatch.\n";
    else
        cout<<"Input terminated for unknown reason.\n";

    inFile.close();     //關閉文件

        //顯示Grand Patrons
    int t=0;    
    cout<<"Grand Patrons:"<<endl;
    for(int n=0; n<num; n++)
    {
        if(pa[n].money>10000)
        {
            cout<<pa[n].name<<endl;
            cout<<pa[n].money<<endl;
            ++t;
        }
    }
    if(t==0)    //判斷有沒有這種類別捐款者
        cout<<"none\n";

    //顯示Patrons
    int f=0;
    cout<<"Patrons:\n";
    for(int m=0; m<num; m++)
    {
        if(pa[m].money<=10000)
        {
            cout<<pa[m].name<<endl;
            cout<<pa[m].money<<endl;
            ++f;
        }
    }
    if(f==0)    //判斷有沒有這種類別捐款者
        cout<<"none\n";

    return 0;
}
發佈了47 篇原創文章 · 獲贊 24 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章