资源描述
学生信息管理 (STL<list>)
2010-01-02 21:19:57| 分类: C++ | 标签: |字号大中小 订阅
/////////////////////////////////////////////////////////////////////////////
//Author:wjx
//
//Date:2009-6-12
//
//Function:bool Enter(); //录入
// bool Dele(); //删除
// bool Mod(); //修改
// bool Find(); //查找
//
// int ShowMenu(); //显示菜单
// void ShowInfo(); //显示学生信心
//
//Remark:主要实现对学生信息的管理:支持增、删、改、存储和显示
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;
struct Student //学生
{
int No; //学号
char Name[10]; //姓名
int Comp; //计算机
int Math; //数学
int Engl; //英语
int Phys; //物理
};
bool Enter(); //录入
bool Dele(); //删除
bool Mod(); //修改
bool Find(); //查找
int ShowMenu(); //显示菜单
void ShowInfo(); //显示学生信心
list<struct Student> StuList;
list<struct Student>::iterator pos = StuList.begin();
//判断是否合法。
inline bool IsOK(int x)
{
if (x < 0 || x > 100 )
return false;
else
return true;
}
//判断是否继续测试
inline bool IsKeep()
{
//提示
cout << "返回菜单请输入0, 继续操作请输入1..." << endl;
//接受用户输入
char AnyKey;
cin >> AnyKey;
//清空屏幕
system("cls");
//判断字符
if ( AnyKey == '0')
return true;
else
return false;
}
//主函数
int main(int argc, char* argv[])
{
int rtn = 1;
while (1)
{
switch(rtn)
{
case 1:
rtn = ShowMenu(); //显示菜单
break;
case 2:
Enter(); //录入信息
rtn = 1;
break;
case 3:
Find(); //查找学生
rtn = 1;
break;
case 4:
Mod(); //修改数据
rtn = 1;
break;
case 5:
Dele(); //删除数据
rtn = 1;
break;
case 6:
ShowInfo(); //显示信息
rtn = 1;
break;
default: //输入错误处理
rtn = 1;
break;
}
system("cls"); //清屏操作
}
cout << endl; //换行
system("pause"); //暂停
return 0;
}
//显示菜单
int ShowMenu()
{
int rtn;
cout << "请选择相应指令:" << endl;
cout << " 1、菜单 " ;
cout << " 2、录入 " ;
cout << " 3、查询 " ;
cout << " 4、修改 " ;
cout << " 5、删除 " ;
cout << " 6、显示" << endl;
cin >> rtn; //记录用户选择的菜单项
return (rtn > 0 && rtn < 7) ? rtn : -1 ;
}
//录入信息
bool Enter()
{
Student newstu;
cout << "录入学生信息" << endl;
cout << "请输入学生的学号: " ;
cin >> newstu.No;
//检测当前输入的数据是否有效
while (!IsOK(newstu.No))
{
cout << "错误! 请输入一个正整数!" << endl;
//提示重输,并接受输入
cout << "请输入学生的学号: " ;
cin >> newstu.No;
}
//输入姓名
cout << "请输入学生的姓名: " ;
cin >> newstu.Name;
//输入计算机成绩
cout << "请输入计算机成绩: " ;
cin >> newstu.Comp;
//检测当前输入的数据是否有效
while (!IsOK(newstu.Comp))
{
cout << "错误!请输入0 - 100以内的数值!" << endl;
cout << "请输入计算机成绩: " ;
cin >> newstu.Comp;
}
//输入数学成绩
cout << "请输入数学成绩: " ;
cin >> newstu.Math;
//检测当前输入的数据是否有效
while (!IsOK(newstu.Math))
{
cout << "错误!请输入0 - 100以内的数值!" << endl;
cout << "请输入数学成绩: " ;
cin >> newstu.Math;
}
//输入英语成绩
cout << "请输入英语成绩: " ;
cin >> newstu.Engl;
//检测当前输入的数据是否有效
while (!IsOK(newstu.Engl))
{
cout << "错误!请输入0 - 100以内的数值!" << endl;
cout << "请输入英语成绩: " ;
cin >> newstu.Engl;
}
//输入物理成绩
cout << "请输入物理成绩: " ;
cin >> newstu.Phys;
//检测当前输入的数据是否有效
while (!IsOK(newstu.Engl))
{
cout << "错误!请输入0 - 100以内的数值!" << endl;
cout << "请输入物理成绩: " ;
cin >> newstu.Phys;
}
//换行
cout << endl;
//遍历查询list中的数据
for (pos = StuList.begin(); pos !=StuList.end(); pos++);
{
cout << "同学ID显示 " << (*pos).No << endl;
//判断是否存在该ID
if (newstu.No == (*pos).No)
{
cout << "已存在该同学!" << endl;
return false;
}
}
//插入List中
StuList.push_back(newstu);
cout << "操作成功! " << endl;
if (IsKeep())
{
Enter();
}
return true;
}
//删除信息
bool Dele()
{
int ID;
cout << "删除学生信息" << endl;
cout << "请输入要删除的学号:" ;
cin >> ID;
for (pos = StuList.begin(); pos != StuList.end(); pos++ )
{
if (ID == (*pos).No)
{
//删除操作
StuList.erase(pos);
cout << "已将学号 " << ID << " 信息删除..." << endl;
//是否继续操作
if (IsKeep())
{
Dele();
}
return true;
}
}
system("pause");
return true;
}
//修改学生信息
bool Mod()
{
int ID;
cout << "修改学生信息" << endl;
cout << "请输入要修改的学号:" ;
cin >> ID;
for (pos = StuList.begin(); pos != StuList.end(); pos++ )
{
if (ID == (*pos).No)
{
//删除操作
StuList.erase(pos);
cout << "已将学号 " << ID << " 信息删除..." << endl;
//是否继续操作
if (IsKeep())
{
Dele();
}
return true;
}
}
system("pause");
return true;
}
//查找信息
bool Find()
{
int ID;
cout << "查询学生信息" << endl;
cout << "请输入要查询的学号:" ;
cin >> ID;
//提示
cout << "学号" << "\t" << "姓名" << "\t" << "计算机" << "\t" << "数学" << "\t" << "英语" << "\t" << "物理" << "\t" << endl;
//遍历list
for (pos = StuList.begin(); pos != StuList.end(); ++pos )
{
//查找相同的学号
if (ID == (*pos).No)
{
//提示
cout << (*pos).No << "\t" << (*pos).Name << "\t"<< (*pos).Comp << "\t"<< (*pos).Math << "\t"<< (*pos).Engl << "\t"<< (*pos).Phys << "\t" << endl;
if (IsKeep())
{
Find();
}
//暂停
system("pause");
//返回
return true;
}
}
//提示
cout << "您查找的学号不存在!" << endl;
//暂停
system("pause");
return false;
}
//显示信息
void ShowInfo()
{
cout << "显示学生信息" << endl;
//信息
cout << "学号" << "\t" << "姓名" << "\t" << "计算机" << "\t" << "数学" << "\t" << "英语" << "\t" << "物理" << "\t" << endl;
//遍历list
for(pos = StuList.begin(); pos != StuList.end(); pos++)
{
cout << (*pos).No << "\t" << (*pos).Name << "\t"<< (*pos).Comp << "\t"<< (*pos).Math << "\t"<< (*pos).Engl << "\t"<< (*pos).Phys << "\t" << endl;
}
//暂停
system("pause");
}
展开阅读全文