c++primer 第五章编程练习答案

5.9.1

#include<iostream>
int main() {
    using namespace std;
    int one, two, temp, sum = 0;
    cout << "input first interger: ";
    cin >> one;
    cout << "input second interger: ";
    cin >> two;
    for (temp = one; temp <= two; ++temp)
        sum += temp;
    cout << "Add from " << one << " to " << two << " is " << sum;
}

5.9.2

#include<iostream>
#include<array>

const int ArSize = 16;

int main() {
    using namespace std;
    array<long double, ArSize> factorials;
    factorials[1] = factorials[0] = 1.0;
    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;
}

5.9.3

#include<iostream>

int main() {
    using namespace std;
    int number, sum = 0;
    cout << "Please input interger number,end with 0." << endl;
    cin >> number;
    while (number) {
        sum += number;
        cout << "Now,sum is " << sum << endl;
        cin >> number;
    }
}

5.9.4

#include<iostream>

const double interest_C = 0.05;
const double interest_D = 0.10;

int main() {
    using namespace std;
    double deposit_D, deposit_C;
    deposit_C = deposit_D = 100.0;
    int year = 0;
    do {
        deposit_C *= 1.05;
        deposit_D += (0.1 * 100);
        ++year;
    } while (deposit_C <= deposit_D);
    cout << "deposit_C is " << deposit_C << endl;
    cout << "deposit_D is " << deposit_D << endl;
    cout << year << " years C‘s deposit more than D";
}

5.9.5

#include<iostream>
#include<string>

int main() {
    using namespace std;
    string months[12] = {
        "一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月",
    };
    int sales[12];
    int sum = 0;
    for (int i = 0; i < 12; ++i) {
        cout << "请输入第" << months[i] << "的销售额: ";
        cin >> sales[i];
    }
    for (int i = 0; i < 12; i++) {
        sum += sales[i];
    }
    cout << "这一年的销售额是:" << sum;
}

5.9.6

#include<iostream>
#include<string>

int main() {
    using namespace std;
    string months[12] = {
        "一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月",
    };
    int sales[3][12];
    int sum_years[3] = { 0,0,0 };
    int sum = 0;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 12; j++) {
            cout << "请输入第" << i + 1 << "年," << "第" << months[j] << "的销售额: ";
            cin >> sales[i][j];
        }
    }
    for (int i = 0; i < 3; i++) {
        cout << "第 " << i+1 << " 年销售额为:";
        for (int j = 0; j < 12; j++) {
            sum_years[i] += sales[i][j];
        }
        cout << sum_years[i] << endl;
    }
    cout << "三年的总销售额为:";
    for (int i = 0; i < 3; i++) {
        sum += sum_years[i];
    }
    cout << sum;
}

5.9.7

#include<iostream>
#include<string>

using namespace std;
struct car
{
    string manufacturer;
    int production_date;
};

int main() {
    int car_number;
    cout << "How many cars do you have? ";
    cin >> car_number;
    car *cars = new car[car_number];
    for (int i = 0; i < car_number; i++) {
        cout << "Car # " << i + 1 << endl;
        cout << "Please enter the make: ";
        cin >> cars[i].manufacturer;
        cout << "Please enter the year made: ";
        cin >> cars[i].production_date;
    }
    cout << "Here is your collection:\n";
    for (int i = 0; i < car_number; i++) {
        cout << cars[i].production_date << "\t" << cars[i].manufacturer << endl;
    }
}

5.9.8

#include<iostream>
#include<cstring>

int main() {
    using namespace std;
    char charr[20];
    cout << "Enter words (to stop,type the word done):\n";
    int sum;
    for (sum=0;strcmp(charr,"done");sum++)
    {
        cin >> charr;
    }
    cout << "for sure\n";
    cout << "You enter a total of " << sum-1 << " words";
}

5.9.9

#include<iostream>
#include<string>

