成绩排序"/>
C++学生类和成绩排序
程序最终输出界面是三个部分,1、录入学生成绩,2、查找学生成绩,3、显示全部学生成绩;首先定义一个学生类,将所有的成员信息放入私有数据,再构造函数默认赋值。处理对象,写出所需要的录入学生成绩信息,姓名、学号、数据结构、软件工程;查找学生:
输入对应的学生姓名。使用strcmp字符串比较,相同返回0;输出对应的学生信息。在main函数里先定义类的对象数组,再定义一个switch,设置功能。学生总成绩的排序输出,利用循环按从大到小输出学生的总成绩。
#include<iostream>
using namespace std;
int a = 0;//学生人数
class student
{
private:char *name;long number;int score1;int score2;int sum;
public:student(char *name = NULL, long number = 0, int score1 = 0, int score2 = 0, int sum=0){this->name = name, this->number = number, this->score1 = score1, this->score2 = score2, this->sum=score1+score1;}~student() {} //析构函数void setstudent()//录入学生信息{cout << "学生姓名:" << endl;name = (char *)malloc(20 * sizeof(char));cin >> name;cout << "学生学号:" << endl;cin >> number;cout << "数据结构:" << endl;cin >> score1;cout << "软件工程:" << endl;cin >> score2;}void findstudent(char *n)//查找学生{sum = score1 + score2;if (0 == strcmp(name, n))//字符串比较,相同返回0{cout << "姓名:" << name << "学号:" << number << endl;cout << "数据结构:" << score1 << "软件工程:" << score2 << endl;cout << "总分:" << sum << endl;}}char *getname() { return name; }long getnumber() { return number; }int getscore1() { return score1; }int getscore2() { return score2; }int getsum() { return sum; }
};
int main()
{int c, d;//功能选择char *N;//名字输入student stu[58];//类的对象数组while(1){cout << "请选择服务:1-录入学生成绩;2-查询学生成绩;3-显示所有学生:4-退出:" << endl;//功能选择cin >> c;switch (c){case 1:{for (int i = 0; i <= 58; i++){stu[i].setstudent();//学生录入a++;cout << " '1'-继续,'2'-返回" << endl;cin >> d;if (d == 2) { break; }else if (d == 1) { continue; }}break;}case 2:{cout << "请输入查找学生姓名:" << endl;N = (char *)malloc(20 * sizeof(char));cin >> N;for (int i = 0; i < a; i++){stu[i].findstudent(N);}break;}case 3:{for (int j = 0; j < a; j++){for (int i = 0; i < a - j; i++){if (stu[i].getsum() < stu[i + 1].getsum()){stu[i] = stu[i];stu[i] = stu[i + 1];stu[i + 1] = stu[i];}}}for (int i = 0; i < a; i++){cout << "姓名:" << stu[i].getname() << "学号:" << stu[i].getnumber() << endl;cout << "数据结构:" << stu[i].getscore1() << "软件工程:" << stu[i].getscore2() << "总分:" << stu[i].getscore1()+stu[i].getscore2()<< endl;} break;}case 4:{return 0;break;} }}return 0;
}
更多推荐
C++学生类和成绩排序
发布评论