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 ...
随机推荐
- swift笔记——环境搭建及Hello,Swift!
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/enson16855/article/details/29829601 首先要下载XCode6,仅仅有 ...
- jqprint 打印网页 jQuery print plugin
ref://jQuery print plugin <!DOCTYPE html> <html lang="en"> <script src=&quo ...
- Excel 查找某一列中包含指定字符的单元格
网上查找相关内容,个人感觉是另一种形式的过滤喽.有的说用FIND,有的用高级筛选.我查找时如下: 1.新拉一列,标注公式“=ISNUMBER(FIND("宣",B2))”,然后拉至 ...
- range精讲
var ec = range.endContainer endContainer不是一个引用类型 range是引用类型 range经过改变范围之后 var ec2 =range.endContaine ...
- des加密——补齐
下面这个网址(英文)介绍的比较全面. http://www.di-mgt.com.au/cryptopad.html
- CSS实现三角形图标的原理!!!!今天总算弄懂了。
网页中经常有一种三角形的图标,鼠标点一下会弹出一个下拉菜单之类的(之前淘宝也有,不过现在改版好像没有了) 之前以为是个png图标背景,后来在bootstrap中看到有一个图标样式叫做caret的用来实 ...
- Python编程-绑定方法、软件开发
一.绑定方法与非绑定方法 1.绑定方法 绑定给谁,谁来调用就自动将它本身当作第一个参数传入 (1)绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_method( ...
- 使用Java实现八种基本排序
插入排序.选择排序.冒泡排序.希尔排序.堆排序.快速排序.归并排序.基数排序 import java.util.ArrayList; import java.util.List; /** * 排序算法 ...
- iOS下的WiFi开发
iOS下Wi-Fi开发需要添加依赖库SystemConfiguration.framework,在需要使用Wi-Fi信息的控制器下引入头文件#import <SystemConfiguratio ...
- INSPIRED启示录 读书笔记 - 第9章 产品副经理
发现帮手 从本质上讲,产品就是创意,产品经理的职责是想出好点并加以实现.我们需要好点子,有些想法是我们自己的创意,但如果仅依靠自己,就会严重限制创意的发挥 做产品要找公司最聪明的人合作,发现公司里潜在 ...