int main() {
    using namespace std;
    string charr;
    cout << "Enter words (to stop,type the word done):\n";
    int sum;
    for (sum = 0; charr!="done"; sum++)
    {
        cin >> charr;
    }
    //cout << "for sure\n";
    cout << "You enter a total of " << sum - 1 << " words";
}

5.9.10

#include<iostream>

int main() {
    using namespace std;
    int rows;
    cout << "Enter number of rows: ";
    cin >> rows;
    char **charr = new char*[rows];//开辟行
    for (int i = 0; i < rows; i++)
    {
        *(charr+i) = new char[rows];//开辟列
    }
    for (int i = 0; i < rows; i++) {
        for (int j = rows - 1; j >= 0; j--) {
            if ((j + i) >= 4)
                charr[i][j] = ‘*‘;
            else
                charr[i][j] = ‘.‘;
        }
    }
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < rows; ++j)
            cout << charr[i][j];
        cout << endl;
    }
    //cout << charr[2][0];
}
时间: 2024-10-12 19:55:42

c++primer 第五章编程练习答案的相关文章

c++primer 第四章编程练习答案

4.13.1 #include<iostream> struct students { char firstname[20]; char lastname[20]; char grade; int age; }; int main() { using namespace std; students student1; cout << "What is your fistname? "; cin.get(student1.firstname, 20).get();

c++primer 第三章编程练习答案

3.7.1 #include<iostream> int main() { using namespace std; const int unit = 12; int height,inch,foot; cout << "please input your height __ inch\b\b\b\b\b\b\b"; cin >> height; inch = height % unit; foot = height / unit; cin.get(

c++ primer plus(第6版)中文版 第九章编程练习答案

首先,说明下环境: linux:fedora14: IDE:eclipse: python:python2.7 python框架:django web服务器:apache web服务器的python模块:mod_wsgi 写在前面: 之前用的windows下面的xampp,写的php后台,现在想转向linux下面的python,跟以前一样,选择apache和eclipse作为自己的开发工具. eclipse的python配置, 参见之前的博客:http://blog.csdn.net/zy416

c++ primer plus(第6版)中文版 第十三章编程练习答案

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include <iostream> #include <cstring> using namespace std; // base class class Cd { char performers[50]; char label[20]; int selections; // number of selections double

c++ primer plus(第6版)中文版 第十二章编程练习答案

第十二章编程练习答案 12.1根据以下类声明,完成类,并编小程序使用它 //12.1根据以下类声明,完成类,并编小程序使用它 #include <iostream> #include <cstring> using namespace std; class Cow{ char name[20]; char * hobby; double weight; public: Cow(); Cow(const char * nm, const char * ho, double wt);

c++ primer plus(第6版)中文版 第七章编程练习答案

第七章编程练习答案 7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 //7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 #include <iostream> using namespace std; double average (unsigned x, unsigned y) { return (2.0 * x * y / (x + y)); } int main () { while (true) { unsigned x, y; cout

java学习之第五章编程题示例(初学篇)

1 /* 2 Animal.java 3 */ 4 package animal; 5 6 public abstract class Animal { 7 public abstract void cry(); 8 public abstract String getanimalName(); 9 } 1 //Dog.java 2 package animal; 3 4 public class Dog extends Animal 5 { 6 7 String aa="旺旺"; 8

C Primer Plus (第五版) 第五章 编程练习

第五章 运算符.表达式和语句 编程练习 1.编写一个程序.将用分钟表示的时间转换成以小时和分钟表示的时间.使用#define或者const来创建一个代表60的符号常量.使用while循环来允许用户重复键入值,并且当键入一个小于等于0的时间时终止循环. #include <stdio.h> #define H 60 int main(void) { //此处注释部分在程序中等价于#define //const int H = 60; int m; printf("请输入分钟(输入0退出

c primer plus(五版)编程练习-第七章编程练习

1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype.h> int main(void){ char ch; int count1,count2,count3; count1 = count2 = count3 = 0;  printf("Enter text to be analyzed(#to terminate):\n"); w