直接贴代码吧,一看就懂
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
/*
从文件中读取数据,并计算相邻两点的距离
上下两行的数据默认是两个相邻点的数据
*/
char buffer[250];
int main(){
double dist;
double x[100],y[100];
int tot=1;
ifstream infile("data.txt");
ofstream outfile("result.txt");
while (!infile.eof()){
infile.getline(buffer,20); //整行读入
sscanf(buffer,"%lf %lf",&x[tot],&y[tot]);
tot++;
}
tot--;
for(int i=1;i<tot;i++) {
dist=sqrt(pow(x[i]-x[i+1],2)+pow(y[i]-y[i+1],2));
outfile<<dist<<endl;
}
infile.close();
outfile.close();
return 0;
}
本文介绍了一个简单的C++程序,该程序从文件'data.txt'中读取坐标数据,并计算每一对相邻点之间的欧氏距离,将结果输出到'result.txt'文件中。
6505

被折叠的 条评论
为什么被折叠?



