//Student.h
#include <string>
using namespace std;
class Student {
string stuid; //每个学生的学号具有唯一性
string name;
int coursenum; //所学的课程数目
float* coursescore; //指向存储课程成绩数组的指针
float average; //平均成绩
string level; //等级
friend class process;//友元类
public:
Student();
Student(string stuid,string name,int coursenum);
Student(Student&s);//注意深复制
~Student();
void copy(Student& s);//复制学生信息
};
//Process.h
#include"Student.h"
class Process
{
Student S[100]; //数组S用来存放所有学生信息
int count; //count用来存放实际学生个数
public:
Process() { count = 0; }
void luru();//录入学生信息,调用一次可以录入任意多个学生信息,注意学号唯一性
void trans();//根据学生平均成绩计算学生成绩的等级(优 大于等于90分,良 大于等于80分,
//中 大于等于70分,及格 大于等于60分,不及格 小于60分).
void stat();//统计并输出不同等级学生人数
void sort();//按照平均成绩对所有学生信息进行降序排序
void show();//显示输出所有学生信息
};
//Student.cpp
#include"Student.h"
//已知一个Student类,包括学生学号、姓名、所学的课程数目、课程成绩、平均成绩和等级,
//除了构造函数和析构函数以外不含任何其它成员函数。
Student::Student()//默认构造函数 初始化数据成员
{
stuid = "";
name = "";
coursenum = 0;
coursescore = NULL;
}
//浅复制传递值(this->指向正在被操作的对象(被赋值的对象))
Student::Student(string stuid, string name, int coursenum)
{
this->stuid = stuid;
this->name = name;
this->coursenum = coursenum;
this->coursescore = new float[coursenum];//指针
}
//注意深复制传递指针地址(自设的拷贝构造函数)//实现对象深拷贝
Student::Student(Student&s)
{
this->stuid = s.stuid;
this->name = s.name;
this->coursenum = s.coursenum;
this->coursescore = new float[this->coursenum];//指针
for (int i = 0; i < s.coursenum; i++)
this->coursescore[i] = s.coursescore[i];
this->average = s.average;
this->level = s.level;
}
void Student::copy(Student& s)//复制学生信息
{
this->stuid = s.stuid;
this->name = s.name;
this->coursenum = s.coursenum;
if (this->coursescore) delete[] this->coursescore;
this->coursescore = new float[this->coursenum];
for (int i = 0; i < s.coursenum; i++)
this->coursescore[i] = s.coursescore[i];
this->average = s.average;
this->level = s.level;
}
Student::~Student()
{
if (coursescore) delete[]coursescore;
}
//Process.cpp
#include"Process.h"
#include<iostream>
//录入学生信息,调用一次可以录入任意多个学生信息,注意学号唯一性
void Process::luru()
{
int i;
char select;
do//实现多次循环
{
//学号唯一性
string stuid;
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> stuid;
for (i = 0; i < count; i++)
{
if (S[i].stuid == stuid)
{
cout << "该学号已经存在,请重新输入:";
cin >> stuid;//再判断有没有重复
i = -1;//运行到这,i=-1 再经过i++,i=0 实现重复判断
}
}
S[count].stuid = stuid;//count每循环一次加一次,循环多少次S[count]就存储多少数据
cout << "姓名:";
cin >> S[count].name;
cout << "所学的课程数目:";
cin >> S[count].coursenum;
S[count].coursescore = new float[S[count].coursenum];//不确定有多少个数,就新开辟一个空间
float sum = 0;//总分
for (int i = 0; i < S[count].coursenum; i++)
{
cout << "第" << i + 1 << "门的成绩为:";
cin >> S[count].coursescore[i];
sum += S[count].coursescore[i];
}
S[count].average = sum / S[count].coursenum;
cout << "是否要继续录入(Y/N)";
cin >> select;
count++;//第一次count=0,不执行for语句
} while (select == 'Y' || select == 'y');
}
//根据学生平均成绩计算学生成绩的等级(优 大于等于90分,良 大于等于80分,
//中 大于等于70分,及格 大于等于60分,不及格 小于60分)
//该步只判断
void Process::trans()
{
for (int i = 0; i < count; i++)
{
if (S[i].average >= 90)
S[i].level = "优";
if (S[i].average >= 80&& S[i].average < 90)
S[i].level = "良";
if (S[i].average >= 70 && S[i].average < 80)
S[i].level = "中";
if (S[i].average >= 60 && S[i].average < 70)
S[i].level = "及格";
if (S[i].average <60)
S[i].level = "不及格";
}
}
//统计并输出不同等级学生人数
void Process::stat()
{
int A = 0, B = 0, C = 0, D = 0, E = 0;
for (int i = 0; i < count; i++)
{
if (S[i].level == "优")A++;
else if (S[i].level == "良")B++;
else if (S[i].level == "中")C++;
else if (S[i].level == "及格")D++;
else E++;
}
cout << "不同等级学生人数:" << endl;
cout << "优\t" << A << endl;
cout << "良\t" << B << endl;
cout << "中\t" << C << endl;
cout << "及格\t" << D << endl;
cout << "不及格\t" << E << endl;
}
//排序
void Process::sort()
{
int i, j, max;
for (i = 0; i < count - 1; i++)
{
max = i;
for (j = i + 1; j < count; j++)
if (S[j].average > S[max].average) max = j;
if (max != i)
{
Student t;
t.copy(S[max]);
S[max].copy(S[i]);
S[i].copy(t);
}
}
}
//显示输出所有学生信息
void Process::show()
{
if (count == 0)
{
cout << "本系统中无学生信息记录!" << endl;//检查系统中是否有记录return;
return;
}
cout << "学号\t" << "姓名\t" << "课程数\t" << "成绩\t\t" << "平均成绩\t" << "等级\t" << endl;
for (int i = 0; i < count; i++)
{
cout << S[i].stuid << "\t" << S[i].name << "'\t" << S[i].coursenum << "\t";
for (int j = 0; j < S[i].coursenum; j++)
cout << S[i].coursescore[j] << "\t";
cout << "\t" << S[i].average << "\t\t" << S[i].level << endl;
}
}
//main
#include<iostream>
#include"Process.h"
using namespace std;
void showMenu()
{
cout << "" << endl;
cout << "" << endl;
cout << "\t\t1.录入学生信息" << endl;
cout << "\t\t2.计算学生成绩的等级" << endl;
cout << "\t\t3.统计并输出不同等级学生人数" << endl;
cout << "\t\t4.排序" << endl;
cout << "\t\t5.显示输出所有学生信息" << endl;
cout << "\t\t0.退出" << endl;
cout << " " << endl;
}
int main()
{
showMenu();
int select;
Process p1;
//由于是一个菜单可以重复使用所以要设置循环,且先执行循环语句再判断
do
{
cout << "请输入您的选择:";
cin >> select;
switch (select)
{
//1.录入学生信息
case 1:
p1.luru();
cout << "命令1成功执行" << endl;
cout << endl;
break;
//2.计算学生成绩的等级
case 2:
p1.trans();
cout << endl;
cout << "计算完成" << endl;
break;
//3.统计并输出不同等级学生人数"
case 3:
p1.stat();
cout << endl;
break;
//4.排序
case 4:
p1.sort();
cout << "排序完成" << endl;
cout << endl;
break;
//5.显示输出所有学生信息
case 5:
p1.show();
cout << endl;
break;
//0.退出
case 0:
cout << "\t\t已成功退出" << endl;
return 0;
break;
default:
cout << "输入错误,请重新输入"<< endl;
break;
}
} while (true);//这里的true和1等价都是代表 真 当()内的语句为 假 时结束循环
}