C++ Primer Plus 第六版 第六章 练习题参考答案

//1.*******************************************************************
//*******************************************************************
int main()
{
    using namespace std;
    char ch;
    char a;
    char b;
    while ((ch = cin.get()) != '@') {
        if (!isdigit(ch))
            if (isupper(ch))
                a = tolower(ch),
                cout << a;
            else if (islower(ch))
                cout <<char (toupper(ch));
            else
                cout << ch ;
        cout << endl;
    }
    system("pause");
}

//2.*******************************************************************
//*******************************************************************
int main()
{
    using namespace std;
    double arr[4];
    double ave = 0;
    int sum = 0;
    int count = 0;
    int data;
    int i = 0;
    cout << "please input the data(type the others form to quit this program/n"<<endl;
    while (i<4&&cin >> data) {
            arr[i] = data;
            sum += arr[i];
            ave = sum / (i+1);
            ++i;
    };
    cout <<"the average is:" <<ave << endl;
    for (int k = 0; k < i; ++k) {
        if (arr[k] > ave)
            ++count;
    }
    cout << "the totals that bigger than  "<<ave<<" is: " <<count << endl;
    system("pause");
}

//3*******************************************************************
//*******************************************************************
int main()
{
    using namespace std;
    char choice;
    cout << "please enter a c,p,t,or g:\n "
        "c) carnivore\t"   "p) pianist\n"
        " t) tree\t   "        "g) game\n";
    while (cin.get(choice)) {
        switch (choice)
        {
        case 'c':
        case 'C': cout << "this is a carnivore.\n";
            break;
        case 'p':
        case 'P': cout << "this is a pianist.\n";
            break;
        case 't':
        case 'T': cout << "this is a tree.\n";
            break;
        case 'g':
        case 'G': cout << "this is a game.\n";
            break;
        default:  cout << "this is not a correct input,please type word c,p,t,g!\n";
            break;
        }
        cin.get(choice);
    }
    system("pause");
}

//4*******************************************************************
//*******************************************************************
using namespace std;
const int strsize = 20;
void showmenu();
void display_name();
void display_botname();
void display_title();
void display_pre();
struct bop {
    char fullname[strsize];
    char title[strsize];
    char botname[strsize];
    int  preference;

};
bop ch[5] = {
        { "Wimp Macho" ,"CEO" ,"Sand" ,0 },
        { "Raki Rhodes", "VC", "X", 1},
        { "Celia Laiter", "CTO", "C", 1 },
        { "Hoppy Hipman", "CFO", "O", 2 },
        { "Pat Hand" ,"CPO" ,"Nor",1 },
};
int main()
{
    showmenu();

    char choice;
    while (cin.get(choice)&&choice!='q') {
        switch (choice)
        {
            case 'a':
            case 'A':  display_name();
                break;
            case 'c':
            case 'C':  display_botname();
                break;
            case 'd':
            case 'D':  display_pre();
                break;
            case 'b':
            case 'B':  display_title();
                break;
            default:    cout << "Incorrect input.Try again!\n";
                break;
        }
        cin.get(choice);
    }
    cout << "Bye!\n ";
    system("pause");
}
void showmenu(){
    cout << "please choose one of follows: \n"
        "a. display by name\t"  "b.display by title\n"
        "c. display by bopname\t"  "d. display by preference\n"
        "q. quit\t\n";

};
void display_name() {
    for (int i = 0; i < 5; ++i) {
        cout << ch[i].fullname << endl;
    }

}
void display_botname(){
    for (int i = 0 ; i < 5; ++i) {
        cout << ch[i].botname << endl;
    }
}
void display_title() {
    for (int i = 0; i < 5; ++i) {
        cout << ch[i].title << endl;
    }
}
void display_pre() {
    for (int i = 0; i < 5; ++i)
    {
        if (ch[i].preference == 0)
            cout << ch[i].fullname << endl;
        else if (ch[i].preference == 1)
            cout << ch[i].title << endl;
        else
            cout << ch[i].botname << endl;
    }
}

