달력

122025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Group.cpp

Programs 2014. 8. 14. 20:12

#include "Inc.hpp"

//클래스 초기화 함수, 처리필드목록을 읽고, 출력파일,로그파일 open
//필드목록 위치와  출력파일 위치를 매개변수로 하여 클래스 초기화
void Group::Init(char* path_label, char* path_key, char* path_out, long MaxPeriod, char* grptype)
{
    //각종 클래스변수 초기화
    strcpy(this->path_label,     path_label);
    strcpy(this->path_ofile    , path_out);   strcat(this->path_ofile,         ".TXT");
    strcpy(this->path_ofile_grp, path_out);   strcat(this->path_ofile_grp, "_grp.TXT");
    strcpy(this->path_ofile_sub, path_out);   strcat(this->path_ofile_sub, "_grp_key");
    strcpy(this->GrpType, grptype);

    FieldInfo.Init(path_label, GrpType);

    this->MT_MaxPeriod    = MaxPeriod;
    this->MT_CurrentArray = 0; 
    this->FieldMaxNum     = FieldInfo.iFieldNumMax();   //필드갯수 상한은 500개!!!
    this->FileNum         = 0;
    this->MergeNum        = 0;
    this->FieldTotNum     = FieldInfo.iFieldNumTot();
    this->FieldORGNum     = FieldInfo.iFieldNumOrg();
    this->FieldGrpNum     = FieldInfo.iFieldNumGrp();
    this->MT_GrpNum       = 0;
    for (int i=0; i<10000; i++) this->MT_GrpKeyIndex[i] = 0;

    //출력파일 open
    OutputFile.open(path_ofile, ofstream::out | ofstream::trunc);
    if (OutputFile==NULL)
    {
        gLogFile << "Error : Out File missing !!! : " << path_ofile << endl;
        cout     << "Error : Out File missing !!! : " << path_ofile << endl;
        system("PAUSE"); exit(0);
    }

    //결과파일 첫행에 필드 레이블 출력
    OutputFile  << "product" << "," << "spcode" << "," << "month";
    for (int j=0; j<FieldORGNum; j++)
    {
        OutputFile    << "," << FieldInfo.cFieldName(j);
    }
    OutputFile    << endl;

    //결과파일 저장용 배열 초기화
    for(int i=0; i<3; i++)
    {
        MT_Result_val[i] = new double* [MT_MaxPeriod];

        for(int j=0; j<MT_MaxPeriod; j++)
        {
            MT_Result_val[i][j] = new double [FieldORGNum];

            for(int k=0; k<FieldORGNum; k++)
            {
               MT_Result_val[i][j][k] = 0.0;
            }
        }
    }

    //Summary 결과파일 저장용 배열 초기화
    Result_Grp_val = new double* [MT_MaxPeriod];
    for (int i=0; i<MT_MaxPeriod; i++)
    {
        Result_Grp_val[i] = new double [FieldGrpNum];
        for (int j=0; j<FieldGrpNum; j++)
        {
            Result_Grp_val[i][j] = 0.0;
        }
    }
       
    this->GrpKey.Init(path_key);
    this->MDEV.Init(MT_MaxPeriod, &FieldInfo);
   
    return;
}

//클래스 종료
//로그파일, 출력파일을 닫고, 각종 배열 반환
Group::~Group()
{
 OutputFile.close();
}


