// FileRead.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <vector>#include <iterator>#include <fstream>#include <iostream>using namespace std;void LoadLine(char* fname,vector<double>& generatedPoints);
double CalFrechetDist(char* fname1, char* fname2, long& pnum);struct D_DOT {
double x; double y;};typedef double* DBLPtr;
//void LoadLine(char* fname,vector<double>& generatedPoints)
//{ // char buf[1024];// fstream infile;// char sep[2]={0};// double d;// strcpy(sep,",");//// infile.open(fname,ios::in);// while(1)// { // buf[0]=0;// infile.getline(buf,1024);// if(strlen(buf)<1)// break;//// d=atof(strtok(buf,sep));// generatedPoints.push_back(d);//// d=atof(strtok(NULL,sep));// generatedPoints.push_back(d);// }//// infile.close();//}void LoadLine(char* fname,vector<double>& generatedPoints)
{ //char buf[1024]; fstream infile; char sep[2]={0}; double d; strcpy(sep,",");infile.open(fname); //,ios::in
printf("d:%s\n",fname);//char str[34];
//fstream fin; //fin.open(fname); //if (!fin) //{ // cout<<"error "<<endl; // exit(1); //} //while(fin.getline(str,34)) //{ // cout<<str<<endl; // /*d=atof(strtok(str,sep)); // generatedPoints.push_back(d); // printf("d:%d\n",d);// d=atof(strtok(NULL,sep));
// printf("d:%d\n",d); // generatedPoints.push_back(d); */ //} //fin.close(); char buf[34]; while(infile.getline(buf,34))//1 { //cout<<buf<<endl; //printf("buf:%s\n",buf);if(buf!="\n"&&strlen(buf)>=1)
{ d=atof(strtok(buf,sep)); //cout<<"d:"<<d<<endl; generatedPoints.push_back(d); d=atof(strtok(NULL,sep)); generatedPoints.push_back(d); } } infile.close(); //很重要别忘了关}int _tmain(int argc, _TCHAR* argv[])
{ int i; double d1; vector<double> dlist; vector<long> pnumlist;char ss[100] = { 0 };
long pnum; for (i = 1; i<40; i++) { sprintf(ss, "C:\\Users\\Administrator\\Desktop\\testtxt\\TestTxt%d.txt", i + 1); //C:\\Users\\lenovo\\Desktop\\wo\\YYI_%d.txt d1 = CalFrechetDist("C:\\Users\\Administrator\\Desktop\\testtxt\\TestTxt1.txt", ss, pnum); //double CalFrechetDist(char* fname1, char* fname2, long& pnum) //cout << "距离:" << d1 << endl; printf("距离:%d\n",d1); dlist.push_back(d1); pnumlist.push_back(pnum); } system("pause"); return 0;}double pi_pj(D_DOT& pi, D_DOT& pj)
{ double dx = pi.x - pj.x; double dy = pi.y - pj.y;return sqrt(dx*dx + dy*dy);
}double c(D_DOT *zb1, D_DOT *zb2, int i, int j, double **ca)
{ int curI, curJ; double **caao; caao = (double **)new DBLPtr[i + 1]; for (curI = 0; curI <= i; curI++) { caao[curI] = new double[j + 1]; }for (curI = 1; curI <= i; curI++)
{ for (curJ = 1; curJ <= j; curJ++) { caao[curI][curJ] = pi_pj(zb1[curI], zb2[curJ]); } }ca[1][1] = caao[1][1];
for (curJ = 1; curJ <= j; curJ++) { for (curI = 1; curI <= i; curI++) { if (curJ == 1) { if (curI>1) { ca[curI][curJ] = caao[curI][curJ]; if (ca[curI][curJ]<ca[curI - 1][1]) { ca[curI][curJ] = ca[curI - 1][1]; } } } else if (curI == 1) { if (curJ>1) { ca[curI][curJ] = caao[curI][curJ]; if (ca[curI][curJ]<ca[1][curJ - 1]) { ca[curI][curJ] = ca[1][curJ - 1]; } } } else if (curI>1 && curJ>1) { ca[curI][curJ] = caao[curI][curJ]; double tmp1 = ca[curI - 1][curJ]; double tmp2 = ca[curI - 1][curJ - 1]; double tmp3 = ca[curI][curJ - 1]; if (tmp1<tmp2&&tmp1<tmp3) { if (ca[curI][curJ]<tmp1) ca[curI][curJ] = tmp1; } else if (tmp2<tmp1&&tmp2<tmp3) { if (ca[curI][curJ]<tmp2) ca[curI][curJ] = tmp2; } else { if (ca[curI][curJ]<tmp3) ca[curI][curJ] = tmp3; } } else ca[curI][curJ] = 9999; } }for (curI = 0; curI <= i; curI++)
{ delete[] caao[curI]; } delete[] caao;return ca[i][j];
}double Frechet(D_DOT *zb1,int n1,D_DOT *zb2,int n2,double** fm)
{ int i,j; double **caa; double result;caa = (double **)new DBLPtr[n1+1];
for(i=0;i<=n1;i++) { caa[i]=new double[n2+1]; } for(i=0;i<=n1;i++) { for(j=0;j<=n2;j++) { caa[i][j] = -1; } } result = c(zb1,zb2,n1,n2,caa); for(i=0;i<=n1;i++) { for(j=0;j<=n2;j++) { fm[j][i] = caa[i][j]; } } for(i=0;i<=n1;i++) { delete [] (caa[i]); }delete [] caa;
return result;}double CalFrechetDist(char* fname1, char* fname2, long& pnum) //计算Frechet距离
{ int i; long len1, len2; double d; vector<double> generatedPoints1; vector<double> generatedPoints2;LoadLine(fname1, generatedPoints1); //读取文件fname1中的内容到generatedPoints1
LoadLine(fname2, generatedPoints2); len1 = generatedPoints1.size() / 2 + 1; D_DOT *lxy1 = new D_DOT[len1]; for (i = 0; i<len1 - 1; i++) { lxy1[i + 1].x = generatedPoints1[i * 2]; lxy1[i + 1].y = generatedPoints1[i * 2 + 1]; } len2 = generatedPoints2.size() / 2 + 1; D_DOT *lxy2 = new D_DOT[len2]; for (i = 0; i<len2 - 1; i++) { lxy2[i + 1].x = generatedPoints2[i * 2]; lxy2[i + 1].y = generatedPoints2[i * 2 + 1]; }DBLPtr* fm = new DBLPtr[len2]; //指向数组的指针?no!指针数组?no!指向指针数组的指针
for (i = 0; i<len2; i++) { fm[i] = new double[len1]; }d = Frechet(lxy1, len1 - 1, lxy2, len2 - 1, (double**)fm);
delete[] lxy1;
delete[] lxy2; for (i = 0; i<len2; i++) { delete[] fm[i]; } delete[] fm;generatedPoints1.clear();
generatedPoints2.clear(); pnum = len2; return d;}