#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<iomanip>
using namespace std;
class Tstu //通过定义一个类来定义数据录入的函数
{
char name[20];
int number;
float math,cpp,english,average,sum;
public:
Tstu()
{};
Tstu ( char n[20], int num,float ma,float cj,float eng )
float getsum();//得到总分的函数
float getaver(); //得到平均分的函数
friend void main(); //将主函数定义为友元函数,方便访问类中的成员
};
Tstu::Tstu ( char n[20], int num,float ma,float cj,float eng )
{
strcpy(name,n); //得到姓名
number=num; //得到学号
math=ma; //得到数学成绩
cpp=cj; //得到c++成绩
english=eng; //得到英语成绩
}
float Tstu::getsum() //得到总分的函数
{
sum=cpp+english+math;
return sum;
}
float Tstu::getaver() //得到平均分的函数
{
average=getsum()/3;
return average;
}
void main()
{
cout<<"*****************
欢迎来到学生成绩查询系统
*****************"<<endl;
cout<<"请选择您需要的操作!"<<endl; //菜单的输出
cout<<"操作:"<<endl;
cout<<"(1)数据录入"<<endl;
cout<<"(2)增加学生"<<endl;
cout<<"(3)删除学生"<<endl;
cout<<"(4)修改数据"<<endl;
cout<<"查询:"<<endl;
cout<<"(5)按总成绩查询"<<endl;
cout<<"(6)按姓名查询"<<endl;
cout<<"(7)输出所有学生的数据"<<endl;
cout<<"成绩统计:"<<endl;
cout<<"(8)每个学生的平均成绩"<<endl;
cout<<"(9)各科不及格人数"<<endl;
cout<<"排名:"<<endl;
cout<<"(10)按总分查询排名"<<endl;
cout<<"(11)退出"<<endl;
cout<<"选择相关操作请输入相对的括号里的阿拉伯数字及字母!"<<endl;
int p=0; //全局变量,用于选择菜单时的输入
char w;//全局变量,用于在大项中选择是否继续操作
Tstu *s[100]; //类定义的指针将所录入的数据按类中的形式分别存储
ofstream *file[100];
int i=0; //全局变量,用于做每个录入数据的下标
int j=0;
int chris1=0; //用于判断
do //判断输入是否正确
{
cin>>p;
if((p>=1&&p<=11))
chris1=1;
else
cout<<"指令错误!请重新输入:"<<endl;
}while(chris1==0);
do{
switch(p)
{
case 1://数据的录入
{
char c; //用于在小项中选择是否继续操作
char name[20];
int number;
float math,cpp,english;
do{
cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入学号:"<<enl;
cin>>number;
cout<<"请输入数学成绩:"<<endl;
cin>>math;
cout<<"请输入C++成绩:"<<endl;
cin>>cpp;cout<<"请输入英语成绩:"<<endl;
cin>>english;
fstream outfile,infile; //作一个文本文档在文件夹中用于显示所录入的数据
outfile.open("f1.txt",ios::out|ios::app);
if(!file)
{
cout<<"f1.txt can not open.\n";
return;
}
outfile<<name<<" "<<"学号:"<<number<<" "<<"数学成绩:"<<math<<" "<<"C++成绩:"<<cpp<<" "<<"外语成绩:"<<english<<endl;
outfile.close();
infile.open("f1.txt",ios::in);
if(!file) //判断文件是否打开
{
cout<<"f1.txt can not open.\n";
return;
}
char ch;
while(infile.get(ch))
cout<<ch;
cout<<endl;
infile.close();
j++;
s[i]=new Tstu(name, number,math, cpp, english);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
chris1=0;
do{
if(c!='y'&&c!='n')
{ cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
else
chris1=1;
}while(chris1==0);
}while(c=='y'); //重复输入
break
}
case 2: //增加学生数据
{
char name[20];
int number;
float math,cpp,english;
char c;
do
{
cout<<"请输入您要增加的学生的姓名:"<<endl;
cin>>name;
cout<<"请输入你要增加的学生的学号:"<<endl;
cin>>number;
cout<<"请输入数学成绩:"<<endl;
cin>>math;
cout<<请输入C++成绩:"<<endl;
cin>>cpp;
cout<<"请输入英语成绩:"<<endl;
cin>>english;
fstream outfile,infile;
outfile.open("f1.txt",ios::out|ios::app);
if(!file)
{
cout<<"f1.txt can not open.\n";
return;
}
outfile<<name<<" "<<"学号:"<<number<<" "<<"数学成绩:"<<math<<" "<<"C++成绩:"<<cpp<<" "<<"外语成绩:"<<english<<endl;
outfile.close();
infile.open("f1.txt",ios::in)
if(!file)
{
cout<<"f1.txt can not open.\n";
return;
}
char h;
while(infile.get(h))
cout<<h;
cout<<endl;
infile.close();
j++;
s[i]=new Tstu(name,number, math, cpp, english)
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break;
}
case 3://删除学生信息
{
char c;
string xingming,line,str,name;
do
{
ifstream fin("f1.txt");
fstream
outfile("f2.txt",ios::trunc|ios::out); //作一个文本文档在文件夹中用于显示所录入的数据
cout<<"请输入您要删除的学生的姓名:"<<endl;
cin>>xingming;
while(!fin.eof() )
{
getline(fin,line);
istringstream stream(line);
name = line.substr(0,line.find(" ",0));//找到名字,判断是否相等
if(name != xingming)
outfile<<line<<endl;
}
outfile.close();
fin.close();
fstream f("f1.txt",ios::trunc|ios::out);
ifstream outf("f2.txt");
f<<outf.rdbuf();
outf.close();
f.close();
cout<<"数据删除成功,想继续删除吗(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break
}
case 4://修改数据
{
string sname,line,str,name;
int nu;float eng,ma,com;int flag;char c;
if(i==0)
{
cout<<"系统中没有输入数据,请先输入数据!"<<endl;break;
}
do
{
flag=0;
cout<<"请输入您要修改的学生的姓名:";
cin>>name;
fstream fin("f1.txt",ios::in);
ofstream outfile("f.txt",ios::trunc|ios::out);
cout<<endl; while(!fin.eof() ) //在文件中查找
{
getline(fin,line);
istringstream stream(line);
sname = line.substr(0,line.find(" ",0));
if(name!=sname)
{
outfile<<line<<endl;
}
else flag=1;
}
fin.close();
if(flag=1)
{
cout<<"请输入修改学生姓名及其新学号,数学,c++和英语成绩:";
cin>>name>>nu>>ma>>com>>eng;
cout<<endl;
outfile<<name<<" "<<"学号:"<<nu<<" "<<"数学成绩:"<<ma<<" "<<"C++成绩:"<<com<<" "<<"外语成绩:"<<eng<<endl;
outfile.close();
fstream outf("f1.txt",ios::trunc|ios::out);
fstream inf("f.txt",ios::in);
outf<<inf.rdbuf()<<endl;
outf.close();
inf.close();
}
if(flag=0)cout<<对不起,您要修改的学生不存在!请查证后输入!"<<endl;
if(i==0)
{
cout<<系统中没有输入数据,请先输入数据!"<<endl;break;
}
int m,n,k,j;m=0;n=0;k=0;
for(j=0;j<i;j++)
{
if(s[j]->math<60)m++;
if(s[j]->cpp<60)n++;
if(s[j]->english<60)k++;
}
cout<<"数学不及格人数为:"<<m<<endl;
cout<<"c++不及格人数为:"<<n<<endl;
cout<<"英语不及格人数为:"<<k<<endl;
break;
}
case 5://按总分排名
{
if(i==0)
cout<<"系统中没有输入数,请先输入数据!"<<endl;break;
}
int h,g,n;int j=0;float a[100],temp;
for(g=0;g<i;g++)
{
a[j]=(*s[g]).getsum();j++;
}
for(h=0;h<i;h++)
{for(n=0;n<i-h;n++)
if(a[n]>a[n+1])
{
temp=a[n];a[n]=a[n+1];a[n+1]=temp;
}
for(n=1;n<=i;n++)
cout<<a[n]<<endl;
break;
}
cout<<" 您想继续进行其他操作吗?(y/n):";
int flag=0;
do
{
cin>>w;
cout<<endl;
if(w!='y'&&w!='n')
cout<<" 指令错误!请重新输入!"<<endl;
else
flag=1;
}while(flag==0);
if(w=='y')
{
cout<<"(1)数据录入"<<endl;
cout<<"(3)删除学生"<<endl;
cout<<"(4)修改数据"<<endl;
cout<<"查询:"<<endl;
cout<<"(5)按总成绩查询"<<endl;
cout<<"(6)按姓名查询"<<endl;
cout<<"(7)输出所有学生的数据"<<endl;
cout<<"成绩统计:"<<endl;
cout<<"(8)每个学生的平均成绩"<<endl;
cout<<"(9)各科不及格人数"<<endl;
cout<<"排名:"<<endl;
cout<<"(10)按总分查询排名"<<endl;
cout<<"(11)退出"<<endl;
cout<<"选择相关操作请输入相对的括号里的阿拉伯数字及字母!"<<endl;
cin>>p;
}
}while(w=='y');
}
课程设计
最新推荐文章于 2024-03-12 12:49:14 发布