C++ primer plus 第六版 第六章 编程练习答案

第六章 编程练习答案

1.

#include<iostream>
#include<cctype>

int main()
{
    using namespace std;
    cout << "Enter text, and type @ to terminate input.\n";
    char ch;

    cin.get(ch);
    while (ch != '@')
    {
        if(isdigit(ch));//是否数值 

        else if(isalpha(ch))//是否字母 
        {
            if(isupper(ch))//是否大写 
            {
                ch = tolower(ch);//转换小写 
                cout << ch;             
            }
            else{
                ch = toupper(ch);//转换大写 
                cout << ch;
            }           
        }
        else
            cout << ch;

        cin.get(ch);//注意去掉输入的换行符        
    }

    cout << "Done!" <<endl; 
    return 0;   
}

2.

#include<iostream>
const int Max = 10;

int main()
{
    using namespace std;
//get data
    double donation[Max];
    cout << "Please enter the donation.\n";
    cout << "You may enter up to " << Max
         << " donation.\n";
    cout << "donation #1: ";

    int i = 0;
    while (i < Max && cin >> donation[i])
    {
        if (++i < Max)
            cout << "donation #" << i+1 << ": ";

    }
//calculate average 
    double total = 0.0;
    double average;
    int j, n = 0;
    for (j = 0; j < i; j++)
        total += donation[j];
    average =  total / i;
//calculate number(>average)    
    for (j = 0; j < i; j++){
        if(donation[j] > average)
            n++;        
    }
//report results    
    if (i == 0)
        cout << "No donation.\n";
    else{
        if(i == 1){
            cout << "average = " << average;
            cout << "No donations bigger than average.\n";      
        }       
        else{
            cout << "Average of " << i << " donations = " << average << endl;
            cout << n << " donations bigger than average." << endl;
         }

    }
    cout << "Done.\n";
    return 0;       
}

3.

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

int main()
{
    showmenu();
    char choice;

    do
    {
        cin >> choice;          
        switch(choice)
        {
            case 'c': cout << "If you describe someone  as a carnvore, you are saying,\n";
                       cout << "especially in a humorous way, that they eat meat.\n";
                       break;
            case 'p': cout << "I want to be a pinoest.\n";
                       break;
            case 't': cout << "A maple is a tree.\n";
                       break;
            case 'g': cout << "I like playing computer game.\n";
                       break;   
            default : cout << "Please enter a c, p, t, or g: ";
        }

    }while ((choice !='c')&&(choice !='p')&&(choice !='t')&&(choice !='g')) ;//字母输入 

    return 0;
}

void showmenu()
{
    cout << "Please enter one of the following choices:\n"
            "c) carnivore           p) pianist\n"
            "t) tree                g) game\n";
}

4.

#include<iostream>
using namespace std;
const int strsize = 20;
const int number = 5;
void showmenu();
void diplay_fullname();
void diplay_title();
void diplay_bopname();
void diplay_perference();

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 people[number] = {
        {
            "Wimp Macho", 
            "BOSS", 
            "WM", 
            0
        },
        {
            "Raki Rhodes",
            "Manager",
            "Junior Programmer",
            2
        },
        {
            "Celia Laiter",
            "MIPS",
            "CL",
            1
        },
        {
            "Hoppy Hipman",
            "Analyst Trainee",
            "AT",
            1
        },
        {
            "Pat Hand",
            "Student",
            "LOOPY",
            2
        }
    };


int main()
{   
//get data and report   
    showmenu();
    char choice;
    cout << "Enter your choice: "; 
    do
    {
        cin >> choice;          
        switch(choice)
        {
            case 'a': diplay_fullname();
                      break;
            case 'b': diplay_title();
                      break;
            case 'c': diplay_bopname();
                      break;
            case 'd': diplay_perference();
                      break;
        }
    if(choice != 'q')
        cout << "Next choice: ";

    }while (choice !='q');

    cout << "Bye!" << endl;
    return 0;   

}
//定义函数 
void showmenu()
{
    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";
}

void diplay_fullname()
{
    int i;
    for(i = 0 ; i < number ; i++)
        cout << people[i].fullname << endl; 
}

void diplay_title()
{
    int i;
    for(i = 0 ; i < number ; i++)
        cout << people[i].title << endl;    
}

void diplay_bopname()
{
    int i;
    for(i = 0 ; i < number ; i++)
        cout << people[i].bopname << endl;  
}

void diplay_perference()
{
    int i;
    for(i = 0 ; i < number ; i++)
    {
       if (people[i].perference == 0)
           cout << people[i].fullname << endl;  
       if (people[i].perference == 1)       
           cout << people[i].title << endl;
       if (people[i].perference == 2)          
           cout << people[i].bopname << endl;               
    }

}

5.

#include<iostream>

using namespace std;
const int tax1 = 5000;
const double interest1 = 0.1;
const int tax2 = 15000;
const double interest2 = 0.15;
const int tax3 = 35000;
const double interest3 = 0.2;
double tax(int);

int main()
{
    int n;
    double money;

    do{
        cout << "Enter your income: ";
        cin >> n;   
        money=tax(n);
        if (n > 0)
            cout << "The tax is: " << money << " tvarps" <<endl;

    }while (n > 0);

    return 0;   
}

