《C++ Primer Plus(第6版)》编程练习代码 Chapter 6

来源:互联网 发布:java 线程是什么 编辑:程序博客网 时间:2024/05/17 23:03

Chapter 6

6.1

#include<iostream>#include<cctype>using namespace std;int main(){    char ch;    cin.get(ch);    while (ch != '@')    {        if (!isdigit(ch))            if (islower(ch))                cout << (char) toupper(ch);            else if (isupper(ch))                cout << (char) tolower(ch);            else                 cout << ch;            cin.get(ch);    }    cin.get();    cin.get();}

6.2

#include<iostream>#include<cctype>using namespace std;int main(){    double number[10];    int i = 0, max = 10;    cout << "number #1: ";    while (cin >> number[i] && i < max)    {        if (++i < max)            cout << "number #" << i + 1 << ": ";    }    if (!cin)    {        cin.clear();        cin.get();    }                     //此处添加的if是为了防止一输入非数字程序直接关闭    cin.get();    cin.get();}

6.3

#include<iostream>using namespace std;int main(){    char ch;    cout << "Please enter one of the folowing choices:\n"        << "c) carnivore\tp) pianist\n"        << "t) tree\t\tg) game\n";    do    {        cin >> ch;        switch (ch)        {            case 'c': cout << "A maple is a carnivore.";                      break;            case 'p': cout << "A maple is a pianist.";                      break;            case 't': cout << "A maple is a tree.";                      break;            case 'g': cout << "A maple is a game.";                      break;            default: cout << "Please enter a c, p, t or g: ";        }    } while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g');    cin.get();    cin.get();}

6.4

#include<iostream>using namespace std;const int strSize = 20;const int num_people = 3;struct bop {    char fullname[strSize];    char title[strSize];    char bopname[strSize];    int preference;};int main(){    char choice;    bop people[num_people] =    {        {"Wimp Macho", "imp", "acho", 1 },        {"Riki Rhodes", "riki","rhodes", 2},        {"Cellia Laiter", "cellia", "Laiter", 3},    };    cout << "Benevolet Order of Programmers Report\n"        << "a. display by name\tb. display by title\n"        << "c. display by bopname\td. display by preference\n"        << "q. quit\n";    cout << "Enter your choice: ";    cin >> choice;    while (choice != 'q')    {        switch (choice)        {        case 'a': for (int i = 0; i < num_people; ++i)                {                    cout << people[i].fullname << endl;                }                  break;        case 'b': for (int i = 0; i < num_people; ++i)                {                    cout << people[i].title << endl;                }                  break;        case 'c': for (int i = 0; i < num_people; ++i)                {                    cout << people[i].bopname << endl;                }                  break;        case 'd': for (int i = 0; i < num_people; ++i)                {                    switch (people[i].preference)                    {                    case 1: cout << people[i].fullname << endl;                            break;                    case 2: cout << people[i].title << endl;                            break;                    case 3: cout << people[i].bopname << endl;                            break;                    };                }                  break;        default: cout << "Please enter a, b, c, d or q: ";        };        if (choice == 'a' || choice == 'b' || choice == 'c' || choice == 'd')            cout << "Next choice: ";        cin >> choice;    }    cin.get();    cin.get();}

6.5

#include<iostream>using namespace std;int main(){    double incomes, duty;    cout << "Please enter your incomes: ";    while (cin >> incomes && incomes >= 0)    //其中cin >> incomes,目的为判断输入是否为数字,不为数字则跳出循环    {        if (incomes <= 5000)            duty = 0;        else if (incomes < 15000)            duty = (incomes - 5000) * 0.1;        else if (incomes < 35000)            duty = 10000 * 0.1 + (incomes - 15000) * 0.15;        else duty = 10000 * 0.1 + 20000 * 0.15 + (incomes - 35000) * 0.2;        cout << "Your duty: " << duty << endl;        cout << "Please enter your incomes: ";    }    cin.get();    cin.get();}

6.6

#include<iostream>#include<string>using namespace std;struct info{    string name;    double money;};int main(){    int n, i = 0, counter1 = 0, counter2 = 0;    cout << "Please enter the number of patrons: ";    cin >> n;    cin.get();    int * normalPatron = new int[n];           //存储普通用户数组下标    info * patronInfo = new info[n];    while (i < n)    {        cout << "Enter patron's name: ";        getline(cin, patronInfo[i].name);        cout << "Enter patron's donation: ";        cin >> patronInfo[i].money;        cin.get();        ++i;    }    cout << "Grand Patrons\n";    //判断捐款是否大于10000,是则输出,否则记录为普通.    for (i = 0; i < n; ++i)    {        if (patronInfo[i].money >= 1e4)        {            cout << patronInfo[i].name << "\t" << patronInfo[i].money << endl;            counter1++;        }        else        {            normalPatron[counter2] = i;            counter2++;        }    }    if (counter1 == 0)        cout << "none\n";    //输出普通    cout << "Patrons\n";    if (counter2 == 0)        cout << "none\n";    else    {        i = 0;        cout << patronInfo[normalPatron[i]].name << "\t" << patronInfo[normalPatron[i]].money << endl;        i++;        while (i < counter2)        {            cout << patronInfo[normalPatron[i]].name << "\t"                << patronInfo[normalPatron[i]].money << endl;            ++i;        }    }    delete [] normalPatron;    delete [] patronInfo;    cin.get();    cin.get();}

6.7

#include<iostream>#include<cctype>#include<cstring>using namespace std;int main(){    int counterV = 0, counterC = 0, count = 0, flag = 1;    char ch;    cout << "Enter words (q to quit):\n";    while (cin.get(ch) && ch != 'q')    {        if (isalpha(ch) && flag)            switch (ch)            {            case ('a') :            case ('A') :            case ('o') :            case ('O') :            case ('u') :            case ('U') : counterV++;                flag = 0;                break;            default: counterC++;                flag = 0;                break;            }        else if (isdigit(ch) && flag)        {            count++;            flag = 0;        }        else if (ch == ' ' || ch == '.') flag = 1;    }    cout << counterV << "words beginning with vowels\n";    cout << counterC << "words beginning with consonants\n";    cout << count << "others.";    cin.get();    cin.get();}

6.8

#include<iostream>#include<fstream>#include<cstdlib>using namespace std;const int SIZE = 60;int main(){    char filename[SIZE];    ifstream inFile;    cout << "Enter the name of 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 ch;    int count = 0;    //此处空格回车都不计入count    while (inFile >> ch)    {        ++count;    }    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 chat processed.\n";    else    {        cout << count << " char.";    }    inFile.close();    cin.get();    cin.get();}

6.9

#include<iostream>#include<fstream>#include<cstdlib>using namespace std;const int SIZE = 60;struct info{    char name[SIZE];    int money;};int main(){    char filename[SIZE];    cout << "Enter filename: ";    cin.getline(filename, SIZE);    ifstream fin;    fin.open(filename);    if (!fin.is_open())    {        cout << "Could not open the file: " << filename << endl;        cout << "Program terminating.\n";        exit(EXIT_FAILURE);    }    int num_people;    fin >> num_people;    fin.get();    info * patronInfo = new info[num_people];    int i = 0;    while (i < num_people)    {        fin.getline(patronInfo[i].name, SIZE);        fin >> patronInfo[i].money;        fin.get();        ++i;    }    for (i = 0; i < num_people; ++i)    {        cout << patronInfo[i].name << "\t" << patronInfo[i].money << endl;    }    fin.close();    delete[] patronInfo;    cin.get();    cin.get();}
阅读全文
0 0