[C++ Primer Plus] 第5章、循环和关系表达式(二)课后习题

1.编写一个要求用户输入两个整数的程序,将程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.

#include<iostream>
using namespace std;

void main() {
    int m, n,sum=0;
    cout << "输入两个整数(从小到大):
";
    cin >> m;
    cin >> n;
    for (int i = m; i <=n; i++)
    {
        sum += i;
    }
    cout << sum<< endl;
    system("pause");
}

2.使用array对象(而不是数组)和long double(而非long long)重新编写程序清单5.4,并计算100!的值。

#include<iostream>
#include<array>
using namespace std;

const int Size = 101;//注意是101

void main() {
    array<long double, Size> factorials;
    factorials[1] = factorials[0] = 1;
    for (int i = 2; i < Size; i++)
    {
        factorials[i] = i*factorials[i - 1];
    }
    for (int i = 0; i < Size; i++)
    {
        cout << i << "!=" << factorials[i] << endl;
    }
    system("pause");
}

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

#include<iostream>
using namespace std;

void main() {
    int n, sum = 0;
    do {
        cin >> n;
        sum += n;
        cout << "**************" << sum << "***************
"<<endl;
    } while (n != 0);
    //system("pause");
}

4 Daphne以10%的单利投资了100美元。也就是说,每一年的利润是投资的10%,即每年10美元: 利息=0.10*原始存款 而Cleo在第一年投资100美元的盈利是5%,也就是说利息是当前存款的5%,请编写一个程序,计算多少年后Cleo的投资才会超过Daphne的投资价值

 题有点没读懂

#include<iostream>
using namespace std;

void main() {
    double n=0.0, sum = 100.0;
    int year = 0;
    while (n<=10)
    {
        n = sum*0.05;
        sum += n;
        year++;
    }
    cout << year << "年后,C的投资价值超过D。此时C的投资价值为" << n << "元。";
    system("pause");
}

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

#include<iostream>
using namespace std;

void main() {
    int sale[12],sum=0;
    char *month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    for (int i = 0; i < 12; i++)
    {
        cout << *(month+i) << "销量:";
        cin >> sale[i];
        sum += sale[i];
    }

    cout << "今年销售书籍" <<sum<< "本。";
    system("pause");
}

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

#include<iostream>
using namespace std;

void main() {
    int sale[3][12], sum[3] = {0};
    char *month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    for (int i = 0; i < 3; i++)
    {
        cout << "请输入第" << (i + 1) << "年的销量情况:" << endl;
        for (int j = 0; j < 12; j++)
        {
            cout << *(month + j) << "销量:	";
            cin >> sale[i][j];
            sum[i] += sale[i][j];
        }
        cout << "" << (i + 1) << "年的销量为"<<sum[i]<<"本。
"<< endl;
    }
    cout << "三年总共销售书籍" <<sum[0]+sum[1]+sum[2]<< "本。";
    system("pause");
}

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>
#include<string>
using namespace std;

struct Car{
    //char maker[30];//cin.getline(p->maker, 30);
    string maker;
    int year;
};

void main() {
    int num;
    cout << "How many cars do you wish to catalog?";
    cin >> num;
    cin.get();
    Car *p = new Car[num];//用了new就要记得用delete
    for (int i = 0; i < num; i++)
    {
        cout << "Car #" << (i + 1) << ":" << endl;
        cout << "Please enter the make:";
        getline(cin,p->maker);//可能有多个单词,所以不能用cin
        cout << "Please enter the year made:";
        cin >> p->year;
        cin.get();
        p++;
    }
    p = p - 2;//回到原来位置
    cout << "Here is your collection:"<<endl;
    for (int i = 0; i < num; i++)
    {
        cout << p->year<<" "<<p->maker<<endl;
        p++;
    }
    p = p - 2;//回到原来位置
    delete []p;
    system("pause");
}

可以用更简单的操作,使指针p不用回到原来的地方

