C++ Primer Plus(第6版)Chapter 5 编程题答案

来源:互联网 发布:解锁姿势体验知乎 编辑:程序博客网 时间:2024/06/08 05:31

C++ Primer Plus(第6版)Chapter 5 编程题答案

第1题:

#include <iostream>#include <string>#include <array>#include <cstring>using namespace std;// task 1int main(){    int i, j;    int sum = 0;    cout << "Enter two int: ";    cin >> i;    cin >> j;    for (int x = i; x <= j; x++)    {        sum += x;    }    cout << "The sum of range(" << i << ", " << j << ") is " << sum << endl;    cin.get();    cin.get();    return 0;}

第2题:

// task 2int main(){    const int size = 101;    array<long double, size> lda = {0};    lda[1] = lda[0] = 1;    for (int i = 2; i < size; i++)    {        lda[i] = i*lda[i - 1];    }    cout << "100! = " << lda[100] << endl;    cin.get();    cin.get();    return 0;}

第3题:

// task 3int main(){    int num;    int sum = 0;    cout << "Enter the numbers: ";    cin >> num;    while (num)    {        sum += num;        cout << "Now sum is " << sum << " continue: ";  //每次输入都显示目前为止所有输入的累积和        cin >> num;    }    cin.get();    cin.get();    return 0;}

第4题:

// task 4int main(){    double d, c;    d = c = 100;    int years = 0;    while (c <= d)    {        d += 10;        c = c + c*0.05;        years++;    }    cout << years << " years later, Now d is " << d << " and c is " << c << endl;    cin.get();    cin.get();    return 0;}

第5题:

// task 5int main(){    int sales[12];    int sum = 0;    string months[12] = {        "Jan",        "Feb",        "Mar",        "Apr",        "May",        "Jun",        "Jul",        "Aug",        "Sep",        "Oct",        "Nov",        "Dec"    };    cout << "Enter the sale of 12 months: ";    for (int i = 0; i < 12; i++)    {        cin >> sales[i];        cout << "The sale of month " << months[i] << " is " << sales[i] << endl;        sum += sales[i];    }    cout << "The total sales of the year is " << sum << endl;    cin.get();    cin.get();    return 0;}

第6题:

// task 6int main(){    int sales[3][12];    int i, j, sum1, sum2;    sum1 = sum2 = 0;    cout << "Enter the sale of months of 3 years: ";    for (i = 0; i < 3; i++)    {        for (j = 0; j < 12; j++)        {            cin >> sales[i][j];            sum2 += sales[i][j];        }        sum1 += sum2;        cout << "The " << i + 1 << "st year sales is " << sum2 << endl;        sum2 = 0;    }    cout << "The sale of 3 years is " << sum1 << endl;    cin.get();    cin.get();    return 0;}

第7题:

// task 7struct car{    char maker[20];    int year;};int main(){    int num;    cout << "How many cars do you wish to catalog? ";    cin >> num;    car* pt = new car[num];    for (int i = 1; i <= num; i++)    {        cin.get();  //吃掉之前那个换行        cout << "Car #" << i << endl;        cout << "Please enter the make: ";        cin.getline((pt + (i - 1))->maker, 20);        cout << "Please enter the year made: ";        cin >> (pt + (i - 1))->year;    }    cout << "Here is your collection:\n";    for (int i = 0; i < num; i++)    {        cout << (pt + i)->year << "\t" << (pt + i)->maker << endl;    }    delete[] pt;  //别忘了释放分配的堆内存    cin.get();    cin.get();    return 0;}

第8题:

// task 8int main(){    const int size = 10;    char word[size];    int count = 0;    cout << "Enter words (to stop, type the word done):\n";    cin >> word;    while (strcmp(word, "done"))    {        count++;        cin >> word;    }    cout << "You entered a total of " << count << " words" << endl;    cin.get();    cin.get();    return 0;}

第9题:

// task 9int main(){    string word;    int count = 0;    cout << "Enter words (to stop, type the word done):\n";    cin >> word;    while (word != "done")    {        count++;        cin >> word;    }    cout << "You entered a total of " << count << " words" << endl;    cin.get();    cin.get();    return 0;}

第10题:

// task 10int main(){    int lines;    cout << "Enter the num of the lines: ";    cin >> lines;    for (int i = 1; i <= lines; i++)    {        for (int j = 1; j <= lines - i; j++)        {            cout << ".";        }        for (int m = 1; m <= i; m++)        {            cout << "*";        }        cout << endl;    }    cin.get();    cin.get();    return 0;}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我,悟道树,从洪荒穿越到武侠 遮天之时之虫 武道从大佛果实开始 原来我真的是剑仙 国姓窃明 四合院之激情岁月 特种岁月之弹道无声 我一个网约车司机有点钱怎么了? 烽火十国 猎人之黑龙咆哮 从墓综世界开始:金乌耀世 奶爸回到乡村搞养殖 火影:木叶村的原神玩家 海贼之全员恶人 终极世界:开局盘点潜力榜 死神里的疾风剑豪 将军,夫人喊你种田了 地星穿越,我从满级国术开始进化 四合院的何大爷 请不要打扰我炼器姬 霍格沃茨之纯血荣耀 亮剑:无限模拟人生,秀翻晋西北 灌篮:我的湘北留学生涯 我在全球游戏植树种田 红云:立大乘佛教成圣 从沙忍开始征服万界 兴趣使然海军大将 我怎么又给华娱续命了? 从超神开始:全都是我聊天群 这不是怪谈 美漫:我在大萧条画超人 乾坤说书人 丑丫修真记 华娱另类导演 我的银幕年华 又见九叔 捉妖局 我在大明观天象 重生年代剧:从四合院开始 道观:开局求雨被拍成了纪录片 和相亲对象被封在一起之后