📄 idw.cpp
字号:
// 输入输出.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;
class Point
{
public:
double X;
double Y;
double Z;
};
int GetSourceData (vector<Point>& TPoint , string sFilefullname)
{
cout << "正在读取已知点数据..." << endl;
string tmpLine;
string::size_type pos = 0, pre_pos = 0;
ifstream infile(sFilefullname.c_str());
Point vfTemp;
if (!infile) {
cerr << "unable to open input file: ";
cin >> tmpLine;
return -1;
}
while(getline(infile,tmpLine,'\n'))
{
pos = tmpLine.find_first_of(',');
vfTemp.X = atof(tmpLine.substr(0, pos).c_str());
pre_pos = ++pos;
pos = tmpLine.find_first_of(',',pos);
vfTemp.Y = atof(tmpLine.substr(pre_pos, pos - pre_pos).c_str());
pre_pos = ++pos;
vfTemp.Z = atof(tmpLine.substr(pos).c_str());
TPoint.push_back(vfTemp);
pos = 0;
}
return 0;
}
int GetTargetData (vector<Point>& TPoint , string sFilefullname)
{
cout << "正在读取目标点数据..." << endl;
string tmpLine;
string::size_type pos = 0, pre_pos = 0;
ifstream infile(sFilefullname.c_str());
Point vfTemp;
if (!infile) {
cerr << "unable to open input file: ";
cin >> tmpLine;
return -1;
}
while(getline(infile,tmpLine,'\n'))
{
pos = tmpLine.find_first_of(',');
vfTemp.X = atof(tmpLine.substr(0, pos).c_str());
pre_pos = ++pos;
vfTemp.Y = atof(tmpLine.substr(pos).c_str());
vfTemp.Z = -9999;
TPoint.push_back(vfTemp);
pos = 0;
}
return 0;
}
int interpolation (vector<Point>& TPoint, vector<Point>& SPoint)
{
cout << "正在进行内插计算..." << endl;
int i = 0,j,k = 0,l;
double sum1 =0.,sum2=0.;
k = TPoint.size();
i = SPoint.size();
double d;
for (l = 0;l < k;l++){
for(j = 0;j < i;j++){
d = sqrt((TPoint[l].X-SPoint[j].X)*(TPoint[l].X-SPoint[j].X)+(TPoint[l].Y-SPoint[j].Y)*(TPoint[l].Y-SPoint[j].Y));
if(d < 70.7){
sum1 += SPoint[j].Z/(d*d);
sum2 += 1/(d*d);
}
}
TPoint[l].Z=sum1/sum2;
cout<<TPoint[l].Z<<endl;
sum1 = 0;
sum2 = 0;
}//...内插算法
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
string sTargetfullname("target.txt");
string sSourcefullname("source.txt");
string sParafullname("para.txt");
string sResultfullname("result.txt");
vector<Point> TPoint; //目标点坐标及属性数组
vector<Point> SPoint; //已知点坐标及属性数组
vector<Point> PARA;
int error;
ofstream info("info.txt", ios::app);
if (!info) {
return -1;
}
error = GetTargetData(TPoint, sTargetfullname);
if (error == -1)
{
info << "false" << endl << "目标点坐标文件无法打开";
return -1;
}
error = GetSourceData(SPoint, sSourcefullname);
if (error == -1)
{
info << "false" << endl << "已知点坐标文件无法打开";
return -1;
}
error = interpolation(TPoint, SPoint);
if (error == -1)
{
info << "false" << endl << "内插过程失败";
return -1;
}
ofstream ofResult(sResultfullname.c_str());
int i = 0;
int j = TPoint.size();
while (i < j)
{
ofResult << TPoint[i].X << ',' << TPoint[i].Y << ',' << TPoint[i].Z << endl;
if (TPoint[i].Z == -9999)
{
info << "false" << endl << "内插计算出现错误,结果输出中断" << endl;
return -1;
}
i = i + 1;
}
info << "ture" << endl ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -