【求助】怎样在查询函数中求出他的平均分呢,string转化为float很难
功能需求 1) 输入学生基本信息及所选课程、课程成绩2) 根据学生姓名和选课名称修改课程成绩
3) 查询所有学生的所有选课信息
4)根据名字查询某个学生的选课信息及所有选课的总平均成绩。
下面是我编的程序:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
#define M 200 //可以存信息总量
int N=0; //学生总量
class student
{
public:
student();
void set(); //初始化信息,第一次对信息的录入
void change(); //修改某学生信息
void search(); //查询某学生信息
void display(); //显示全部学生信息
private:
string name,sex,Math,Physical,Computer,English,Score;static int num;
};
int student::num=110;
student::student() {}
void student::set()
{
int i;
string setname,setMath,setPhysical,setComputer,setEnglish,setsex;
ofstream outfile;
outfile.open("f1.txt",ios::out); //----打开文件----
if(!outfile)
{
cerr<<" open error"<<endl;
exit(1);
}
//----依次录入相应个数的学生信息----
for(i=0;i<M;i++)
{
cout<<"当姓名输入为'0'时,停止输入!!!"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>setname;
name=setname;
if(setname=="0") break;
if(setname!="0")
{
N++;
num++;
outfile<<num<<" ";
outfile<<setname<<" ";
cout<<"请输入性别:"<<endl;
cin>>setsex;
sex=setsex;
outfile<<setsex<<" ";
cout<<"请输入数学成绩:"<<endl;
cin>>setMath;
Math=setMath;
outfile<<setMath<<" ";
cout<<"请输入物理成绩:"<<endl;
cin>>setPhysical;
Physical=setPhysical;
outfile<<setPhysical<<" ";
cout<<"请输入计算机成绩:"<<endl;
cin>>setComputer;
Computer=setComputer;
outfile<<setComputer<<" ";
cout<<"请输入英语成绩:"<<endl;
cin>>setEnglish;
English=setEnglish;
outfile<<setEnglish<<" "<<endl;
}
}
outfile.close(); //----关闭文件----
}
void student::change()
{
int i,k;
int num[M];
string name[M],sex[M],Math[M],Physical[M],Computer[M],English[M];
ifstream infile("f1.txt",ios::in);
if(!infile)
{
cerr<<" open error"<<endl;
exit(1);
}
for(i=0;i<N;i++)
{
infile>>num[i];
cout<<setw(9)<<num[i];
infile>>name[i];
cout<<setw(9)<<name[i];
infile>>sex[i];
cout<<setw(9)<<sex[i];
infile>>Math[i];
cout<<setw(9)<<Math[i];
infile>>Physical[i];
cout<<setw(9)<<Physical[i];
infile>>Computer[i];
cout<<setw(9)<<Computer[i];
infile>>English[i];
cout<<setw(9)<<English[i]<<endl;
}
infile.close();
ofstream outfile("f1.txt",ios::out);
if(!outfile)
{
cerr<<" open error"<<endl;
exit(1);
}
cout<<"你想修改第几个学生的成绩?"<<endl;
cin>>k;
k=k-1;
cout<<"请输入新的数据:"<<endl;
cout<<"请输入姓名:"<<endl;
cin>>name[k];
cout<<"请输入性别:"<<endl;
cin>>sex[k];
cout<<"请输入数学成绩:"<<endl;
cin>>Math[k];
cout<<"请输入物理成绩:"<<endl;
cin>>Physical[k];
cout<<"请输入计算机成绩:"<<endl;
cin>>Computer[k];
cout<<"请输入英语成绩:"<<endl;
cin>>English[k];
for(i=0;i<N;i++)
{
outfile<<num[i]<<" ";
outfile<<name[i]<<" ";
outfile<<sex[i]<<" ";
outfile<<Math[i]<<" ";
outfile<<Physical[i]<<" "<<endl;
outfile<<Computer[i]<<" ";
outfile<<English[i]<<" ";
}
outfile.close();
cout<<"已修改成功!"<<endl;
}
void student::search()
{
int i,w=0,num[M],a[M];
string searchname,name[M],sex[M],Math[M],Phyiscal[M],Computer[M],English[M],b[M][6];
ifstream infile("f1.txt",ios::in);
if(!infile)
{
cerr<<" open error"<<endl;
exit(1);
}
for(i=0;i<N;i++)
{
infile>>num[i];a[i]=num[i];
infile>>name[i];b[i][0]=name[i];
infile>>sex[i];b[i][1]=sex[i];
infile>>Math[i];b[i][2]=Math[i];
infile>>Physical[i];b[i][3]=Physical[i];
infile>>Computer[i];b[i][4]=Computer[i];
infile>>English[i];b[i][5]=English[i];
}
cout<<"请输入姓名!"<<endl;
cin>>searchname;
for(i=0;i<N;i++)
if(name[i]==searchname)
{
w=1;
cout<<" 学号 姓名 性别 数学 物理 计算机 英语"<<endl;
cout<<" ";
cout<<setw(9)<<a[i];
cout<<setw(9)<<b[i][0]<<" ";
cout<<setw(9)<<b[i][1]<<" ";
cout<<setw(9)<<b[i][2]<<" ";
cout<<setw(9)<<b[i][3]<<" ";
cout<<setw(9)<<b[i][4]<<" ";
cout<<setw(9)<<b[i][5]<<" "<<endl;
}
if(w==0)
{cout<<"无此人信息!"<<endl;
}
}
void student::display()
{
int k,num;
string name,sex,Math,Physical,Computer,English;
ifstream infile("f1.txt",ios::in);
if(!infile)
{
cerr<<" open error"<<endl;
exit(1);
}
cout<<" 学号 姓名 性别 数学 物理 计算机 英语"<<endl;
for(k=0;k<N;k++)
{
cout<<" ";
infile>>num;
cout<<setw(9)<<num;
infile>>name;
cout<<setw(9)<<name;
infile>>sex;
cout<<setw(9)<<sex;
infile>>Math;
cout<<setw(9)<<Math;
infile>>Physical;
cout<<setw(9)<<Physical;
infile>>Computer;
cout<<setw(9)<<Computer;
infile>>English;
cout<<setw(9)<<English<<endl;
}
infile.close();
}
student stu;
void main()
{
char ch;
void menu();
menu();
cin>>ch;
while(ch!='0')
{
system("cls");
switch(ch)
{
case '1':stu.set();break;
case '2':stu.change();break;
case '3':stu.search();break;
case '4':stu.display();break;
case '0':cout<<"退出系统!";break;
default:cout<<"没有此选项!"<<endl;break;
}
menu();
cin>>ch;
}
}
void menu()
{
cout<<"******************"<<endl;
cout<<"* 1.输入 *"<<endl;
cout<<"* 2.修改 *"<<endl;
cout<<"* 3.查询 *"<<endl;
cout<<"* 4.显示全部 *"<<endl;
cout<<"* 0.退出 *"<<endl;
cout<<"******************"<<endl;
}