《C++ primer plus》第5章练习题
1.输入两个整数,输出两个整数之间所有整数的和,包括两个整数。
#include<iostream>
using namespace std; int main()
{
int num1, num2,num_left,num_right,sum = 0; cout << "Input two integers:" << endl; cin >> num1 >> num2; //比较大小,从小的开始累加
num_left = num1 < num2 ? num1 : num2;
num_right = num1 > num2 ? num1 : num2; for (int i = 0; (num_left+i) <= num_right; i++)
{
sum += num_left + i;
} cout << "Sum of all integers between the two numbers:" << sum << endl; system("pause"); }
2.输入一个整数,计算它的阶乘,要能够计算100的阶乘(使用long double)。
#include<iostream>
using namespace std; int main()
{
double num;
long double res; cin >> num; res = num; for (int i = 1; num - i >0; i++)
{
res *= (num - i);
} cout << res << endl; system("pause"); }
3.每次输入一个数,输出:到目前为止,前面输入的所有数的和。输入0结束。
#include<iostream>
using namespace std; int main()
{
double input_number, sum = 0; cout << "Input a number to add(input 0 to quit):" << endl; cin >> input_number; while (input_number)
{
sum += input_number; cout << "Until now,the sum of all numbers before: " << sum << endl;
cout << "Input next number to add(input 0 to quit):" << endl; cin >> input_number;
} cout << "Final result:" << sum << endl;
cout << "done." << endl; system("pause"); }
4.Daphne进行单利投资,Cleo进行复利投资,两人都投资100美元。Daphne每年的利息(收益)是:原始存款×0.10,Cleo每年的利息(收益)是:当前存款×0.05。也就是说,Daphne每年固定盈利100×0.10=10美元;Cleo今年投资100美元,按5%盈利,下一年的盈利就是5美元,下下年的盈利就是105×5%=5.25美元。计算多少年后,Cleo的投资价值才能超过Daphne,并显示此时两人的投资价值。
#include<iostream>
using namespace std; const double ratio_D = 0.1, ratio_C = 0.05; int main()
{
double mon_D = 100, mon_C = 100;
double intst_D = mon_D * ratio_D; int y = 0;
do
{
++y;
mon_D = mon_D + intst_D;
mon_C = (1+ratio_C)*mon_C;
} while (mon_C < mon_D); cout << "After " << y << " years." << endl;
cout << "Daphne:$" << mon_D << "\tCleo:$" << mon_C << endl; system("pause"); }
5.假设销售一本书,用char数组(或string对象数组)提示用户输入一年中所有月份的销售量,将输入的销售量存储在一个int数组中,然后程序计算数组中元素的总和,报告一年的销售情况。
#include<iostream>
#include<string>
using namespace std; int main()
{
string prmt[] =
{ "January","February","March","April",
"May","June","July","August",
"September","October","November","December"
}; int booksales[12],sales_sum = 0; for (int i = 0; i < 12; i++)
{
cout << "Input books sales in " << prmt[i] << ":\n";
cin >> booksales[i];
sales_sum += booksales[i];
} cout << "The whole sales in this year is:" << sales_sum << endl; system("pause"); }
6.在第5题的基础上修改程序,使用二维数组来存储输入——3年中每个月的销售量,程序将报告每年的销售量和三年的总销售量。
#include<iostream>
#include<string>
using namespace std; const int years = 3; int main()
{
string prmt[] =
{ "January","February","March","April",
"May","June","July","August",
"September","October","November","December"
}; int booksales[years][12], sum_py[years] = {},sum_ay = 0; for ( int y = 0; y < years; y++)
{
cout << "\t|| Books sales for " << "YEAR " << y + 1 << " ||\n\n";
for (int m = 0; m < 12; m++)
{
cout << "Input books sales in " << prmt[m] << ":\n";
cin >> booksales[y][m];
sum_py[y] += booksales[y][m];
}
cout << "\nBooks sales in " << "YEAR " << y + 1 << " is:" << sum_py[y] << "\n\n";
} for (int i = 0; i < years; i++)
sum_ay += sum_py[i]; cout << "Books sales of " << years << " years:" << sum_ay << endl; system("pause"); }
7.设计一个car结构,存储汽车的生产商(字符数组或string对象)和生产年份(整数)。编写程序,向用户询问有多少辆车。随后,程序使用new创建一个由相应数量的car结构组成的动态数组。然后,程序依次提示用户输入生产商和年份。最后,输出所有存储的信息。
#include<iostream>
using namespace std; struct car_product
{
char producer[20];
int year;
}; int main()
{
int counts; cout << "How many cars do your wish catalog? ";
cin >> counts;
cin.get(); //清空缓冲区的换行符,防止后面cin.get()停止 car_product *ptr = new car_product[counts]; for (int i = 0; i < counts; i++)
{
cout << "Car #" << i+1 << ":\n";
cout << "Please enter the make: ";
cin.get(ptr[i].producer,20); //cin.get()会读取整行字符,包括空格,遇到换行符停止
cout << "Please enter the year made: ";
cin >> ptr[i].year;
cin.get(); //同上,清空缓冲区的换行符
} cout << "Here is your collection:" << endl;
for (int i = 0; i < counts; i++)
{
cout << ptr[i].year << " " << ptr[i].producer << endl;
} delete[]ptr; system("pause"); }
*要特别注意其中cin和cin.get()的用法。这里学习了输入流的概念,用户的输入都会预先存到缓冲区内,cin有关的输入会先从缓冲区内取数据。cin取数据时,遇到空格或换行符都会停止,取完数据后,会留下换行符。而cin.get()会把空格也读入,遇到换行符停止,同样也会把换行符留在缓冲区内。
*cin.get()不指定读取到哪个对象和长度,会默认读走一个字符,所以也可以起到清空缓冲区的作用。
8.用户输入一系列单词,中间用空格隔开。程序使用char数组存储所有单词,然后统计单词“done”之前有多少个单词。
#include<iostream>
using namespace std; const int MAXSIZE = 100; int main()
{
char store[MAXSIZE];
int counts = 0; cin.get(store,MAXSIZE); for (int i = 0; i<MAXSIZE ;i++)
{
if (store[i] == ' ')
counts++;
else if ((store[i] == 'd') && (store[i + 1] == 'o') && (store[i + 2] == 'n') && (store[i + 3] == 'e'))
break;
else {};
} cout << "You entered a total of "<<counts<<" words.\n"; system("pause"); }
9.编写循环嵌套程序,要求用户输入一个值,指出要显示多少行,然后,程序按下面的规律显示,假如输入的是5:
....*
...**
..***
.****
*****
第一行显示一个星号,其余用点补充,下面以此类推,直到第五行显示出五个星号。
#include<iostream>
using namespace std; int main()
{
int num; cout << "Enter number of rows: ";
cin >> num; for (int i = 0; i < num; i++)
{
for (int j = num-1; j >i; j--)
{
cout << ".";
} for (int k = 0; k <= i; k++)
{
cout << "*";
} cout << "\n";
} system("pause"); }
《C++ primer plus》第5章练习题的更多相关文章
- C Primer Plus_第6章_循环_编程练习
1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- 《C++ primer plus》第3章练习题
注:有的题设条件自己改动了一下,比如英寸英尺改成米和厘米,部分题目自己加了点额外要求. 1.要求用户输入身高(m),用下划线提示用户输入,将身高转化成"米"加"厘米&qu ...
- C Primer Plus 第3章 数据和C 编程练习
1. /* 整数上溢 */ #include <stdio.h> int main(void) { ; unsigned ; /* 无符号整数j像一个汽车里程指示表(形容的太好了,可参考& ...
- C++ Primer 5th 第1章 开始
*****代码在Ubuntu g++ 5.31 / clang++ 3.8(C++11)下编写调试***** 每个C++程序必须有一个main( )函数,main( )函数的返回值也必须是int类型, ...
- C++ Primer 笔记 第三章
C++ Primer 第三章 标准库类型 3.1using声明 例: using namespace atd; using std::cin; 3.2string类型 初始化方式 string s1 ...
- C++primer拾遗(第二章:变量和基本类型)
这是我对c++primer第二章的一个整理总结,算是比较适用于我自己吧,一小部分感觉不用提及的就省略了,只提了一下平时不注意,或者不好记住的内容. 排版太费劲了,直接放了图片格式.从自己的oneNot ...
- python第一章练习题
本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...
随机推荐
- Labview学习之路(三)前面板数值控件
首先看一下前面板都有什么数值控件(我用的labview是17年的,其他版本可能会有不同) 我个人将他们分成了六个部分 第一部分 这个部分大家很好理解,数值输入数值输出,时间输入和时间输出,这里我们讲一 ...
- Unity3d UGUI插件之TSTableView
TSTableView是Tacticsoft工作室开发的一款适用于UGUI的列表(Table)插件,设计灵感来源于iOS/Mac的UITableView,提供高复用.高性能的列表,其主要特点是: 采用 ...
- 深入了解Netty【八】TCP拆包、粘包和解决方案
1.TCP协议传输过程 TCP协议是面向流的协议,是流式的,没有业务上的分段,只会根据当前套接字缓冲区的情况进行拆包或者粘包: 发送端的字节流都会先传入缓冲区,再通过网络传入到接收端的缓冲区中,最终由 ...
- 2020JavaWeb实现文件下载
Servlet实现文件下载: package com.demo.test; import org.apache.commons.io.IOUtils; import javax.servlet.Ser ...
- chrome设置跨域访问
1.新建目录 /usr/local/opt/myChromData 2.输入命令行 open -n /Applications/Google\ Chrome.app/ --args --disable ...
- 跟着尚硅谷系统学习Docker-【day05】
day05-20200717 p21.docker容器数据卷容器 就是活动硬盘上面挂载硬盘进行数据的传递. [docker run -it --name dc01 fyr/centos ...
- java-数组的排序
package day02; public class SelectSort { public static void selectSort(int[] arr){ for(int x=0;x< ...
- [Java数据结构]Map的contiansKey和List的contains比较
Map的containskey方法使用哈希算法查找key是否存在,运算时间是常数: List的contains方法是将元素在列表中遍历,运算时间和列表长度有关. 我使用两种不同SQL语句获取两种不同类 ...
- SpringMVC-整合SSM
整合SSM 目录 整合SSM 1. 设计流程 2. 创建一个数据库表 3. 配置依赖 4. 准备项目框架 5. Mybatis层 1. 编写实体类 2. 编写Mapper接口和xml 1. Mappi ...
- IMX6ULL开发板Linux_WIFI驱动实验
1.在迅为i.MX6ULL开发板上使用的是 usb 接口的 RTL8723 wifi 模块,原理图如下所示:可以看到 RTL8723 模块的接口非常简单,只有 DP1 和 DM1 连接到 usb HU ...