1.输入两个整数,输出两个整数之间所有整数的和,包括两个整数。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int num1, num2,num_left,num_right,sum = 0;
  7.  
  8. cout << "Input two integers:" << endl;
  9.  
  10. cin >> num1 >> num2;
  11.  
  12. //比较大小,从小的开始累加
  13. num_left = num1 < num2 ? num1 : num2;
  14. num_right = num1 > num2 ? num1 : num2;
  15.  
  16. for (int i = 0; (num_left+i) <= num_right; i++)
  17. {
  18. sum += num_left + i;
  19. }
  20.  
  21. cout << "Sum of all integers between the two numbers:" << sum << endl;
  22.  
  23. system("pause");
  24.  
  25. }

2.输入一个整数,计算它的阶乘,要能够计算100的阶乘(使用long double)。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double num;
  7. long double res;
  8.  
  9. cin >> num;
  10.  
  11. res = num;
  12.  
  13. for (int i = 1; num - i >0; i++)
  14. {
  15. res *= (num - i);
  16. }
  17.  
  18. cout << res << endl;
  19.  
  20. system("pause");
  21.  
  22. }

3.每次输入一个数,输出:到目前为止,前面输入的所有数的和。输入0结束。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double input_number, sum = 0;
  7.  
  8. cout << "Input a number to add(input 0 to quit):" << endl;
  9.  
  10. cin >> input_number;
  11.  
  12. while (input_number)
  13. {
  14. sum += input_number;
  15.  
  16. cout << "Until now,the sum of all numbers before: " << sum << endl;
  17. cout << "Input next number to add(input 0 to quit):" << endl;
  18.  
  19. cin >> input_number;
  20. }
  21.  
  22. cout << "Final result:" << sum << endl;
  23. cout << "done." << endl;
  24.  
  25. system("pause");
  26.  
  27. }

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,并显示此时两人的投资价值。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const double ratio_D = 0.1, ratio_C = 0.05;
  5.  
  6. int main()
  7. {
  8. double mon_D = 100, mon_C = 100;
  9. double intst_D = mon_D * ratio_D;
  10.  
  11. int y = 0;
  12. do
  13. {
  14. ++y;
  15. mon_D = mon_D + intst_D;
  16. mon_C = (1+ratio_C)*mon_C;
  17. } while (mon_C < mon_D);
  18.  
  19. cout << "After " << y << " years." << endl;
  20. cout << "Daphne:$" << mon_D << "\tCleo:$" << mon_C << endl;
  21.  
  22. system("pause");
  23.  
  24. }

5.假设销售一本书,用char数组(或string对象数组)提示用户输入一年中所有月份的销售量,将输入的销售量存储在一个int数组中,然后程序计算数组中元素的总和,报告一年的销售情况。

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string prmt[] =
  8. { "January","February","March","April",
  9. "May","June","July","August",
  10. "September","October","November","December"
  11. };
  12.  
  13. int booksales[12],sales_sum = 0;
  14.  
  15. for (int i = 0; i < 12; i++)
  16. {
  17. cout << "Input books sales in " << prmt[i] << ":\n";
  18. cin >> booksales[i];
  19. sales_sum += booksales[i];
  20. }
  21.  
  22. cout << "The whole sales in this year is:" << sales_sum << endl;
  23.  
  24. system("pause");
  25.  
  26. }

6.在第5题的基础上修改程序,使用二维数组来存储输入——3年中每个月的销售量,程序将报告每年的销售量和三年的总销售量。

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. const int years = 3;
  6.  
  7. int main()
  8. {
  9. string prmt[] =
  10. { "January","February","March","April",
  11. "May","June","July","August",
  12. "September","October","November","December"
  13. };
  14.  
  15. int booksales[years][12], sum_py[years] = {},sum_ay = 0;
  16.  
  17. for ( int y = 0; y < years; y++)
  18. {
  19. cout << "\t|| Books sales for " << "YEAR " << y + 1 << " ||\n\n";
  20. for (int m = 0; m < 12; m++)
  21. {
  22. cout << "Input books sales in " << prmt[m] << ":\n";
  23. cin >> booksales[y][m];
  24. sum_py[y] += booksales[y][m];
  25. }
  26. cout << "\nBooks sales in " << "YEAR " << y + 1 << " is:" << sum_py[y] << "\n\n";
  27. }
  28.  
  29. for (int i = 0; i < years; i++)
  30. sum_ay += sum_py[i];
  31.  
  32. cout << "Books sales of " << years << " years:" << sum_ay << endl;
  33.  
  34. system("pause");
  35.  
  36. }

7.设计一个car结构,存储汽车的生产商(字符数组或string对象)和生产年份(整数)。编写程序,向用户询问有多少辆车。随后,程序使用new创建一个由相应数量的car结构组成的动态数组。然后,程序依次提示用户输入生产商和年份。最后,输出所有存储的信息。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct car_product
  5. {
  6. char producer[20];
  7. int year;
  8. };
  9.  
  10. int main()
  11. {
  12. int counts;
  13.  
  14. cout << "How many cars do your wish catalog? ";
  15. cin >> counts;
  16. cin.get(); //清空缓冲区的换行符,防止后面cin.get()停止
  17.  
  18. car_product *ptr = new car_product[counts];
  19.  
  20. for (int i = 0; i < counts; i++)
  21. {
  22. cout << "Car #" << i+1 << ":\n";
  23. cout << "Please enter the make: ";
  24. cin.get(ptr[i].producer,20); //cin.get()会读取整行字符,包括空格,遇到换行符停止
  25. cout << "Please enter the year made: ";
  26. cin >> ptr[i].year;
  27. cin.get(); //同上,清空缓冲区的换行符
  28. }
  29.  
  30. cout << "Here is your collection:" << endl;
  31. for (int i = 0; i < counts; i++)
  32. {
  33. cout << ptr[i].year << " " << ptr[i].producer << endl;
  34. }
  35.  
  36. delete[]ptr;
  37.  
  38. system("pause");
  39.  
  40. }

