实验项目名称 学生学籍管理系统
一、实验目的
通过设计一个小型的应用系统,使学生进一步掌握面向对象的程序设计方法,运用C++中的类与对象的概念结合面向对象程序设计的思想,独立完成应用系统的设计,并养成良好的编程习惯。通过这个实践教学平台,培养学生对计算机应用系统的综合设计能力,培养学生正确分析和解决问题的能力,了解系统开发的过程,逐步熟悉程序设计的方法。
二、实验要求
设计一个程序,对学生的学籍信息进行管理。通过该系统实现对学生基本信息的录入、保存、删除、修改、查询等操作。
设计要求及提示如下:
1、学生基本信息包括:学号、姓名、性别、年龄、班号、专业名称、系别等。
2、使用类和对象的概念实现程序设计。
3、以菜单形式显示各功能项。
4、各功能项如下:
(1)学生基本信息的录入。
(2)学生基本信息的删除。
(3)学生基本信息的修改。
(4)学生基本信息的查询。
(a)按学号查询单个学生信息;
(b)按姓名查询单个学生信息;
©按性别查询学生信息;
(d)按班号查询学生信息;
(5)学生基本信息的统计。
(a)按性别统计学生人数;
(b)按班号统计学生人数;
©按年龄统计学生人数;
(d)按系别统计学生人数;
(6)退出系统。
5、执行某个功能之后,程序将重新显示菜单供用户选择。
6、将学生学籍信息保存到文件中。
三、使用仪器、材料
使用CODE::BLACK编译器以及相关文件。
四.功能模块的设计分析及算法描述。
1.对于学生信息使用结构体Stu来储存;
2.创建一个Student,包含成员数据a[100](结构体Stu数组)和len(表总人数)以及各种成员函数file_cin()(从XS.txt中读取数据);
stu_cin()(录入函数);
stu_delete(int n)(删除函数);
stu_show() (输出到运行框上);
stu_vodify(int i)(修改函数);
stu_check(int flag)(查找函数);
stu_cal(int flag)(统计函数);
save()(输出到XS.txt);
3.使用system(“cls”)和while(cin>>flag)来实现界面的转换;
4.使用重定向来实现数据的读取与保存;(数据被储存在XS.txt中)
5.使用ofstream file(“XS.txt”, ios::trunc)来实现数据的更新;
6.使用文件夹将txt与运行程序放在一起,方便查找;
五.实验过程原始记录(源程序以及程序运行时的效果图)
#include<bits/stdc++.h>
using namespace std;
struct Stu//储存学生信息;
{
int num;//学号
string name;//名字
string sex;//性别
int year;//年龄
int grade;//班级
string major;//专业
};
class Student
{
public:
Stu a[100];
int len;
void file_cin()//从XS.txt中读取数据
{
freopen("XS.txt", "r", stdin);//重定向
int n;cin>>n;
len = n;
for (int i=1; i<=n; i++)
{
cin>>a[i].num>>a[i].name>>a[i].sex>>a[i].year>>a[i].grade>>a[i].major;
}
freopen("CON", "r", stdin);//关闭重定向
}
void stu_cin()//录入函数
{
len++;
cout << "学号: ";
cin>>a[len].num;
cout << "姓名: ";
cin>>a[len].name;
cout << "性别: ";
cin>>a[len].sex;
cout << "年龄: ";
cin>>a[len].year;
cout << "班级: ";
cin>>a[len].grade;
cout << "专业: ";
cin>>a[len].major;
}
void stu_delete(int n)//删除函数
{
for (int i=n; i<len; i++)
{
a[i].num=a[i+1].num;
a[i].name=a[i+1].name;
a[i].sex=a[i+1].sex;
a[i].year=a[i+1].year;
a[i].grade=a[i+1].grade;
a[i].major=a[i+1].major;
}
len--;
}
void stu_show()//输出到运行框上
{
for (int i=1; i<=len; i++)
{
cout << i << " ";
cout << a[i].num << " ";
cout << a[i].name << " ";
cout << a[i].sex << " ";
cout << a[i].year << " ";
cout << a[i].grade << " ";
cout << a[i].major << endl;
}
}
void stu_vodify(int i)//修改函数
{
cout << a[i].num << " "
<< a[i].name << " "
<< a[i].sex << " "
<< a[i].year << " "
<< a[i].grade << " "
<< a[i].major <<endl;
cin>>a[i].num
>>a[i].name
>>a[i].sex
>>a[i].year
>>a[i].grade
>>a[i].major;
}
void stu_check(int flag)//查找函数
{
if (flag==1)//按学号查询单个学生信息
{
cout << "请输入学号: ";
int temp;cin>>temp;
int judge=0;
for (int i=1; i<=len; i++)
{
if (a[i].num==temp)
{
cout << a[i].num << " "
<< a[i].name << " "
<< a[i].sex << " "
<< a[i].year << " "
<< a[i].grade << " "
<< a[i].major <<endl;
judge=1;
}
}
if (judge==0)cout << "数据库中午该学生信息! " << endl;
}
else if (flag==2)//按姓名查询单个学生信息
{
cout << "请输入姓名: ";
string temp;cin>>temp;
int judge=0;
for (int i=1; i<=len; i++)
{
if (a[i].name==temp)
{
cout << a[i].num << " "
<< a[i].name << " "
<< a[i].sex << " "
<< a[i].year << " "
<< a[i].grade << " "
<< a[i].major <<endl;
judge=1;
}
}
if (judge==0)cout << "数据库中午该学生信息! " << endl;
}
else if (flag==3)//按性别查询学生信息
{
cout << "请输入性别: ";
string temp;cin>>temp;
int judge=0;
for (int i=1; i<=len; i++)
{
if (a[i].sex==temp)
{
cout << a[i].num << " "
<< a[i].name << " "
<< a[i].sex << " "
<< a[i].year << " "
<< a[i].grade << " "
<< a[i].major <<endl;
judge=1;
}
}
if (judge==0)cout << "数据库中午该学生信息! " << endl;
}
else if (flag==4)//按班号查询学生信息
{
cout << "请输入班别: ";
int temp;cin>>temp;
int judge=0;
for (int i=1; i<=len; i++)
{
if (a[i].grade==temp)
{
cout << a[i].num << " "
<< a[i].name << " "
<< a[i].sex << " "
<< a[i].year << " "
<< a[i].grade << " "
<< a[i].major <<endl;
judge=1;
}
}
if (judge==0)cout << "数据库中午该学生信息! " << endl;
}
}
void stu_cal(int flag)//统计函数
{
if (flag==1)//按性别统计学生人数
{
map<string ,int> ans;
for (int i=1; i<=len; i++)
{
ans[a[i].sex]++;
}
map<string ,int >::iterator it;
for (it=ans.begin(); it!=ans.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
else if (flag==2)//按班号统计学生人数
{
map<int ,int> ans;
for (int i=1; i<=len; i++)
{
ans[a[i].grade]++;
}
map<int ,int >::iterator it;
for (it=ans.begin(); it!=ans.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
else if (flag==3)//按年龄统计学生人数
{
map<int ,int> ans;
for (int i=1; i<=len; i++)
{
ans[a[i].year]++;
}
map<int ,int >::iterator it;
for (it=ans.begin(); it!=ans.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
else if (flag==4)//按系别统计学生人数
{
map<string ,int> ans;
for (int i=1; i<=len; i++)
{
ans[a[i].major]++;
}
map<string ,int >::iterator it;
for (it=ans.begin(); it!=ans.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
}
}
void save()//输出到XS.txt
{
ofstream file("XS.txt", ios::trunc);
file << len << endl;
for (int i=1; i<=len; i++)
{
file << a[i].num << " ";
file << a[i].name << " ";
file << a[i].sex << " ";
file << a[i].year << " ";
file << a[i].grade << " ";
file << a[i].major << endl;
}
file.close();
}
};
void print_windows()//打印选择功能界面
{
cout << "\t\t\t\t\t\t\t学生学籍管理系统" << endl << endl << endl;
cout << "\t\t\t\t(1)学生基本信息的录入。\t(2)学生基本信息的删除。" << endl<< endl << endl;
cout <<"\t\t\t\t(3)学生基本信息的修改。\t(4)学生基本信息的查询。" << endl<< endl << endl;
cout << "\t\t\t\t(5)学生基本信息的统计。\t(6)退出系统。" << endl << endl << endl;
}
int main()
{
Student stu;//实例化
stu.file_cin();//输入系统数据
print_windows();//打印选择功能界面
int flag;//用户选择的功能
while(cin>>flag)
{
system("cls");
if (flag==6)break;//退出系统
else if (flag<1||flag>6)//非法输入的检查
{
cout << "无效操作" << endl;
continue;
}
else if (flag==1)//学生基本信息的录入
{
cout <<"录入请输入1,返回请输入0" << endl;
int temp_flag;
while(cin>>temp_flag)
{
system("cls");
if (temp_flag==0)
{
break;
}
else
{
stu.stu_cin();
}
cout <<"录入请输入1,返回请输入0" << endl;
}
}
else if (flag==2)//学生基本信息的删除
{
stu.stu_show();
int temp_flag;
cout << "请输入想删除信息的序号或者输入0返回: ";
while(cin>>temp_flag)
{
system("cls");
if (temp_flag==0){
break;}
else if (temp_flag>0&&temp_flag<=stu.len)
{
stu.stu_delete(temp_flag);
stu.stu_show();
cout << "请输入想删除信息的序号或者输入0返回: ";
}
else
{
cout << "输入非法!请再次输入: ";
}
}
}
else if (flag==3)//学生基本信息的修改
{
stu.stu_show();
cout << "请输入需要修改信息的序号或者输入0返回: ";
int temp_flag;
while(cin>>temp_flag)
{
system("cls");
if (temp_flag==0)break;
else if (temp_flag>0&&temp_flag<=stu.len)
stu.stu_vodify(temp_flag);
else cout << "输入非法! 请重新输入: ";
cout << "请输入需要修改信息的序号或者输入0返回: ";
}
}
else if (flag==4)//学生基本信息的查询
{
cout << "(1)按学号查询单个学生信息;" << endl;
cout << "(2)按姓名查询单个学生信息;" << endl;
cout << "(3)按性别查询学生信息;" << endl;
cout << "(4)按班号查询学生信息;" << endl << endl;
cout << "请选择任一项或者输入0返回: ";
int temp_flag;
while(cin>>temp_flag)
{
system("cls");
if(temp_flag==0)break;
else if (temp_flag>0&&temp_flag<=4)
{
stu.stu_check(temp_flag);
}else cout << "输入非法! 请重新输入: ";
cout << "(1)按学号查询单个学生信息;" << endl;
cout << "(2)按姓名查询单个学生信息;" << endl;
cout << "(3)按性别查询学生信息;" << endl;
cout << "(4)按班号查询学生信息;" << endl << endl;
cout << "请选择任一项或者输入0返回: ";
}
}
else if (flag==5)//学生基本信息的统计
{
cout << "(1)按性别统计学生人数;" << endl;
cout << "(2)按班号统计学生人数;" << endl;
cout << "(3)按年龄统计学生人数;" << endl;
cout << "(4)按系别统计学生人数;" << endl;
cout << "请选择一项或者输入0返回: ";
int temp_flag;
while(cin>>temp_flag)
{
system("cls");
if (temp_flag==0)break;
else if (temp_flag>0&&temp_flag<=4)
{
stu.stu_cal(temp_flag);
}else cout << "输入非法! 请重新输入: ";
cout << "(1)按性别统计学生人数;" << endl;
cout << "(2)按班号统计学生人数;" << endl;
cout << "(3)按年龄统计学生人数;" << endl;
cout << "(4)按系别统计学生人数;" << endl;
cout << "请选择一项或者输入0返回: ";
}
}
print_windows();//打印选择功能界面
}
stu.save();
return 0;
}
运行图略
六.实验总结与心得
总结:通过类和结构体可以有效提高代码重用性和可读性;
可以使用文件来储存数据;
面向对象的思想让程序维护更简单,添加功能更方便