⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gplib_loadsave.cpp

📁 遗传规划算法 智能算法:遗传规划
💻 CPP
字号:
// *****************// loadsave.cpp// Loading / Saving of files// Colin Frayn// December 2006// *****************
// GPLib v2.0, A Genetic programming Library for C++
// Copyright (C) 2006  Colin Frayn
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#include "GPLib.h"void GPLIB_Environment::LoadCSV(string strName) {  int n=0,nCol=0,test,l;
  float val;
  FILE *fp;
  FloatVector iv;
  char strLine[1<<16], strTitle[512], *loc;
  
  if ((fp = fopen(strName.c_str(),"r")) == NULL) {
    fprintf(stderr,"GPLIB Error : Could not load specified file \"%s\"\n",strName.c_str());
    fscanf(stdin,"\n");    exit(0);
  }

  // Get the title line
  do {
    fgets(strLine,sizeof(strLine),fp);
  } while (strLine[0] == '#' || strlen(strLine)<3);

  // Count the number of columns  strcpy(strTitle,"");  l=0;  for (n=0;n<(int)strlen(strLine);n++) {    if (strLine[n] == ',') {      strTitle[l]=0;      strVariables.push_back(strTitle);      strcpy(strTitle,"");      l=0;      nCol++;    }    else strTitle[l++]=strLine[n];  }  strTitle[l] = 0;  loc = strchr(strTitle,'\n');  if (loc) *loc=0;  strTarget = strTitle;  // Add one : number_of_columns = number_of_commas + 1   nCol++;  if (nCol==0) {    fprintf(stderr,"GPLIB Error : Did not understand CSV File \"%s\" - no columns found\n",strName.c_str());    fscanf(stdin,"\n");    exit(0);  }
  do {
    iv.clear();
    for (n=0;n<nCol-1;n++) {
      if (n>0) {
        test = fscanf(fp,",");
        if (test!=0) break;
      }
      test = fscanf(fp,"%f",&val);
      if (test!=1) break;
      iv.push_back(val);
    }
    test = fscanf(fp,",%f",&val);
    if (test!=1) break;
    yval.push_back(val);
    if ((int)iv.size() != (nCol-1)) {
      fprintf(stderr,"GPLIB Error : Column counts do not match in file \"%s\", line %d\n",strName.c_str(),(int)yval.size());
      fscanf(stdin,"\n");      exit(0);
    }
    xval.push_back(iv);

  } while (!feof(fp) && (int)yval.size()<GPLIB_MaxData);
  if ((int)yval.size() >= GPLIB_MaxData) {
    fprintf(stderr,"Maximum data count reached. Truncating input file.\n");
  }
  fclose(fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -