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. app开发需求文档怎么写

    我们在开发app前都会做需求分析,这个app开发需求文档怎么写呢?一般可以从这几点入手:确定APP方案的目标,APP方案的受众分析,APP开发方案功能设计,APP的操作系统说明方案,APP是是否是原生 ...

  2. SAP后勤模块实施攻略——1.ERP和SAP

    近日接到任务,看完乐立骏老师的SAP后勤模块实施攻略这本书,现在把第一章内容简单整理.第一章讲的是关于ERP和SAP的介绍. 1.ERP E:Enterprise / 企业 R:Resource / ...

  3. SOA和微服务之间的区别

    近几年,我们有很多文章对SOA和微服务之间的不同点和相似点进行了分析.有些人认为SOA有很多地方是值得微服务学习的,而有些人则认为区别对待微服务和SOA会更好.而Neal Ford认为,将单体迁移到面 ...

  4. Spring_HelloWord

    环境:IntelliJ 14 : jdk1.8   Spring操作步骤 1.新建项目---Spring Batch 2.IntelliJ会自动加载jar包 3.现在就可以在src目录下写Java类文 ...

  5. json教程系列(4)-optXXX方法的使用

    在JSONObject获取value有多种方法,如果key不存在的话,这些方法无一例外的都会抛出异常.如果在线环境抛出异常,就会使出现error页面,影响用户体验,针对这种情况最好是使用optXXX方 ...

  6. Oracle表与约束关系

    手动回收表的存储方式: SQL> alter table aux_emp deallocate unused; //回收所有未使用的存储空间 表已更改. 回收aux_emp的存储空间,保留50K ...

  7. HTML系列(3)基本的HTML标签(二)

        本节继续介绍HTML的常用标签.     (1)input标签之文本域(text和textarea).密码域(password): <!DOCTYPE html> <html ...

  8. Django源码剖析

    一.Django底层剖析之一次请求到响应的整个流程 As we all know,所有的Web应用,其本质上其实就是一个socket服务端,而用户的浏览器就是一个socket客户端 #!/usr/bi ...

  9. 20165101刘天野 2017-2018-2 《Java程序设计》第7周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章JDBC与MySQL数据库 JDBC简介 JDBC(Java Databas ...

  10. 适配iOS9问题汇总

    iOS 9适配过程中出现的问题,收集的链接资料供大家学习分享. http://wiki.mob.com/ios9-对sharesdk的影响(适配ios-9必读)/ http://www.cocoach ...