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”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...
随机推荐
- [CA]一个证书两个域名
一般一个证书是绑定一个Common name,出于某种测试的需要,我们可能要求一个Site的证书可以是针对2个域名的. 操作如下: 1.CA上CMD输入下面命令,回车: Certutil –setre ...
- 【转】使用unity3d需要注意到细节
原 文:http://cache.baiducontent.com /c?m=9d78d513d9841df41ea6837e7c01a6660e20f6743da7c76508c3e34f84152 ...
- tyvj P1952 Easy(递推+期望)
P1952 Easy 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化一下 ...
- 《University Calculus》-chape5-积分法-积分的定义
这一章节讨论积分的定义以及微积分基本定理. 笔者先前在数学证明专栏中关于高斯定理的证明的开头,给出了一段关于微积分思想的概括,文中提到根据导数(微分)的定义,根据其逆定义来给出积分的定义和计算方法,这 ...
- hdoj 2199 Can you solve this equation?【浮点型数据二分】
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- ng-select ng-options ng-repeat的用法与区别
最近在用angularJS里的ng-select,ng-options,ng-repeat,发现有两点不太方便: 1.当数据复杂时,循环起来比较麻烦 2.首选项如果不设置,就会为空 整理一下它们的用法 ...
- linux系统启动oracle
linux下启动oracle需要两步:一.启动监听 二.启动服务 一.启动监听 监听命令:lsnrctl ,具体使用方法如下 1.lsnrctl status:检查当前监听器的状态 2.lsnrct ...
- Adatper中获取宽高为0的问题
但是我们想在getView()中获取ImageView的宽和高存在问题,在getView()里面刚开始显示item的时候利用ImageView.getWidth() 获取的都是0,为什么刚开始获取不到 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码 上一讲我们创建了一系列的解决方案,我们通过一个例子来看看层与层之间的关系 ...
- 由 argv引出的main参数 分类: C/C++ 2014-11-08 18:00 154人阅读 评论(0) 收藏
我们经常用的main函数都是不带参数的.因此main 后的括号都是空括号.实际上,main函数可以带参数,这个参数可以认为是 main函数的形式参数.C语言规定main函数的参数只能有两个, 习惯上这 ...