Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何从文本文件一次计算两个数组中的元素_C - Fatal编程技术网

C 如何从文本文件一次计算两个数组中的元素

C 如何从文本文件一次计算两个数组中的元素,c,C,我有一个编码任务,我需要从文本文档中获取点(x,y,z),并在控制台中打印出来,然后一次比较两个坐标,找出两者之间的距离,然后继续下一个两点。所有点都需要与前面的点和后面的点进行比较,因此每个点(第一点和最后一点除外)都需要进行两次比较。我试过很多不同的方法,也试过在网上寻找类似的问题,但都没有找到帮助。我知道如何从文本文档中获取点并将其输入控制台,但不知道如何比较点。我在文本文档中的点是(1,2,3),文档中的下一行是(0,0,0),第三行是(1,2,3),所以到最后,我应该有这两个点的点5.

我有一个编码任务,我需要从文本文档中获取点(x,y,z),并在控制台中打印出来,然后一次比较两个坐标,找出两者之间的距离,然后继续下一个两点。所有点都需要与前面的点和后面的点进行比较,因此每个点(第一点和最后一点除外)都需要进行两次比较。我试过很多不同的方法,也试过在网上寻找类似的问题,但都没有找到帮助。我知道如何从文本文档中获取点并将其输入控制台,但不知道如何比较点。我在文本文档中的点是(1,2,3),文档中的下一行是(0,0,0),第三行是(1,2,3),所以到最后,我应该有这两个点的点5.6568,我的点只是为了测试,代码需要使用300+个不同的点。谢谢你事先的帮助

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX_STRING_LENGTH 100

typedef struct Points Points;
struct Points
{
float x;
float y;
float z;
};
void getNumber(char string[], Points* point) 
{
int commaIndex = -1;
char* result = NULL;
result = strchr(string, ',');
char* stringStart = &string[0];
commaIndex = result - stringStart;

char* xString = malloc((commaIndex + 1) * sizeof(char));
strncpy(xString, string, commaIndex);
xString[commaIndex] = '\0';
point->x = atof(xString);

string = &string[0] + commaIndex + 1;
result = strchr(string, ',');
stringStart = &string[0];
commaIndex = result - stringStart;

char* yString = malloc((commaIndex + 1) * sizeof(char));
strncpy(yString, string, commaIndex);
yString[commaIndex] - '\0';
point->y = atof(yString);

string = &string[0] + commaIndex + 1;
point->z = atof(string);
}

int main(int argc, char** argv)
{
printf("This program will calculate distance between points and how long it will take.\n");

int numLines = 0, sum = 0, ysum = 0, zsum = 0;
float currentLine[MAX_STRING_LENGTH];

FILE *data = fopen("points.txt", "r");
if (data == NULL)
{
    printf("Error");
    return (EXIT_FAILURE);
}

while (!feof(data))
{
    fgets(currentLine, MAX_STRING_LENGTH, data);
    numLines++;
}
rewind(data);

Points* point = malloc(numLines * sizeof(Points));
for (int i = 0; i < numLines; i++)
{
    fgets(currentLine, MAX_STRING_LENGTH, data);
    getNumber(currentLine, &point[i]);
}
fclose(data);
for (int i = 0; i < numLines; i++)
{
    printf("X:%f ", point[i].x);
    printf("Y:%f ", point[i].y);
    printf("Z:%f ", point[i].z);
    printf("\n");
}



//float timeSum = sum + ysum + zsum;
//printf("%f", timeSum);

//distance = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2);

return 0;
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
#包括
#定义最大字符串长度100
typedef结构点;
结构点
{
浮动x;
浮动y;
浮动z;
};
void getNumber(字符字符串[],点*点)
{
int commaIndex=-1;
char*result=NULL;
结果=strchr(字符串',');
char*stringStart=&string[0];
commaIndex=结果-字符串开始;
char*xString=malloc((commaIndex+1)*sizeof(char));
strncpy(xString、string、commaIndex);
xString[commaIndex]='\0';
点->x=atof(xString);
字符串=&string[0]+commaIndex+1;
结果=strchr(字符串',');
stringStart=&string[0];
commaIndex=结果-字符串开始;
char*yString=malloc((commaIndex+1)*sizeof(char));
strncpy(yString、string、commaIndex);
yString[commaIndex]-“\0”;
点->y=atof(y字符串);
字符串=&string[0]+commaIndex+1;
点->z=atof(字符串);
}
int main(int argc,字符**argv)
{
printf(“此程序将计算点之间的距离以及所需的时间。\n”);
int numLines=0,sum=0,ysum=0,zsum=0;
浮动电流线[最大字符串长度];
文件*data=fopen(“points.txt”、“r”);
如果(数据==NULL)
{
printf(“错误”);
返回(退出失败);
}
而(!feof(数据))
{
fgets(当前线、最大字符串长度、数据);
numLines++;
}
倒带(数据);
点*point=malloc(numLines*sizeof(Points));
对于(int i=0;i
比较列表中的两个元素非常容易。下面是如何创建一个函数来计算点之间的距离

float TravDistance(Points* points, int n){
    float distance=0;
    for(int i=0; i<n-1; i++){
        distance+=sqrt( (double)pow((double)(points[i+1].x-points[i].x),(double)2)+
                (double)pow((double)(points[i+1].y-points[i].y),(double)2)+
                (double)pow((double)(points[i+1].z-points[i].z),(double)2));
    }
}
float travdance(点*点,整数n){
浮动距离=0;

对于(int i=0;i比较列表中的两个元素非常容易。下面是如何制作一个函数来计算点之间的距离

float TravDistance(Points* points, int n){
    float distance=0;
    for(int i=0; i<n-1; i++){
        distance+=sqrt( (double)pow((double)(points[i+1].x-points[i].x),(double)2)+
                (double)pow((double)(points[i+1].y-points[i].y),(double)2)+
                (double)pow((double)(points[i+1].z-points[i].z),(double)2));
    }
}
float travdance(点*点,整数n){
浮动距离=0;

对于(int i=0;i到底是什么问题?只需迭代数组,并对每个
索引
“比较”(因为您没有清楚地解释它的含义)在
index-1
index
index+1
index
之间的点(!feof(数据))
给出错误的结果。看到这个确认,没有缩进。伤了我的眼睛。使用
strtol
并检查第二个参数,而不是用strchr找到逗号。不需要倒带,只需读取一次数据。伪代码是:
while(数据可用){if-prev(比较当前和以前);prev=current}
根据您的需要捕获开始和结束边缘情况。具体问题是什么?只需迭代数组,并对每个
索引
“比较”(因为您没有清楚解释它的含义)在
索引-1
索引
索引+1
索引
之间的点(!feof(数据))
给出错误的结果。看到这个确认,没有缩进。伤了我的眼睛。使用
strtol
并检查第二个参数,而不是用strchr找到逗号。不需要倒带,只需读取一次数据。伪代码是:
while(数据可用){if-prev(比较当前和以前);prev=current}
根据需要捕捉开始和结束边缘案例。