资源描述
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include <stdlib.h>
#include <time.h>
//using namespace std;
const int count =3;
class situaction;
class student
{ public:
int sno;
char sname[20];
char sex[3];
int age;
char depart[20];
char classno[20];
char lianx[40];
//public:
friend void sselect();
friend void supdate();
friend void sdelet();
friend void saccount();
friend void sdisplay();
friend istream & operator >> (istream & in,student &stu)
{
in>>stu.sno>>stu.sname>>stu.sex>>stu.age>>stu.depart>>stu.classno>>stu.lianx;
return in;
}
friend ostream & operator<< ( ostream & out,student &stu)
{
out<<stu.sno<<" "<<stu.sname<<" "<<stu.sex<<" "<<stu.age<<" "<<stu.depart<<" "<<stu.classno<<" "<<stu.lianx<<endl;
return out;
}
friend void xkadd();
friend void sadd();
};
void sadd()
{ while(1)
{
int flag1=0;
student s;
cout<<"请输入要添加的学生的学号、姓名、性别、年龄、系别、班级、联系方式"<<endl;
cin>>s;
ifstream in("e:\\studentfile.txt");
if(!in)
{
cout<<"打开学生信息文件失败!"<<endl;
return ;
}
student stu;
in.read((char*)&stu,sizeof(student));
while(!in.eof())
{ if(s.sno==stu.sno) {flag1=1; break;}
in.read((char*)&stu,sizeof(student));
}
in.close();
if(flag1==1) cout<<"该学生已经在文件中存在不允许添加!" <<endl;
else
{ ofstream out("e:\\studentfile.txt",ios::app);
out.write((char*)&s,sizeof(student));
out.close();
}
system("pause");
break;
}
}
void sselect()
{ while(1)
{ system("cls");
cout<<"***********请选择要查询的类别:*************"<<endl;
cout<<"*********** 按学生学号 请按1:************* "<<endl;
cout<<"*********** 按学生姓名 请按2:*************"<<endl;
cout<<"*********** 按学生系别 请按3:*************"<<endl;
cout<<"*********** 按学生班级 请按4:*************"<<endl;
cout<<"*********** 返回上一级界面 请按5:*************"<<endl;
int sn;
cin>>sn;
while(sn>5||sn<1)
{
cout<<"选择的数据无效,请重新输入:"<<endl;
cin>>sn;
}
cout<<endl;
ifstream in("e:\\studentfile.txt");
if(!in)
{ cout<<"打开学生信息文件失败!"<<endl;
return ;
}
int flag=0;
switch(sn)
{
case 1: {
int no;
cout<<"请输入要查询的学生的学号"<<endl;
cin>>no;
student stu;
in.read((char*)&stu,sizeof(student));
while(!in.eof())
{ if(no==stu.sno) {cout<<stu<<endl; flag=1;}
in.read((char*)&stu,sizeof(student));
}
break;
}
case 2: {
char name[20];
cout<<"请输入要查询的学生的姓名"<<endl;
cin>>name;
student stu;
in.read((char*)&stu,sizeof(student));
while(!in.eof())
{ if(strcpy(name,stu.sname)==0) {cout<<stu<<endl; flag=1;}
in.read((char*)&stu,sizeof(student));
}
break;
}
case 3: {
char dep[20];
cout<<"请输入要查询的学生的系别"<<endl;
cin>>dep;
student stu;
in.read((char*)&stu,sizeof(student));
while(!in.eof())
{ if(strcmp(dep,stu.depart)==0) {cout<<stu<<endl; flag=1;}
in.read((char*)&stu,sizeof(student));
}
break;
}
case 4: {
char clno[20];
cout<<"请输入要查询的学生的班级"<<endl;
cin>>clno;
student stu;
in.read((char*)&stu,sizeof(student));
while(!in.eof())
{ if(strcmp(clno,stu.classno)==0) {cout<<stu<<endl; flag=1;}
in.read((char*)&stu,sizeof(student));
}
break;
}
case 5: return ;
}
in.close();
if(flag==0) cout<<"要查询的学生在文件不存在!" <<endl;
system("pause");
}
}
void supdate()
{ while(1)
{ system("cls");
cout<<"***********请选择按下列类别查询后修改:*************"<<endl;
cout<<"*********** 按学生学号 请按1:************* "<<endl;
cout<<"*********** 按学生姓名 请按2:*************"<<endl;
cout<<"*********** 按学生系别 请按3:*************"<<endl;
cout<<"*********** 按学生班级 请按4:*************"<<endl;
cout<<"*********** 返回上一级界面 请按5:*************"<<endl;
int sn;
cin>>sn;
while(sn>5||sn<1)
{
cout<<"选择的数据无效,请重新输入:"<<endl;
cin>>sn;
}
cout<<endl;
fstream in("e:\\studentfile.txt",ios::in|ios::out);
if(!in)
{ cout<<"打开学生信息文件失败!"<<endl;
return ;
}
int flag=0;
switch(sn)
{
case 1: {
int no;
cout<<"请输入要查询的学生的学号"<<endl;
cin>>no;
student stu;
int i=0;
in.seekg(i*sizeof(student),ios::beg);
in.read((char*)&stu,sizeof(student));
while(!in.eof())
{ if(no==stu.sno)
{ cout<<stu<<endl;
cout<<"请输入要修改的学生的学号、姓名、性别、年龄、系别、班级、联系方式"<<endl;
cin>>stu;
in.seekp(i*sizeof(student),ios::beg);
in.write((char*)&stu,sizeof(student));
flag=1;
}
i++;
in.seekg(i*sizeof(student),ios::beg);
in.read((char*)&stu,sizeof(student));
}
break;
}
case 2:
case 3:
case 4:
case 5: return ;
}
in.close();
if(flag==0) cout<<"要修改的学生在文件不存在!" <<endl;
system("pause");
}
}
void sdelet()
{ while(1)
{ system("cls");
cout<<"***********请选择按下列类别查询后删除:*************"<<endl;
cout<<"*********** 按学生学号 请按1:************* "<<endl;
cout<<"*********** 按学生姓名 请按2:*************"<<endl;
cout<<"*********** 按学生系别 请按3:*************"<<endl;
cout<<"*********** 按学生班级 请按4:*************"<<endl;
cout<<"*********** 返回上一级界面 请按5:*************"<<endl;
int sn;
cin>>sn;
while(sn>5||sn<1)
{
cout<<"选择的数据无效,请重新输入:"<<endl;
cin>>sn;
}
cout<<endl;
student stu[20];
int flag=0;
switch(sn)
{
case 1: { ifstream in("e:\\studentfile.txt");
if(!in)
{ cout<<"打开学生信息文件失败!"<<endl;
return ;
}
int n=0;
in.read((char*)&stu[n],sizeof(student));
while(!in.eof())
{ n++;
in.read((char*)&stu[n],sizeof(student));
}
in.close();
int no;
cout<<"请输入要删除的学生的学号"<<endl;
cin>>no;
for(int i=0;i<n;i++)
{ if(no==stu[i].sno)
{ cout<<stu[i]<<endl;
cout<<"是否要删除该学生的信息!"<<endl;
char ch;
cin>>ch;
if( ch='Y'||ch=='y')
{
for(int j=i;j<n;j++)
stu[j]=stu[j+1];
n--;
}
flag=1;
}
}
if(flag==0) cout<<"要修改的学生在文件不存在!" <<endl;
else
{
ofstream out("e:\\studentfile.txt");
if(!out)
{ cout<<"打开学生信息文件失败!"<<endl;
return ;
}
for(int k=0;k<n;k++)
{ out.seekp(k*sizeof(student),ios::beg);
out.write((char*)&stu[k],sizeof(student));
}
out.close();
}
break;
}
case 2:
case 3:
case 4:
case 5: return ;
}
system("pause");
}
}
void saccount()
{ ifstream ifs("e:\\studentfile.txt");
if(!ifs)
{
cout<<"打开文件失败!";
system("pause");
return ;
}
int i=0;
student s;
ifs.read((char*)&s,sizeof(student));
while(!ifs.eof())
{
i++;
ifs.read((char*)&s,sizeof(student));
}
ifs.close();
cout<<"学生:"<<i<<"人"<<endl;
ifs.close();
system("pause");
}
void sdisplay()
{ ifstream in("e:\\studentfile.txt",ios::in);
if(!in)
{
cout<<"打开学生信息文件失败!"<<endl;
return ;
}
student stu;
in.read((char*)&stu,sizeof(student));
while(!in.eof())
{ cout<<stu<<endl;
in.read((char*)&stu,sizeof(student));
}
in.close();
system("pause");
}
class course
{ public:
int cno;
char cname[20];
char cxz[10];
int zxs;
int xf;
char kcxq[10];
int xxrs;
//public:
friend void cadd();
friend void cselect();
friend void cupdate();
friend void cdelet();
friend void caccount();
friend void cdisplay();
friend istream & operator >> (istream & in, course &cou)
{
in>>o>>ame>>cou.cxz>>cou.zxs>>cou.xf>>cou.kcxq>>cou.xxrs;
return in;
}
friend ostream & operator <<(ostream & out,course &cou)
{ out<<o<<" "<<ame<<" "<<cou.cxz<<" "<<cou.zxs<<" "<<cou.xf<<" "<<cou.kcxq<<" "<<cou.xxrs<<endl;
return out;
}
friend void xkadd();
};
void cadd()
{ int flag1=0;
course c;
cout<<"请输入要添加的课程的课程代码、课程名称、课程性质、总学时、学分、开课学期、选修人数"<<endl;
cin>>c;
ifstream in("e:\\coursefile.txt");
if(!in)
{
cout<<"打开课程信息文件失败!"<<endl;
return ;
}
course cou;
in.read((char*)&cou,sizeof(course));
while(!in.eof())
{ if(o==o) {flag1=1; break;}
in.read((char*)&cou,sizeof( course));
}
in.close();
if(flag1==1) cout<<"该课程已经在文件中存在不允许添加!" <<endl;
else
{ ofstream out("e:\\coursefile.txt",ios::app);
out.write((char*)&c,sizeof(course));
out.close();
}
system("pause");
}
void cselect()
{while(1)
{ system("cls");
cout<<"*********** 请选择要查询的类别:*************"<<endl;
cout<<"*********** 按课程编号 请按1:************* "<<endl;
cout<<"*********** 按课程名称 请按2:*************"<<endl;
cout<<"*********** 返回上一级界面 请按3:*************"<<endl;
int sn;
cin>>sn;
while(sn>3||sn<1)
{
cout<<"选择的数据无效,请重新输入:"<<endl;
cin>>sn;
}
cout<<endl;
ifstream in("e:\\coursefile.txt");
if(!in)
{ cout<<"打开课程信息文件失败!"<<endl;
return ;
}
int flag=0;
switch(sn)
{
case 1: {
int no;
cout<<"请输入要查询的课程的编号"<<endl;
cin>>no;
course ke;
in.read((char*)&ke,sizeof(course));
while(!in.eof())
{ if(no==o) {cout<<ke<<endl; flag=1;}
in.read((char*)&ke,sizeof(course));
//if(no==ke. char*)&ke,sizeof(course));
}
break;
}
case 2: {
char name[25];
cout<<"请输入要查询的课程的名称"<<endl;
cin>>name;
course ke;
in.read((char*)&ke,sizeof(course));
while(!in.eof())
{ if(strcpy(name,ame)==0) {cout<<ke<<endl; flag=1;}
in.read((char*)&ke,sizeof(course));
}
break;
}
case 3
展开阅读全文