当前位置:编程学习 > C/C++ >>

c++编程学生档案库

请选择

1管理员(登入要密码)

2用户

3退出

管理员主菜单

1添加记录

2删除记录

3查询

4返回上级菜单

用户权限

1以年龄查询

2以姓名查询

3以班级查询

4以性别查询

5以学号查询

6退出系统

要求输入信息能够保存在文件中(*.tet)

答案:# include<iostream.h>
# include<string.h>
# include<stdio.h>
# include<stdlib.h>
#include <sstream>
# include<fstream.h>
struct stuatom
{
char name[10];
char zy[20];
char id[20];
char sr[20];
char dh[20];
char sex;

};
class student
{
public:
stuatom ob[50];
void add();
void del();
void chaxunid();
void chaxunzy();
void chaxunxm();
void chaxundh();
};


//*******************************************************************************学号
void student::chaxunid()
{
int i,t;
char xhc[20];
ifstream fin("list.txt");
if(!fin)
cout<<"文件打开失败";
for(i=1;i<=50;i++)
{
fin>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].zy>>ob[i].sr>>ob[i].dh;
}
fin.close();
cout<<"请输入要查找的学生学号";
cin>>xhc;
for(i=1;i<=50;i++)
{
if(strcmp(xhc,ob[i].id)==0)
{
t=i;
cout<<">>>>>>>>>>>>>>>>>>>>该学生的档案为<<<<<<<<<<<<<<<<<<<<"<<endl<< "学号 "<<ob[t].id<<" 姓名 "<<ob[t].name<<" 性别 "<<ob[t].sex<<" 生日 "<<ob[t].sr<<" 专业 "<<ob[t].zy<<" 年龄 "<<ob[t].dh<<endl;
}
}
}
//*******************************************************************************专业
void student::chaxunzy()
{
int i,t;
char xhc[20];
ifstream fin("list.txt");
if(!fin)
cout<<"文件打开失败";
for(i=1;i<=50;i++)
{
fin>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].zy>>ob[i].sr>>ob[i].dh;
}
fin.close();
cout<<"请输入要查找的学生专业";
cin>>xhc;
for(i=1;i<=50;i++)
{
if(strcmp(xhc,ob[i].zy)==0)
{
t=i;
cout<<">>>>>>>>>>>>>>>>>>>>该学生的档案为<<<<<<<<<<<<<<<<<<<<"<<endl<< "学号 "<<ob[t].id<<" 姓名 "<<ob[t].name<<" 性别 "<<ob[t].sex<<" 生日 "<<ob[t].sr<<" 专业 "<<ob[t].zy<<" 年龄 "<<ob[t].dh<<endl;
}
}
}
//*******************************************************************************姓名
void student::chaxunxm()
{
int i,t;
char xhc[20];
ifstream fin("list.txt");
if(!fin)
cout<<"文件打开失败";
for(i=1;i<=50;i++)
{
fin>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].zy>>ob[i].sr>>ob[i].dh;
}
fin.close();
cout<<"请输入要查找的学生姓名";
cin>>xhc;
for(i=1;i<=50;i++)
{
if(strcmp(xhc,ob[i].name)==0)
{
t=i;
cout<<">>>>>>>>>>>>>>>>>>>>该学生的档案为<<<<<<<<<<<<<<<<<<<<"<<endl<< "学号 "<<ob[t].id<<" 姓名 "<<ob[t].name<<" 性别 "<<ob[t].sex<<" 生日 "<<ob[t].sr<<" 专业 "<<ob[t].zy<<" 年龄 "<<ob[t].dh<<endl;
}
}
}
//*******************************************************************************年龄
void student::chaxundh()
{
int i,t;
char xhc[20];
ifstream fin("list.txt");
if(!fin)
cout<<"文件打开失败";
for(i=1;i<=50;i++)
{
fin>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].zy>>ob[i].sr>>ob[i].dh;
}
fin.close();
cout<<"请输入要查找的学生年龄";
cin>>xhc;
for(i=1;i<=50;i++)
{
if(strcmp(xhc,ob[i].dh)==0)
{
t=i;
cout<<">>>>>>>>>>>>>>>>>>>>该学生的档案为<<<<<<<<<<<<<<<<<<<<"<<endl<< "学号 "<<ob[t].id<<" 姓名 "<<ob[t].name<<" 性别 "<<ob[t].sex<<" 生日 "<<ob[t].sr<<" 专业 "<<ob[t].zy<<" 年龄 "<<ob[t].dh<<endl;
}
}
}
//*******************************************************************************删除
void student::del()
{
int n(0),ss;
fstream in;
in.open("list.txt",ios::in);
if(!in)
{
cerr<<"文本打开失败.\n";
abort();
}
char s[80];
cout<<endl<<" - 学生信息表 -"<<endl;
cout<<endl<<" 学号 姓名 性别 专业 生日 年龄 "<<endl;
while (!in.eof())
{
in.getline(s,sizeof(s));
n++;
}
in.close();
int i,t;
char xhc[20];
ifstream fin("list.txt");
if(!fin)
cout<<"文件打开失败";
for(i=1;i<=n;i++)
{
fin>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].zy>>ob[i].sr>>ob[i].dh;
}
fin.close();
cout<<"请输入要删除的学生学号";
cin>>xhc;
for(i=1;i<=50;i++)
{
if(strcmp(xhc,ob[i].id)==0)
{
t=i;
}
}
cout<<">>>>>>>>>>>>>>>>>>>>删除学生的档案为<<<<<<<<<<<<<<<<<<<<"<<endl<< "学号 "<<ob[t].id<<" 姓名 "<<ob[t].name<<" 性别 "<<ob[t].sex<<" 生日 "<<ob[t].sr<<" 专业 "<<ob[t].zy<<" 年龄 "<<ob[t].dh<<endl;
cout<<"确定删除请按1"<<endl;
cin>>ss;
if(ss==1)
{
for(i=t;i<=--n;i++)
{
strcpy(ob[i].name,ob[i+1].name);
ob[i].sex=ob[i+1].sex;
strcpy(ob[i].sr,ob[i+1].sr);
strcpy(ob[i].dh,ob[i+1].dh);
strcpy(ob[i].zy,ob[i+1].zy);
strcpy(ob[i].id,ob[i+1].id);
}
ofstream fout("list.txt");
if(!fout)
cout<<"删除失败";
for(i=1;i<=n;i++)
{
fout<<ob[i].id<<" "<<ob[i].name
<<" "<<ob[i].sex<<" "<<
ob[i].sr<<" "<<ob[i].zy<<" "<<ob[i].dh<<endl;}
fout.close();
}

