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章练习题的更多相关文章

  1. 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 ...

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

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

  3. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  4. 《C++ primer plus》第3章练习题

    注:有的题设条件自己改动了一下,比如英寸英尺改成米和厘米,部分题目自己加了点额外要求. 1.要求用户输入身高(m),用下划线提示用户输入,将身高转化成"米"加"厘米&qu ...

  5. C Primer Plus 第3章 数据和C 编程练习

    1. /* 整数上溢 */ #include <stdio.h> int main(void) { ; unsigned ; /* 无符号整数j像一个汽车里程指示表(形容的太好了,可参考& ...

  6. C++ Primer 5th 第1章 开始

    *****代码在Ubuntu g++ 5.31 / clang++ 3.8(C++11)下编写调试***** 每个C++程序必须有一个main( )函数,main( )函数的返回值也必须是int类型, ...

  7. C++ Primer 笔记 第三章

    C++ Primer 第三章 标准库类型 3.1using声明 例: using namespace atd; using std::cin; 3.2string类型 初始化方式 string s1 ...

  8. C++primer拾遗(第二章:变量和基本类型)

    这是我对c++primer第二章的一个整理总结,算是比较适用于我自己吧,一小部分感觉不用提及的就省略了,只提了一下平时不注意,或者不好记住的内容. 排版太费劲了,直接放了图片格式.从自己的oneNot ...

  9. python第一章练习题

    本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...

随机推荐

  1. windows版redis报错:本地计算机上的Redis服务启动后停止

    解决 1.如果需要临时启动Redis 使用命令:redis-server.exe   redis.windows.conf   --maxheap 200m 说明:200m是指定最大堆内存是200m, ...

  2. 【python】迭代器与生成器到底是什么?看完你就知道

    迭代器跟生成器,与上篇文章讲的装饰器一样,都是属于我的一个老大难问题. 通常就是遇到的时候就去搜一下,结果在一大坨各种介绍博客中看了看,回头又忘记了. 你是不是也是这样呢? 俗话说:好记性不如烂笔头, ...

  3. 截图还在使用QQ的Ctrl + Alt + A 截图?还不会网页长截图?

    截图还在使用QQ的Ctrl + Alt + A 截图?还不会网页长截图?   手机自带快捷键,常常使用组合键进行快速截图编辑发好友.保存等,但是貌似到了电脑截图就出现了一大堆拍屏幕党,不少人需要打开微 ...

  4. Activiti7 学习总结

    什么是工作流? 就是通过计算机对业务流程进行自动化处理,实现多个参与者按照预定义的流程去自动执行业务流程 什么是Activiti? Activiti是一个工作流引擎,开源的架构,基于BPMN2.0标准 ...

  5. JQuery生成图片列表

    <!DOCTYPE html> <html> <head> <title>生成图片列表</title> <style type=&qu ...

  6. 20190917-02Linux网络配置 000 003

    重启网络 最后reboot重启系统 继续ping 自己的window电脑的ip ctrl+C结束 继续在windows电脑上ping虚拟机上的linux系统

  7. Kubernetes-5:搭建企业级私有仓库Harbor

    搭建企业级私有仓库Harbor 安装需求 python版本 >= 2.7 Docker引擎版本 >= 1.10 docker-compose版本 >= 1.6.0 安装环境 一.Py ...

  8. shell 设置进程数运行

    问题描述 在服务器上提交任务时,需要限制运行的核的数目.程序本身是单线程的,但是不同的输入参数需要跑很多组,粗暴的方法是开多个终端,不断地去提交任务.但这比较麻烦,可以用 shell 实现. 基础 首 ...

  9. Django-配置镜像源,虚拟环境详解

    0.配源虚拟 全局配源 阿里云镜像站:https://developer.aliyun.com/mirror/ # 在cmd中操作,查找文件 C:\Users\Administrator>pip ...

  10. adb连接手机

    1. 通过wifi, 利用adb来连接手机. 在pc的cmd中输入命令: adb connect 192.168.1.100 其中adb就是手机的ip. 如果连接成功, 就可以进入android的sh ...