C++ C++;读取数据文件
我已成功打开并丢弃标题。然后我计算了点1和其他点之间的距离以及最小距离。然而,我不知道如何使代码工作超过4点。我试图从中提取数据的文本文件如下所示: 多点坐标(第1行) x y z(秒) -0.06325 0.03597 0.042823(第三行) 。 . . . . . . 继续。。点1有坐标(-0.06325,0.03597,0.042823) 另外,我需要告诉哪一点是离点1最近的点,以及点1和输出屏幕上最近点之间的距离,你能帮我吗?多谢各位C++ C++;读取数据文件,c++,C++,我已成功打开并丢弃标题。然后我计算了点1和其他点之间的距离以及最小距离。然而,我不知道如何使代码工作超过4点。我试图从中提取数据的文本文件如下所示: 多点坐标(第1行) x y z(秒) -0.06325 0.03597 0.042823(第三行) 。 . . . . . . 继续。。点1有坐标(-0.06325,0.03597,0.042823) 另外,我需要告诉哪一点是离点1最近的点,以及点1和输出屏幕上最近点之
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
double x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, dist_12, dist_13,
dist_14, minimum_distance;
ifstream inFile("input-week6-ad-q4-2.txt"); // ifstream function to read the
file
string line, c; // To read the characters
char ch;
if (inFile.is_open())
{
getline(inFile, line); // To read the header of the input file then
discard it
getline(inFile, line);
inFile >> x1 >> y1 >> z1;
inFile >> x2 >> y2 >> z2;
inFile >> x3 >> y3 >> z3;
inFile >> x4 >> y4 >> z4;
int i = 1;
while (inFile >> xi >> yi >> zi) {
i++;
}
int number_of_points = i;
inFile.close();
}
else
cout << "The file could not be opened." << "\n"; // To check for any
error
system("pause");
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
双x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4,距离12,距离13,
距离14,最小距离;
ifstream infle(“input-week6-ad-q4-2.txt”);//用于读取
文件
字符串行,c;//读取字符
char ch;
if(infle.is_open())
{
getline(infle,line);//读取输入文件的头,然后
丢弃它
getline(填充,行);
填充>>x1>>y1>>z1;
填充>>x2>>y2>>z2;
填充>>x3>>y3>>z3;
填充>>x4>>y4>>z4;
int i=1;
而(注)>席>
i++;
}
整数点的个数=i;
infle.close();
}
其他的
CUT< P>好问题。如评论所述,用<代码> IFSturi读取未知长度的文件的首选方法是使用类似“代码>”(Fiely>席> > Yi> Zi)< /C> > < /P>
要跟踪最近的点和距离,请在每次读取后检查此项,如果发现最短距离和点索引比上一点更近,则更新最短距离和点索引
因为您已经在使用sqrt()
,所以我认为可以使用pow()函数来计算距离公式中的平方。祝您好运,让我知道这是否有帮助,或者是否有令人困惑的地方
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <vector>
int main()
{
double xFirst, yFirst, zFirst, xi, yi, zi;
std::ifstream
inFile("input-week6-ad-q4-2.txt");
std::string line; // To read the characters
double min_distance{UINT_MAX}; // Start with largest distance possible.
uint closest_point;
if (inFile.is_open()) {
// read and discard first 2 lines of file
getline(inFile, line); getline(inFile, line);
// Save first point's coordinates
inFile >> xFirst >> yFirst >> zFirst;
int i = 1;
while (inFile >> xi >> yi >> zi) { // safer way to read 3 vars from file
i++;
// use the pow(base, power) function for squaring
double distance = sqrt(pow((xi - xFirst), 2)
+ pow((yi - yFirst), 2)
+ pow((zi - zFirst), 2));
if (distance < min_distance) {
min_distance = distance;
closest_point = i;
}
}
std::cout << "Read " << i << " points\n";
inFile.close();
}
std::cout << "The minimum distance is " << min_distance
<< ", which is the distance between point[1] and point["
<< closest_point << "], using 1-based indexing.\n";
std::cin >> line;
}
#包括
#包括
#包括
#包括
#包括
int main()
{
双首、先、祖、席、义、字;
std::ifstream
infle(“input-week6-ad-q4-2.txt”);
std::string line;//读取字符
双最小距离{UINT\u MAX};//从可能的最大距离开始。
uint最近点;
if(infle.is_open()){
//读取并丢弃文件的前2行
getline(填充,线);getline(填充,线);
//保存第一个点的坐标
infle>>xFirst>>yFirst>>zFirst;
int i=1;
当(fILIL> XI>易席> Zi){//从文件中读取3个VAR的更安全方法
i++;
//使用pow(基本功率)函数进行平方运算
双倍距离=sqrt(功率(xi-xFirst),2)
+战俘((伊-伊第一),2)
+战俘((zi-zFirst),2);
如果(距离<最小距离){
最小距离=距离;
最近的_点=i;
}
}
STD::我建议替换<代码>((f.If.EfFor)){ fILIL> >席>义席>; > <代码>(IfLy> Xi > Yi> Zi){
。谢谢你的评论,但你能解释一下原因吗?我是一个初学者。数据文件中有100多行,我想一直读到最后,是否不需要使用eof?如果你a)只问一个问题,B)尝试解决问题,你会有更好的堆栈溢出体验。如果你的解决方案很有效,太好了!你完成了!如果不成功,你可能只会出局一点点,答案可能会很快就出来。如果你出局很多,1)人们知道你尝试过。这对社会很重要,2)人们有一个基线,他们可以用来做出更好的有针对性的答案。我完全理解。我尝试过使用for loop但是当我尝试调试器时,它不起作用。所以我从我的代码中删除了。JJL,它在链接的问题()中得到了很好的介绍,但基础是在读取之前测试文件的结尾。如果读取失败是因为它到达了文件的结尾怎么办?哦,还有更多的事情可能会在读取文件时出错,而不仅仅是到达文件的结尾。如果出现任何情况,您会错过它们并继续尝试读取损坏的文件。这通常是无限循环beca使用无法到达的末端。