首页 > 其他分享 >实验二 控制结构和函数

实验二 控制结构和函数

时间:2022-08-30 20:58:27浏览次数:50  
标签:11 函数 temp int void 控制结构 temps 实验 cout

打印温度柱状图

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//输入n个温度
void inputTemps(int temp[], int n);
//显示月间温度的柱状图
void displayTemps(int temp[], int n);
//显示月间温度中的峰值
void displayPeaks(int temp[], int n);
//显示月间持续最久的温度
void displayFlat(int temp[], int n);
//最低温度
int minTemp(int temp[], int n);
//显示零下温度的柱状图
void subzeroTemps(int temp[], int n);
//求出现次数最多的温度及出现的次数
void maxTimes(int temp[], int n);

int main() {
	int temps[30] = { 11,-12,13,-11,11,11,11,10,-9,13,13,11,-16,14,15 };

	//inputTemps(temps, 30);
	//displayTemps(temps, 15);
	//displayPeaks(temps, 15);
	//displayFlat(temps, 15);
	subzeroTemps(temps, 15);
	//maxTimes(temps, 15);

	return 0;
}

void inputTemps(int temp[], int n) {
	cout << "Please input the tempratures:" << endl;
	for (int i = 0; i < n; i++) {
		cin >> temp[i];
	}
}

void displayTemps(int temp[], int n) {
	cout << "显示柱状图如下:" << endl;
	for (int i = 0; i < n; i++) {
		cout << setw(2) << i + 1 << "     ";
		for (int j = 0; j < temp[i]; j++) {
			cout << '*';
		}
		cout << endl;
	}
}

void displayPeaks(int temp[], int n) {
	cout << "显示峰值如下:" << endl;
	for (int i = 1; i < n - 1; i++) {
		if (temp[i - 1]<temp[i] && temp[i]>temp[i + 1]) {
			cout << "Max at day " << i + 1 << " is " << temp[i] << endl;
		}
	}
}

void displayFlat(int temp[], int n) {
	cout << "显示崮的长度如下:" << endl;
	int num = 1;
	int max_num = 1;
	for (int i = 1; i < n; i++) {
		if (temp[i - 1] == temp[i]) {
			num++;
			if (max_num < num) {
				max_num = num;
			}
		} else {
			num = 1;
		}
	}
	cout << "The length of longest flat is " << max_num << endl;
}

int minTemp(int temp[], int n) {
	int min_temp = temp[0];
	for (int i = 1; i < n; i++) {
		if (temp[i] < min_temp) {
			min_temp = temp[i];
		}
	}
	return min_temp;
}

void subzeroTemps(int temp[], int n) {
	cout << "显示温度柱状图如下:" << endl;
	int min_wide = abs(minTemp(temp, n)) + 3;
	for (int i = 0; i < n; i++) {
		cout.width(3);
		cout << temp[i];
		if (temp[i] < 0) {
			for (int j = 0; j < min_wide - abs(temp[i]); j++) {
				cout << " ";
			}
			for (int j = 0; j < abs(temp[i]); j++) {
				cout << "*";
			}
			cout << "|" << endl;
		} else {
			for (int j = 0; j < min_wide; j++) {
				cout << " ";
			}
			cout << "|";
			for (int j = 0; j < temp[i]; j++) {
				cout << "*";
			}
			cout << endl;
		}
	}
}

void maxTimes(int temp[], int n) {
	int times[100] = { 0 };
	int max_times = 0;
	int max_temp;
	for (int i = 0; i < n; i++) {
		times[temp[i]]++;
	}
	for (int i = 0; i < 100; i++) {
		if (times[i] > max_times) {
			max_times = times[i];
			max_temp = i;
		}
	}
	cout << "出现次数最多的是 " << max_temp << " 度,出现了 " 
		 << max_times << " 次。" << endl;
}

滑块游戏

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

void nextPattern(int n, char pattern[]);
void insertSort(int n, char next[10][10]);

