C++ primer Plus(第六版)中文版 第五章 循环和关系表达式 编程练习答案

第五章 编程练习
1. 编写一个要求用户输入两个整数的程序。该程序将计算并输出两个整数之间(包括这两个整数)所有整数的和。
   这里假设先输入较小的整数。例如:如果用户输入的是2和9,则程序将指出2~9 之间所有的整数的和为44.
1.1 for 循环版

#include <iostream>

int main()
{
    using namespace std;

    int a;
    int b;
    int sum = 0;

    cout << "请输入两个整数,以求得他们之间(包括这两个整数)所有整数的和\n";
    cout << "请输入第一个整数(较大的) ";
    cin >> a;
    cout << "请输入第二个整数(较小的) ";
    cin >> b;

    for (int i = a; i <= b; i++)
        sum = sum + i;

    cout << "第一个整数" << a << "到第二个整数" << b
        << "之间(包括这两个整数)所有整数的和为: " << sum;

    return 0;
}

1.2 while 循环版

#include <iostream>

int main()
{
    using namespace std;

    int a;
    int b;
    int sum = 0;
    int i;

    cout << "请输入两个整数,以求得他们之间(包括这两个整数)所有整数的和\n";
    cout << "请输入第一个整数(较大的) ";
    cin >> a;
    cout << "请输入第二个整数(较小的) ";
    cin >> b;

    i = a;
    
    while(i <= b)
    {
        sum = sum + i;
        i += 1;
    }
    cout << "第一个整数" << a << "到第二个整数" << b
        << "之间(包括这两个整数)所有整数的和为: " << sum;

    return 0;
}

1.3 使用if,允许先输入较大的数

#include <iostream>

int main()
{
    using namespace std;

    int a;
    int b;
    int sum = 0;

    cout << "请输入两个整数,以求得他们之间(包括这两个整数)所有整数的和\n";
    cout << "请输入第一个整数 ";
    cin >> a;
    cout << "请输入第二个整数 ";
    cin >> b;

    if (a <= b)
    {
        for (int i = a; i <= b; i++)
            sum = sum + i;
    }
    else
    {
        for (int i = a; i >= b; i--)
            sum = sum + i;
    }
    
    cout << "第一个整数" << a << "到第二个整数" << b
        << "之间(包括这两个整数)所有整数的和为: " << sum;

    return 0;
}

2. 使用array对象(而不是数组)和 long double (而不是long long)重新编写程序清单5.4,并计算100!的值。
程序清单5.4 如下
// formore.cpp -- more looping with for
#include <iostream>
const int ArSize = 16;      // example of external declaration
int main()
{
    long long factorials[ArSize];
    factorials[1] = factorials[0] = 1LL;
    for (int i = 2; i < ArSize; i++)
        factorials[i] = i * factorials[i-1];
    for (int i = 0; i < ArSize; i++)
        std::cout << i << "! = " << factorials[i] << std::endl;    return 0;
}
2.1 修改后

#include <iostream>
#include <array>

const int ArSize = 101;      // example of external declaration
int main()
{
    using namespace std;
    array<long double, ArSize> factorials;
    factorials[1] = factorials[0] = 1L;
    for (int i = 2; i < ArSize; i++)
        factorials[i] = i * factorials[i-1];
    for (int i = 0; i < ArSize; i++)
        cout << i << "! = " << factorials[i] << endl;

    return 0;
}

3. 编写一个要求用户输入数字的程序,每次输入后,程序都将报告到目前为止,所有输入的累积和。
   当用户输入为0时,程序结束
3.1 基础版

#include <iostream>

int main()
{
    using namespace std;
    
    double a;
    double sum = 0;
    
    cout << "请输入数字:";
    cin >> a;
    while(1)            //或for(;;)
    {
        sum = sum + a;
        cout << "到目前为止,所有输入的累积和为:" << sum << endl;
        cout << "请输入数字:";
        cin >> a;
    }
    return 0;
}    

3.2 判断输入是否有效(输入除数字以外的字符即停止)

#include <iostream>

int main()
{
    using namespace std;
    
    double a;
    double sum = 0;
    
    cout << "请输入数字:";
    while (cin >> a)
    {
        sum = sum + a;
        cout << "到目前为止,所有输入的累积和为:" << sum << endl;
        cout << "请输入数字:";
    }
    return 0;
}    