//Group 클래스내 결과파일 읽어들이는 함수
//처리대상파일을 읽어 들여 필드순서를 정렬하에 어레이에 저장
//매개변수는 멀티쓰레딩을 위해 어느 어레이에 저장할지를 지정하는 어레이번호
void Group::MT_Read(short currentarray)
{
    short CA = currentarray; 
    strcpy(MT_Result_prod[CA], MT_product);
    MT_Result_spcode[CA] = MT_spcode;

    //결과값 저장용 배열 초기화
    for(int j=0; j<MT_MaxPeriod; j++)
    {
        for(int k=0; k<FieldORGNum; k++)
        {
           MT_Result_val[CA][j][k] = 0.0;
        }
    }

    FileNum++;
    cout    << FileNum << " : " << MT_path << " : " << MT_product << " : " << MT_spcode << endl;

    //처리대상파일의 레코드길이 상한은 5000바이트!!!
    char rec[5000], buffer[100], fieldname[FieldMaxNum][50];
    long i,k, mon=0, m,n=0;
    bool DataYN = false;
    char* pch;
    char imsi[5000]="";

    //처리대상파일 open
    ifstream InputFile;
    InputFile.open(MT_path, ifstream::in);
   
    if (InputFile==NULL)
    {
        gLogFile << "Error : Result File missing !!! : " << MT_path << endl;
        cout     << "Error : Result File missing !!! : " << MT_path << endl;
        return;
    }

    MergeNum++;  //결과파일을 성공적으로 읽었을 때만 카운트 실시
   
    //처리대상파일을 읽어 필드순서를 정렬하여 출력파일에 저장
    //처리대상파일의 필드명 부분과 DATA 시작부분 인식이 필요
    //레코드값이 '!' 인 레코드의 다음 레코드가 필드명
    //레코드값이 '*' 인 레코드의 다음 레코드가 DATA 시작
    //FieldNum = 0; i=0; k=0; n=0; mon = 0;
    i=0; k=0; n=0; mon = 0;
    while(1)
    {
        InputFile.getline(rec,5000,'\n');
        if (InputFile.eof()==true) break;
        i++;
       
        if (DataYN==false)
        {
            if (strcmp(rec, "\"*\"")==0) //데이터 시작 부분 인식
            {
                DataYN = true;
                InputFile.getline(rec,5000,'\n');
                if (InputFile.eof()==true) break;
                i++;
            }
            else if (strcmp(rec, "\"!\"")==0) //필드명 읽기
            {
                InputFile.getline(rec,5000,'\n');
                FieldInfo.CurFile(rec);

            }//if (strcmp(rec, "!")==0) //필드명 읽기

            if (DataYN==false) continue;
        } //if (DataYN==false)

        for (int j=0; j<FieldInfo.iFieldNumCur(); j++)
        {
            if (j==0) pch = strtok(rec, ",");   
            else      pch = strtok(NULL,","); 
           
            if (FieldInfo.iMappingCur(j)<FieldORGNum) MT_Result_val[CA][mon][FieldInfo.iMappingCur(j)] = atof(pch); //필드순서를 조정하여 필드값 배열에 저장 
        }

        mon++;
        if (mon>=MT_MaxPeriod) break;

    }
    
 InputFile.close();
 MT_Result_period[CA] = mon;

    return;
}

//Group 클래스내 결과파일 누적저장하는 함수
//필드순서가 정렬되어 저장된 어레이를 텍스트 파일로 출력
void Group::MT_WriteOrg(short currentarray)
{
    char  buffer[100];
    short CA = currentarray;
    short period = MT_Result_period[CA]; if (period>MT_MaxPeriod) period = MT_MaxPeriod;

    for (int i=0; i<period; i++)
    {
        OutputFile << MT_Result_prod[CA] << "," << MT_Result_spcode[CA] << "," << i;

        for (int j=0; j<FieldORGNum; j++)
        {
            gcvt(MT_Result_val[CA][i][j],20,buffer);
            OutputFile << "," << buffer;
        }

        OutputFile << endl;
    }

    return;
}


