5.9.1

#include<iostream>
int main() {
using namespace std;
int one, two, temp, sum = ;
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 = ; int main() {
using namespace std;
array<long double, ArSize> factorials;
factorials[] = factorials[] = 1.0;
for (int i = ; i < ArSize; i++)
factorials[i] = i*factorials[i - ];
for (int i = ; i < ArSize; i++)
cout << i << "! = " << factorials[i] << endl;
}

5.9.3

#include<iostream>

int main() {
using namespace std;
int number, sum = ;
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 = ;
do {
deposit_C *= 1.05;
deposit_D += (0.1 * );
++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[] = {
"一月",
"二月",
"三月",
"四月",
"五月",
"六月",
"七月",
"八月",
"九月",
"十月",
"十一月",
"十二月",
};
int sales[];
int sum = ;
for (int i = ; i < ; ++i) {
cout << "请输入第" << months[i] << "的销售额: ";
cin >> sales[i];
}
for (int i = ; i < ; i++) {
sum += sales[i];
}
cout << "这一年的销售额是:" << sum;
}

5.9.6

#include<iostream>
#include<string> int main() {
using namespace std;
string months[] = {
"一月",
"二月",
"三月",
"四月",
"五月",
"六月",
"七月",
"八月",
"九月",
"十月",
"十一月",
"十二月",
};
int sales[][];
int sum_years[] = { ,, };
int sum = ;
for (int i = ; i < ; ++i) {
for (int j = ; j < ; j++) {
cout << "请输入第" << i + << "年," << "第" << months[j] << "的销售额: ";
cin >> sales[i][j];
}
}
for (int i = ; i < ; i++) {
cout << "第 " << i+ << " 年销售额为:";
for (int j = ; j < ; j++) {
sum_years[i] += sales[i][j];
}
cout << sum_years[i] << endl;
}
cout << "三年的总销售额为:";
for (int i = ; i < ; 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 = ; i < car_number; i++) {
cout << "Car # " << i + << 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 = ; 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[];
cout << "Enter words (to stop,type the word done):\n";
int sum;
for (sum=;strcmp(charr,"done");sum++)
{
cin >> charr;
}
cout << "for sure\n";
cout << "You enter a total of " << sum- << " 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 = ; charr!="done"; sum++)
{
cin >> charr;
}
//cout << "for sure\n";
cout << "You enter a total of " << sum - << " 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 = ; i < rows; i++)
{
*(charr+i) = new char[rows];//开辟列
}
for (int i = ; i < rows; i++) {
for (int j = rows - ; j >= ; j--) {
if ((j + i) >= )
charr[i][j] = '*';
else
charr[i][j] = '.';
}
}
for (int i = ; i < rows; ++i) {
for (int j = ; j < rows; ++j)
cout << charr[i][j];
cout << endl;
}
//cout << charr[2][0];
}

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

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

    4.13.1 #include<iostream> struct students { ]; ]; char grade; int age; }; int main() { using n ...

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

    3.7.1 #include<iostream> int main() { using namespace std; ; int height,inch,foot; cout <&l ...

  3. c primer plus(五版)编程练习-第六章编程练习

    1.编写一个程序,创建一个具有26 个元素的数组,并在其中存储26 个小写字母.并让该程序显示该数组的内容. #include<stdio.h> #define SIZE 26 int m ...

  4. c++ primer plus 第五章 课后题答案

    #include <iostream> using namespace std; int main() { ; cout << "Please enter two n ...

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

    1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...

  6. c++primer 第l六章编程练习答案

    6.11.1 #include<iostream> #include<cctype> int main() { using namespace std; char ch; ci ...

  7. c++primer 第二章编程练习答案

    2.7.1 #include<iostream> int main() { using namespace std; ]; ]; cout << "input nam ...

  8. C++Primer 第五章

    //1.表达式语句的作用:执行表达式并丢弃求值结果 ; value + ; //执行,并丢弃结果 //2.复合语句是指用花括号括起来的语句和声明的序列,复合语句称为块.一个块就是一个作用域.块不以分号 ...

  9. c++ primer plus 第二章 课后题答案

    #include<iostream> using namespace std; int main() { cout << "My name is Jiantong C ...

随机推荐

  1. Python函数之—— 装饰器(Day13)

    一.什么是装饰器 顾名思义,装饰器指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 二.装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数 ...

  2. commonAncestor

    commonAncestor 光标或选区所在区域最外层的祖先节点

  3. 026_默认的MapReduce Driver(最小驱动问题)

    1. 最小配置的MapReduce Driver 读取输入文件中的内容,输出到指定目录的输出文件中,此时文件中的内容为: Key---输入文件每行内容的起始位置. Value---输入文件每行的原始内 ...

  4. jpa,jdbc,hibernate/mybatis,数据库驱动

    JPA是规范,hibernate/mybatis是对规范的实现,hibernate/mybatis是对jdbc的封装,也就是说hibernate/mybatis还是会调用jdbc.    我们平时使用 ...

  5. 请求静态文件,返回http状态码405,not allowed

    昨天在首页加了一个链接,点击这个a标签,会进入http://121.43.68.40/boxpro/template/addsite.pdf,测试环境完全没有问题,上传到正式服务器之后,点击A标签,死 ...

  6. iOS_数据存取(一)

    目录: 一.沙盒机制 二.用户偏好设置 三.归档 一.沙盒机制 每个iOS应⽤都有⾃己的应用沙盒(应⽤沙盒就是⽂件系统⽬录),与其他文件系统隔离.应⽤必须待在⾃己的沙盒⾥,其他应用不能访问该应用沙盒的 ...

  7. c++ boost库学习三:实用工具

    noncopyable 大家都知道定义一个空类的时候,它实际包含了构造函数,拷贝构造函数,赋值操作符和析构函数等. 这样就很容易产生一个问题,就是当用户调用A a(“^_^") 或者A c= ...

  8. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

  9. linux下ipython的安装

    第一种:ipython源码安装ipython的源码下载页面为:https://pypi.python.org/pypi/ipython 或者是到git页面下载:https://github.com/i ...

  10. 关于centos7下/etc/sysconfig/目录没有iptables问题

    在新买的centos7服务器中想打开防火墙,采用传统centos6的方式用service iptables restart/stop/status 之后报错: 而在/etc/sysconfig/目录下 ...