学生个人信息管理系统
#include<iostream.h>#include<iomanip.h>
#include<string.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
class student
{
private:
int sno;//学号
char name[8],sex[2];
int age;
char birth[10];//出生日期
char folk[2];//民族
public:
student();
~student();
void luru();//学生信息录入构造函数
void shuchu();//学生信息输出构造函数
void setsno(int temp);
int getsno();
char *getname();
char *getspeciality();
void change();//学生信息修改构造函数
void read();
void write();
};
student::student()
{}
student::~student()
{}
int student::getsno() {return sno;}
char *student::getname() {return name;}
void student::setsno(int temp) {sno=temp;}
void student::luru()
{
ifstream tfile("max.txt",ios::binary|ios::nocreate);
tfile.close();
ofstream ofile("max.txt");
tfile.close();
cout<<"请输入学生信息"<<endl;
cout<<"学生学号"<<endl;
cin>>sno;
cout<<"学生姓名:"<<endl;
cin>>name;
cout<<"性别(男,女):"<<endl;
cin>>sex;
while(strcmp(sex,"男")!=0&&strcmp(sex,"女")!=0)
{ cout<<"错误请重新输入!"<<endl;
cin>>sex;
}
cout<<"年龄:"<<endl;
cin>>age;
cout<<"出生日期:"<<endl;
cin>>birth;
cout<<"民族:"<<endl;
cin>>folk;
}
void student::shuchu()
{
cout<<setw(8)<<setiosflags(ios::left)<<sno<<setw(10)<<setiosflags(ios::left)<<name<<setw(8)<<setiosflags(ios::left)<<sex<<setw(8)<<setiosflags(ios::left)<<age<<setw(10)<<setiosflags(ios::left)<<birth<<setw(8)<<setiosflags(ios::left)<<folk<<endl;
}
void student::change()
{
cout<<"请输入要修改人员的信息:"<<endl;
cout<<"学号是不是要改写(Y/N)"<<endl;
char ch;
cin>>ch;
if(ch=='Y'||ch=='y')
{
cout<<"请输入新学号"<<endl;
cin>>sno;
cout<<"请输入新姓名:"<<endl;
cin>>name;
}
cout<<"性别(男,女):"<<endl;
cin>>sex;
if (strcmp(sex,"男")!=0&&strcmp(sex,"女")!=0)
{
cout<<"错误请重新输入!:"<<endl;
cin>>sex;
}
cout<<"年龄:"<<endl;
cin>>age;
cout<<"出生日期:"<<endl;
cin>>birth;
cout<<"民族:"<<endl;
cin>>folk;
cout<<"你刚刚输入的学生信息如下:"<<endl;
shuchu();
}
student information;//student 类的全局变量
void menu()
{
cout<<" 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓"<<endl;
cout<<" Ψ‖小小的班级管理系统‖Ψ"<<endl;
cout<<" 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓"<<endl;
cout<<" "<<endl;
cout<<" 0.退出系统 "<<endl;
cout<<" "<<endl;
cout<<" 1.学生信息录入 "<<endl;
cout<<" "<<endl;
cout<<" 2.学生信息修改 "<<endl;
cout<<" "<<endl;
cout<<" 3.学生信息删除 "<<endl;
cout<<" "<<endl;
cout<<" 4.学生信息输出 "<<endl;
cout<<" "<<endl;
cout<<" 请选择操作(0=======4) "<<endl;
cout<<" "<<endl;
}
void read()
{
ifstream afile("student.txt",ios::nocreate);
afile.read((char*)&information,sizeof information);
}
void write()
{
ofstream bfile("student.txt",ios::app);
bfile.write((char*)&information,sizeof information);
}
void input(void)
{
char ch;
information.luru();
write();
cout<<"你还要输入下一位学生的信息吗(Y/N)!"<<endl;
cin>>ch;
if(ch=='Y'||ch=='y')
input();
}
void change()
{
int ch,p=0,l=0;
cout<<"请输入你要修改的学生的学号:"<<endl;
cin>>ch;
ifstream efile("student.txt",ios::nocreate);
while(efile.good())
{
efile.seekg(sizeof(information)*p);
if(efile.read((char *) &information,sizeof information))
{
if(ch==information.getsno())
{
information.shuchu();
ch=0;
information.change();
ofstream ffile("student.txt",ios::nocreate|ios::in);
ffile.seekp(sizeof(information)*p);
ffile.write((char *) &information,sizeof information);
ffile.close();
l++;
return;
}
}
p++;
}
efile.close();
if(l!=1)
cout<<"没有你所要修改的学生信息!"<<endl;
}
void shanchu()//删除函数
{
int ch;
cout<<"请输入要删除的学生的学号:"<<endl;
cin>>ch;
ifstream out("student.txt",ios::nocreate);
ofstream in("student1.txt",ios::trunc);
while(out)
{ if(out.read((char*)&information,sizeof information))
if(ch!=information.getsno())
{
in.write((char*)&information,sizeof information);
}
}
in.close();
out.close();
ofstream gfile("student.txt",ios::trunc);
gfile.close();
ifstream ifile("student1.txt",ios::nocreate);
ofstream hfile("student.txt",ios::trunc);
while(ifile)
{
if(ifile.read((char*)&information,sizeof information))
hfile.write((char *)&information,sizeof information);
}
ifile.close();
hfile.close();
}
void output()
{
int i=0;
ifstream cfile("student.txt",ios::nocreate);
while(cfile)
{
if( cfile.read((char *)&information,sizeof information))
{
information.shuchu();
i++;
}
}
cout<<setw(8)<<"学号"<<setw(10)<<"姓名"<<setw(8)<<"姓别"<<setw(8)<<"年龄"<<setw(10)<<"出生日期"<<setw(8)<<"民族"<<endl;
if(i!=0)
cout<<"一共有"<<i<<"个学生信息"<<endl;
else
cout<<"还没有输入学生信息"<<endl;
}
void select()//选择菜单函数
{
int ch;
do{
cin>>ch;
switch(ch)
{
case 0:
exit(0);
menu();
break;
case 1:
input();
menu();
break;
case 2:
change();
menu();
break;
case 3:
shanchu();
menu();
break;
case 4:
output();
menu();
break;
default :
menu();
cout<<"输入无效≌请重新输入(0======== 4)"<<endl;
break;
}
}while(1);
}
void main()
{
menu();
select();
}