//Group 클래스내 결과파일 그룹핑하는 함수
//키별로 누적하여 메모리에 저장
//기존에 동일키가 저장되어 있으면 누적하고
//동일키가 없으면 새로이 메모리를 할당하여 저장
void Group::MT_SaveGrp(short currentarray)
{
    short  CA       = currentarray;
    char   prod[20]; strcpy(prod, MT_Result_prod[CA]);
    long   spcode   = MT_Result_spcode[CA];
    char** key      = GrpKey.KeySearch(prod,spcode);
    char*  mcode    = GrpKey.KeySearch(prod,spcode,"M_CODE");
    char*  alfakey2 = GrpKey.KeySearch(prod,spcode,"ALFAKEY2");

    this->MT_CalcGrp(CA,mcode,alfakey2);
   
    //동일키가 있는지 확인해서 누적
    for (int i=0; i<MT_GrpNum; i++)
    {
        if (RUnit[i]->GroupCmp(key)==true)
        {
            RUnit[i]->Save(Result_Grp_val);

            return;
        }
    }

    //동일키가 없으면 새로운 저장소 추가
    ResultUnit* imsi; imsi = new ResultUnit;
    RUnit.push_back(imsi);
    MT_GrpNum++;

    RUnit[MT_GrpNum-1]->Init(MT_MaxPeriod, FieldGrpNum, GrpKey.KeyFieldNum, key);
    RUnit[MT_GrpNum-1]->Save(Result_Grp_val);

    return;
}

//Group 클래스내 그룹핑 결과를 텍스트파일로 저장하는 함수
void Group::MT_WriteGrp()
{
    //그룹핑 출력파일 open
    OutputFileGrp.open(path_ofile_grp, ofstream::out | ofstream::trunc);
    if (OutputFileGrp==NULL)
    {
        gLogFile << "Error : Out File missing !!! : " << path_ofile_grp << endl;
        cout     << "Error : Out File missing !!! : " << path_ofile_grp << endl;
        system("PAUSE"); exit(0);
    }

    //long fieldnum = FieldInfo.iFieldNumGrp();
    long index = 0;
   
    //결과파일 첫행에 필드 레이블 출력 - Key 부분
    for (int i=0; i<GrpKey.KeyFieldNum; i++)
    {
        OutputFileGrp << GrpKey.KeyLabel[i] << ",";
    }

    OutputFileGrp << "month";

    for (int j=0; j<FieldGrpNum; j++)
    {
        index = FieldInfo.iMappingGrp(j);
        OutputFileGrp << "," << FieldInfo.cFieldName(index);
    }
    OutputFileGrp << endl;
   
    char buffer[100];

    //그룹핑별로 키값 및 누계값 출력
    for (int i=0; i<MT_GrpNum; i++)
    {
        for (int j=0; j<MT_MaxPeriod; j++)    //키값 출력
        {
            for (int k=0; k<RUnit[i]->KeyNum; k++)
            {
                OutputFileGrp << RUnit[i]->Key[k] << ",";
            }

            OutputFileGrp << j;               //해당월(회차)값 출력

            for (int k=0; k<FieldGrpNum; k++)    //누적값 출력
            {
                gcvt(RUnit[i]->value[j][k],20,buffer);
                OutputFileGrp << "," << buffer;
            }

            OutputFileGrp << endl;
        }
    }

 OutputFileGrp.close();

    //그룹핑키별 추가 그룹핑 결과 출력
    for (int k=0; k<RUnit[0]->KeyNum; k++)
    {
        this->MT_WriteGrp(k);
    }

    return;
}

