1.编写一个要求用户输入两个整数的程序,将程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.

#include<iostream>
using namespace std; void main() {
int m, n,sum=;
cout << "输入两个整数(从小到大):\n";
cin >> m;
cin >> n;
for (int i = m; i <=n; i++)
{
sum += i;
}
cout << sum<< endl;
system("pause");
}

2.使用array对象(而不是数组)和long double(而非long long)重新编写程序清单5.4,并计算100!的值。

#include<iostream>
#include<array>
using namespace std; const int Size = ;//注意是101 void main() {
array<long double, Size> factorials;
factorials[] = factorials[] = ;
for (int i = ; i < Size; i++)
{
factorials[i] = i*factorials[i - ];
}
for (int i = ; i < Size; i++)
{
cout << i << "!=" << factorials[i] << endl;
}
system("pause");
}

3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累积和,输入0时,程序结束

#include<iostream>
using namespace std; void main() {
int n, sum = ;
do {
cin >> n;
sum += n;
cout << "**************" << sum << "***************\n"<<endl;
} while (n != );
//system("pause");
}

4 Daphne以10%的单利投资了100美元。也就是说,每一年的利润是投资的10%,即每年10美元: 利息=0.10*原始存款 而Cleo在第一年投资100美元的盈利是5%,也就是说利息是当前存款的5%,请编写一个程序,计算多少年后Cleo的投资才会超过Daphne的投资价值

题有点没读懂

#include<iostream>
using namespace std; void main() {
double n=0.0, sum = 100.0;
int year = ;
while (n<=)
{
n = sum*0.05;
sum += n;
year++;
}
cout << year << "年后,C的投资价值超过D。此时C的投资价值为" << n << "元。";
system("pause");
}

5.假设要销售《C++ For Fools》一书,请编写一个程序,输入全年中每个月的销售额。程序通过循环,初始化为月份的字符串char*数组逐月进行提示,并将输入的数据储存在一个int数组中,然后,程序计算数组中各种元素的总数。并报告一年的销售情况

