想要实现的功能
1,可以增加学生的信息,包括(姓名,学号,c++成绩,高数成绩,英语成绩)
2,可以删除学生信息
3,修改学生信息
4,显示所有学生信息
5,查询学生信息
6,把所有学生根据平均成绩进行排名
注意:以下写作流程是按照思考的顺序书写的,如果想看完整代码,看最后!!!
大致思路:
首先创建一个Student类,这类里面包含学生的姓名,学号,c++成绩,高数成绩,英语成绩。在创建一个班级类,将学生类作为成员变量,这样每一个学生就可以放到班级类这个里面。
class Student {
private:
string _name;
string _stuNum;
double _gradeC;
double _gradeM;
double _gradeE;
public:
Student(string name = "", string stuNum = "", double gradeC = 0, double gradeM = 0, double gradeE = 0)
:_name(name)
, _stuNum(stuNum)
, _gradeC(gradeC)
, _gradeM(gradeM)
, _gradeE(gradeE)
{}
}
class StudentClass {
private:
Student* _arr;
int _useSize;
int _capacity;
public:
StudentClass(int capacity = 4) {
_arr = new Student[capacity];
_useSize = 0;
_capacity = capacity;
}
然后写运行的大致框架:首先,实例化一个班级。打印出一个菜单,菜单上有各种那个操作。然后写一个循环(do-while循环:最少循环一次)然后输入要进行的操作,通过(switch)语句,对应到相应操作的语句。
void menu() {
cout << "1,学生信息录入" << endl;
cout << "2,删除学生信息" << endl;
cout << "3,修改学生信息" << endl;
cout << "4,显示所有学生信息" << endl;
cout << "5,查询学生成绩" << endl;
cout << "6,成绩排名" << endl;
cout << "0,退出系统" << endl;
}
int main() {
StudentClass studentClass(1);
do
{
menu();
cout << "请输入操作选项:" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1://学生信息录入
{
GetIn(studentClass);
break;
}
case 2://删除学生信息
{
Delete(studentClass);
break;
}
case 3://修改学生信息
{
Amend(studentClass);
break;
}
case 4://显示所有学生信息
{
Print(studentClass);
break;
}
case 5://查询学生成绩
{
Search(studentClass);
break;
}
case 6://成绩排名
{ Order(studentClass);
break;
}
case 0:
cout << " 退出系统" << endl;
exit(0);
default:
cout << "输入错误,重新输入:" << endl;
break;
}
} while (1);
return 0;
}
达到这种效果:
第一个操作对应的功能:
分别输入姓名,学号,c++成绩,高数成绩,英语成绩,然后实例化一个学生的引用。将这些作为构造函数是参数。然后将这个"学生"放到"班级"里面。想要实现这个操作就要在班级这个类里面写一个成员函数。把这个学生对象放到班级类指定的空间里面。这时就需要考虑班级这个类在初始化的时候开辟的空间是否够用。如果不够放下这个"学生"就要考虑扩容。
void GetIn(StudentClass& studentClass) {
cout << "请输入学生信息->" << endl;
cout << "姓名:" << endl;
string name;
cin >> name;
cout << "学号:" << endl;
string stuNum;
cin >> stuNum;
cout << "C语言成绩:" << endl;
double a;
cin >> a;
cout << "高数成绩:" << endl;
double b;
cin >> b;
cout << "英语成绩:" << endl;
double c;
cin >> c;
Student stu(name, stuNum, a, b, c);
studentClass.putin(stu);
}
注意:这是在班级这个类开辟的空间上,把学生这个对象放进去,所以这个函数是班级的成员函数
void putin(Student s) {
if (_useSize == _capacity) {
_capacity = _capacity == 0 ? _capacity = 4 : _capacity * 2;
Student* temp = new Student[_capacity];
for (int i = 0; i < _useSize; i++) {
temp[i] = _arr[i];
}
delete[]_arr;
_arr = temp;
}
_arr[_useSize] = s;
_useSize++;
}
效果如图所示:
第二个操作对应的功能:
删除学生信息,我们需要输入学生的姓名或者学号,如果没找到不作操作,如果找到了这位学生,按照如下图思路将其覆盖,也就是删除
void Delete(StudentClass& studentClass) {
cout << "请输入学生的学号/姓名:" << endl;
string stuNum;
cin >> stuNum;
studentClass.del(stuNum);
}
班级的成员函数:
void del(string num) {
int x = getUseSize();
int i;
for (i = 0;i < x;i++) {
if (_arr[i].getStuNum() == num || _arr[i].getStuName() == num) {
break;
}
}
if (i==x) {
cout << "查无此人! 删除失败!!!" << endl;
return;
}else{
for (;i < _useSize - 1;i++) {
_arr[i] = _arr[i + 1];
}
_useSize--;
cout << " 删除成功" << endl;
return;
}
}
int getUseSize() {
return _useSize;
}
学生的成员函数(因为名字和学号都是私有,所以要有一个函数是的让这些数据可以得到,这样做的目的是不让成员变量随意被更改)
string getStuNum() {
return _stuNum;
}
string getStuName() {
return _name;
}
效果如图所示:
第三个操作对应的功能:
修改学生信息首先输入学号或者姓名,找到这个学生。然后打印一个目录,上面为要修改的信息选项,然后选择要修改的内容做出修改
void fix(string num) {
int x = getUseSize();
int i = 0;
for ( i = 0;i < x;i++) {
if (_arr[i].getStuNum() == num || _arr[i].getStuName() == num) {
_arr[i].print();
do
{
menu();
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入修改后的名字:" << endl;
_arr[i].fixname(Cin("a"));
_arr[i].print();
break;
case 2:
cout << "请输入修改后的学号:" << endl;
_arr[i].fixnum(Cin("a"));
_arr[i].print();
break;
case 3:
cout << "请输入修改后的C语言成绩:" << endl;
_arr[i].fixC(Cin(1.1));
_arr[i].print();
break;
case 4:
cout << "请输入修改后的高数成绩:" << endl;
_arr[i].fixM(Cin(1.1));
_arr[i].print();
break;
case 5:
cout << "请输入修改后的英语成绩:" << endl;
_arr[i].fixE(Cin(1.1));
_arr[i].print();
break;
case 0:
cout << "完成修改" << endl;
_arr[i].print();
return;
default:
cout << "输入错误,重新输入:" << endl;
break;
}
} while (1);
}
}
if (i == x) {
cout << "查无此人! 修改失败!!!" << endl;
return;
}
}
void menu() {
cout << "1,修改姓名" << endl;
cout << "2,修改学号" << endl;
cout << "3,修改C语言成绩" << endl;
cout << "4,修改高数成绩" << endl;
cout << "5,修改英语成绩" << endl;
cout << "0,完成修改" << endl;
}
string Cin(string) {
string s;
cin >> s;
return s;
}
double Cin(double) {
double s;
cin >> s;
return s;
}
void fixname(string s) {
_name = s;
}
void fixnum(string s) {
_stuNum = s;
}
void fixC(double x) {
_gradeC = x;
}
void fixM(double x) {
_gradeM = x;
}
void fixE(double x) {
_gradeE = x;
}
void print() {
cout << "姓名: " << _name << " 学号:" << _stuNum << " C语言成绩:" << _gradeC << " 高数成绩"
<< _gradeM << " 英语成绩:" << _gradeE << endl;
}
效果如下:
第四个操作对应的功能:
显示所以信息,只需要for循环班级指向的空间,将所有学生打印出来即可
void Print(StudentClass& studentClass) {
studentClass.print();
}
班级的成员函数:
void print() {
int x = getUseSize();
for (int i = 0;i < x;i++) {
_arr[i].print();
}
}
实现效果:
第五个操作对应的功能:
查询学生信息,for循化找到该学生然后打印这个学生的信息
void Search(StudentClass& studentClass) {
cout << "请输入学生的学号/姓名:" << endl;
string stuNum;
cin >> stuNum;
studentClass.grade(stuNum);
}
班级成员函数:
void grade(string num) {
int x = getUseSize();
int i = 0;
for (i = 0;i < x;i++) {
if (_arr[i].getStuNum() == num || _arr[i].getStuName() == num) {
_arr[i].getgrade();
return;
}
}
if (i == x) {
cout << "查无此人!" << endl;
return;
}
}
学生成员函数:
void getgrade() {
cout << "姓名:" << _name << endl;
cout << endl;
cout << " C语言成绩:" << _gradeC << " 高数成绩" << _gradeM << " 英语成绩:" << _gradeE << " 平均成绩:" << everage() << endl;
}
效果如下:
第六个操作对应的功能:
排序,实例化一个临时班级对象,将原来的班级拷贝到这个临时对象上面,对临时对象进行排序(冒泡排序),然后依次打印
StudentClass(StudentClass& stuclass) {
_arr = (Student*)malloc(sizeof(Student) * stuclass._capacity);
if (NULL == _arr) {
perror("申请空间失败");
return;
}
memcpy(_arr, stuclass._arr, sizeof(Student) * stuclass._useSize);
_useSize = stuclass._useSize;
_capacity = stuclass._capacity;
}
void Order(StudentClass& studentClass) {
StudentClass stuclass = studentClass;
studentClass.order(stuclass);
stuclass.print1();
}
班级成员变量:
void swap(Student& p1, Student& p2) {
Student tmp = p2;
p2 = p1;
p1 = tmp;
}
void order(StudentClass& stuclass) {
int x = getUseSize();
for (int i = 0;i < x;i++) {
for (size_t j = 0; j < x-i-1; j++)
{
if (stuclass._arr[j].everage() < stuclass._arr[j+1].everage()) {
swap(stuclass._arr[j], stuclass._arr[j + 1]);
}
}
}
}
学生成员变量:
void getordergrade() {
cout << "姓名: " << _name << " 学号:" << _stuNum << " C语言成绩:" << _gradeC << " 高数成绩"
<< _gradeM << " 英语成绩:" << _gradeE << " 平均成绩:" << everage()<< endl;
}
效果:
全部代码:
#include<iostream>
#include<string>
using namespace std;
class Student {
private:
string _name;
string _stuNum;
double _gradeC;
double _gradeM;
double _gradeE;
public:
Student(string name = "", string stuNum = "", double gradeC = 0, double gradeM = 0, double gradeE = 0)
:_name(name)
, _stuNum(stuNum)
, _gradeC(gradeC)
, _gradeM(gradeM)
, _gradeE(gradeE)
{}
void print() {
cout << "姓名: " << _name << " 学号:" << _stuNum << " C语言成绩:" << _gradeC << " 高数成绩"
<< _gradeM << " 英语成绩:" << _gradeE << endl;
}
string getStuNum() {
return _stuNum;
}
string getStuName() {
return _name;
}
void getgrade() {
cout << "姓名:" << _name << endl;
cout << endl;
cout << " C语言成绩:" << _gradeC << " 高数成绩" << _gradeM << " 英语成绩:" << _gradeE << " 平均成绩:" << everage() << endl;
}
void getordergrade() {
cout << "姓名: " << _name << " 学号:" << _stuNum << " C语言成绩:" << _gradeC << " 高数成绩"
<< _gradeM << " 英语成绩:" << _gradeE << " 平均成绩:" << everage()<< endl;
}
double everage() {
return (_gradeC + _gradeM + _gradeE) / 3;
}
void fixname(string s) {
_name = s;
}
void fixnum(string s) {
_stuNum = s;
}
void fixC(double x) {
_gradeC = x;
}
void fixM(double x) {
_gradeM = x;
}
void fixE(double x) {
_gradeE = x;
}
};
class StudentClass {
private:
Student* _arr;
int _useSize;
int _capacity;
public:
StudentClass(StudentClass& stuclass) {
_arr = (Student*)malloc(sizeof(Student) * stuclass._capacity);
if (NULL == _arr) {
perror("申请空间失败");
return;
}
memcpy(_arr, stuclass._arr, sizeof(Student) * stuclass._useSize);
_useSize = stuclass._useSize;
_capacity = stuclass._capacity;
}
StudentClass(int capacity = 4) {
_arr = new Student[capacity];
_useSize = 0;
_capacity = capacity;
}
void putin(Student s) {
if (_useSize == _capacity) {
_capacity = _capacity == 0 ? _capacity = 4 : _capacity * 2;
Student* temp = new Student[_capacity];
for (int i = 0; i < _useSize; i++) {
temp[i] = _arr[i];
}
delete[]_arr;
_arr = temp;
}
_arr[_useSize] = s;
_useSize++;
}
void del(string num) {
int x = getUseSize();
int i;
for (i = 0;i < x;i++) {
if (_arr[i].getStuNum() == num || _arr[i].getStuName() == num) {
break;
}
}
if (i==x) {
cout << "查无此人! 删除失败!!!" << endl;
return;
}else{
for (;i < _useSize - 1;i++) {
_arr[i] = _arr[i + 1];
}
_useSize--;
cout << " 删除成功" << endl;
return;
}
}
int getUseSize() {
return _useSize;
}
void print() {
int x = getUseSize();
for (int i = 0;i < x;i++) {
_arr[i].print();
}
}
void print1() {
int x = getUseSize();
for (int i = 0;i < x;i++) {
_arr[i].getordergrade();
}
}
void grade(string num) {
int x = getUseSize();
int i = 0;
for (i = 0;i < x;i++) {
if (_arr[i].getStuNum() == num || _arr[i].getStuName() == num) {
_arr[i].getgrade();
return;
}
}
if (i == x) {
cout << "查无此人!" << endl;
return;
}
}
void menu() {
cout << "1,修改姓名" << endl;
cout << "2,修改学号" << endl;
cout << "3,修改C语言成绩" << endl;
cout << "4,修改高数成绩" << endl;
cout << "5,修改英语成绩" << endl;
cout << "0,完成修改" << endl;
}
string Cin(string) {
string s;
cin >> s;
return s;
}
double Cin(double) {
double s;
cin >> s;
return s;
}
void fix(string num) {
int x = getUseSize();
int i = 0;
for ( i = 0;i < x;i++) {
if (_arr[i].getStuNum() == num || _arr[i].getStuName() == num) {
_arr[i].print();
do
{
menu();
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入修改后的名字:" << endl;
_arr[i].fixname(Cin("a"));
_arr[i].print();
break;
case 2:
cout << "请输入修改后的学号:" << endl;
_arr[i].fixnum(Cin("a"));
_arr[i].print();
break;
case 3:
cout << "请输入修改后的C语言成绩:" << endl;
_arr[i].fixC(Cin(1.1));
_arr[i].print();
break;
case 4:
cout << "请输入修改后的高数成绩:" << endl;
_arr[i].fixM(Cin(1.1));
_arr[i].print();
break;
case 5:
cout << "请输入修改后的英语成绩:" << endl;
_arr[i].fixE(Cin(1.1));
_arr[i].print();
break;
case 0:
cout << "完成修改" << endl;
_arr[i].print();
return;
default:
cout << "输入错误,重新输入:" << endl;
break;
}
} while (1);
}
}
if (i == x) {
cout << "查无此人! 修改失败!!!" << endl;
return;
}
}
void swap(Student& p1, Student& p2) {
Student tmp = p2;
p2 = p1;
p1 = tmp;
}
void order(StudentClass& stuclass) {
int x = getUseSize();
for (int i = 0;i < x;i++) {
for (size_t j = 0; j < x-i-1; j++)
{
if (stuclass._arr[j].everage() < stuclass._arr[j+1].everage()) {
swap(stuclass._arr[j], stuclass._arr[j + 1]);
}
}
}
}
};
void menu() {
cout << "1,学生信息录入" << endl;
cout << "2,删除学生信息" << endl;
cout << "3,修改学生信息" << endl;
cout << "4,显示所有学生信息" << endl;
cout << "5,查询学生成绩" << endl;
cout << "6,成绩排名" << endl;
cout << "0,退出系统" << endl;
}
void GetIn(StudentClass& studentClass) {
cout << "请输入学生信息->" << endl;
cout << "姓名:" << endl;
string name;
cin >> name;
cout << "学号:" << endl;
string stuNum;
cin >> stuNum;
cout << "C语言成绩:" << endl;
double a;
cin >> a;
cout << "高数成绩:" << endl;
double b;
cin >> b;
cout << "英语成绩:" << endl;
double c;
cin >> c;
Student stu(name, stuNum, a, b, c);
studentClass.putin(stu);
}
void Delete(StudentClass& studentClass) {
cout << "请输入学生的学号/姓名:" << endl;
string stuNum;
cin >> stuNum;
studentClass.del(stuNum);
}
void Amend(StudentClass& studentClass) {
cout << "请输入学生的学号/姓名:" << endl;
string stuNum;
cin >> stuNum;
studentClass.fix(stuNum);
}
void Print(StudentClass& studentClass) {
studentClass.print();
}
void Search(StudentClass& studentClass) {
cout << "请输入学生的学号/姓名:" << endl;
string stuNum;
cin >> stuNum;
studentClass.grade(stuNum);
}
void Order(StudentClass& studentClass) {
StudentClass stuclass = studentClass;
studentClass.order(stuclass);
stuclass.print1();
}
int main() {
StudentClass studentClass(1);
do
{
menu();
cout << "请输入操作选项:" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1://学生信息录入
{
GetIn(studentClass);
break;
}
case 2://删除学生信息
{
Delete(studentClass);
break;
}
case 3://修改学生信息
{
Amend(studentClass);
break;
}
case 4://显示所有学生信息
{
Print(studentClass);
break;
}
case 5://查询学生成绩
{
Search(studentClass);
break;
}
case 6://成绩排名
{ Order(studentClass);
break;
}
case 0:
cout << " 退出系统" << endl;
exit(0);
default:
cout << "输入错误,重新输入:" << endl;
break;
}
} while (1);
return 0;
}