//그룹핑키별 추가 그룹핑 결과를 텍스트파일로 저장하는 함수
void Group::MT_WriteGrp(short keyindex)
{
    long   GrpNumSub = 0;
    long   index     = 0;
    bool   samekeyyn = false;
    char** key; key = new char * [1]; key[0] = new char [30];

    vector<ResultUnit*> RUnitSub;

    //결과값을 해당키별로 다시 Summary
    for (int i=0; i<MT_GrpNum; i++)
    {
        strcpy(key[0], RUnit[i]->Key[keyindex]);
        samekeyyn = false;

        //동일키가 있는지 확인해서 누적
        for (int j=0; j<GrpNumSub; j++)
        {
            if (strcmp(RUnit[j]->Key[keyindex], key[0])==0)
            {
                RUnitSub[j]->Save(RUnit[i]->value);
                samekeyyn = true;
                break;
            }
        }

        //동일키가 없으면 새로운 저장소 추가
        if (samekeyyn==false)
        {
            ResultUnit* imsi; imsi = new ResultUnit;
            RUnitSub.push_back(imsi);
            GrpNumSub++;
            RUnitSub[GrpNumSub-1]->Init(MT_MaxPeriod, FieldGrpNum, (short)1, key);
            RUnitSub[GrpNumSub-1]->Save(RUnit[i]->value);
        }
    }//for (int i=0; i<MT_GrpNum; i++)  

    //그룹핑 출력파일 open
    char path[500] = ""; char buffer[100] = "";
    strcpy(path, path_ofile_sub); itoa(keyindex+1,buffer,10); strcat(path, buffer); strcat(path, ".TXT");
   
    OutputFileGrp.open(path, ofstream::out | ofstream::trunc);
    if (OutputFileGrp==NULL)
    {
        gLogFile << "Error : Out File missing !!! : " << path << endl;
        cout     << "Error : Out File missing !!! : " << path << endl;
        system("PAUSE"); exit(0);
    }
   
    //결과파일 첫행에 필드 레이블 출력 - Key 부분
    OutputFileGrp << GrpKey.KeyLabel[keyindex] << ",";
    OutputFileGrp << "month";

    //결과파일 첫행에 필드 레이블 출력 - 값 부분
    for (int j=0; j<FieldGrpNum; j++)
    {
        index = FieldInfo.iMappingGrp(j);
        OutputFileGrp << "," << FieldInfo.cFieldName(index);
    }
    OutputFileGrp << endl;

    //그룹핑별로 키값 및 누계값 출력
    for (int i=0; i<GrpNumSub; i++)
    {
        for (int j=0; j<MT_MaxPeriod; j++)    //키값 출력
        {
            OutputFileGrp << RUnitSub[i]->Key[0] << ",";
            OutputFileGrp << j;               //해당월(회차)값 출력

            for (int k=0; k<FieldGrpNum; k++)    //누적값 출력
            {
                gcvt(RUnitSub[i]->value[j][k],20,buffer);
                OutputFileGrp << "," << buffer;
            }

            OutputFileGrp << endl;
        }
    }

 OutputFileGrp.close();

    delete [] key[0];
    delete [] key;

    return;
}

void Group::MT_Setting(char* path, char* product, long spcode)
{
    strcpy(this->MT_path,    path);
    strcpy(this->MT_product, product);
    this->MT_spcode = spcode;
 
    return;
}

//Group 클래스내 결과필드끼리 연산을 수행하는 함수 
//연산 수행후 저장값만 지정된 배열에 저장
void Group::MT_CalcGrp(short currentarray, char* mcode, char* alfakey2)
{
    short CA    = currentarray;
    long  index = 0;

    //결과 그룹핑 타입이 "EV"가 아닌 경우, 필드연산 없이 출력대상 필드만 순서를 조정하여 저장
    if (strcmp(GrpType, "EV")!=0)
    {
        for(int j=0; j<FieldGrpNum; j++)
        {
            index = FieldInfo.iMappingGrp(j);
            for(int i=0; i<MT_MaxPeriod; i++) Result_Grp_val[i][j] = MT_Result_val[CA][i][index];
        }

        return;
    }

    //결과 그룹핑 타입이 "EV"인 경우 ModelEV 클래스를 이용해 필드 연산 수행 
    if (strcmp(GrpType, "EV")==0)
    {
        MDEV.StartUp(MT_Result_val[CA], &FieldInfo, mcode, alfakey2);

        for (int i=0; i<MT_MaxPeriod; i++)
        {
            //여기에 필드연산함수와 저장배열을 순서대로 연결할것!!!
            Result_Grp_val[i][ 0] = MDEV.GEN_PREM(i);
            Result_Grp_val[i][21] = MDEV.RISK_PREM_SB_IF(i);
        }

        return;
    }

    return;
}

'Programs' 카테고리의 다른 글

ModelEV.cpp  (2) 2014.08.14
GrpKeyTable.cpp  (0) 2014.08.14
ResultUnit.cpp  (0) 2014.08.14
FieldInfoSet.cpp  (0) 2014.08.14
Inc.hpp  (0) 2014.08.14
Posted by Analytical Warrior
|