#include<iostream>
using namespace std; void main() {
int sale[],sum=;
char *month[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
for (int i = ; i < ; i++)
{
cout << *(month+i) << "销量:";
cin >> sale[i];
sum += sale[i];
} cout << "今年销售书籍" <<sum<< "本。";
system("pause");
}

6.完成编程练习5,但这次一次使用一个二维数组来存储输入——3年中每个月的销售量以及三年的总销售量

#include<iostream>
using namespace std; void main() {
int sale[][], sum[] = {};
char *month[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
for (int i = ; i < ; i++)
{
cout << "请输入第" << (i + ) << "年的销量情况:" << endl;
for (int j = ; j < ; j++)
{
cout << *(month + j) << "销量:\t";
cin >> sale[i][j];
sum[i] += sale[i][j];
}
cout << "第" << (i + ) << "年的销量为"<<sum[i]<<"本。\n"<< endl;
}
cout << "三年总共销售书籍" <<sum[]+sum[]+sum[]<< "本。";
system("pause");
}

7. 设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存在字符数组或string对象的的字符串中)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别的小心,因为它将交替读取数值和字符串。最后程序显示每个结构的内容。程序运行情况如下:

How many cars do you wish to catalog? 2 
Car #1: 
Please enter the make: Hudson Hornet 
Please enter the year made: 1952 
Car #2: 
Please enter the make: Kaiser 
Please enter the year made: 1951 
Here is your collection: 
1952 Hudson Hornet 
1951 Kaiser

#include<iostream>
#include<string>
using namespace std; struct Car{
//char maker[30];//cin.getline(p->maker, 30);
string maker;
int year;
}; void main() {
int num;
cout << "How many cars do you wish to catalog?";
cin >> num;
cin.get();
Car *p = new Car[num];//用了new就要记得用delete
for (int i = ; i < num; i++)
{
cout << "Car #" << (i + ) << ":" << endl;
cout << "Please enter the make:";
getline(cin,p->maker);//可能有多个单词,所以不能用cin
cout << "Please enter the year made:";
cin >> p->year;
cin.get();
p++;
}
p = p - ;//回到原来位置
cout << "Here is your collection:"<<endl;
for (int i = ; i < num; i++)
{
cout << p->year<<" "<<p->maker<<endl;
p++;
}
p = p - ;//回到原来位置
delete []p;
system("pause");
}

可以用更简单的操作,使指针p不用回到原来的地方

#include<iostream>
#include<string>
using namespace std; struct Car{
//char maker[30];//cin.getline(p->maker, 30);
string maker;
int year;
}; void main() {
int num;
cout << "How many cars do you wish to catalog?";
cin >> num;
cin.get();//抵消换行符
Car *p = new Car[num];//用了new就要记得用delete
for (int i = ; i < num; i++)
{
cout << "Car #" << (i + ) << ":" << endl;
cout << "Please enter the make:";
getline(cin,p[i].maker);//可能有多个单词,所以不能用cin
cout << "Please enter the year made:";
cin >> p[i].year;
cin.get();//抵消换行符
}
cout << "Here is your collection:"<<endl;
for (int i = ; i < num; i++)
{
cout << p->year<<" "<< p[i].maker<<endl;
}
delete []p;
system("pause");
}

8.编写一个程序,它使用一个char数组合循环来每次读取一个单词,直到用户输入done为止。随后显示该用户输入了多少个单词。下面是该程序的运行情况: 
Enter words (to stop,type the word done) :
anteater birthday category dumpster 
envy finagle geometry done for sure 
You entered a total of 7 words.

您应在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试。

#include<iostream>
#include<cstring>
using namespace std; void main() {
char s[][];
char s_temp[];
const char s_done[] = "done";
int i=,en=,n =;
cout << "Enter words (to stop,type the word done): " << endl;
while (en == )
{
cin.getline(s_temp, );//输入,存到s_temp中
int j = , k = ;
while (s_temp[j])
{
if (s_temp[j] != ' ')//不是空格时
{
s[i][k] = s_temp[j];
k++;
j++;
}
else//检测到空格时
{
s[i][k] = '\0';
i++;
j++;
k = ;
}
}
s[i][k] = '\0';
i++;//到末尾时,检测到的是‘\0’而不是空格,所以在循环体中并没有++,所以需要+1 for (n = ; n <= i; n++)
{
if (strcmp(s[n], s_done) == )//等于0时两个字符串相等
{
en = ;//break退出for循环,所以需要将en置1才能跳出while循环
break;
}
}
}
cout << "You entered a total of " << n << " words" << endl;
cin.get();
}

9.编写一个程序满足前一个程序,但使用string对象而不是字符数组。请在程序包含头文件string,并使用关系运算符进行比较测试。

#include<iostream>
#include<string>
using namespace std; void main() {
string str[];
string temp;
const string done = "done";
int n, i = ,flag = ;
cout << "Enter words (to stop,type the word done): " << endl; while (flag)
{
getline(cin, temp);
int end = ;
int start = ;
while (temp[end])//temp[end]在判断语句中等同于temp[end]!=‘\0’
{
if (temp[end] != ' ') {
end++;
}
else {
str[i].assign(temp, start, end - start);//assign用法: string str1, str2 = "War and Peace"; str1.assign( str2, 4, 3 ); -->str1="and"
i++;
start = ++end;
}
}
str[i].assign(temp, start, end - start);
i++;//到末尾时,检测到的是‘\0’而不是空格,所以在循环体中并没有++,所以需要+1 for (n = ; n <= i; n++)
{
if (done==str[n]) {
flag = ;//break退出for循环,所以需要将flag置0才能跳出while循环
break;
}
}
temp.clear();
}
cout << "You entered a total of " << n << " words" << endl;
system("pause");
}

10. 编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应的行数的星号,其中 第一行包括一个星号,第二行包括两个星号,依次类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号 前面加上句点。该程序的运行情况如下:

Enter number of rows: 5 
....* 
...** 
..*** 
.**** 
*****

#include<iostream>
using namespace std; void main() {
cout << "Enter number of rows:";
int rows,count;
cin >> rows;
for (int i = ; i < rows; i++)
{
count = rows--i;
for (int j = ; j < rows; j++)
{
if (j < count)
cout << ".";
else
cout << "*";
}
cout << endl;
}
system("pause");
}

[C++ Primer Plus] 第5章、循环和关系表达式(二)课后习题的更多相关文章

  1. C++ primer plus读书笔记——第5章 循环和关系表达式

    第5章 循环和关系表达式 1. cout.setf(ios_base::boolalpha); cout << (100 > 3) << endl;将输出true,而不是 ...

  2. 《C++ Primer Plus》第5章 循环和关系表达式 学习笔记

    C++提供了3种循环: for 循环. while 循环 和 do while 循环 .如果循环测试条件为 true 或非零,则循环将重复执行一组指令: 如果测试条件为 false 或 0 , 则结束 ...

  3. 《C++ Primer Plus》读书笔记之三—循环与关系表达式

    第五章 循环与关系表达式 1.表达式是值或者值与操作符的结合,每个C++表达式都有值.表达式到语句的转换只要加一个分号就可以完成.但是,反过来,从语句中删除分号,并不一定能将它转化成表达式. 2.前缀 ...

  4. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  5. [C++ Primer Plus] 第5章、循环和关系表达式(一)程序清单——指针自加减优先级

    程序5.4 #include<iostream> using namespace std; ; void main() { long long factorials[Size]; fact ...

  6. c++primerplus(第六版)编程题——第5章(循环和关系表达式)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  7. [C++ Primer Plus] 第8章、函数探幽(二)课后习题

    1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数.不过,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第 ...

  8. 学习C++.Primer.Plus 5 循环和关系表达式

    C++将赋值表达式的值定义为左侧成员的值 赋值操作符是自右向左结合的 cout.setf(ios:: boolalpha);//调用设置标记,命令cout输出true或false,而非1或0. 任何表 ...

  9. C++ Primer Plus读书笔记(五)循环和关系表达式

    1.前缀运算符的优先级是从右到左,例如: 1 *++p; //这句话的含义是先对p进行++,再取* 2.循环 c++11新增了一种循环方式,有点和python类似,如下: 1 array<int ...

随机推荐

  1. JDBC driver for MySQL连接提示"The connection property 'zeroDateTimeBehavior' acceptable values are: 'CONVERT_TO_NULL', 'EXCEPTION' or 'ROUND'. The value 'convertToNull' is not acceptable."解决方案

    1.使用了8.0以上版本的JDBC driver for MySQL,降回5.x版本. 2.不对JDBC driver for MySQL降级.修改数据库连接字符串,添加serverTimezone属 ...

  2. linux安装jdk和tomcat命令

    1.linux centos6.5 安装jdk1.在/usr/local/src目录下,创建java文件夹,拷贝jdk安装包到/usr/local/src/java下面:cd /usr/local/s ...

  3. Vue.js中滚动条加载更多数据

    本文章参考:http://www.cnblogs.com/ssrsblogs/p/6108423.html 分析:1.需要判断滚动条是否到底部: 需要用到DOM的三个属性值,即scrollTop.cl ...

  4. unicode 与 utf-8 编码概念及区别

    unicode 是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案.每个字符都对应一个编号,编号的范围是0-0x10FFFF来.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为 ...

  5. 利用mimikatz破解远程终端凭据,获取服务器密码

    测试环境:windows 10 道友们应该碰到过管理在本地保存远程终端的凭据,凭据里躺着诱人的胴体(服务器密码),早已让我们的XX饥渴难耐了.但是,胴体却裹了一身道袍(加密),待老衲操起法器将其宽衣解 ...

  6. 查看linux系统的运行级别

    查看当前系统的运行级别[root@apenglinux ~]# runlevel3 5查看系统的默认级别[root@apenglinux ~]# systemctl get-defaultgraphi ...

  7. MAVEN_day03 整合SSH框架

    一.整合SSH工程环境准备 1.创建MAVEN工程>>添加>>"web.xml"文件解决工程红色叹号. new Maven Project>>在 ...

  8. docker基本知识

  9. Django进阶之查询优化、extra注入SQL及批量创建

    Django查询优化 Django的查询优化用到两个函数——select_related()和prefetch_related(). select_related()用的是连表join的方式,主要处理 ...

  10. vue中使用scss

    之前项目里我一般是使用less的,朋友问到如何引入scss,于是我就简单的跑了一下,以下主要供自己学习,如有更好的方法可以一起交流讨论一下 第一步,安装依赖 cnpm install node-sas ...