4. Daphne以10%的单利投资了100美元。也就是说。每一年的利润都是投资的10%,即每年10美元。(每年的利息=0.10*原始存款),而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%。(每一年的利息=0.05*当前存款),Cleo在第一年投资100美元的盈利是5%——得到了105美元,下一年的盈利是105美元的5%——即5.25 美元,以此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的价值投资。
4.1 用while循环

#include <iostream>

int main()
{
    using namespace std;
    const double daphne_rate = 0.1;
    const double cleo_rate = 0.05;
    
    const double daphne_init = 100;
    const double cleo_init = 100; 
    
    int year = 0;
    double daphne = daphne_init;
    double cleo = cleo_init;    
    while (daphne >= cleo)
    {
        year += 1;
        daphne = daphne_init * daphne_rate * year + daphne_init;
        cleo = cleo * cleo_rate + cleo;  
    }
    
    cout << "第" << year << "年时Cleo的投资价值才能超过Daphne的投资价值\n";
    cout << "此时Daphne的投资价值为: " <<  daphne << endl;
    cout << "此时Cleo的投资价值为: " <<  cleo << endl;
    
    return 0;
}

4.2 用for循环

#include <iostream>

int main()
{
    using namespace std;
    const double daphne_rate = 0.1;
    const double cleo_rate = 0.05;
    
    const double daphne_init = 100;
    const double cleo_init = 100; 
    
    int year;
    double daphne = daphne_init;
    double cleo = cleo_init;    
    for(int i = 1; daphne >=cleo; i++)
    {
        year = i;
        daphne = daphne_init * daphne_rate * year + daphne_init;
        cleo = cleo * cleo_rate + cleo;  
    }
    
    cout << "第" << year << "年时Cleo的投资价值才能超过Daphne的投资价值\n";
    cout << "此时Daphne的投资价值为: " <<  daphne << endl;
    cout << "此时Cleo的投资价值为: " <<  cleo << endl;
    
    return 0;
}

5. 假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。
   程序通过循环,使用初始化为月份的字符串char*数组(或string对象数组)逐月进行提示,
   并将输入的数据存储在一个int数组中,然后程序计算数组中各元素的总数,并报告这一年的销售情况。
5.1 使用字符串char*数组

#include <iostream>

int main()
{
    using namespace std;
    
    const char* month[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
    int num[12];
    int sum = 0;
    
    for (int i = 0; i<12; i++)
    {
        cout << "请输入" << month[i] << "的销售量: ";
        cin >> num[i];
    }
    
    for (int i = 0; i<12; i++)
    {    
        cout << month[i] << "的销售量为: \t" << num[i] << endl;
        sum = sum +  num[i];
    }
    
    cout << "一年的销售情况为: \t" << sum;
    
    return 0;
}

5.2 使用string对象数组

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    const string month[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
    int num[12];
    int sum = 0;
    
    for (int i = 0; i<12; i++)
    {
        cout << "请输入" << month[i] << "的销售量: ";
        cin >> num[i];
    }
    
    for (int i = 0; i<12; i++)
    {    
        cout << month[i] << "的销售量为: \t" << num[i] << endl;
        sum = sum +  num[i];
    }
    
    cout << "一年的销售情况为: \t" << sum;
    
    return 0;
}

6. 完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    const string month[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
    int num[3][12];
    int sum = 0;
    
    for (int j = 0; j < 3; j++)
    {
        cout << "第" << j+1 <<"年:\n";
        for (int i = 0; i < 12; i++)
        {
            cout << "请输入" << month[i] << "的销售量: ";
            cin >> num[j][i];
        }
    }
    
    for (int j = 0; j < 3; j++)
    {
        cout << "第" << j+1 <<"年:\n";
        for (int i = 0; i < 12; i++)
        {
            cout << month[i] << "的销售量为: \t" << num[j][i] << endl;
            sum = sum + num[j][i];
        }
    }
    
    cout << "三年的销售情况为: \t" << sum;
    
    return 0;
}

7. 设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string对象中的字符串)、
   生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。
   随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。
   接下来,程序提示用户输入每辆汽车的生产商(可能由多个单词组成)和年份信息。
   请注意,这需要特别小心,因为它将交替读取数值和字符串。最后,程序将显示每个结构的内容。
   该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser

