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. Can’t connect to local MySQL server through socket 解决办法

    启动mysql 报错: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/m ...

  2. Javascript 广告浮动效果在浏览器中间N秒后移动到右下角

    Javascript 广告浮动效果在浏览器中间N秒后移动到右下角 闲着无聊做了一个,本人原创...就是这个页面的广告效果....怎么样???? 刚刚学习的javascript

  3. Python操作——Memcached

    Memcached是一个高性能的分布式内存对象缓存系统,用于Web应用以减轻数据库的负载. 它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. Memcached ...

  4. UI控件之UINavigationController

    ViewController1 *vc1=[[ViewController1 alloc]init]; UINavigationController *nav1=[[UINavigationContr ...

  5. $ListView的优化机制和滑动时数据错乱的讨论

    Refer:http://www.myexception.cn/mobile/1612364.html (一)Android ListView的基本用法 1.创建一个实体类Person,为其添加Get ...

  6. layer满屏/禁止最大化最小化 可以做选择框使用

    1.layer弹窗最大化 var index=layer.open(); layer.full(index); 2.layer禁止最大化最小化 layer.open( [ type:2, title: ...

  7. VSCode eslint校验 tab改为2个空格

    修改:.eslintrc.json

  8. TIJ读书笔记02-控制执行流程

      TIJ读书笔记02-控制执行流程 TIJ读书笔记02-控制执行流程 if-else 迭代 无条件分支 switch语句 所有条件语句都是以条件表达式的真假来决定执行路径,也就是通过布尔测试结果来决 ...

  9. I.MX6Q(TQIMX6Q/TQE9)学习笔记——U-Boot移植

    其实Freescale的BSP移植文档已经将u-boot的移植步骤讲述的非常详细了,但为了以后方便查阅,还是按照自己的理解记录在这里. 获取源码 根据前一篇文章搭建好LTIB环境后就可以非常方便的导出 ...

  10. OpenStack虚拟机创建过程中镜像格式的的变化过程

    Glance用来作为独立的大规模镜像查找服务,当它与Nova和Swift配合使用时,就为OpenStack提供了虚拟机镜像的查找服务,像所有的OpenStack项目一样,遵循以下设计思想: 基于组件的 ...