c++primer 第五章编程练习答案
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 第五章编程练习答案的更多相关文章
- c++primer 第四章编程练习答案
4.13.1 #include<iostream> struct students { ]; ]; char grade; int age; }; int main() { using n ...
- c++primer 第三章编程练习答案
3.7.1 #include<iostream> int main() { using namespace std; ; int height,inch,foot; cout <&l ...
- c primer plus(五版)编程练习-第六章编程练习
1.编写一个程序,创建一个具有26 个元素的数组,并在其中存储26 个小写字母.并让该程序显示该数组的内容. #include<stdio.h> #define SIZE 26 int m ...
- c++ primer plus 第五章 课后题答案
#include <iostream> using namespace std; int main() { ; cout << "Please enter two n ...
- c primer plus(五版)编程练习-第七章编程练习
1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...
- c++primer 第l六章编程练习答案
6.11.1 #include<iostream> #include<cctype> int main() { using namespace std; char ch; ci ...
- c++primer 第二章编程练习答案
2.7.1 #include<iostream> int main() { using namespace std; ]; ]; cout << "input nam ...
- C++Primer 第五章
//1.表达式语句的作用:执行表达式并丢弃求值结果 ; value + ; //执行,并丢弃结果 //2.复合语句是指用花括号括起来的语句和声明的序列,复合语句称为块.一个块就是一个作用域.块不以分号 ...
- c++ primer plus 第二章 课后题答案
#include<iostream> using namespace std; int main() { cout << "My name is Jiantong C ...
随机推荐
- 李治军老师操作系统课程资源分享(视频+pdf)
最近别人推荐,看看了哈工大的李治军老师主讲的操作系统,李治军老师通过linux0.11内核源码的讲解,学习了很多,更加形象了解了理论知识. 分享给大家,有pdf 链接:https://pan.baid ...
- python更新模块
pip install -U 模块名 # 这是 python2+ 版本的用法更新模块 pip3 install -U 模块名 # 这是 python3+ 版本的用法更新模块
- js文件操作
IE下 1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码代码如下: Var fso=new ActiveXObject(Scripting.FileSys ...
- loadrunder之脚本篇——action分类
Action分类 l . Vuser_init 2. Vuser_end 3. Action 在lr中用户的初始化操作应该存放在Vuser_init中.用户的结束操作存放在Vuser_end中.因为 ...
- json教程系列(4)-optXXX方法的使用
在JSONObject获取value有多种方法,如果key不存在的话,这些方法无一例外的都会抛出异常.如果在线环境抛出异常,就会使出现error页面,影响用户体验,针对这种情况最好是使用optXXX方 ...
- springboot获取URL请求参数的几种方法
原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于pos ...
- $.queue() 与 $.dequeue() -- 队列
JQuery 运用队列为动画模块服务,但好像它应该有更多用处,我觉得的,那试试就知道咯. 简单的来讲,它就是形成队列和出列, 也就因此可以进行很有规律的回调和延时了呀(暂停感觉有难度),当然这就是后面 ...
- P4317 花神的数论题
题目 洛谷 数学方法学不会%>_<% 做法 爆搜二进制下存在\(i\)位\(1\)的情况,然后快速幂乘起来 My complete code #include<bits/stdc++ ...
- 简要总结ajax工作原理及优缺点
虽然在实际的项目中使用多种ajax请求,但就其工作原理,优缺点尚未深入总结, 参考:http://www.cnblogs.com/SanMaoSpace/archive/2013/06/15/3137 ...
- 数据结构与算法之美 06 | 链表(上)-如何实现LRU缓存淘汰算法
常见的缓存淘汰策略: 先进先出 FIFO 最少使用LFU(Least Frequently Used) 最近最少使用 LRU(Least Recently Used) 链表定义: 链表也是线性表的一种 ...