源代码:
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
class student
{
private:
string num;
string name;
double listen;
double analyze;
double think;
double cplus;
double speak;
double jihe;
double gaodai;
double sum;
public:
void luru(string num1,string name1,double listen1,double analyze1,double think1, double cplus1,double speak1,double jihe1,double gaodai1);
double getsum();
string geth(){return num;}
string getw(){return name;}
double geta(){return listen;}
double getb(){return analyze;}
double getc(){return think;}
double getd(){return cplus;}
double gete(){return speak;}
double getf(){return jihe;}
double getg(){return gaodai;}
double gets() {return sum;}
};
void student::luru(string num1,string name1,double listen1,double analyze1,double think1, double cplus1,double speak1,double jihe1,double gaodai1)
{
num=num1;
name=name1;
listen=listen1;
analyze=analyze1;
think=think1;
cplus=cplus1;
speak=speak1;
jihe=jihe1;
gaodai=gaodai1;
}
double student::getsum()
{
sum=(listen+analyze+think+cplus+speak+jihe+gaodai)/7.0;
}
int main()
{
student e1[40];
double a,b,c,d,e,f,g;
string p,w;
ifstream infile("C:\a\ab.txt"); //录入的文件一定要是txt格式,且各个信息间用一个空格隔开
if(!infile.is_open())
cout<<"can not"<<endl;
else
{
for(int i=1;i<=39;i++)
{
infile>>p>>w>>a>>b>>c>>d>>e>>f>>g;
e1[i].luru(p,w,a,b,c,d,e,f,g);
e1[i].getsum();
}
}
infile.close();
ofstream outfile("C:\a\au.xls"); //这是创建的一个空白Excel文件
if(!outfile.is_open())
{
cout<<"can not"<<endl;
}
else
{
for(int j=1;j<=39;j++){
outfile<<e1[j].geth()<<' '<<e1[j].getw()<<' '<<e1[j].geta()<<' '<<e1[j].getb()<<' '<<e1[j].getc()<<' '<<e1[j].getd()<<' '<<e1[j].gete()<<' '<<e1[j].getf()<<' '<<e1[j].getg()<<' '<<e1[j].gets()<<'
';
}
}
return 0;
}
C:\a\ab.txt 中的信息:如图1
C:\a\au.xls中的信息:如图1