#include<iostream>
#include<iomanip>
#include<string>
#include<stdlib.h>
using namespace std;
struct student
{
char xuehao[10];//学号
char name[10];//姓名
char sex[5];//性别
char jiguan[10];//籍贯
char xueyuan[10];//学院
char banji[10];//班级
};
typedef struct LNode
{
student data;
struct LNode *next;
}*LinkList;
/////////////////////////////存储结构(单链表)所必须的实现函数
class Link
{
public:
LinkList mHead;
//初始化
Link() :mHead(NULL) {}
~Link()
{
while (mHead) {
LinkList p = mHead->next;
delete mHead;
mHead = p;
}
}
//插入操作 Insert before i
void insert(const student& e)
{
LinkList s = new LNode;
s->next = mHead;
s->data = e;
mHead = s;
}
};
class StudentSys
{
Link mHdr;
public:
void runLoop();
protected:
//菜单
int showMenu();//显示菜单
bool doAction(int choose);
void delOne(); //删除信息
bool addOne(); //添加信息
void query(); //查询信息
void showTbl();//报表,显示全部学生信息
bool showHelp();//帮助信息
private:
LinkList find(const char xuehao[]);// 插值学号节点
void Exception();//用于处理用户误操作
};
void StudentSys::runLoop()
{
do {
int choose = showMenu();
if (!doAction(choose)) break;
} while (1);
}
void StudentSys::Exception()
{
cout << "----------------------------------" << endl;
cout << endl;
cout << "非法输入,请仔细操作!" << endl;
system("pause");
system("cls");
cout << "本系统自动重新开始!" << endl;
cout << endl;
cout << "----------------------------------" << endl;
}
int StudentSys::showMenu()
{
cout << "------The Student Management System is running------" << endl;
cout << "菜单:" << endl;
cout << "----------------------------------------------------" << endl;
cout << "| N.退出系统(N=0或任何非数字字串). |" << endl;
cout << "| 1.添加学生信息. |" << endl;
cout << "| 2.删除学生信息. |" << endl;
cout << "| 3.查询学生信息. |" << endl;
cout << "| 4.报表学生信息. |" << endl;
cout << "| 5.查看帮助信息. |" << endl;
cout << "----------------------------------------------------" << endl;
cout << "请选择操作!" << endl;
cout << "----------------your work is beginning-------------------" << endl;
int choose;
cin >> choose;
return choose;
}
bool StudentSys::doAction(int choose)
{
do {
switch (choose)
{
default: Exception(); continue; //cout << "非法输入!系统自动退出!" << endl;
case 0: cout << "成功退出,欢迎再来!" << endl; continue;
case 1: if (addOne()) break; continue;
case 2: delOne(); break;
case 3: query(); break;
case 4: showTbl(); break;
case 5: if (showHelp()) break; continue;//帮助信息显示函数
}
//操作之后的选择
cout << "------------your work is over,please give new choice!-------" << endl;
char xuanze;
cout << "结束程序? y? " << endl; cin >> xuanze;
if (xuanze == 'y'||xuanze == 'Y') break;
system("cls");
return true;
} while (0);
return false;
}
void StudentSys::delOne()
{
char temp[128];
cout << "------------you are deleting the student's info-------" << endl;
cout << "请输入要删除的学号:" << endl; cin >> temp;
for (LinkList p = mHdr.mHead, *pre = &mHdr.mHead; p;) {
if (strcmp((p->data).xuehao, temp)) {
pre = &p->next;
p = p->next;
}
else {
*pre = p->next;
delete p;
cout << "删除成功!" << endl;
}
}
cout << "删除失败,无此学号信息!" << endl;
}
bool StudentSys::addOne()
{
cout << "------------you are adding the student's info-------" << endl;
do {
student info;
do {
cout << "姓名:"; cin >> info.name;
cout << "性别:"; cin >> info.sex;
cout << "学号:"; cin >> info.xuehao;
//用于判断添加的学号是否重复
if (!find(info.xuehao)) break;
cout << "学号重复!请检查后重新添加!" << endl;
cout << "----------------The exception is passed-------------" << endl;
cout << "现在重新添加!" << endl;
} while (1);
cout << "籍贯:"; cin >> info.jiguan;
cout << "学院:"; cin >> info.xueyuan;
cout << "班级:"; cin >> info.banji;
mHdr.insert(info);
///////////////////////////////////////////////////
char temp;
cout << "继续添加吗?" << endl << "1→继续添加 2→结束添加" << endl; cin >> temp;
switch (temp)
{
case '1': cout << "--------next one-------" << endl; break;
case '2': return true;
default: Exception(); return false;
}
} while (1);
return true;
}
void StudentSys::showTbl()
{
cout << "------------There is all the students' info---------" << endl;
cout << "学号" << setw(12) << "姓名" << setw(12) << "性别" << setw(12)
<< "籍贯" << setw(12) << "学院" << setw(12) << "班级" << endl;
for (LinkList p = mHdr.mHead; p; p = p->next) {
cout << (p->data).xuehao << setw(10) << (p->data).name
<< setw(10) << (p->data).sex << setw(10) << (p->data).jiguan
<< setw(10) << (p->data).xueyuan << setw(10) << (p->data).banji << endl;
}
cout << endl;
}
bool StudentSys::showHelp()
{
int t;
cout << "-------you are reading the helpful info------------" << endl;
cout << "a.本系统尚未采用文件读写机制,仅供测试算法使用。" << endl;
cout << "b.测试之前,请按提示添加部分信息,且不要关闭程序。" << endl;
cout << "c.菜单中的序号为操作代号,提示信息中,依据提示代号。" << endl;
cout << "d.做的不好,多多指教,谢谢!是否返回?1→继续。" << endl;
cin >> t;
return (t == 1);
}
LinkList StudentSys::find(const char xuehao[])//查询
{
for (LinkList p = mHdr.mHead; p; p = p->next) {
if (strcmp((p->data).xuehao, xuehao) == 0)
return p;
}
return NULL;
}
void StudentSys::query()//查询
{
cout << "------------you are searching the student's info-------" << endl;
int flag = 0;
student que;
cout << "请输入要查询的学号:" << endl; cin >> que.xuehao;
if (LinkList p = find(que.xuehao)) {
cout << "检索成功!" << endl;
cout << "学号" << setw(12) << "姓名" << setw(12) << "性别" << setw(12) << "籍贯" << setw(12) << "学院" << setw(12) << "班级" << endl;
cout << (p->data).xuehao << setw(10) << (p->data).name << setw(10) << (p->data).sex << setw(10) << (p->data).jiguan << setw(10) << (p->data).xueyuan << setw(10) << (p->data).banji << endl;
}
else
{
cout << "查询失败,无此学号信息!" << endl;
}
}
//主函数部分
void main()
{
cout << "-----------------IMPORTANT INFO ---------------------" << endl;
cout << "学生管理系统" << endl;
cout << "编程作业" << endl;
cout << "------------------------------------------------------" << endl;
StudentSys ss;
ss.runLoop();
}