c++primerplus(第六版)编程题——第5章(循环和关系表达式)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。
(具体方式参见第3章模板)
1. 编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。
#include <iostream>
using namespace std; void cprimerplus_exercise_5_1()
{
cout << "Please input two integers!" <<endl;
int a , b, c;
int sum = 0;
cout << "Please input the smaller one:";
cin >> a;
c = a;
cout <<"Please input the larger one:";
cin >> b;
for ( a; a <= b; a++)
{
sum += a;
} cout << "The sum of numbers between " << c << " and " << b << " is "<< sum << endl; }
2.使用array对象(不是数组)和long double(不是long long)重新编写程序清单5.4,并计算100!的值。(编译器不支持array)
#include <iostream>
#include <array>
using namespace std;
const int ArSize = 100;
void cprimerplus_exercise_5_2() //have a problem
{
array<long double, 100>arr;
arr[1] = arr[0] = 1; for( int i = 2; i <= ArSize; i++)
arr[i] = i * arr[i-1]; for (int i = 0; i <= ArSize; i++)
{
cout << i << " != "<< arr[i] << endl;
}
}
3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累积和。当用户输入0是结束程序。
#include <iostream>
using namespace std; void cprimerplus_exercise_5_3()
{ int a = 0;
cout << "Please input a number:";
cin >> a;
int sum = 0;
int cnt = 0; while (a != '\0')
{
sum += a;
++ cnt;
cout << "utile now, the total sum of "<< cnt <<" numbers is " << sum << endl; cout << "Please input a number:";
cin >> a;
} }
4. Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元;而Cleo以5%的复利投资100美元,也就是说,利息是当前存款(包括获得的利息)的5%;请编写一个程序,计算多少年以后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
#include <iostream>
using namespace std;
const double simplerate = 0.1;
const double compoundrate = 0.05;
const int principal = 100;
void cprimerplus_exercise_5_4()
{
double sum1 = principal;
double sum2 = 0.0;
int year = 0;
while (sum2 < sum1)
{
++year;
sum1 +=10;
sum2 = (principal + sum2) * compoundrate + sum2; } cout << "After" << year << "years, Cleo's investment income can surpass Daphne!" <<endl;
cout << "At the time, Cleo's income is "<< sum2 << " , while Daphne's income is " << sum1 << endl;
//system("pause");
}
5. 假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据储存在int数组中。然后,程序计算数组中个元素的总数,并报告这一年的销售情况。
#include <iostream>
using namespace std;
void cprimerplus_exercise_5_5()
{
const char* months[12] = { "January", "February", "March", "April",\
"May", "June", "July", "August",\
"September", "October", "November", "December"}; int salesnumber[12], sum = 0;
for (int i = 0; i < 12; i++)
{
cout << "Please input the " << *(months + i) << " sales numbers:"<< endl;
cin >> salesnumber[i];
cin.get();
sum += salesnumber[i];
} cout << "The total sales number of this year is " << sum << endl;
}
6.完成编程练习5,当这一次使用一个二维数组来储存输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
#include <iostream>
using namespace std;
void cprimerplus_exercise_5_6()
{
const char* months[12] = { "January", "February", "March", "April",\
"May", "June", "July", "August",\
"September", "October", "November", "December"};
const char* years[3] = { "First year", "Second year", "Third year"}; int salesnumber[3][12], sum = 0, tmp =0, year_sale[3];
for (int i = 0; i < 3; i++)
{
cout << "Please input the " << *(years + i) << " years every month's numbers:"<< endl;
for (int j = 0; j < 12; j++)
{
cout << *( months + j) << " sales number is :";
cin >> salesnumber[i][j];
cin.get();
tmp += salesnumber[i][j];
}
year_sale[i] = tmp;
tmp = 0; sum += year_sale[i]; }
for (int i = 0; i < 3; i++)
{
cout << *(years + i) << "year sales number is " << year_sale[i] <<endl;
}
cout << "The total sales number of this year is " << sum << endl;
}
7.设计一个名为car的结构,用它储存下述有关汽车的信息;生产商(储存在字符数组或string对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个有相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能有多个单词组成)和年份信息。请注意:这需特别小心,因为它将交替读取数值和字符串。最后,程序将显示每个结构的内容。
#include <iostream>
#include <string> using namespace std; void cprimerplus_exercise_5_7()
{
struct car
{
string carmaker;
//char carmaker[20];
int makeyear;
}; cout << "How many cars do you wish to catalog?";
int num;
cin >> num;
cin.get(); car *cars = new car[num]; for (int i = 0; i < num; i++)
{
cout << "Car #" << i+1 << ":"<< endl; cout << "Please enter the maker:";
//cin >> cars[i].carmaker;
getline(cin, cars[i].carmaker); cout << "Please enter the year made:" ;
cin >> cars[i].makeyear;
cin.get();
} cout << "Here is your collection:" << endl;
for (int i = 0; i< num; i++)
{
cout << cars[i].makeyear << '\t' << cars[i].carmaker << endl;
}
}
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入多少个单词(不包括done在内)。你应在程序中包含头文件cstring.h,并使用函数strcmp()来进行比较测试。
#include <iostream>
#include <cstring> using namespace std; void cprimerplus_exercise_5_8()
{
cout << "Enter words (to stop, type the word 'done'):"<<endl;
char words[20];
cin >> words;
int cnt = 0; while (strcmp(words,"done") != 0)
{
++cnt;
cin >> words; } cout << "you entered a total of " << cnt << " words." << endl;
}
9.编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
#include <iostream>
#include <string> using namespace std; void cprimerplus_exercise_5_9()
{
cout << "Enter words (to stop, type the word 'done'):"<<endl;
string words;
cin >> words; int cnt = 0; while (words != "done")
{
++cnt;
cin >> words; } cout << "you entered a total of " << cnt << " words." << endl;
}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,以此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句号。
#include <iostream> using namespace std; void cprimerplus_exercise_5_10()
{
cout << "Enter number of rows:";
int rows;
cin >> rows;
cin.get();
for (int i = 1; i <= rows; i++)
{
for( int j = 0; j < rows - i; j++)
{
cout << ".";
}
for (int k = 0; k < i; k++)
{
cout << "*";
}
cout << endl; } }
c++primerplus(第六版)编程题——第5章(循环和关系表达式)的更多相关文章
- c++primerplus(第六版)编程题——第4章(复合类型)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- c++primerplus(第六版)编程题——第3章(数据类型)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. 工程命名和文件命名可以命名成易识 ...
- 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)
第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...
- C程序设计(谭浩强)第五版课后题答案 第一章
大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...
- c primer plus(五版)编程练习-第七章编程练习
1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...
- 编程题:利用for循环打印 9*9 表?
利用for循环打印 9*9 表? 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 ...
- PMP 项目管理第六版- 组织治理与项目治理之间的关系
组织治理: 1.组织治理通过制定政策和流程,用结构化方式指明工作方向并进行控制,以便实现战略和运营目标. 2,组织治理通常由董事会执行,以确保对相关方的最终责任得以落实,并保持公平和透明. 项目治理: ...
- C算法编程题(六)串的处理
前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...
随机推荐
- 【转】VC++ MFC 常用技巧(一)
原文网址:http://www.lewensky.cn/read.php/106.htm (-). 下面是常见的Afx全局函数: AfxFormatString1:类似printf一般地将字符串格式化 ...
- 什么要缓存curl资源
在看公司的代码框架底层时,发现了一个问题,如下: 代码中调用接口时,使用的是curl,框架将curl资源以IP :端口的形式缓存了下来,例如: 10.10.10.10:80 curl1 10. ...
- C#中HashTable的用法示例1
一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,其中 ...
- 访问者模式(Visitor)
@@@模式定义: 表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下 定义作用于这些元素的新操作. @@@练习示例: 扩展客户管理的功能 @@@示例代码: \patter ...
- 圣诞节来了,雪花纷飞的CSS3动画
原文链接:http://www.html5think.com/article/index/id/80
- 提高你30%的设计效率的PPT快捷键
在编辑幻灯片的状态下: [Ctrl]+[A]选择全部对象或幻灯片 [Ctrl]+[B]应用(解除)文本加粗 [Ctrl]+[C]复制 [Ctrl]+[D]快速复制对象 [Ctrl]+[E]段落居中对齐 ...
- cocos2d-x创建精灵动画
创建动画一般过程: 1.创建精灵框架缓存,并向其中添加相应的动画文件(plist),最后,通过动画集缓存生产动画 CCSpriteFrameCache *cache = CCSpriteFrameCa ...
- POJ3723 Conscription
http://poj.org/problem?id=3723 这题虽然简单,但是还是错了很多次. 因为这题构建的图可能是不连通的.也就是说可能有很多棵树. 所以我以前写的并查集用在这上面会出问题的. ...
- 怎么进行robot检測
服务端能够通过三种途径进行robot检測: 第一种,利用http的User-Agent header进行推断,这样的是最正常的推断,但这样的不能检測出不友好的请求,它能够伪造. 另外一种,限制请求频率 ...
- PIC16F877A最小功能板 - 原理图系列
一.顶层 主要由port转换.MCU.复位.键盘.晶振和显示等5部分电路组成. 二.模块层 1. port转换电路 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZ ...