c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)

声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。

(具体方式参见第3章模板)

1.编写一个小程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。

#include <iostream>
#include <cctype>
using namespace std;
void cprimerplus_exercise_6_1()
{
    char letter;
    cout << "Please input letters:";
    cin >> letter;
    cin.get();
    
    while ( letter != @)
    {
        if (isdigit(letter))
        {
            cin.get(letter);
        }
        else
        {
            if (islower(letter))
            {
                letter = toupper(letter);
            }else if (isupper(letter))
            {
                letter = tolower(letter);
            }

            cout << letter;
            cin >> letter;
            cin.get();
        }
        
    }
    
}

2.编写一个程序,最多将10个donation值读入到一个double数组中(如果你愿意,也可以使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

#include <iostream>
#include <cctype>
using namespace std;
void cprimerplus_exercise_6_2()
{
    double donation[10];
    double sum = 0.0;
    double average = 0.0;
    double tmp; 
    int i = 0;
    int cnt = 0;
    
    while ( cin >> tmp && i < 10)
    {
        donation[i] = tmp;
        sum +=donation[i];
        ++i;
    }

    if ( i != 0)
    {
        average = sum / i;
    }

    for (int j = 0; j < i; j++)
    {
        if (donation[j] > average)
        {
            ++cnt;
        }
    }

    cout << "The average is:" << average << endl;
    cout << "There are " << cnt << " numbers are above the average!"<< endl;

}

3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。

#include <iostream>

using namespace std;
void cprimerplus_exercise_6_3()
{
    cout << "Please enter one of the following choices:" << endl        <<"c) carnivore  \tp) pianist \nt) tree \tg) game" << endl;

    cout << "Please enter a c, p, t, or g:";

    char letter;
    cin >> letter;

    while ( letter != c && letter != p && letter != t && letter != g)
    {        
        cout << "Please enter a c, p, t, or g:";
        cin >> letter;
    }
    switch (letter)
    {
    case c:
        cout << "A maple is a carnivore";
        break;
    case p:
        cout << "A maple is a pianist";
    case t:
        cout << "A maple is a tree";
        break;
    case g:
        cout << "A maple is a game";
        break;
    default:
        break;
    }
}
4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他,请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面结构

//Benevolent Order of Programmers name structure

Struct bop

{

char fullname[strsize];

char title[strsize];

char bopname[strsize];

int preference;

};

该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值,另外,该程序使用一个循环让用户在下面的选项中进行选择:

A. Display by name B. Display by title C. Display by bopname

D. Display by preference Q. quit

#include <iostream>

using namespace std;
void cprimerplus_exercise_6_4()
{
    const int strsize = 20;
    const int mennum = 3;
    struct bop
    {
        char fullname[strsize];   //real  name
        char title[strsize];      //job title
        char bopname[strsize];    //secret BOP name
        int preference;            // 0 = fullname, 1 = title, 2 = bopname
    };

    bop member[mennum] = {
        { "thm", "leader", "sb", 0},
        { "cgf", "sb", "sb", 1},
        { "th", "dsb", "ssb", 2} };
    cout << "Enter your choice!";
    char ch;
    cin.get(ch);
    while (cin >> ch && ch != q)
    {
        switch (ch)
        {
        case a:
            for (int i = 0; i < mennum; i++)
                cout << member[i].fullname << endl;
            break;
        case b:
            for (int i = 0; i < mennum; i++)
                cout << member[i].title << endl;
            break;
        case c:
            for (int i = 0; i < mennum; i++)
                cout << member[i].bopname << endl;
            break;
        case d:
            for (int i = 0; i < mennum; i++)
            {
                if (member[i].preference == 0)
                {
                    cout << member[i].fullname << endl;
                }else if (member[i].preference = 1)
                {
                    cout << member[i].title << endl;
                }else if (member[i].preference = 2)
                {
                    cout << member[i].bopname << endl;
                }
            }
            break;
        default:
            break;
        }
        cout << "Next choice:";
    }
    cout << "Bye!\n";
    
}
5. 在neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:

5000 tvarps:不收税

5001~15000 tvarps:10%

15001~35000 tvarps:15%

35000 tvarps以上:10%

如:收入38000 tvarps,所得税:5000*0.0+10000*0.1+20000*0.15+3000*0.2;

#include <iostream>

using namespace std;
void cprimerplus_exercise_6_5()
{
    cout << "please input your income:";
    double income, revenue;
    while (cin >> income && income >= 0)
    {
        if (income <= 5000)
        {
            revenue = 0.0;
        }else if ( income > 5000 && income <= 15000)
        {
            revenue = (income - 5000) * 0.1;
        }else if( income > 15000 && income < 35000)
        {
            revenue = 5000 * 0.00 + 10000 * 0.10 + (income -20000) * 0.15;
        }else if( income > 35000)
        {
            revenue = 5000 * 0.00 + 10000 * 0.10 + + 20000 * 0.15 + (income -35000) * 0.15;
        }

        cout << "your revenue is:" << revenue << endl;
        cout << "please enter your income:";

    }
}

6.编写一个程序,记录捐助给“维护合法权利团体”的资金,该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项,这些信息都被存储在一个动态分配的结构数组中,每个结构有两个成员;用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名以及捐款的数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某类别没有捐款人,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。

#include <iostream>
#include <string>

using namespace std;
void cprimerplus_exercise_6_6()
{
    int num;
    cout << "please input the donate num:";
    cin >> num;
    cin.get();

    struct patron
    {
        string name;
        double money;
    };

    patron *ps = new patron[num];
    
    for (int i = 0; i < num; ++i)
    {
        cout <<"please input the "<< i+1 <<"th patron name:";
        getline(std::cin, ps[i].name);
        cout << "please input the " << i+1 << "th patron money:";
        cin >> ps[i].money;
        cin.get();
    }
    int cnt = 0, snt = 0;
    cout << "Grand Patrons:" << endl ;
    for (int i = 0; i < num; i++)
    {
        if (ps[i].money >= 10000)
        {
            cout <<  ps[i].name << \t <<ps[i].money << endl;
            ++cnt;
        }

    }
    if ( cnt == 0)
    {
        cout << "none";
    }

    cout << "\nPatrons:" << endl;
    for (int i = 0; i < num; i++)
    {
        if (ps[i].money < 10000)
        {
            cout << ps[i].name << \t << ps[i].money << endl;
            ++snt;
        }

    }
    if ( snt == 0)
    {
        cout << "none";
    }

    delete []ps;

}

7.编写一个程序,他每次读取一个单词,直到用户只输入q。然后,该程序指出有多少单词以元音打头,有多少个单词以辅音打头,还有多少单词不属于这两类。为此,方法之一是使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过salpha()测试的单词,使用if或switch语句来确定哪些是以元音打头。

#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;
void cprimerplus_exercise_6_7()
{
    string word;
    cout << "Enter words (q to quit):";
    int cnt = 0;
    int vowel = 0, constant = 0, other = 0;
    while (cin >> word && !word.empty())
    {
        if(isalpha(word[0]))
        {
            if (word[0] == q && word.length() == 1)
                break;
            else if( word[0] == a || word[0] == e || word[0] == i || word[0] == o || word[0] == u)
            {
                ++vowel;
            }else
            {
                ++constant;
            }
        }else
        {
            ++other;
        }
    }

    cout << vowel << " words beginning with vowels "<<  endl;
    cout << constant <<" words beginning with constants " << endl;
    cout << other  <<" others"  << endl;
}

8.编写一个程序,它打开一个文件夹,逐个字符的读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。

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

using namespace std;
void cprimerplus_exercise_6_8()
{
    char ch;
    int sum = 0;
    ifstream inFile;
    inFile.open("1.txt");
    if( !inFile.is_open())
    {
        cout << "Could not open the file!\n";
        cout << "Program terminating.\n";
        exit(EXIT_FAILURE);
    }

    inFile >> ch;
    while (inFile.good())
    {
        ++sum;
        inFile >> ch;
    }
    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";
    }
    cout << "there are " << sum << " chars in this file!\n";
}

