参考以下,部分代码改了即可: 首先将记录储存再TXT文件下,格式如下所示:学号 姓名 性别 等级 笔试 机试 类别
1 张三 男 二级 66 77
2 李四 男 三级 88 99
3 张二 男 二级 40 60
4 李二 女 二级 50 59
5 王五 女 三级 99 99
6 王三 男 二级 77 61
7 刘四 男 四级 60 59
8 刘五 女 二级 88 77
9 张五 女 二级 64 81
10 李六 女 二级 59 30-------------------------------------------------------------代码如下,本人亲自编写,无错误通过--------------------------------------------------------------------#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;void add_information(string str);
void delete_information(string str);
void edit_information(string str);
void scanf_information(string str);
void score_scanf(string str);
void pingfen(string str);
void pass_total(string str);main()
{
cout<<"本程序完成学生信息管理"<<endl;
cout<<"注意功能一定要按顺序执行,一旦平完分数,有些功能不能用,也是本系统的不完善之处!"<<endl;
cout<<"当然可以改进,鉴于时间原因,我就不多加改了,整个模板你可以参考,基本符合要求。。"<<endl;
cout<<endl;
string str;
cout<<"请输入一个学生信息文件名: ";
getline(cin,str);
char ch;
cin.get(ch);
cout<<"(1)信息维护 (2)评分 (3)信息查询 (4)成绩合格统计"<<endl;
cout<<"请输入你所要选择的功能编号: ";
int number;
cin>>number;
string name;//统计用的科目名
switch(number)
{
case 1:
cout<<"(1)增加学生信息 (2)删除学生信息 (3)修改学生信息 (4) 浏览信息"<<endl;
cout<<"请输入你所要选择的功能编号: ";
cin>>number;
switch(number)
{
case 1:
add_information(str);
break;
case 2:
delete_information(str);
break;
case 3:
edit_information(str);
break;
case 4:
scanf_information(str);
break;
default:
cout<<"对不起,你的输入错误!";
break;
}
break;
case 2:
score_scanf(str);
break;
case 3:
pingfen(str);
break;
case 4:
pass_total(str);
break;
default:
cout<<"对不起,你的输入错误!";
break;
} cout<<endl;
return 0;
}
void add_information(string str)
{
ofstream outstream;
outstream.open(str.data(),ios::ate|ios::app);
outstream<<endl;
string name;
cout<<"请输入准考证号、姓名、性别、报考等级、笔试成绩、上机成绩,用空格分开 :"<<endl;
string a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
outstream.precision(6);//显示精度
outstream<<name<<" "<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f;//写入文件末尾
outstream.close();
}
void delete_information(string str)
{
ifstream instream;
instream.open(str.data());
string name_1,name_2;
cout<<"请输入需要删除人的准考证号: ";
cin>>name_1;
string a,b,c,d,e,f;
ofstream outstream;
//暂存的中间文件
outstream.open("temp.txt");
bool flag=0;//是否查找到
while(1)
{
//删除操作,自己感觉都有点麻烦
instream>>a>>b>>c>>d>>e>>f;
if(name_1!=a)
{
outstream<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f;
outstream<<endl;
}
else if (name_1==a) flag=1;//查找到
if(instream.eof()) break;
}
if(!flag) cout<<"没有找到该记录"<<endl;
instream.close();
outstream.close();
//将暂存的文件写回到原文件中
ifstream in;
ofstream out;
in.open("temp.txt");
out.open(str.data());
string line;
while(1)
{
getline(in,line);
out<<line;
out<<endl;
if(in.eof()) break;
}
in.close();
out.close();
}
void edit_information(string str)
{
ifstream instream;
instream.open(str.data());
string name_1,name_2;
cout<<"请输入需要修改人的准考号: ";
cin>>name_1;
string a,b,c,d,e,f;
ofstream outstream;
//暂存的中间文件
outstream.open("temp.txt");
bool flag=0;//是否查找到
while(1)
{
//删除操作,自己感觉都有点麻烦
instream>>a>>b>>c>>d>>e>>f;
if(name_1==a)
{
//找到修改的记录
flag=1;
cout<<"请输入新的准考证号、姓名、性别、报考等级、笔试成绩、上机成绩,成绩类别,用空格分开:";
cin>>a>>b>>c>>d>>e>>f;
}
outstream<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f;
outstream<<endl;
if(instream.eof()) break;
}
if(!flag) cout<<"没有找到该记录"<<endl;
instream.close();
outstream.close();
//将暂存的文件写回到原文件中
ifstream in;
ofstream out;
in.open("temp.txt");
out.open(str.data());
string line;
while(1)
{
getline(in,line);
out<<line;
out<<endl;
if(in.eof()) break;
}
in.close();
out.close();}
void scanf_information(string str)
{
ifstream instream;
instream.open(str.data());
string name_1,name_2;
string a,b,c,d,e,f;
string line;
getline(instream,line);
cout<<line<<endl;
while(1)
{
instream>>a>>b>>c>>d>>e>>f;
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f;
cout<<endl;
if(instream.eof()) break;
}
instream.close();}
void score_scanf(string str)
{
ifstream instream;
instream.open(str.data());
string name_1,name_2;
cout<<"请输入需要查看的类别: ";
cin>>name_1;
string a,b,c,d,e,f,g;
bool flag=0;//是否查找到
while(1)
{
//删除操作,自己感觉都有点麻烦
instream>>a>>b>>c>>d>>e>>f>>g;
if(name_1==g)
{
//找到修改的记录
flag=1;
cout<<"准考证号为:"<<a<<"的信息为:";
cout<<" 姓名 "<<b<<" 性别 "<<c<<" 报考等级:"<<d<<" 笔试成绩 "<<e<<"上机成绩"<<f<<endl;
}
if(instream.eof()) break;
}
if(!flag) cout<<"没有找到该记录"<<endl;
instream.close();
}
void pingfen(string str)
{
ifstream instream;
instream.open(str.data());
string a,b,c,d;
int e,f;
ofstream outstream;
outstream.open("temp.txt");
string line;
getline(instream,line);
outstream<<line;
outstream<<endl;
while(1)
{ instream>>a>>b>>c>>d>>e>>f;
outstream<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" ";
if(e>=85)
{
if(f>=85) outstream<<"优秀";
else if(f>=70 && f<=84) outstream<<"良好";
else if(f>=60 && f<=69) outstream<<"及格";
else outstream<<"不及格";
}
else if(e>=70 && e<=84)
{
if(f>=70) outstream<<"良好";
else if(f>=60 && f<=69) outstream<<"及格";
else outstream<<"不及格";
}
else if(e>=60 && e<=69)
{
if(f>=60) outstream<<"及格";
else outstream<<"不及格";
}
else outstream<<"不及格";
outstream<<endl;
if(instream.eof()) break;
}
instream.close();
outstream.close();
ifstream in;
ofstream out;
in.open("temp.txt");
out.open(str.data());
line="";
while(1)
{
getline(in,line);
out<<line;
out<<endl;
if(in.eof()) break;
}
in.close();
out.close();}
void pass_total(string str)
{
ifstream instream;
instream.open(str.data());
string a,b,c,d,e,f,g;
int count=0;
while(1)
{
//删除操作,自己感觉都有点麻烦
instream>>a>>b>>c>>d>>e>>f>>g;
if(g=="优秀" ||g=="良好" ||g=="及格")
count++;
if(instream.eof()) break;
}
cout<<"合格的人数为:"<<count<<endl;
instream.close();
}
温馨提示:内容为网友见解,仅供参考