#include <iostream>
int main()
{
    using namespace std;
    
    int num;
    struct car 
    {
        char maker[20];
        int year;
    };
    
    cout << "How many cars do you wish to catalog? ";
    cin >> num;
    cin.get();
    
    car * catalog = new car [num];
    
    for (int i = 0; i < num; i++)
    {
        cout << "Car #" << i+1 << ":\n";
        cout << "Please enter the make: ";
        cin.get(catalog[i].maker, 20).get(); 
        cout << "Please enter the year made: ";
        cin >> catalog[i].year;
        cin.get();
    }
    
    cout << "Here is your collection:\n";
    for (int i = 0; i < num; i++)
        cout << catalog[i].year << " " <<catalog[i].maker << endl;
    
    return 0;
}


8. 编写一个程序,它使用一个char数组和循环来读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少单词(不包括done在内)。
   下面是该程序的运行情况:
   Enter words (to stop, type the word done):
   anteater birthday category dumpster
   envy finagle geometry done for sure 
   You entered a total of 7 words.
   您应在程序中包含头文件csting,并使用函数strcmp()函数来进行比较测试

#include <iostream>
#include <cstring>
int main()
{
    using namespace std;
    char word[20];
    int sum = 0;
    
    cout << "Enter words (to stop, type the word done): " << endl;
    
    cin >> word;
    while(strcmp(word,"done"))
    {
        sum++;
        cin >> word;
    }
    cout << "You entered a total of " << sum << " words." << endl;
    
    return 0; 
}

//或者
#include <iostream>
#include <cstring>
int main()
{
    using namespace std;
    char word[20];
    int sum = 0;
    
    cout << "Enter words (to stop, type the word done): " << endl;
    
    while(cin >> word && strcmp(word,"done"))
        sum++;
    cout << "You entered a total of " << sum << " words." << endl;
    
    return 0; 
}

9. 编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含string,并使用关系运算符来进行比较测试。

#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string word;
    int sum = 0;
    
    cout << "Enter words (to stop, type the word done): " << endl;
    
    cin >> word;
    while(word != "done")
    {
        sum++;
        cin >> word;
    }
    cout << "You entered a total of " << sum << " words." << endl;
    
    return 0; 
}

//或者
#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string word;
    int sum = 0;
    
    cout << "Enter words (to stop, type the word done): " << endl;
    
    while(cin >> word && word != "done")
        sum++;
    cout << "You entered a total of " << sum << " words." << endl;
    
    return 0; 
}

10. 编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。
    然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推。
    每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。
    该程序运行情况如下:
10.1  for循环版

#include <iostream>

int main()
{
    using namespace std;
    int num;
    
    cout << "Enter number of rows: ";
    cin >> num;
    
    for (int i = 1; i <= num; i++)
    {
        for (int j = 0; j < num-i; j++)
            cout << ".";
        for(int k = 0; k < i; k++)
            cout << "*";
        cout << "\n";
    }
    return 0;
}

10.2 while 循环版

#include <iostream>

int main()
{
    using namespace std;
    int num;
    
    cout << "Enter number of rows: ";
    cin >> num;
    
    int i = 1;
    int j = 0;
    int k = 0;
    
    while (i <= num)
    {
        j = 0;
        while(j < num-i)
        {
            cout << ".";
            j+=1;
        }
        
        k = 0;
        while(k < i)
        {
            cout << "*";
            k+=1;
        }
        cout << endl;
        i+=1;
    }
    
    return 0;
}

10.3 动态string类对象数组版

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    int line,i,j,k;
    string point = ".";
    string star = "*";
    string enter = "\n";
    cout << "请输入要显示的行数:";
    cin >> line;
    string* pointandstar = new string [line];
    
    for (i=0; i<line; i++)
    {
        for (j=0; j<line-i-1; j++)
        {
            *pointandstar += point;
        }

        for (k=0; k<i+1; k++)
        {
            *pointandstar += star;
        }
        
        *pointandstar +=enter;
    }

    cout << *pointandstar;
    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/weixin_42411044/article/details/85377611
今日推荐