题目
#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那多看看……