要求:
一 能够输出所有学生信息
二 能够添加学生
三 能够删除学生
四 能够查看学生的课程信息
五 异常处理、控制台循环
代码如下:
1 课程信息
struct SelectedClass
{
string s_Class;
int i_Score;
SelectedClass(string c, int s) :s_Class(c), i_Score(s) {}
};
2 学生信息
struct Student
{
string s_Name;
string s_Sex;
int i_Num;//选课数
vector<SelectedClass*> Classes;
Student(string n, string s, int num, vector<SelectedClass*> c) :s_Name(n), s_Sex(s), i_Num(num), Classes(c) {};
~Student()
{
while (Classes.size() != 0)
{
delete Classes.back();
Classes.back() = NULL;
Classes.pop_back();
}
}
};
3 SIMS(学生信息管理系统)
class SIMS
{
public:
SIMS();
void enterTochoose();
void addStudent();
void deleteStudent();
void outpuAllInformation();
void outputSingleInformation();
inline void outputStudentClass(map<int, Student*>::iterator it);
void rewrite();
inline bool begPardon();
inline bool begcontinue();
~SIMS();
void printClasses()
{
map<int, string>::iterator it = availableClass.begin();
while (it!=availableClass.end())
{
cout << it->first << " " << it->second << endl;
++it;
}
}
private:
map<int, Student*> stu;
map<int, string> availableClass
{
{1,"C++"},
{2,"离散数学"},
{3,"数据库"},
{4,"英语视听说"}
};
string s_Path = "f://SIMS.txt";
fstream file;
};
SIMS::SIMS()
{
cout << "欢迎来到学生信息管理系统!" << endl;
string s_SLine;
file.open(s_Path, ios::in);
if (!file.is_open())
{
cout << "找不到文件!请确认文件路径为:" << s_Path << endl;
exit(1);
}
try
{
while (getline(file, s_SLine))
{
istringstream sin(s_SLine);
int i, num;
string n, s;
vector<SelectedClass*> c;
sin >> i >> n >> s >> num;
int count = 0;
while (count < num)
{
string cla;
int sco;
sin >> cla >> sco;
SelectedClass *temp = new SelectedClass(cla, sco);
c.push_back(temp);
++count;
}
Student *p = new Student(n, s, num, c);
stu.insert(pair<int, Student*>(i, p));
}
file.close();
enterTochoose();
}
catch (const std::exception&e)
{
cout << "未知错误" << endl;
}
}
SIMS::~SIMS()
{
rewrite();
map<int, Student*>::iterator it = stu.begin();
while (it!=stu.end())
{
delete it->second;
it->second = NULL;
stu.erase(it++);
}
cout << "感谢您使用系统" << endl;
}
void SIMS::enterTochoose()
{
cout << "请输入编号选择功能:" << endl;
int n;
cout << "1 输出所有学生信息" << endl;
cout << "2 添加学生" << endl;
cout << "3 删除学生" << endl;
cout << "4 查看学生课程" << endl;
cin >> n;
switch (n)
{
case(1):
outpuAllInformation();
break;
case(2):
addStudent();
break;
case(3):
deleteStudent();
break;
case(4):
outputSingleInformation();
break;
default:
cout << "无效输入,请重新输入!" << endl;
if (begPardon()) enterTochoose();
break;
}
if (begPardon()) enterTochoose();
}
void SIMS::addStudent()
{
cout << "请输入学生姓名:" << endl;
string n, s;
cin >> n;
cout << "请输入学生性别:" << endl;
cin >> s;
if (s != "男" &&s != "女") {
cout << "无效输入!请重新输入!" << endl;
if (begcontinue())
{
addStudent();
return;
}
else if (begPardon())
{
enterTochoose();
return;
}
else return;
}
cout << "请输入选课数:" << endl;
int num;
cin >> num;
if (num<0||num>4) {
cout << "无效输入!请重新输入!" << endl;
if (begcontinue())
{
addStudent();
return;
}
else if (begPardon())
{
enterTochoose();
return;
}
else return;
}
int count = 0;
vector<SelectedClass*> c;
while (count<num)
{
string cla;
int ans, sco;
cout << "请输入科目对应的编号(一次只输入一个)" << endl;
printClasses();
cin >> ans;
if (ans < 1 || ans>4) {
cout << "无效输入!请重新输入!" << endl;
if (begcontinue())
{
addStudent();
return;
}
else if (begPardon())
{
enterTochoose();
return;
}
else return;
}
else
{
cla = availableClass[ans];
}
cout << "请输入该科目的分数:" << endl;
cin>> sco;
if (sco < 0 || sco>100)
{
cout << "无效输入!请重新输入!" << endl;
if (begcontinue())
{
addStudent();
return;
}
else if (begPardon())
{
enterTochoose();
return;
}
else return;
}
SelectedClass *temp = new SelectedClass(cla, sco);
c.push_back(temp);
++count;
}
int i;
if (stu.size() == 0) i = 1;
else
{
map<int, Student*>::iterator it = stu.end();
i = ((--it)->first + 1);
}
Student *newStudent = new Student(n, s, num, c);
stu.insert(pair<int, Student*>(i, newStudent));
if (begcontinue()) addStudent();
}
void SIMS::deleteStudent()
{
if (stu.size() == 0)
{
cout << "现在无学生信息,请录入学生信息" << endl;
addStudent();
return;
}
else
{
cout << "请输入要删除的学生学号:" << endl;
int id;
cin >> id;
if (!stu.count(id))
{
cout << "无效学号,请重新输入!" << endl;
if (begcontinue())
{
deleteStudent();
return;
}
else return;
}
cout << "该生为:" << endl;
outputStudentClass(stu.find(id));
cout << "是否确认删除?确定请输入y,否则输入n" << endl;
char result;
cin >> result;
if (result == 'y')
{
delete stu.find(id)->second;
stu.find(id)->second = NULL;
stu.erase(stu.find(id));
}
else if (result == 'n') {
if (begcontinue())
{
deleteStudent();
return;
}
else return;
}
else {
cout << "无效输入!请重新输入!";
}
if (begcontinue()) deleteStudent();
else return;
}
}
void SIMS::outpuAllInformation()
{
cout << "以下是所有学生信息:" << endl;
if (stu.size() == 0)
{
cout << "现在无学生信息,请录入学生信息" << endl;
addStudent();
return;
}
else
{
map<int, Student*>::iterator it = stu.begin();
while (it != stu.end())
{
outputStudentClass(it);
++it;
}
}
}
inline void SIMS::outputStudentClass(map<int, Student*>::iterator it)
{
cout << it->first << " " << it->second->s_Name << " " << it->second->s_Sex << " 已选课程:" << it->second->i_Num << " ";
int count = 0;
while (it->second->i_Num > count)
{
cout << it->second->Classes[count]->s_Class << ":" << it->second->Classes[count]->i_Score << " ";
++count;
}
cout << endl;
}
void SIMS::rewrite()
{
file.open(s_Path, ios::out);
if (!file.is_open())
{
cout << "找不到文件!请确认文件路径为:" << s_Path << endl;
exit(1);
}
map<int, Student*>::iterator it = stu.begin();
while (it!=stu.end())
{
file << it->first << " " << it->second->s_Name << " " << it->second->s_Sex << " " << it->second->i_Num << " ";
int count = 0;
while (it->second->i_Num>count)
{
file << it->second->Classes[count]->s_Class << " " << it->second->Classes[count]->i_Score << " ";
++count;
}
file << "\n";
++it;
}
return;
}
void SIMS::outputSingleInformation()
{
if (stu.size() == 0)
{
cout << "现在无学生信息,请录入学生信息" << endl;
addStudent();
return;
}
cout << "请输入学生学号:" << endl;
int id;
cin >> id;
if (stu.count(id))
{
outputStudentClass(stu.find(id));
if (begcontinue()) {
outputSingleInformation();
return;
}
else return;
}
cout << "无效学号,请重新输入!" << endl;
if (begcontinue())
{
addStudent();
return;
}
else if (begPardon())
{
enterTochoose();
return;
}
else return;
}
inline bool SIMS::begPardon()
{
cout << "是否继续执行其他功能?是请输入y,否则输入n!" << endl;
char result;
cin >> result;
if (result == 'y') return true;
else if (result == 'n') return false;
else { cout << "无效输入!请重新输入!"; return begPardon(); }
}
inline bool SIMS::begcontinue()
{
cout << "是否继续执行该功能?是请输入y,否则输入n!" << endl;
char result;
cin >> result;
if (result == 'y') return true;
else if (result == 'n') return false;
else { cout << "无效输入!请重新输入!";return begcontinue(); }
}