245 lines
4.9 KiB
C++
245 lines
4.9 KiB
C++
#ifndef _COMMOND_H
|
||
#define _COMMOND_H
|
||
#include <iostream>
|
||
#include <fstream>
|
||
#include <sstream>
|
||
#include <iomanip>
|
||
#include <string.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#define ELE "-e"
|
||
#define DAT "-d"
|
||
#define FIE "-f"
|
||
#define RANGE "-r"
|
||
#define OUTPUT "-o"
|
||
#define MAX 1e+20
|
||
#define GRD ".grd"
|
||
using namespace std;
|
||
|
||
int typeget(char *str, const char* str2,string &str3)
|
||
{
|
||
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
||
if (strp == NULL)
|
||
{
|
||
return 0; // 如果没有找到,返回
|
||
}
|
||
string temp = str;
|
||
str3=temp.substr(strlen(str2));//提出除str2外的子句
|
||
return 1;
|
||
}
|
||
|
||
string& replace_all(string& str,const string& old_value,const string& new_value,int &num) //替换str中所有lod_value为new_value,返回被替换的lod_value的个数
|
||
{
|
||
int count = 0;
|
||
for(string::size_type pos(0);pos!=string::npos;pos+=new_value.length()){
|
||
if((pos=str.find(old_value,pos))!=string::npos){
|
||
str.replace(pos,old_value.length(),new_value);
|
||
count++;
|
||
}
|
||
else break;
|
||
}
|
||
num = count;
|
||
return str;
|
||
}
|
||
|
||
int nameget(char *str, const char* str2,string &str3)//提出尾端的子句
|
||
{
|
||
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
||
if (strp == NULL)
|
||
{
|
||
return 0; // 如果没有找到,返回
|
||
}
|
||
string temp = str;
|
||
str3=temp.substr(0,strlen(str)-strlen(str2));//提出除str2外的子句
|
||
return 1;
|
||
}
|
||
|
||
class CON
|
||
{
|
||
public:
|
||
CON();
|
||
~CON();
|
||
int run(string,string,string,int,double,double*);
|
||
int readdata(string,string);
|
||
int fie(double);
|
||
int fileout(string,int,double*);
|
||
private:
|
||
int M,N;
|
||
double xmin,xmax,ymin,ymax,zmin,zmax;
|
||
double dx,dy;
|
||
double* data;
|
||
double* relief;
|
||
};
|
||
|
||
CON::CON()
|
||
{
|
||
data = NULL;
|
||
relief = NULL;
|
||
}
|
||
|
||
CON::~CON()
|
||
{
|
||
if(data!=NULL) delete[] data;
|
||
if(relief!=NULL) delete[] relief;
|
||
}
|
||
|
||
int CON::run(string inname,string inname2,string outname,int type,double topo,double* range)
|
||
{
|
||
if(readdata(inname,inname2)) return 1;
|
||
if (type==1&&inname2=="") fie(topo);
|
||
fileout(outname,type,range);
|
||
return 0;
|
||
}
|
||
|
||
int CON::readdata(string inname,string inname2)
|
||
{
|
||
string head;
|
||
int M2,N2;
|
||
double xmin2,xmax2,ymin2,ymax2,zmin2,zmax2;
|
||
|
||
const char* name1 = inname.c_str();
|
||
ifstream grdin(name1);
|
||
if (!grdin)
|
||
{
|
||
cout<<inname<<" not found, program stopped..."<<endl;
|
||
return 1;
|
||
}
|
||
else
|
||
{
|
||
grdin>>head>>N>>M>>xmin>>xmax>>ymin>>ymax>>zmin>>zmax;
|
||
dx = (xmax-xmin)/M+1;
|
||
dy = (ymax-ymin)/N+1;
|
||
data = new double [M*N];
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
grdin>>data[i];
|
||
}
|
||
grdin.close();
|
||
}
|
||
|
||
if (inname2=="")
|
||
{
|
||
relief = new double [M*N];
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
relief[i]=0;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
const char* name2 = inname2.c_str();
|
||
ifstream grd2in(name2);
|
||
if (!grd2in)
|
||
{
|
||
cout<<inname2<<" not found, program stopped..."<<endl;
|
||
return 1;
|
||
}
|
||
else
|
||
{
|
||
grd2in>>head>>N2>>M2>>xmin2>>xmax2>>ymin2>>ymax2>>zmin2>>zmax2;
|
||
if (N!=N2||M!=M2)
|
||
{
|
||
cout<<"data martixs don't agree, program stopped..."<<endl;
|
||
return 1;
|
||
}
|
||
else
|
||
{
|
||
relief = new double [M*N];
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
grd2in>>relief[i];
|
||
}
|
||
grd2in.close();
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int CON::fie(double topo)
|
||
{
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
relief[i]=topo;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int CON::fileout(string outname,int type,double* range)
|
||
{
|
||
double xtemp,ytemp;
|
||
if ((*range)!=MAX)
|
||
{
|
||
double xmin_r = *range;
|
||
double xmax_r = *(range+1);
|
||
double ymin_r = *(range+2);
|
||
double ymax_r = *(range+3);
|
||
if (type==1)
|
||
{
|
||
const char* name = (outname+".fie").c_str();
|
||
ofstream outfile(name);
|
||
int count=0;
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
xtemp = (i%N)*dx+xmin;
|
||
ytemp = (i/N)*dy+ymin;
|
||
if (xtemp>=xmin_r&&xtemp<=xmax_r&&ytemp>=ymin_r&&ytemp<=ymax_r)
|
||
{
|
||
count++;
|
||
}
|
||
}
|
||
outfile<<count<<endl;
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
xtemp = (i%N)*dx+xmin;
|
||
ytemp = (i/N)*dy+ymin;
|
||
if (xtemp>=xmin_r&&xtemp<=xmax_r&&ytemp>=ymin_r&&ytemp<=ymax_r)
|
||
{
|
||
outfile<<setprecision(12)<<xtemp<<" "<<ytemp<<" "<<relief[i]<<" "<<data[i]<<endl;
|
||
}
|
||
}
|
||
outfile.close();
|
||
}
|
||
else if (type==2)
|
||
{
|
||
const char* name = (outname+".dat").c_str();
|
||
ofstream outfile(name);
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
xtemp = (i%N)*dx+xmin;
|
||
ytemp = (i/N)*dy+ymin;
|
||
if (xtemp>=xmin_r&&xtemp<=xmax_r&&ytemp>=ymin_r&&ytemp<=ymax_r)
|
||
{
|
||
outfile<<setprecision(12)<<xtemp<<" "<<ytemp<<" "<<data[i]<<endl;
|
||
}
|
||
}
|
||
outfile.close();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (type==1)
|
||
{
|
||
const char* name = (outname+".fie").c_str();
|
||
ofstream outfile(name);
|
||
outfile<<M*N<<endl;
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
outfile<<setprecision(12)<<(i%N)*dx+xmin<<" "<<(i/N)*dy+ymin<<" "<<relief[i]<<" "<<data[i]<<endl;
|
||
}
|
||
outfile.close();
|
||
}
|
||
else if (type==2)
|
||
{
|
||
const char* name = (outname+".dat").c_str();
|
||
ofstream outfile(name);
|
||
for (int i = 0; i < M*N; i++)
|
||
{
|
||
outfile<<setprecision(12)<<(i%N)*dx+xmin<<" "<<(i/N)*dy+ymin<<" "<<data[i]<<endl;
|
||
}
|
||
outfile.close();
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
#endif |