版本1:
头文件Student.h:
#ifndef stu_h
#define stu_h
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX_SIZE = 60;
class Student
{
private:
string name;
string id;
double math;
double English;
double computer;
public:
Student() :name(""), id(""), math(100.0), English(100.0), computer(100.0) {}
Student(string n) :name(n), id(""), math(100.0), English(100.0), computer(100.0) {}
Student(string n, string i):name(n), id(i), math(100.0), English(100.0), computer(100.0) {}
void SetName(string n)
{
name = n;
}
void SetId(string i)
{
id = i;
}
void SetMath(double m)
{
math = m;
}
void SetEnglish(double e)
{
English = e;
}
void SetComputer(double c)
{
computer = c;
}
string GetName()
{
return name;
}
string GetId()
{
return id;
}
double GetMath()
{
return math;
}
double GetEnglish()
{
return English;
}
double GetComputer()
{
return computer;
}
void SetAll(double m, double e, double c)
{
math = m;
English = e;
computer = c;
}
double GetSum()
{
return math + English + computer;
}
bool Match(string s, short flag)
{
if (flag == 1)
return name == s;
else
return id == s;
}
void Print()
{
cout << name << '\t' << id << endl;
cout << "数学:" << math << "\t英语:" << English << "\t计算机:" << computer << endl;
}
};
bool cmp1(Student st1, Student st2)
{
return st1.GetId() < st2.GetId();
}
bool cmp2(Student st1, Student st2)
{
return st1.GetSum() > st2.GetSum();
}
class DataBase
{
private:
Student stu[MAX_SIZE];
short size;
public:
DataBase() :size(0) {}
bool Push(string n, string i, double m, double e, double c)
{
if (size == MAX_SIZE)
return false;
Student st(n, i);
st.SetAll(m,e,c);
stu[size++] = st;
return true;
}
bool Push()
{
if (size == MAX_SIZE)
{
cout << "系统不能容纳更多学生。" << endl;
system("pause");
return false;
}
string n, i;
cout << "请输入要添加的学生姓名:";
cin >> n;
int idx = 0;
do
{
cout << "请输入该学生的学号:";
cin >> i;
for (idx = 0; idx < size; idx++)
{
if (stu[idx].Match(i, 2))
{
cout << "该学号已存在,不能重复输入!" << endl;
break;
}
}
} while (idx> m;
cout << "请输入该学生的英语成绩:";
cin >> e;
cout << "请输入该学生的计算机成绩:";
cin >> c;
stu_tmp.SetAll(m, e, c);
stu[size++] = stu_tmp;
cout << "添加成功!" << endl;
system("pause");
return true;
}
short AimedSearch(short start_id, string str, short flag)
{
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 = 0;
do
{
cout << "请问你要按什么条件搜索? 1.姓名 2.学号 ";
cin >> choice;
} while (choice != 1 && choice != 2);
string match;
cout << "请输入你要找的" << (choice == 1 ? "姓名" : "学号") << ":";
cin >> match;
short result = 0;
char nod = 0;
while (true)
{
result = AimedSearch(result, match, choice);
if (result == -1)
{
cout << "未找到匹配信息。" << endl;
system("pause");
return result;
}
cout << "这是你要找的人吗?(y/n)";
cin >> nod;
nod = tolower(nod);
if (nod == 'y')
return result;
else
result++;
}
}
bool Delete()
{
short result = 0;
char choice = 0;
result = Search();
if (result == -1)
return false;
cout << "是否要删除这条信息?(y/n)";
cin >> choice;
choice = tolower(choice);
short idx = 0;
if (choice == 'y')
{
for (idx = result; idx < size - 1; idx++)
{
stu[idx] = stu[idx + 1];
}
size--;
cout << "删除成功!" << endl;
system("pause");
return true;
}
cout << "信息未删除!" << endl;
system("pause");
return false;
}
bool Alter()
{
short result = 0;
string rename;
double rescore = 0.0;
result = Search();
if (result == -1)
return false;
char choice = 0;
cout << "是否要修改这条信息?(y/n)";
cin >> choice;
choice = tolower(choice);
if (choice == 'y')
{
cout << "修改什么? 1.名字 2.数学成绩 3.英语成绩 4.计算机成绩" << endl;
char subchoice = 0;
cin >> subchoice;
switch (subchoice)
{
case '1':
{
cout << "输入一个新名字:";
cin >> rename;
stu[result].SetName(rename);
cout << "修改成功!" << endl;
system("pause");
break;
}
case '2':
{
cout << "输入数学成绩:";
cin >> rescore;
stu[result].SetMath(rescore);
cout << "修改成功!" << endl;
system("pause");
break;
}
case '3':
{
cout << "输入英语成绩:";
cin >> rescore;
stu[result].SetEnglish(rescore);
cout << "修改成功!" << endl;
system("pause");
break;
}
case '4':
{
cout << "输入计算机成绩:";
cin >> rescore;
stu[result].SetComputer(rescore);
cout << "修改成功!" << endl;
system("pause");
break;
}
default:
{
cout << "修改失败!" << endl;
return false;
}
}
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 << endl;
for (int i = 0; i < size; i++)
{
cout << setw(12) << stu[i].GetName()<> choice;
} while (choice != '1' && choice != '2');
if (choice == '1')
{
sort(&stu[0], &stu[0] + size, cmp1);
}
else
{
sort(&stu[0], &stu[0] + size, cmp2);
}
cout << "重新排序完成。" << endl;
system("pause");
}
char ShowMenu()
{
char choice = 0;
do
{
system("cls");
cout << "----------欢迎使用成绩管理系统----------" << endl << 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;
}
bool Call(char choice)
{
switch (choice)
{
case '1':
{
Push();
return true;
}
case '2':
{
Search();
return true;
}
case '3':
{
Delete();
return true;
}
case '4':
{
Sort();
return true;
}
case '5':
{
Display();
return true;
}
case '6':
{
Alter();
return true;
}
case '7':
{
return false;
}
default:
break;
}
}
};
#endif
源文件main.cpp:
#include"Student.h"
int main()
{
DataBase student1;
bool quit = false;
student1.Push("徐","00001",100.0,100.0,100.0);
student1.Push("吴", "00002", 98.0, 85.0, 99.0);
while (!quit)
{
if (!student1.Call(student1.ShowMenu()))
quit = true;
}
return 0;
}
版本2:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX_SIZE = 60;
class Student
{
private:
string name;
string id;
double math;
double English;
double computer;
public:
Student() :name(""), id(""), math(100.0), English(100.0), computer(100.0) {}
Student(string n) :name(n), id(""), math(100.0), English(100.0), computer(100.0) {}
Student(string n, string i) :name(n), id(i), math(100.0), English(100.0), computer(100.0) {}
void SetName(string n)
{
name = n;
}
void SetId(string i)
{
id = i;
}
void SetMath(double m)
{
math = m;
}
void SetEnglish(double e)
{
English = e;
}
void SetComputer(double c)
{
computer = c;
}
string GetName()
{
return name;
}
string GetId()
{
return id;
}
double GetMath()
{
return math;
}
double GetEnglish()
{
return English;
}
double GetComputer()
{
return computer;
}
void SetAll(double m, double e, double c)
{
math = m;
English = e;
computer = c;
}
double GetSum()
{
return math + English + computer;
}
bool Match(string s, short flag)
{
if (flag == 1)
return name == s;
else
return id == s;
}
void Print()
{
cout << name << '\t' << id << endl;
cout << "数学:" << math << "\t英语:" << English << "\t计算机:" << computer << endl;
}
};
bool cmp1(Student st1, Student st2)
{
return st1.GetId() < st2.GetId();
}
bool cmp2(Student st1, Student st2)
{
return st1.GetSum() > st2.GetSum();
}
class DataBase
{
private:
Student stu[MAX_SIZE];
short size;
public:
DataBase() :size(0) {}
bool Push(string n, string i, double m, double e, double c)
{
if (size == MAX_SIZE)
return false;
Student st(n, i);
st.SetAll(m, e, c);
stu[size++] = st;
return true;
}
bool Push()
{
if (size == MAX_SIZE)
{
cout << "系统不能容纳更多学生。" << endl;
system("pause");
return false;
}
string n, i;
cout << "请输入要添加的学生姓名:";
cin >> n;
int idx = 0;
do
{
cout << "请输入该学生的学号:";
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);
double m = 0.0, e = 0.0, c = 0.0;
cout << "请输入该学生的数学成绩:";
cin >> m;
cout << "请输入该学生的英语成绩:";
cin >> e;
cout << "请输入该学生的计算机成绩:";
cin >> c;
stu_tmp.SetAll(m, e, c);
stu[size++] = stu_tmp;
cout << "添加成功!" << endl;
system("pause");
return true;
}
short AimedSearch(short start_id, string str, short flag)
{
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 = 0;
do
{
cout << "请问你要按什么条件搜索? 1.姓名 2.学号 ";
cin >> choice;
} while (choice != 1 && choice != 2);
string match;
cout << "请输入你要找的" << (choice == 1 ? "姓名" : "学号") << ":";
cin >> match;
short result = 0;
char nod = 0;
while (true)
{
result = AimedSearch(result, match, choice);
if (result == -1)
{
cout << "未找到匹配信息。" << endl;
system("pause");
return result;
}
cout << "这是你要找的人吗?(y/n)";
cin >> nod;
nod = tolower(nod);
if (nod == 'y')
return result;
else
result++;
}
}
bool Delete()
{
short result = 0;
char choice = 0;
result = Search();
if (result == -1)
return false;
cout << "是否要删除这条信息?(y/n)";
cin >> choice;
choice = tolower(choice);
short idx = 0;
if (choice == 'y')
{
for (idx = result; idx < size - 1; idx++)
{
stu[idx] = stu[idx + 1];
}
size--;
cout << "删除成功!" << endl;
system("pause");
return true;
}
cout << "信息未删除!" << endl;
system("pause");
return false;
}
bool Alter()
{
short result = 0;
string rename;
double rescore = 0.0;
result = Search();
if (result == -1)
return false;
char choice = 0;
cout << "是否要修改这条信息?(y/n)";
cin >> choice;
choice = tolower(choice);
if (choice == 'y')
{
cout << "修改什么? 1.名字 2.数学成绩 3.英语成绩 4.计算机成绩" << endl;
char subchoice = 0;
cin >> subchoice;
switch (subchoice)
{
case '1':
{
cout << "输入一个新名字:";
cin >> rename;
stu[result].SetName(rename);
cout << "修改成功!" << endl;
system("pause");
break;
}
case '2':
{
cout << "输入数学成绩:";
cin >> rescore;
stu[result].SetMath(rescore);
cout << "修改成功!" << endl;
system("pause");
break;
}
case '3':
{
cout << "输入英语成绩:";
cin >> rescore;
stu[result].SetEnglish(rescore);
cout << "修改成功!" << endl;
system("pause");
break;
}
case '4':
{
cout << "输入计算机成绩:";
cin >> rescore;
stu[result].SetComputer(rescore);
cout << "修改成功!" << endl;
system("pause");
break;
}
default:
{
cout << "修改失败!" << endl;
return false;
}
}
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 << endl;
for (int i = 0; i < size; i++)
{
cout << setw(12) << stu[i].GetName() << setw(12) << stu[i].GetId();
printf("%-8.1lf%-8.1lf%-8.1lf\n", stu[i].GetMath(), stu[i].GetEnglish(), stu[i].GetComputer());
}
cout << resetiosflags(ios::right);
system("pause");
}
void Sort()
{
char choice = 0;
do
{
cout << "请输入排序方式 1.学号升序 2.总成绩降序" << endl;
cin >> choice;
} while (choice != '1' && choice != '2');
if (choice == '1')
{
sort(&stu[0], &stu[0] + size, cmp1);
}
else
{
sort(&stu[0], &stu[0] + size, cmp2);
}
cout << "重新排序完成。" << endl;
system("pause");
}
char ShowMenu()
{
char choice = 0;
do
{
system("cls");
cout << "----------欢迎使用成绩管理系统----------" << endl << 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;
}
bool Call(char choice)
{
switch (choice)
{
case '1':
{
Push();
return true;
}
case '2':
{
Search();
return true;
}
case '3':
{
Delete();
return true;
}
case '4':
{
Sort();
return true;
}
case '5':
{
Display();
return true;
}
case '6':
{
Alter();
return true;
}
case '7':
{
return false;
}
default:
break;
}
}
};
int main()
{
DataBase student1;
bool quit = false;
student1.Push("徐","00001",100.0,100.0,100.0);
student1.Push("吴", "00002", 98.0, 85.0, 99.0);
while (!quit)
{
if (!student1.Call(student1.ShowMenu()))
quit = true;
}
return 0;
}