直接贴代码吧,一看就懂
#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;
}