1. 程式人生 > >c++ primer第六章習題

c++ primer第六章習題

#include <iostream>
#include <cctype>
#include <string>
#include <array>
#include <iomanip>
#include <fstream>

using namespace std;

//practice 1
void p6_1(void){
    char ch=0;
    while ((ch=cin.get()) != '@') {
            if (isdigit(ch)){
                continue;
            }
            else if (islower(ch)){
                cout<<(char)toupper(ch);
            }
            else if (isupper(ch)){
                cout<<(char)tolower(ch);
            }
    }
    cout<<"Done!"<<endl;
}
//practice 2
void p6_2(void){
    const unsigned int SIZE=10;
    array<double, SIZE> donation;
    unsigned int enter=0;
    double total_value=0.0;
    double avg=0.0;
    unsigned int large_avg=0;
    
    cout << "Please enter up to ten double value, Non-digital to exit: " << endl;
    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){
            large_avg++;
        }
    }
    cout << "The average value is " << avg << ", and there are " << large_avg << " double value large than agerage value!" << endl;


}

//practice 3
void p6_3(void){
    char ch=0;
    
    cout << "Please enter one of the following choices: " << endl;
    cout.flags(ios::left);
    cout << setw(20) << "c) carnivore" << "p) pianist" << endl;
    cout << setw(20) << "t) tree" << "g) game" << endl;
    bool exit=false;
    
    while (cin>>ch&&!exit) {
        switch (ch) {
            case 'c':
                cout << "Tyrannosaurus rex is a carnivore." << endl;
                exit=true;
                break;
            
            case 'p':
                cout << "Langlang is a pianist." << endl;
                exit=true;
                break;
                
            case 't':
                cout << "A maple is a tree." << endl;
                exit=true;
                break;
                
            case 'g':
                cout << "Dota2 is a game." << endl;
                exit=true;
                break;
                
            default:
                cout << "Please enter one of the following choices: " << endl;
                break;
        }
    }
}


//practice 4
const unsigned int strsize=50;
struct bop{
    char fullname[strsize];     //fullname
    char title[strsize];        //job title
    char bopname[strsize];      //secret bopname
    int preference;             //0=fullname, 1=title, 2=bopname
};
void display_by_name(const struct bop *bopArray, unsigned int size){
    for (size_t i=0;i<size;i++){
        cout<<bopArray[i].fullname<<endl;
    }
}
void display_by_title(const struct bop *bopArray, unsigned int size){
    for (size_t i=0;i<size;i++){
        cout<<bopArray[i].title<<endl;
    }
}
void dispaly_by_bopname(const struct bop *bopArray, unsigned int size){
    for (size_t i=0;i<size;i++){
        cout<<bopArray[i].bopname<<endl;
    }
}
void display_by_preference(const struct bop *bopArray, unsigned int size){
    for (size_t i=0; i<size; i++){
        if(bopArray[i].preference==0){
            cout<<bopArray[i].fullname<<endl;
        }
        else if (bopArray[i].preference==1){
            cout<<bopArray[i].title<<endl;
        }
        else if(bopArray[i].preference==2){
            cout<<bopArray[i].bopname<<endl;
        }
    }
}
void p6_4(void){
    const struct bop bopArray[5] = {
        {"Wimp Macho", "YYY", "Y----", 0},
        {"XXXXXXXX", "2XXXX", "3XXXXX", 1},
        {"AAAAAAAA", "2AAAA", "3AAAAA", 2},
        {"BBBBBBBB", "2BBBB", "3BBBBB", 0},
        {"CCCCCCCC", "4CCCC", "3CCCCC", 1}
    };
    char choice=0;
    
    cout << left << setw(30) << "a. display by name" << "b. display by title" << endl;
    cout << left << setw(30) << "c. display by bopname" << "d. display by preference" << endl;
    cout << left << setw(30) << "q. quit" << endl;
    cout << "Enter your choice:";
    
    while(cin>>choice){
        if (choice=='q'){
            break;
        }
        switch (choice) {
            case 'a':
                display_by_name(bopArray, 5);
                break;
            case 'b':
                display_by_title(bopArray, 5);
                break;
            case 'c':
                dispaly_by_bopname(bopArray, 5);
                break;
            case 'd':
                display_by_preference(bopArray, 5);
                break;
            default:
                break;
        }
        cout<<"next choice";
    }
    cout<<"bye"<<endl;
    return;
}

