主要实现以下功能:
1、添加学生;
2、查找学生;
3、删除学生;
4、修改学生;
5、重新排序;
6、显示全部;
7、退出。
C++代码:
#include
#include
#include
#include //用到system()
#include
using namespace std;
const short MAX_SIZE = 50;
class Student {
//学生类,完成个人信息的输出和返回
/*
SetMath();//输入数学成绩
SetEnglish();//输入英语成绩
SetComputer();//输入电脑成绩
SetName();//输入姓名
GetSum();//总分
Match();//输入字符串信息与名字或学号是否匹配
Print();//显示该学生信息
*/
public:
Student() {};//分号加与不加均可通过编译
Student(char *n) {
//待实参传入时,形参就会指向实参的地址,不再是野指针
strcpy_s(name, n);
}
Student(char *n, char *i) {
strcpy_s(name, n);
strcpy_s(id, i);
}
void SetMath(float m) {
math = m;
}
void SetEnglish(float e) {
english = e;
}
void SetComputer(float c) {
computer = c;
}
void SetName(char *n) {
strcpy_s(name, n);
}
string GetName() {
return name;
}
string GetId() {
return id;
}
float GetSum() {
return math + english + computer;
}
void SetAllScore(float m, float e, float c) {
math = m;
english = e;
computer = c;
}
bool Match(char *str, short flag) {
//flag为1,与姓名匹配;flag为2,与学号匹配
return flag == 1 ? strcmp(name, str) == 0 : strcmp(id, str) == 0;
}
float GetMath() {
return math;
}
float GetEnglish() {
return english;
}
float GetComputer() {
return computer;
}
void Print() {
cout << name << '\t' << id << endl;
cout << "MathScore:" << setprecision(1) << setiosflags(ios::fixed) << math << '\t' << "EnglishScore:" << english << '\t' << "ComputerScore:" << computer << endl;
}
private:
char name[12];
char id[10];
float math;
float english;
float computer;
};
bool Compare1(Student stu1, Student stu2) {
return stu1.GetId() < stu2.GetId();
}
bool Compare2(Student stu1, Student stu2) {
return stu1.GetSum() > stu2.GetSum();
}
class DataBase {
//数据采集类,将学生类的数组作为一个成员,封装对数组添加、插入、查找、删除元素等操作
/*
Push();//添加
Search();//查找
Alter();//修改
Delete();//删除
Display();//显示所有学生信息
ShowMenu();//显示功能菜单
*/
public:
DataBase() {
size = 0;
}
bool Push(char *n,char *i,float m,float e,float c) {
if (size == MAX_SIZE) {
return false;
}
Student st(n, i);
st.SetAllScore(m, e, c);
stu[size++] = st;
return true;
}
bool Push() {
if (size == MAX_SIZE) {
cout << "系统不能容纳更多学生。" << endl;
system("pause");
return false;
}
char n[12], i[10];
cout << "Please input name:";
cin >> n;
int idx;
do {
cout << "Please input id:";
cin >> i;
for (idx = 0; idx < size; idx++) {
if (stu[idx].Match(i, 2)) {
cout << "该学号已存在,不能重复输入。" << endl;
break;
}
}
} while (idx < size);
Student stu_tmp(n, i);
float m, e, c;
cout << "Please input math score:";
cin >> m;
cout << "Please input english score:";
cin >> e;
cout << "Please input computer score:";
cin >> c;
stu_tmp.SetAllScore(m, e, c);
stu[size] = stu_tmp;
size++;
cout << "添加成功!" << endl;
system("pause");
return true;
}
short AimedSearch(short start_id, char *str, short flag) {
//start_id为起始下标
for (short i = start_id; i < size; i++) {
if (stu[i].Match(str, flag)) {
stu[i].Print();
return i;
}
}
return -1;//未找到
}
short Search() {
//实现查找,提供输入接口
short choice;
do {
cout << "请问按什么条件搜索?1、姓名 2、学号" << endl;
cin >> choice;
} while (choice != 1 && choice != 2);
char match[12];
cout << "请输入你要找的" << (choice == 1 ? "姓名:" : "学号:");
cin >> match;
short result = 0;
char nod;
while (true) {
//若数组中有重复姓名,且先出现的不是自己想找的那个学生
result = AimedSearch(result, match, choice);
if (choice == -1) {
cout << "未找到匹配信息。" << endl;
system("pause");
return result;
}
cout << "这是你要找的人吗?(y/n)";
cin >> nod;
if (nod == 'y' || nod == 'Y')
return result;
else
result++;
}
}
bool Delete() {
short result = 0;
result = Search();
char nod;
short idx;
if (result == -1)
return false;
cout << "是否要进行删除?(y/n)" << endl;
cin >> nod;
if (nod == 'y' || nod == 'Y') {
for (idx = result; idx < size; idx++) {
stu[idx] = stu[idx + 1];
}
size--;
cout << "删除成功!" << endl;
system("pause");
return true;
}
else {
cout << "删除失败!" << endl;
system("pause");
return false;
}
}
bool Alter() {
short result = 0;
result = Search();
char nod;//选择是否修改
char choice;//选择要修改什么
char newname[12];
float newscore;
if (result == -1) {
cout << "未找到想要修改的元素。" << endl;
system("pause");
return false;
}
cout << "是否要进行修改?(y/n)" << endl;
cin >> nod;
if (nod == 'Y' || nod == 'y') {
cout << "修改什么?1、Name 2、MathScore 3、EnglishScore 4、ComputerScore" << endl;
cin >> choice;
switch (choice) {
case '1':
cout << "Please input a new name:";
cin >> newname;
stu[result].SetName(newname);
cout << "修改成功!" << endl;
system("pause");
break;
case '2':
cout << "Please input a new mathscore:";
cin >> newscore;
stu[result].SetMath(newscore);
cout << "修改成功!" << endl;
system("pause");
break;
case '3':
cout << "Please input a new englishscore:";
cin >> newscore;
stu[result].SetEnglish(newscore);
cout << "修改成功!" << endl;
system("pause");
break;
case '4':
cout << "Please input a new computerscore:";
cin >> newscore;
stu[result].SetComputer(newscore);
cout << "修改成功!" << endl;
system("pause");
break;
default:
break;
}
return true;
}
cout << "未修改!" << endl;
system("pause");
return false;
}
void Display() {
cout << endl << setw(12) << setiosflags(ios::left) << "姓名" << setw(12) << "学号" << setw(8) << "数学" << setw(8) << "英语" << setw(8) << "计算机" << endl;
cout << endl;
cout << setprecision(1) << setiosflags(ios::fixed);
for (int i = 0; i < size; i++) {
cout << setw(12) << stu[i].GetName() << setw(12) << stu[i].GetId() << setw(8) << stu[i].GetMath() << setw(8) << stu[i].GetEnglish() << setw(8) << stu[i].GetComputer() << endl;
}
cout << resetiosflags(ios::left);
system("pause");
}
void Sort() {
char choice;
do {
cout << "请选择排序方式:1、学号升序 2、总成绩降序";
cin >> choice;
} while (choice != '1'&&choice != '2');
if (choice == '1') {
sort(&stu[0], &stu[0] + size, Compare1);
}
else {
sort(&stu[0], &stu[0] + size, Compare2);
}
cout << "排序完成!" << endl;
system("pause");
}
char ShowMenu() {
char choice;
do {
system("cls");
cout << " --------欢迎使用学生成绩管理系统-------- " << endl;
cout << endl;
cout << " 1、添加学生" << endl;
cout << " 2、查找学生" << endl;
cout << " 3、删除学生" << endl;
cout << " 4、修改学生" << endl;
cout << " 5、重新排序" << endl;
cout << " 6、显示全部" << endl;
cout << " 7、退出" << endl;
cin >> choice;
} while (choice < '1' && choice>'7');
return choice;
}
private:
Student stu[MAX_SIZE];
short size;
};
int main() {
DataBase db;
bool quit = false;
char choice;
db.Push("张三", "00006", 70, 72, 76);
db.Push("李四", "00003", 62, 60, 70);
db.Push("王五", "00001", 61.5, 74, 68.5);
while (!quit) {
choice = db.ShowMenu();
switch (choice) {
case '1':
db.Push();
break;
case '2':
db.Search();
break;
case '3':
db.Delete();
break;
case '4':
db.Alter();
break;
case '5':
db.Sort();
break;
case '6':
db.Display();
break;
case '7':
quit = true;
break;
}
}
return 0;
}