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

//ALL RIGHTS RESERVED.
//Copyright by Xt


//5.设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。 程序通过循环,使用初始化为月份字符串的char数组(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中 然后,程序计算数组中各元素的总数,并报告这一年的销售情况。  *
const int Month = 12;
int main()
{   
    using namespace std;
    char *month[Month] = {"January","February","March","April","May","June","July",
                         "August","September","Octorber","November","December"};
    // Additional access to the definition about varible array " month[]"
    string month_add1[Month]={"January","February","March","April","May","June","July",
                         "August","September","Octorber","November","December"};

    char month_add2[Month][20]={"January","February","March","April","May","June","July",
                         "August","September","Octorber","November","December"};
    // the majority of this code.
    int sold[Month];
    int sum_sales = 0;
    for (int i = 0; i < Month; ++i) {
        cout << "plz input the sales of " << month[i] << '\n';
        cin >> sold[i];
        sum_sales += sold[i];
    }
    for (int i = 0; i < Month; ++i) {
        cout << "the sales of " << month[i] << "is : \t" << sold[i] << '\n';    
    }
    cout << "the total sales of whole year is " << sum_sales << '\t';
    system("pause");
}

//6完成编程练习4,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。将报告每年的销售量以及 三年的总销售量.
const int Month = 12;
const int Year = 3;
int main()
{   
    using namespace std;
    string month[Month] =  { 
        "January","February","March","April","May","June","July","August","September","Octorber","November","December" 
    };
    int sold[Year][Month];
    int sum_sales = 0;
    int sum_singleyear[Year] = {0};
    for (int j = 0; j < Year; ++j) {
        for (int i = 0; i < Month; ++i) {
            cout << "plz input the sales of " << j+1<<"th year/ "<<month[i] << '\n';
            cin >> sold[j][i];
            sum_singleyear[j] += sold[j][i];
        }
        sum_sales += sum_singleyear[j];
    }
    for (int j = 0; j < Year; ++j) {
            cout << "the sales of " << j+1 <<"th year is \t" << sum_singleyear[j] << '\n';
    }
    cout << "the total sales of three years is " << sum_sales << '\t';
    system("pause");
}

//7.设计一个名为car的结构,用它存储下述有关汽车的信息: 生产商(存储在字符数组或string对象中的字符串)、 
//生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。 
//接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取 
//数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:
struct car {
    char broad[20];
    int made_year;
};
int main()
{   
    using namespace std;
    cout << "how many cars do you want to catalog?\n";
    int n;
    (cin>>n).get();
    car *p = new car[n];
    for (int i = 0; i < n; ++i) {
        cout << "car #" << i + 1 << endl;
        cout << "plz enter the make: " ;
        cin >> p[i].broad;
        //cin.getline(p[i].broad, 20); another approach to input.
        cout << "plz enter the year: ";
        cin >> p[i].made_year;
    }
    cout << "here is your collection:\n";
    for (int i = 0; i < n; ++i) {
        cout << p[i].made_year << " " << p[i].broad<<endl;
    }
    delete[] p;
    system("pause");
}

//8  &  9
// 编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入 
//了多少个单词(不包括done在内)。下面是该程序的运行情况: 
//Enter words (to stop, type the word done): 
//anteater birthday category dumpster 
//envy finagle geometry done for sure 
//You entered a toal of 7 words. 
//您应该在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试 
int main()
{   
    using namespace std;
    char ch[20];
    int count = 0;
    int i = 0;
    cout << "please input words:(use the word 'done' to quit)\n";
    while (cin>>ch&&strcmp(ch,"done")) {
        i++;
    cout << ch<<'\0';
    }
    cout << endl;
    cout << "You entered a toal of " << i <<" words\n"<< endl;
    
    system("pause");
}
//9
int main()
{
    using namespace std;
    string ch;
    int count = 0;
    cout << "please input the word:(use the word <done> to quit)\n";
    cin >> ch;
    while (ch != "done") {
        ++count;
        cout << ch;
        cin >> ch;
        cout << endl;
    }
    cout <<"you just have typed the total of "<< count<<" words" << endl;
    system("pause");
}

//10 编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应的行数的星号,其中 
 //第一行包括一个星号,第二行包括两个星号,依次类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号 
//前面加上句点。该程序的运行情况如下:
//Enter number of rows: 5 
//   ....* 
//   ...** 
//   ..*** 
//   .**** 
//   ***** 
int main()
{
    using namespace std;
    cout << "Enter number of rows: \n";
    int row;
    cin >> row;
    for (int i = 0; i < row; ++i) {
        for (int j = row-1; j > i ; --j) {
            cout << ".";
        }
        for (int j = 0; j <= i; ++j) {
            cout << "*";
        }
        cout << endl;
    }
    system("pause");
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容