*要特别注意其中cin和cin.get()的用法。这里学习了输入流的概念,用户的输入都会预先存到缓冲区内,cin有关的输入会先从缓冲区内取数据。cin取数据时,遇到空格或换行符都会停止,取完数据后,会留下换行符。而cin.get()会把空格也读入,遇到换行符停止,同样也会把换行符留在缓冲区内。

*cin.get()不指定读取到哪个对象和长度,会默认读走一个字符,所以也可以起到清空缓冲区的作用。

8.用户输入一系列单词,中间用空格隔开。程序使用char数组存储所有单词,然后统计单词“done”之前有多少个单词。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const int MAXSIZE = 100;
  5.  
  6. int main()
  7. {
  8. char store[MAXSIZE];
  9. int counts = 0;
  10.  
  11. cin.get(store,MAXSIZE);
  12.  
  13. for (int i = 0; i<MAXSIZE ;i++)
  14. {
  15. if (store[i] == ' ')
  16. counts++;
  17. else if ((store[i] == 'd') && (store[i + 1] == 'o') && (store[i + 2] == 'n') && (store[i + 3] == 'e'))
  18. break;
  19. else {};
  20. }
  21.  
  22. cout << "You entered a total of "<<counts<<" words.\n";
  23.  
  24. system("pause");
  25.  
  26. }

9.编写循环嵌套程序,要求用户输入一个值,指出要显示多少行,然后,程序按下面的规律显示,假如输入的是5:

  1. ....*
  2. ...**
  3. ..***
  4. .****
  5. *****

第一行显示一个星号,其余用点补充,下面以此类推,直到第五行显示出五个星号。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int num;
  7.  
  8. cout << "Enter number of rows: ";
  9. cin >> num;
  10.  
  11. for (int i = 0; i < num; i++)
  12. {
  13. for (int j = num-1; j >i; j--)
  14. {
  15. cout << ".";
  16. }
  17.  
  18. for (int k = 0; k <= i; k++)
  19. {
  20. cout << "*";
  21. }
  22.  
  23. cout << "\n";
  24. }
  25.  
  26. system("pause");
  27.  
  28. }

《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. 数据结构与算法系列2 线性表 链表的分类+使用java实现链表+链表源码详解

    数据结构与算法系列2.2 线性表 什么是链表? 链表是一种物理存储单元上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表的链接次序实现的一系列节点组成,节点可以在运行时动态生成,每个节点包括两个 ...

  2. C# 压缩、解压文件夹或文件(带密码)

    今天梳理一下项目中用到的压缩.解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件.今天就好好整理记录下,别下次遇到需求又重头开始了. DotNetZip DotNetZip是一个开源的免 ...

  3. MSP430-LED中断闪烁代码详解

    使用MSP430F149的开发板,首先对LED闪烁灯的例程进行讲解,然后下边是自己写的,将部分代码写入了新建的led.c程序中 #include  <msp430x14x.h>       ...

  4. python3笔记-读取ini配置文件

    在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: [path] back_dir = /Users/abc/PycharmP ...

  5. Left Mouse Button (bfs)

    Mine sweeper is a very popular small game in Windows operating system. The object of the game is to ...

  6. 小程序开发-组件navigator导航篇

    navigator 页面链接 navigator的open-type属性 可选值 navigate.redirect.switchTab,对应于wx.navigateTo.wx.redirectTo. ...

  7. 2020BJDCTF

    diff: 不得不说这种题目挺有意思的,现在记录一下阶梯过程: 先登录远程,发现有两个文件: 虽然直接能卡到flag文件,但是我们是以ctf用户登录的,并不能直接打开flag文件.仔细观察diff文件 ...

  8. 11.redis cluster的hash slot算法和一致性 hash 算法、普通hash算法的介绍

    分布式寻址算法 hash 算法(大量缓存重建) 一致性 hash 算法(自动缓存迁移)+ 虚拟节点(自动负载均衡) redis cluster 的 hash slot 算法 一.hash 算法 来了一 ...

  9. 【源码讲解】Spring事务是如何应用到你的业务场景中的?

    初衷 日常开发中经常用到@Transaction注解,那你知道它是怎么应用到你的业务代码中的吗?本篇文章将从以下两个方面阐述Spring事务实现原理: 解析并加载事务配置:本质上是解析xml文件将标签 ...

  10. Django循环创造div后,对各个div操作后触发事件,传递数据(Django九)

    前面我用for循环创建了div,每个div中有各自的数据以及同样的布局 效果图如下:部分代码如下: 现在,我希望在点击每个div里的发表按钮时,能在js里获取{{problem.pro_id}}以及{ ...