9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。及该文件类似于下面:

4
Sam Stone
2000
Freida Flass
10050
Tammy Tubbs
5000
Rich Raptor
5500

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int SIZE = 60;


void cprimerplus_exercise_6_9()
{
    char filename[SIZE];
    ifstream inFile;
    cout << "Enter the name of the 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);
    }

    struct patron
    {
        string name;
        double money;
    }; 

    int num, cnt = 0, snt = 0;
    inFile >> num;
    inFile.get();

    patron *ps = new patron[num];
    for (int i = 0; i < num; i++)
    {
        getline(inFile, ps[i].name);
        inFile >> ps[i].money;
        inFile.get();
    }
    cout << "Grand patrons:\n";
    for (int i = 0; i < num; i++)
    {
        if (ps[i].money >= 10000)
        {
            cout << ps[i].name <<  \n << ps[i].money << endl;
            ++cnt;
        }
    }

    if ( cnt == 0)
    {
        cout << "none";
    }

    cout << "\nPatrons:" << endl;
    for (int i = 0; i < num; i++)
    {
        if (ps[i].money < 10000)
        {
            cout << ps[i].name << \t << ps[i].money << endl;
            ++snt;
        }

    }
    if ( snt == 0)
    {
        cout << "none";
    }

    delete []ps;

    inFile.close();
}

c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符),布布扣,bubuko.com

c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)

上一篇:beetl-spring Beetl的Spring整合扩展(2):Beetl与Spring MVC整合


下一篇:JAVA wait(), notify(),sleep详解