C++实验二 控制结构和函数 打印温度柱状图

来源:互联网 发布:网络舆情信息综合分析 编辑:程序博客网 时间:2024/05/17 18:02

2.1 打印温度柱状图


下图是某城市 15 天的气温变化曲线。其中标注为 A 的地方称为峰点,标记为 B 的地方称为谷点,而标记为 C 的地方称为崮。要求编写 1 个函数输入 15 天的气温,然后通过 3 个函数分别实现下述功能:

(1)打印每天温度的柱状图(仅考虑温度为正值的情况)。

(2)打印所有峰点的位置(该月的第几天)及峰值。如果没有,则打印没有峰值。

(3)打印最长的崮的长度。只使用一重循环即可求出.

----------------------------------------------------

------------------------------------------------------

/*输入n个温度 11 12 13 11 11 11 11 10 9 13 13 11 16 14 15

*/

void inputTemps( int temp[], int n);

/*显示柱状图

*/

void displayTemps(int temp[], int n) ;

/*显示月间温度中所有的峰值

*/

void displayPeaks(int temp[], int n);

/*显示月间持续最久的温度,

即崮

*/

void displayFlat (int temp[], int n);

//main函数int main(){

int temps[30];

inputTemps(temps, 15);

displayTemps(temps, 15);

displayPeaks(temps, 15);

displayFlat(temps, 15);}

---------------------------------

-----------------------------------

Please input the tempratures:

11 12 13 11 11 11 11 10 9 13 13 11 16 14 15

柱状图如下图所示:

1**********

2***********

3************

4**********

5**********

6**********

7**********

8*********

9********

10************

11************

12**********

13***************

14*************

15**************

显示峰值如下:

Max at day 3 is 13

Max at day 13 is 16

显示崮的长度如下:

The length of longest flat is 4


#include<iostream>using namespace std;/*输入n个温度  11 12 13 11 11 11 11 10 9 13 13 11 16 14 15*/void inputTemps( int temp[], int n) {int i;cout << "Please input the tempratures:" << endl;for (i = 0; i<n; i++) {cin >> temp[i] ;}cout << endl;}/*显示柱状图*/void displayTemps(int temp[], int n) {int i,j;int t = 1;cout << "柱状图如下图所示:" << endl;for (i = 0; i < n; i++){cout << t++;cout << "\t";for (j = 1; j < temp[i]; j++){cout << "*";}cout << endl;}cout << endl;}/*显示月间温度中所有的峰值*/void displayPeaks(int temp[], int n) {int i;for (i = 1; i < n-1; i++){if (temp[i] > temp[i - 1] && temp[i] > temp[i + 1]) {cout << "Max at day " << i+1 << " is " << temp[i] << endl;}}cout << endl;}/*显示月间持续最久的温度*/void displayFlat (int temp[], int n) {int i;int t = 1;int Max = 0;for (i = 0; i < n; i++) {if (temp[i] ==temp[i + 1]) {t = t + 1;}if (temp[i]!= temp[i + 1]) {t = 1;}if (t > Max) {Max = t;}}cout << "The length of longest flat is " << Max << endl;}/*主函数*/int main(){int temps[30];inputTemps(temps, 15);displayTemps(temps, 15);displayPeaks(temps, 15);    displayFlat(temps, 15);}




原创粉丝点击