2021.4.12
1.
/*在主函数中输入一个一维数组,调用函数maxAndMin得到数组元素中的最大值与最小值
* 信1905-2聂旭东20194006
*/
#include <iostream>
using namespace std;
#define  SIZE 10
void maxAndMin(int a[], int& maximum, int& minimum) {
    maximum = a[0];
	minimum = a[0];
	for (int i = 1; i < SIZE; i++) {
		if (a[i] > maximum)
			maximum = a[i];
		if (a[i] < minimum)
			minimum = a[i];
	}
}
int main(){
	int numbers[SIZE], maxValue, minValue, i;
	cout << "Please input " << SIZE << " numbers:";
	for (i = 0; i < SIZE; i++)
		cin >> numbers[i];
maxAndMin(numbers, maxValue, minValue);
	cout << "The maximum is: " << maxValue << endl;
	cout << "The minimum is: " << minValue << endl;
	return 0;
}
2.
/*用C++语言编写函数,使用函数重载,能求两个整数的最大数、三个整数的最大数、四个整数的最大数
* 信1905-2聂旭东20194006
*/
#include<iostream>
using namespace std;
int max(int a, int b){
	if (a > b) 
		return a;
	else return b;
}
int max(int a, int b, int c){
	if (a > b)	{
		if (a > c) 
			return a;
		else return c;
	}
	else	{
		if (b > c) 
			return b;
		else return c;
	}
}
int max(int a[4])
{
	int i, max = a[0];
	for (i = 0; i < 3; i++)	{
		if (max < a[i]) max = a[i];
	}
	return max;
}
void main()
{
	int i, n, x, a[4];
	cout << "请输入你想要的数字:";
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	if (n == 2)
		x = max(a[0], a[1]);
	else if (n == 3)
		x = max(a[0], a[1], a[2]);
	else if (n == 4)
		x = max(a);
	cout << "最大值是:" << x << endl;
}
3.
/*定义一个学生类
*在主函数中定义一个有3个元素的对象数组并分别输入,然后输出对象数组的信息
* 信1905-2聂旭东20194006
*/
#include<iostream>
using namespace std;
class Student
{
public:
	Student();
	void Input(int a, string str);
	void Output();
private:
	int age;
	string name;
};
Student::Student()
{
}
void Student::Input(int a, string str)
{
	age = a;
	name = str;
}
void Student::Output()
{
	cout << "age:" << age << "," << "name:" << name << endl;
}
int main()
{
	int i, age;
	string name;
	Student stus[3];
	for (i = 0; i < 3; i++)	{
		cout << "请输入学生年龄:";
		cin >> age;
		cout << "请输入学生姓名:";
		cin >> name;
		stus[i].Input(age, name);
	}
	for (i = 0; i < 3; i++)	{
		stus[i].Output();
	}
	return 0;
}
                    
                
                
            
        
浙公网安备 33010602011771号