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. MySQL 在高并发下的 订单撮合 系统使用 共享锁 与 排他锁 保证数据一致性

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  2. C# IO流的操作(二)

    文件在操作系统中是以二进制(01)的形式保存到磁盘上的,在C#程序当中,我们可以通过读取流将文件读取到byte[]当中(读到内存中),也可以通过写入流将byte[]写入文件(保存到磁盘上).下面将演示 ...

  3. A Java back-end engineer's study notes

    loveincode's notes 学习工作中的一些记录,收藏. 人气很高的链接库 计算机基础相关笔记 操作系统 , 编译原理 , 计算机网络 , 互联网协议... 常用数据结构与算法 Java 实 ...

  4. Wooden Sticks---(贪心)

    Problem Description There is a pile of n wooden sticks. The length and weight of each stick are know ...

  5. JQuery限制文本框只能输入数字和小数点的方法

    <input type="text" class="txt NumText"  Width="100px"  /> $(func ...

  6. java Api 读取HDFS文件内容

    package dao; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java ...

  7. Java开发岗位面试题归类

    一.Java基础 1. String类为什么是final的. ( 1.由于String类不能被继承,所以就不会被修改,这就避免了因为继承引起的安全隐患: 2.String类在程序中出现的频率比较高,如 ...

  8. SQL Server 2008作业失败:无法确定所有者是否有服务器访问权限

    转自:http://www.mzwu.com/article.asp?id=3592 下午准备将服务器数据导一份到本地,连上服务器找数据库备份文件发现最后一次备份是7月1日,竟然十几天没生成新备份,查 ...

  9. [Day11]接口、多态

    1.接口 (1)接口定义:interface关键字 ,所在文件仍然是.java文件,编译后仍产生.class文件.       定义格式 public interface 接口名{ 抽象方法1: 抽象 ...

  10. Python爬虫实例(四)网站模拟登陆

    一.获取一个有登录信息的Cookie模拟登陆 下面以人人网为例,首先使用自己的账号和密码在浏览器登录,然后通过抓包拿到cookie,再将cookie放到请求之中发送请求即可,具体代码如下: # -*- ...