继承、类、new指针数组、随机数

该程序使用C++编写,随机生成计算机科学、网络工程和软件工程三个专业的学生成绩,包括英语、数学及专业课分数,并计算平均分。通过switch语句分配学生专业,然后动态创建并输入每个专业学生的成绩,最后按专业展示所有学生的详细成绩。
摘要由CSDN通过智能技术生成

题目

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;
string t = "\t";
class Student
{
protected:
	int Id, Eng, Math;
	double Average;
public:


};
class CS :public Student
{
protected:
	double Ai;
public:
	void Input(int);
	void Show();
};
class NE :public Student
{
protected:
	double Cn;
public:
	void Input(int);
	void Show();
};
class SE :public Student
{
protected:
	double Se;
public:
	void Input(int);
	void Show();
};
void CS::Input(int a)
{
	Eng = rand() % 100 + 1;
	Math = rand() % 100 + 1;
	Ai = rand() % 100 + 1;
	Average = (Eng + Math + Ai) / 3;
	Id = a;
}
void CS::Show()
{
	cout << setw(8) << Id << setw(8) << Eng << setw(8) << Math << setw(8) << Ai << setw(10) << Average << endl;
}
void NE::Input(int a)
{
	Eng = rand() % 100 + 1;
	Math = rand() % 100 + 1;
	Cn = rand() % 100 + 1;
	Average = (Eng + Math + Cn) / 3;
	Id = a;
}
void NE::Show()
{
	cout << setw(8) << Id << setw(8) << Eng << setw(8) << Math << setw(8) << Cn << setw(10) << Average << endl;
}
void SE::Input(int a)
{
	Eng = rand() % 100 + 1;
	Math = rand() % 100 + 1;
	Se = rand() % 100 + 1;
	Average = (Eng + Math + Se) / 3;
	Id = a;
}
void SE::Show()
{
	cout << setw(8) << Id << setw(8) << Eng << setw(8) << Math << setw(8) << Se << setw(10) << Average << endl;
}
int main()
{
	int a, n1 = 0, n2 = 0, n3 = 0;
	srand(time(0));
	for (int i = 1;i < 51;i++)
	{
		a = rand() % 3 % 3;//妙!
		switch (a)
		{
		case 0:n1++;break;//用case记得要break!
		case 1:n2++;break;
		case 2:n3++;break;
		}
	}
	CS** stu1 = new CS * [n1];
	for (int i = 0;i < n1;i++)
	{
		stu1[i] = new CS;
		stu1[i]->Input(i + 1);
	}
	NE** stu2 = new NE * [n2];
	for (int i = 0;i < n2;i++)
	{
		stu2[i] = new NE;
		stu2[i]->Input(i + 1);
	}
	SE** stu3 = new SE * [n3];
	for (int i = 0;i < n3;i++)
	{
		stu3[i] = new SE;
		stu3[i]->Input(i + 1);
	}
	cout << "计科学生成绩:" << endl;
	cout << setw(8) << "学号" << setw(8) << "英语" << setw(8) << "数学" << setw(8) << "智能" << setw(10) << "平均分" << endl;
	for (int i = 0;i < n1;i++)
	{
		stu1[i]->Show();
	}
	cout << "----------------------------------------------------" << endl;
	cout << "网络学生成绩:" << endl;
	cout << setw(8) << "学号" << setw(8) << "英语" << setw(8) << "数学" << setw(8) << "网络" << setw(10) << "平均分" << endl;
	for (int i = 0;i < n2;i++)
	{
		stu2[i]->Show();
	}
	cout << "----------------------------------------------------" << endl;
	cout << "软件学生成绩:" << endl;
	cout << setw(8) << "学号" << setw(8) << "英语" << setw(8) << "数学" << setw(8) << "软件" << setw(10) << "平均分" << endl;
	for (int i = 0;i < n3;i++)
	{
		stu3[i]->Show();
	}
	for (int i = 0; i < n1; i++)                         
	{
		delete stu1[i];
	}
	delete[] stu1;
	for (int i = 0; i < n2; i++)                         
	{
		delete stu2[i];
	}
	delete[] stu2;
	for (int i = 0; i < n3; i++)           
	{
		delete stu3[i];
	}
	delete[] stu3;
	return 0;
}

switch那多看看……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值