//----计算税费---------- 
double tax(int n)
{
    double index;
    if (n <= tax1)
        index = n*0;
    else if(n> tax1 && n<=tax2)
        index = (n-tax1)*interest1;
    else if(n> tax2 && n<=tax3)
        index = (n - tax2)*interest2+(tax2 - tax1)*interest1;       
    else 
       index = (n-tax3)*interest3+(tax3 - tax2)*interest2+(tax2 - tax1)*interest1 ;
    return index;   
}

6.

#include<iostream>
using namespace std;
const int namesize = 20;
const double stand = 10000.0;
struct patron{
    char name[namesize];
    double money;
};

int main()
{
//动态数组 
    int count;
    cout << "Enter the number of patrons:" << endl;
    cin >> count;
    cin.get();
    patron * peoplelist = new patron[count];    
//输入数据  
    cout << "Enter the patrons:" << endl;
    for(int i = 0; i < count  ;i++)
    {
        cout << "Patrons name:" << endl;    
        cin.getline(peoplelist[i].name, namesize);  
        cout << "donation:" << endl;        
        cin >> peoplelist[i].money;
        cin.get();
    }       
//输出数据
    cout << "\n******************" << endl;
    cout << "\nGrand Patrons: " << endl;
    int n = 0;
    for(int i = 0; i < count ;i++){
        if(peoplelist[i].money > stand)
        {
            cout << "Patrons name: " << peoplelist[i].name << endl;
            cout << "donation: " << peoplelist[i].money << endl;
            n++;    
        }
    }   
    if(n == 0)
        cout << "none";

    int m = 0;
    cout << "\nPatrons: " << endl;      
    for(int i = 0; i < count ;i++){

        if(peoplelist[i].money <= stand){
            cout << "Patrons name: " << peoplelist[i].name << endl;
            cout << "donation: " << peoplelist[i].money << endl;
            m++;
        }
    }           
    if(m == 0)
        cout << "none";     

    delete peoplelist;
    return 0;
}

7.

#include<iostream>
#include<cctype>
#include<string>//头文件不能忘记 

int main()
{
    using namespace std;
    cout << "Enter words (q to quit): \n";

    string ch;//使用string,便于寻找首字母 
    int vowel = 0;
    int consonant = 0;
    int others = 0;

    cin >> ch;//注意输入类型,输出形式 
    while (ch != "q")//Char用‘’,string用" "
    {

        if(isalpha(ch[0]))//Char用‘’,string用" "
        {
            switch(ch[0])
            {
                case 'a':
                case 'A':vowel++;break;
                case 'e':
                case 'E':vowel++;break;             
                case 'i':
                case 'I':vowel++;break;
                case 'o':
                case 'O':vowel++;break;
                case 'u':
                case 'U':vowel++;break;
                default :consonant++;   
            }   
        }
        else
            others++;
        cin >> ch;
    }
    cout << vowel << " words beginning with vowels\n" << consonant
         << " words beginning with consonants\n"  << others << " others\n";

    return 0;   

}

8.

#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 60;

int main()
{
//-----打开测试文件U6p8test.txt- 
    using namespace std;
    char filename[SIZE];
    ifstream inFile;
    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    inFile.open(filename);
    if (!inFile.is_open())
    {
        cout << "Could not open the file " << filename << endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }
//开始读取数据 

    char value;
    int count = 0;  
    inFile >> value;
    while (inFile.good())
    {
        ++count;
        inFile >> value;
    }
//文件读取末尾 
    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";
    if (count == 0)
        cout << "No data processed.\n";
    else
    {
        cout << "chars: " << count << endl;     
    }
    inFile.close();
    return 0;

}

9.

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

using namespace std;
const int SIZE = 60;
const double stand = 10000.0;

struct patron{
    string name;
    double money;
};

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

//------打开文件------  
    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    inFile.open(filename);
    if (!inFile.is_open())
    {
        cout << "Could not open the file " << filename << endl;
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }

//------读取数据------
    int count;
    inFile >> count;
    inFile.get();//读取文件数据 

    patron * peoplelist = new patron[count];
    for(int i = 0; i < count  ;i++)
    {
        getline(inFile, peoplelist[i].name);

        inFile >> peoplelist[i].money;
        inFile.get();       

    }   


//------输出数据------  
    cout << "\nGrand Patrons: " << endl;
    int n = 0;
    for(int i = 0; i < count ;i++)
    {
        if(peoplelist[i].money > stand)
        {
            cout << "Patrons name: " << peoplelist[i].name << endl;
            cout << "donation: " << peoplelist[i].money << endl;
            n++;    
        }
    }   
    if(n == 0)
        cout << "none";

    int m = 0;
    cout << "\nPatrons: " << endl;      
    for(int i = 0; i < count ;i++)
    {
        if(peoplelist[i].money <= stand)
        {
            cout << "Patrons name: " << peoplelist[i].name << endl;
            cout << "donation: " << peoplelist[i].money << endl;
            m++;
        }
    }           
    if(m == 0)
        cout << "none";   

//----释放内存,关闭文件----  
    delete peoplelist;
    inFile.close();
    return 0;   
}

猜你喜欢

转载自blog.csdn.net/weixin_41882882/article/details/81286900