//c语言程序设计
//学生管理系统
#include <iostream>
#include <iomanip>
using namespace std;
struct tagSTUDENT
{
int Sno;
char Sname[10];
int Sage;
//tagSTUDENT *pPrev;
tagSTUDENT *pNext;
};
int ShowMenu();
void InputInfo();
void PrintInfo();
void DeleteInfo();
void FindName();
void FindNumber();
tagSTUDENT *pHead = NULL;
//tagSTUDENT *pEnd = NULL;
int ShowMenu()
{
int iChoose = -1;
while (1)
{
system("cls");
iChoose = -1;
cout << "******学生管理系统******" << endl;
cout << " 1. 录入学生信息" << endl;
cout << " 2. 打印所有学生" << endl;
cout << " 3. 删除一个学生" << endl;
cout << " 4. 按照姓名查询" << endl;
cout << " 5. 按照学号查询" << endl;
cout << " 0. 退出" << endl;
cout << "************************" << endl;
cout << "请选择:" ;
cin >> iChoose;
cin.sync();
cin.clear();
if (iChoose >= 0 && iChoose <= 5) break ;
}
return iChoose;
}
void InputInfo()
{
tagSTUDENT *pNew = new tagSTUDENT;
while (1)
{
cout << "请输入学号:";
cin >> pNew->Sno;
if (pNew->Sno > 0) break;
cin.sync();
cin.clear();
}
while (1)
{
cout << "请输入姓名:";
cin >> pNew->Sname;
if (pNew->Sname) break;
cin.sync();
cin.clear();
}
while (1)
{
cout << "请输入年龄:";
cin >> pNew->Sage;
if (pNew->Sage > 0) break;
cin.sync();
cin.clear();
}
if (pHead == NULL)
{
pNew->pNext = NULL;
pHead = pNew;
//pEnd = pNew;
return;
}
tagSTUDENT *p1 = NULL;
tagSTUDENT *p2 = pHead;
while (p2 != NULL && pNew->Sno >= p2->Sno)
{
if (p2->Sno == pNew->Sno)
{
cout <<"该学号已经存在!";
return;
}
else
{
p1 = p2;
p2 = p2->pNext;
}
}
if (p1 == NULL)
{
pNew->pNext = pHead;
pHead = pNew;
}
else if (p2 != NULL)
{
p1->pNext = pNew;
pNew->pNext = p2;
}
else
{
p1->pNext = pNew;
pNew->pNext = NULL;
}
}
void PrintInfo()
{
cout << setw(6) << "学号"
<< setw(8) << "姓名"
<< setw(6) << "年龄" << endl;
tagSTUDENT *p = pHead;
while (p != NULL)
{
cout << setw(6) << p->Sno
<< setw(8) << p->Sname
<< setw(6) << p->Sage << endl;
p = p->pNext;
}
}
void DeleteInfo()
{
tagSTUDENT *pNew = new tagSTUDENT;
cout << "请输入要删除学生的学号:";
cin >> pNew->Sno;
if (pHead == NULL)
{
cout << "当前系统没有数据!"<<endl;
return;
}
tagSTUDENT *p1 = NULL;
tagSTUDENT *p2 = pHead;
while (p2 != NULL && pNew->Sno != p2->Sno)
{
p1 = p2;
p2 = p2->pNext;
}
if (p2 ==NULL)
{
cout << "该学生信息不存在!" << endl;
return;
}
else if(p1 == NULL)
{
p2 = p2->pNext;
pHead = p2;
cout << "删除成功!" <<endl;
}
else
{
p2 = p2->pNext;
p
1->pNext = p2;
cout << "删除成功!" <<endl;
}
}
void FindName()
{
tagSTUDENT *pNew = new tagSTUDENT;
cout <<
"请输入要查找学生的姓名:";
cin >> pNew->Sname;
if (pHead == NULL)
{
cout << "当前系统没有数据!"<<endl;
return;
}
tagSTUDENT *p1 = NULL;
tagSTUDENT *p2 = pHead;
while (p2 != NULL && strcmp(pNew->Sname, p2->Sname) != 0)
{
p1 = p2;
p2 = p2->pNext;
}
if (p2 ==NULL)
{
cout << "该学生信息不存在!" << endl;
return;
}
else if(p1 == NULL)
{
cout << setw(6) << "学号"
<< setw(8) << "姓名"
<< setw(6) << "年龄" << endl;
cout << setw(6) << pHead->Sno
<< setw(8) << pHead->Sname
<< setw(6) << pHead->Sage << endl;
}
else
{
cout << setw(6) << "学号"
<< setw(8) << "姓名"
<< setw(6) << "年龄" << endl;
cout << setw(6) << p2->Sno
<< setw(8) << p2->Sname
<< setw(6) << p2->Sage << endl;
}
}
void FindNumber()
{
tagSTUDENT *pNew = new tagSTUDENT;
cout << "请输入要查找学生的学号:";
cin >> pNew->Sno;
if (pHead == NULL)
{
cout << "当前系统没有数据!"<<endl;
return;
}
tagSTUDENT *p1 = NULL;
tagSTUDENT *p2 = pHead;
while (p2 != NULL && pNew->Sno != p2->Sno)
{
p1 = p2;
p2 = p2->pNext;
}
if (p2 ==NULL)
{
cout << "该学生信息不存在!" << endl;
return;
}
else if(p1 == NULL)
{
cout << setw(6) << "学号"
<< setw(8) << "姓名"
<< setw(6) << "年龄" << endl;
cout << setw(6) << pHead->Sno
<< setw(8) << pHead->Sname
<< setw(6) << pHead->Sage << endl;
}
else
{
cout << setw(6) << "学号"
<< setw(8) << "姓名"
<< setw(6) << "年龄" << endl;
cout << setw(6) << p2->Sno
<< setw(8) << p2->Sname
<< setw(6) << p2->Sage << endl;
}
}
void main()
{
while (1)
{
int iChoose = ShowMenu();
switch (iChoose)
{
case 0:
return;
break;
case 1:
InputInfo();
system("pause");
break;
case 2:
PrintInfo();
system("pause");
break;
case 3:
DeleteInfo();
system("pause&qu
ot;);
break;
case 4:
FindName();
system("pause");
break;
case 5:
FindNumber();
system("pause");
break;
}
}
}