(一)过程
首先建立一个项目,再根据需求创建一个学生类,包括:

接着进行程序编写:
(1)学生类
静态属性:姓名、各科成绩、总成绩、成绩等级
成员函数:为了实现对数据成员的擦欧总,需要一组体术数据成员值的成员函数,除此外还有初始化函数、展示函数、修改函数等等。
- class Score
- {
- char *name;
- double per_score[4];
- double total_score;
- char rate;
-
- public:
- Score();
- Score(char *na, double *per);
- double Gettotal();
- char* GetName();
- void Modify();
- char GetRate();
- void Display();
- void Input();
- };
(2)类体的实现
类体的实现程序存为与头文件相同的文件Score.cpp,在该文件中进行各函数的编写,包括初始化、展示函数、修改函数等等。
(3)主函数
在主函数中,编写一个菜单函数,并定义一个全局变量N,用以记录Student的个数,首先定义一组学生类,然后用do while循环展示菜单、接收用户选择,直至用户选择退出信息管理系统,则do while 循环结束,此外,还编写了输出对象数组元素、按姓名查询、输入对象数组元素等函数,组成main函数中中根据用户选择进行switch case的部分。
遇到的问题:
(1)关于严重性代码说明项目文件
在调试过程中,出现了使用strcpy函数不安全,可能会发生缓冲区溢出的报错。
解决方法:更改项目属性(将 SDL 检查 选择为 否),或者在main函数前加上 #pragma warning(disable:4996) 这一语句(可以关掉关于函数不安全的警告)
采用第一种方法。
(二)代码
三个文件代码:
(1)Score.h:
- #include
- using namespace std;
-
- class Score
- {
- char *name;
- double per_score[4];
- double total_score;
- char rate;
-
- public:
- Score();
- Score(char *na, double *per);
- double Gettotal();
- char* GetName();
- void Modify();
- char GetRate();
- void Display();
- void Input();
- };
(2)Score.cpp:
- #include "score.h"
- #include <iostream>
- #include <string.h>
- #include <stdlib.h>
- using namespace std;
-
- Score::Score()
- {
- name = NULL;
- rate = 'B';
- total_score = 0;
- }
-
- Score::Score(char * na, double * per)
- {
- if (na)
- {
- name = new char[strlen(na) + 1];
- strcpy(name, na);
- }
- //per_score = new double[4];
- for (int i = 0; i < 4; i++)
- {
- per_score[i] = 0;
- }
- }
-
- char * Score::GetName()
- {
- return name;
- }
-
- double Score::Gettotal()
- {
- double x1 = ((per_score[0] + per_score[1]) / 20) * 100 * 0.25; //两次随堂测试成绩
- double x2 = (per_score[2] / 100) * 100 * 0.25; //期中考
- double x3 = (per_score[3] / 100) * 100 * 0.5; //期末考
- total_score = x1 + x2 + x3;
- return x1 + x2 + x3;
- }
-
- void Score::Modify()
- {
- int choice;
- cout << "你要修改哪门成绩?(1-随堂测试1,2-随堂测试2,3-期中考试,4-期末考试):";
- cin >> choice;
- double x;
- cout << "你要将其改为?:";
- cin >> x;
- if (choice == 1)
- per_score[0] = x;
- else if (choice == 2)
- per_score[1] = x;
- else if (choice == 3)
- per_score[2] = x;
- else
- per_score[3] = x;
-
- }
-
- char Score::GetRate()
- {
- if (total_score >= 90)
- return 'A';
- else if (total_score >= 80)
- return 'B';
- else if (total_score >= 70)
- return 'C';
- else if (total_score >= 60)
- return 'D';
- else
- return 'E';
- }
-
- void Score::Display() //输出数据
- {
- cout << "姓名: " << name << endl;
- cout << "各科成绩:" << "随堂成绩1——" << per_score[0] << endl;
- cout << "\t随堂测试2——" << per_score[1] << endl;
- cout << "\t期中考试——" << per_score[2] << endl;
- cout << "\t期末考试——" << per_score[3] << endl;
- cout << "总成绩:" << Gettotal() << endl;
- cout << "该学生成绩等级为:" << GetRate() << endl;
- }
-
- void Score::Input() //输入数据
- {
- char na[10];
- cout << "请输入姓名:";
- cin >> na;
- if (name)
- delete[]name;
- name = new char[strlen(na) + 1];
- strcpy(name, na);
- char per[4];
- cout << "请输入该学生第一次随堂测试成绩:";
- cin >> per_score[0];
- cout << "请输入该学生第二次随堂测试成绩:";
- cin >> per_score[1];
- cout << "请输入该学生期中考试成绩:";
- cin >> per_score[2];
- cout << "请输入该学生期末考试成绩:";
- cin >> per_score[3];
- }
(3)main(score).cpp:
- #include "score.h"
- #include <iostream>
- #include <string.h>
- #include <stdlib.h>
- using namespace std;
- #pragma warning(disable:4996)
-
-
- int N = 0;
- void menu();
- void Output(Score *array);
- void Input(Score *array);
- void Modify(Score *array);
-
- int main()
- {
- Score Array[5];
- int choice;
- do
- {
- menu(); //展示菜单
- cout << "请输入你的选择:";
- cin >> choice;
- if (choice >= 1 && choice <= 4)
- {
- switch (choice)
- {
- case 1:
- Input(Array);
- break;
- case 2:
- Output(Array);
- break;
- case 3:
- Modify(Array);
- break;
- default:break;
- }
- }
- } while (choice != 4);
-
- }
-
- void menu()
- {
- cout << "________________________________________________________" << endl;
- cout << "|\t\t1、录入该学生成绩\t\t\t|" << endl;
- cout << "|\t\t2、查看该学生成绩\t\t\t|" << endl;
- cout << "|\t\t3、修改该学生成绩\t\t\t|" << endl;
- cout << "|\t\t4、退出\t\t\t\t\t|" << endl;
- cout << "________________________________________________________" << endl;
- }
-
- void Output(Score * array)
- {
- cout << "学生总人数 = " << N << endl;
- for (int i = 0; i < N; i++)
- {
- array[i].Display();
- cout << endl;
- }
- }
-
- void Input(Score * array)
- {
- char ch;
- do
- {
- array[N].Input();
- N ++;
- cout << "继续输入吗?(Y or N ):";
- cin >> ch;
- } while (ch == 'Y');
- }
-
- void Modify(Score * array)
- {
- int choice;
- char ch;
- do
- {
- cout << "你要修改第几个学生的成绩?:";
- cin >> choice;
- array[choice-1].Modify();
- cout << "还要修改吗?(Y or N ):";
- cin >> ch;
- } while (ch == 'Y');
- }
(三)运行结果


