从一个表格文件中录入信息,进行计算后,在文件中输出这个表格

源代码:

#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()<<'\t'<<e1[j].getw()<<'\t'<<e1[j].geta()<<'\t'<<e1[j].getb()<<'\t'<<e1[j].getc()<<'\t'<<e1[j].getd()<<'\t'<<e1[j].gete()<<'\t'<<e1[j].getf()<<'\t'<<e1[j].getg()<<'\t'<<e1[j].gets()<<'\n';
        }
    }
    return 0;
}


C:\\a\\ab.txt 中的信息:如图1

C:\\a\\au.xls中的信息:如图1


posted on 2016-05-18 07:50  我是蒟蒻  阅读(109)  评论(0编辑  收藏  举报

导航