//practice 5
const double RATE1=0.1;
const double RATE2=0.15;
const double RATE3=0.2;

void p6_5(void){
    double income=0.0;
    double tax=0.0;
    
    cout<<"please enter your income: ";
    while((cin>>income)&&(income>0)){
        if(income<+5000){
            tax=0.0;
        }
        else if(income>5000&&income<=15000){
            tax=(income-5000)*RATE1;
        }
        else if(income>15000&&income<=35000){
            tax=(15000-5000)*RATE1+(income-15000)*RATE2;
        }
        else {
            tax=(15000-5000)*RATE1+(35000-15000)*RATE2+(income-35000)*RATE3;
        }
        cout<<"income: "<<income<<"  tax: "<<tax<<endl;
    }
    
}

//practice 6
struct pat_info{
    string name;
    double account;
};
void p6_6(void){
    unsigned int contributors = 0;
    unsigned int tmp = 0;
    
    cout << "Enter the number of contributors:";
    cin >> contributors;
    cin.get();
    
    struct pat_info *pCon=new struct pat_info[contributors];
    
    for(size_t i=0;i<contributors;i++){
        cout<<"enter the name of "<<i+1<< "contributors: ";
        getline(cin, pCon[i].name);
        
        cout<<"enter the donation";
        cin>>pCon[i].account;
        cin.get();
    }
    cout<<"Grand Pators: "<<endl;
    for(size_t i=0;i<contributors;i++){
        if(pCon[i].account>10000){
            cout<<"name: "<<pCon[i].name<<endl;
            cout<<"donation: "<<pCon[i].account<<endl;
            tmp++;
        }
    }
    if(tmp==0){
        cout<<"None"<<endl;
    }
    tmp=0;
    cout<<"patrs: "<<endl;
    for(size_t i=0;i<contributors;i++){
        if(pCon[i].account<10000){
            cout<<"name: "<<pCon[i].name<<endl;
            cout<<"name: "<<pCon[i].account<<endl;
            tmp++;
    }
}
    if(tmp==0){
        cout<<"None"<<endl;
    }
}
//practice 7
    void p6_7(void){
        unsigned int vowels = 0;
        unsigned int consonants = 0;
        unsigned int others = 0;
        string input;
        
        cout << "Enter words (q to quit): " << endl;
        while((cin>>input)){
            if (input[0]=='q'&&input.length()==1){
                break;
            }
            if(isalpha(input[0])){
                if(input[0]=='a'||input[0]=='e'||input[0]=='i'||input[0]=='o'||input[0]=='u'){
                    vowels++;
                }
                else
                    consonants++;
            }
            else
                others++;
        }
        cout<<"vowel: "<<vowels<<endl;
        cout<<"other: "<<others<<endl;
        cout<<"consonants"<<consonants<<endl;

    }

//practice 8
void p6_8(void){
    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;
}

//practice 9
void p6_9(void){
    unsigned int contributors=0;
    unsigned int tmp=0;
    string FileName;
    ifstream inFile;
    
    cout<<"enter the filename: ";
    getline(cin, FileName);
    inFile.open(FileName.c_str());
    inFile>>contributors;
    inFile.get();
    
    struct pat_info *pContributors=new struct pat_info[contributors];
    
    for(size_t i=0;i<contributors;i++){
        cout<<"enter the name of "<<i+1<<" contributors: ";
        getline(inFile, pContributors[i].name);
        
        cout<<"enter the amount of donation: ";
        inFile>>pContributors[i].account;
        inFile.get();
        
    }
    cout<<"Grand Pators"<<endl;
    for(size_t i=0;i<contributors;i++){
        if(pContributors[i].account>10000){
            cout<<"name: "<<pContributors[i].name<<endl;
            cout<<"donation: "<<pContributors[i].account<<endl;
            tmp++;
        }
    }
    if(tmp==0){
        cout<<"None"<<endl;
    }
    tmp=0;
    cout<<"Pators"<<endl;
    for(size_t i=0;i<contributors;i++){
        if(pContributors[i].account<=10000){
            cout<<"name: "<<pContributors[i].name<<endl;
            cout<<"donation: "<<pContributors[i].account<<endl;
        }
    }
    if(tmp==0){
        cout<<"None"<<endl;
    }
}
    
int main(int argc, char**argv){
    p6_5();
    while (cin.get());
    return 0;
}