#include<iostream>
#include<string>
using namespace std;

struct Car{
    //char maker[30];//cin.getline(p->maker, 30);
    string maker;
    int year;
};

void main() {
    int num;
    cout << "How many cars do you wish to catalog?";
    cin >> num;
    cin.get();//抵消换行符
    Car *p = new Car[num];//用了new就要记得用delete
    for (int i = 0; i < num; i++)
    {
        cout << "Car #" << (i + 1) << ":" << endl;
        cout << "Please enter the make:";
        getline(cin,p[i].maker);//可能有多个单词,所以不能用cin
        cout << "Please enter the year made:";
        cin >> p[i].year;
        cin.get();//抵消换行符
    }
    cout << "Here is your collection:"<<endl;
    for (int i = 0; i < num; i++)
    {
        cout << p->year<<" "<< p[i].maker<<endl;
    }
    delete []p;
    system("pause");
}

8.编写一个程序,它使用一个char数组合循环来每次读取一个单词,直到用户输入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.

您应在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试。

#include<iostream>
#include<cstring>
using namespace std;

void main() {
    char s[20][15];
    char s_temp[100];
    const char s_done[] = "done";
    int i=0,en=0,n =0;
    cout << "Enter words (to stop,type the word done): " << endl;
    while (en == 0)
    {
        cin.getline(s_temp, 100);//输入,存到s_temp中
        int j = 0, k = 0;
        while (s_temp[j])
        {
            if (s_temp[j] != ' ')//不是空格时
            {
                s[i][k] = s_temp[j];
                k++;
                j++;
            }
            else//检测到空格时
            {
                s[i][k] = '';
                i++;
                j++;
                k = 0;
            }
        }
        s[i][k] = '';
        i++;//到末尾时,检测到的是‘’而不是空格,所以在循环体中并没有++,所以需要+1

        for (n = 0; n <= i; n++)
        {
            if (strcmp(s[n], s_done) == 0)//等于0时两个字符串相等
            {
                en = 1;//break退出for循环,所以需要将en置1才能跳出while循环
                break;
            }
        }
    }
    cout << "You entered a total of " << n << " words" << endl;
    cin.get();
}

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

#include<iostream>
#include<string>
using namespace std;

void main() {
    string str[20];
    string temp;
    const string done = "done";
    int n, i = 0,flag = 1;
    cout << "Enter words (to stop,type the word done): " << endl;

    while (flag)
    {
        getline(cin, temp);
        int end = 0; 
        int start = 0;
        while (temp[end])//temp[end]在判断语句中等同于temp[end]!=‘’
        {
            if (temp[end] != ' ') {
                end++;
            }
            else {
                str[i].assign(temp, start, end - start);//assign用法: string str1, str2 = "War and Peace";    str1.assign( str2, 4, 3 );    -->str1="and"
                i++;
                start = ++end;
            }
        }
        str[i].assign(temp, start, end - start);
        i++;//到末尾时,检测到的是‘’而不是空格,所以在循环体中并没有++,所以需要+1

        for (n = 0; n <= i; n++)
        {
            if (done==str[n]) {
                flag = 0;//break退出for循环,所以需要将flag置0才能跳出while循环
                break;
            }
        }
        temp.clear();
    }
    cout << "You entered a total of " << n << " words" << endl;
    system("pause");
}

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

Enter number of rows: 5 
....* 
...** 
..*** 
.**** 
*****

#include<iostream>
using namespace std;

void main() {
    cout << "Enter number of rows:";
    int rows,count;
    cin >> rows;
    for (int i = 0; i < rows; i++)
    {
        count = rows-1-i;
        for (int j = 0; j < rows; j++)
        {
            if (j < count)
                cout << ".";
            else
                cout << "*";
        }
        cout << endl;
    }
    system("pause");
}

原文地址:https://www.cnblogs.com/little-monkey/p/7505838.html