else
{
cout<<"删除失败"<<endl;
}
} //*******************************************************************************添加
void student::add()
{
int i,b(0);
fstream in;
in.open("list.txt",ios::in);
if(!in)
{
cerr<<"文本打开失败.\n";
abort();
}
char s[80];
while (!in.eof())
{
in.getline(s,sizeof(s));
b++;
}
in.close();
b++;
cout<<"输入要添加学生的信息:"<<endl;
char n[20],e[20],m[20],co[20],d[20]; char xx;
cout<<" 学号: ";
cin>>d;
strcpy(ob[b].id,d);
cout<<" 姓名: ";
cin>>n;
strcpy(ob[b].name,n);
cout<<" 性别(m/f):" ;
cin>>xx;
ob[b].sex=xx;
cout<<" 生日: ";
cin>>m;
strcpy(ob[b].sr,m);
cout<<" 年龄: ";
cin>>co;
strcpy(ob[b].dh,co);
cout<<" 专业: ";
cin>>e;
strcpy(ob[b].zy,e);
cout<<endl<<"----------------------------- 学生信息表 ------------------------------------"<<endl;
cout<<endl<<" 学号 "<<ob[b].id<<" 姓名 "<<ob[b].name<<" 性别 "<<ob[b].sex<<" 生日 "<<ob[b].sr<<" 专业 "<<ob[b].zy<<" 年龄 "<<ob[b].dh<<endl;
cout<<endl<<"添加成功"<<endl;
ofstream fout("list.txt",ios::app);
if(!fout)
{
cou

上一个:C++有什么用
下一个:设置对话框不能移动 C++

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,