//5*******************************************************************
//*******************************************************************
int main()
{
    int income, tax;
    cout << "input your income: \n";
    while (cin >> income&&income >= 0) {
        if (income <= 5000)
            tax = 0;
        else if (income >= 5001 && income <= 15000)
            tax = income*0.1;
        else if (income >= 15001 && income <= 35000)
            tax = income*0.1 + (income - 15000)*0.15;
        else 
            tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000)*0.2;
        cout << "the corresponding tax is: " << tax<<endl;
    }
    cout << "Wrong input!\n";
    system("pause");
}

//6*******************************************************************
//*******************************************************************
struct info {
    char name[20];
    double donate;
};

int main()
{
    using namespace std;
    int donate;
    cout << "Please enter the number of donors: \n ";
    cin >> donate;
    cin.get();
    info *p = new info[donate];
    for (int i = 0; i < donate; ++i) {
        cout << "Please enter the name of "<< i+1<<" th donors: \n";
        cin.getline(p[i].name, 20);
        cout << "Please enter the donation of "<< i+1 <<" th donors: \n";
        (cin >> p[i].donate).get();
    }
    cout << "the donation info is given below: \n";
    cout << "Grand Patrons are: \n";
    for (int i = 0; i < donate; ++i) {
        if (p[i].donate >= 10000)
            cout << p[i].name << " " << p[i].donate << endl;
    }
    cout << endl;
    cout << "other Patrons: \n";
    for (int i = 0; i < donate; ++i) {
        if (p[i].donate < 10000)
            cout << p[i].name << " " << p[i].donate;
    }
    delete[]p;
    system("pause");
}

//7*******************************************************************
//*******************************************************************
int main()
{
    string word;
    int word_consonants=0,word_vowels=0,others=0;
    cout << "Enter words (q to quit) : \n";
    cin>> word;
    while (  word!= "q")
    {
 
        if (isalpha(word[0]))
        {
            char ch;
            ch = tolower(word[0]);
            switch (ch)
            {
                case 'a' :
                case 'A':
                case 'e' :
                case 'E':
                case 'I':
                case 'i' :
                case 'O':
                case 'o' :
                case 'U'
                case 'u' :
                            ++word_vowels; 
                       break;
                default:
                            ++word_consonants; 
            }
        }
        else
            ++others;
        cin>> word;
    }
    cout << word_vowels <<" words beginning with vowels \n";
    cout << word_consonants <<" words beginning with consonants \n";
    cout << others <<" others \n";
        
    system("pause");
    return 0;   
}

//8*******************************************************************
//*******************************************************************
int main()
{
    using namespace std;
    ifstream infile;
    infile.open("carinfo.txt");
    if (!infile.is_open()) {
        cout << "could not open the file!\n";
        exit(EXIT_FAILURE);
    }
    char w[50];
    int count = 0;
    while (infile.good()) {
        ++count;
        infile >> w;
    }
    if (infile.eof())
        cout << "end of the file reached.\n";
    else if (infile.fail())
        cout << "input terminated by data error!\n";
    infile.close();
    cout << "the total of this file is: " << count <<  endl;
    system("pause");
}

//9*******************************************************************
//*******************************************************************
struct info {
    char name[20];
    double donate;
};

int main()
{
    using namespace std;
    int donate;
    ifstream infile;
    infile.open("carinfo.txt");
    infile >> donate;
    infile.get();
    info *p = new info[donate];
    for (int i = 0; i < donate; ++i) {
        infile.getline(p[i].name, 20);
        (infile >> p[i].donate).get();
    }
    cout << "the donation info is given below: \n";
    cout << "Grand Patrons are: \t";
    for (int i = 0; i < donate; ++i) {
        if (p[i].donate >= 10000)
            cout << p[i].name << " " << p[i].donate << endl;
    }
    cout << endl;
    cout << "other Patrons: \n";
    for (int i = 0; i < donate; ++i) {
        if (p[i].donate < 10000)
            cout << p[i].name << " " << p[i].donate<<"\t";
    }
    delete[]p;
    system("pause");
}
//*******************************************************************
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。