int main() {
	int n;
	int num[10];
	char pattern[10][10];
	cout << "请输入格局数:";
	cin >> n;
	cout << "请输入格局:" << endl;
	for (int i = 0; i < n; i++) {
		cin >> num[i];
		for (int j = 0; j < 2 * num[i] + 1; j++) {
			cin >> pattern[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		cout << "结果_" << i + 1 << endl;
		nextPattern(num[i], pattern[i]);
	}
	return 0;
}

void nextPattern(int n, char pattern[]) {
	bool flag=true;
	int empty;
	int count = 0;
	char temp[10];
	char next[10][10];

	for (int i = 0, j = 0; i < 2 * n + 1; i++) {
		if (pattern[i] != 'E') {
			temp[j++] = pattern[i];
		} else {
			empty = i;
		}
	}
	for (int i = 0; i < 2 * n - 1; i++) {
		if (temp[i] < temp[i + 1]) {
			flag = false;
			break;
		}
	}
	if (flag) {
		cout << "目标格局!" << endl;
	} else {
		for (int i = empty + 3; i >= empty - 3; i--) {
			if ((i != empty) && i >= 0 && i <= 2 * n) {
				strcpy(temp, pattern);
				temp[empty] = temp[i];
				temp[i] = 'E';
				strcpy(next[count], temp);
				count++;
			}
		}
		insertSort(count, next);
		for (int i = 0; i < count; i++) {
			cout << next[i] << endl;
		}
	}
}

void insertSort(int n, char next[10][10]) {
	char temp[10];
	for (int i = 0; i < n - 1; i++) {
		int k = i;
		for (int j = i + 1; j < n; j++) {
			if (strcmp(next[k], next[j]) > 0) {
				k = j;
			}
		}
		if (k != i) {
			strcpy(temp, next[i]);
			strcpy(next[i], next[k]);
			strcpy(next[i], temp);
		}
	}
}

标签:11,函数,temp,int,void,控制结构,temps,实验,cout
From: https://www.cnblogs.com/catting123/p/16640755.html

相关文章

  • Linux系统应用实验一:Linux系统安装与桌面环境使用
    说明:本文结尾提供了本文所有资料下载的链接供读者下载!实验指导书:实验报告:简要的说明和概述一下centos7系统根目录下各个文件目录:bin目录:bin是Bin......
  • 实验一 c++简单程序设计
    题目代码第一题#include<iostream>usingnamespacestd;//求数字根intdigital_root(intn){ while(n>=10){ n=n/10+n%10; } returnn;}in......
  • awk时间函数
    awk时间函数_jalele的博客-CSDN博客_awk生成时间 https://blog.csdn.net/chenglian1987/article/details/52525391awk提供了两个函数来获取时间和格式化时间戳:systime......
  • 关于 JavaScript 函数的思考
    函数可以将一堆重复的代码整合成一个整体,在需要改变的地方通过参数传值来改变。比如,根据类型查询数据,接口返回的数据一样,后续处理这个数据的逻辑也是一样的,只有类型和输入......
  • Python中函数或者类对象带()与不带()的区别——闭包和函数返回时的常见现象
    Python中函数或者类对象带()与不带()的区别-----闭包和函数返回时的常见现象-函数不带括号时,调用的是这个函数本身,是整个函数体,是一个函数对象,不需等该函数执行完成,返回一个......
  • 高斯超几何函数如何运作(数学)
    高斯超几何函数如何运作(数学)Photoby乔什·皮福德on不飞溅计算合流和高斯超几何函数的数值方法(arXiv)作者:约翰·W·皮尔逊,希恩·奥尔弗,梅森·波特抽......
  • seurat单细胞数据分析实现 DimHeatmap函数
     上游分析:https://www.jianshu.com/p/4f7aeae81ef1001、cell<-pbmc[["pca"]]@cell.embeddingscell<-cell[order(cell[,1],decreasing=T),]cell<-rownames(......
  • 作用域与立即执行函数
    应用场景:改变变量作用域;上述代码就是改变变量作用域,一个很好的例子。封装临时变量;加载只需要执行一次的代码,比如显示时间。这些代码也需要一些临时的变......
  • R语言中seq函数
     001、seq(10)seq(2,10,2)##设置起始位置,步长  002、seq(2,10,length=2)##设置返回值的个数seq(2,10,le......
  • 函数
    4、函数1.什么是函数?具备某一功能的函数,事先准备好一个工具2,为何要使用函数?1>.不适应函数组织结构不清晰,可读性差2>.代码冗余,不好维护3>.如何使用函数原则:先定义后......