代码:
#include <fstream.h>
#include <iostream>
#include <string>
class xinxi//声明一个类为信息类
{
public:
void setn(){cin>>name;}//输入姓名
void setd()
{cin>>day;}//输入日期
void setk()
{cin>>n;}//输入课时
void setm()
{cin>>kname;}//输入课程名称
void setl()
{cin>>lx;}//输入缺课类型
char * getn()
{return name;}
char * getd()
{return day;}
int getk()
{return n;}
char * getm()
{return kname;}
char * getl()
{return lx;}
private:
char day[20];
char name[20];
int n;
char kname[20];
char lx[20];
};
void shuru()//输入函数,录入学生信息
{
int j, i;
xinxi x[20];
fstream file;
file.open("D:\\学生考勤.txt",ios::in|ios::out);//文件存盘
cout<<"请输入要录入几名同学信息:";
cin>>j;
cout<<"请输入"<<j<<"名同学信息:"<<endl;
for(i=0;i<j;i++)
{
cout<<"请输入姓名:"; //调用函数
x[i].setn();
cout<<"请输入日期:";
x[i].setd();
cout<<"请输入课时:";
x[i].setk();
cout<<"请输入课程名称:";
x[i].setm();
cout<<"请输入缺课类型:";
x[i].setl();
file.write((char *)&x[i], sizeof(x[i]));//写文件
}
cout<<"已经成功输入同学信息!"<<endl;
file.close();//关闭文件
}
void chaxun()//查询函数,查询学生的缺勤情况
{
xinxi st;
char name[20],z;
fstream f;
a1: int a=0,flag=0;// goto语句
f.open("D:\\学生考勤.txt",ios::in);
cout<<"请输入您要查询的同学姓名:"<<endl;
cin>>name;
f.read((char *)&st, sizeof(st));
while(!f.eof())//原文件到头结束
{
if(!strcmp(name, st.getn()))
{
cout<<"学生姓名:"<<st.getn()<<endl;
cout<<"缺课日期:"<<st.getd()<<endl;
cout<<"所缺课时:"<<st.getk()<<endl;
cout<<"课程名称:"<<st.getm()<<endl;
cout<<"缺课类型:"<<st.getl()<<endl;
flag=1;
break;
}
f.read((char *)&st, sizeof(st));
}
f.close();
if(flag==0)
{
cout<<"您查询的同学无缺课现象,请返回"<<endl;
}
cout<<"如继续查询请输入Y,否则按任意键返回"<<endl;
cin>>z;
if(z=='Y'||z=='y')
goto a1;
}
void xiugai() //修改函数
{
xinxi l;
char xname[20], w;
fstream xf;
xf.open("D:\\学生考勤.txt",ios::in|ios::out);
xf.read((char *)&l, sizeof(l));//读文件到L里
cout<<"请输入您要修改的同学姓名:"<<endl;
cin>>xname;
while(!xf.eof())//调用EOF判断函数
{
if(!strcmp(xname, l.getn()))
{
cout<<"学生姓名:"<<l.getn()<<endl;
cout<<"缺课日期:"<<l.getd()<<endl;
cout<<"所缺课时:"<<l.getk()<<endl;
cout<<"课程名称:"<<l.getm()<<endl;
cout<<"缺课类型:"<<l.getl()<<endl;
cout<<"确认修改请按Y,否则按任意键"<<endl;
cin>>w;
if(w=='Y'||w=='y')
{
cout<<"学生姓名:";
l.setn();
cout<<"缺课日期:";
l.setd();
cout<<"所缺课时:";
l.setk();
cout<<"课程名称:";
l.setm();
cout<<"缺课类型:";
l.setl();
xf.open("D:\\学生考勤.txt",ios::in);
xf.write((char *)&l, sizeof(l));
}
else
{
break;
}
}
xf.read((char *)&l, sizeof(l));//读文件到L里
}
xf.close();
}
void renci() //统计人数函数
{
int i=0,j=1,x=0,y=0;
xinxi s;
fstream d;
d.open("D:\\学生考勤.txt",ios::in);
d.read((char *)&s, sizeof(s));
i=1;
cout<<"早退:"<<x<<endl;
cout<<"请假:"<<y<<endl;
cout<<"旷课:"<<i<<endl;
cout<<"迟到:"<<j<<endl;
cout<<"在"<<s.getd()<<"有:"<<endl;
{
cout<<"课程名称:"<<s.getm()<<endl;
}
d.read((char *)&s, sizeof(s));//读文件到L里
d.close(); //关闭
}
void tongji()
{
int i=0,j=1,x=0,y=0;
xinxi m;
fstream b;
b.open("D:\\学生考勤.txt",ios::in);
b.read((char *)&m, sizeof(m));
i=1;
cout<<"早退次数:"<<x<<endl;
cout<<"请假次数:"<<y<<endl;
cout<<"旷课次数:"<<i<<endl;
cout<<"迟到次数:"<<j<<endl;
cout<<"在"<<m.getd()<<"有:"<<endl;
{
cout<<"姓名:"<<m.getn()<<endl;
}
b.read((char *)&m, sizeof(m));//读文件到L里
b.close();
}
int main()
{
int c;
while(1)
{
cout<<"\t\t***********************************************"<<endl;
cout<<"\t\t************欢迎进入学生考勤系统**************"<<endl;
cout<<"\t\t************************************************"<<endl;
cout<<"\t\t************************************************"<<endl;
cout<<"\t\t***1、录入学生的缺课记录************************"<<endl;
cout<<"\t\t "<<endl;
cout<<"\t\t***2、修改某个学生的缺课记录********************"<<endl;
cout<<"\t\t "<<endl;
cout<<"\t\t***3、查询某个学生的缺课情况********************"<<endl;
cout<<"\t\t "<<endl;
cout<<"\t\t***4、统计一段时间内旷课学生姓名及旷课次数******"<<endl;
cout<<"\t\t "<<endl;
cout<<"\t\t***5、统计一段时间内有学生旷课的课程和人次******"<<endl;
cout<<"\t\t "<<endl;
cout<<"\t\t***6、退出**************************************"<<endl;
cout<<"\t\t "<<endl;
cout<<"\t\t************************************************" <<endl <<endl <<"\t\t";
cout<<"\t\t******请选择您所需要的信息******"<<endl;
cin>>c;
switch(c)
{
case 1:shuru();break;
case 2:xiugai();break;
case 3:chaxun();break;
case 4:tongji();break;
case 5:renci();break;
case 6: break;
}
if(c==6)
{
cout<<"退出 !"<<endl;
break;
}
}
return 0;
}