Arrays 检查struct数组中是否存在strng值
我有一个结构,用户可以在其中输入他们选择的多个坐标并标记每个坐标。因此,如果他们选择输入3个坐标,他们需要标记所有3个坐标并给出坐标。我可以遍历所有数据并验证标签是否存在,但如果找不到,则无法为struct标签赋值 这是我的密码Arrays 检查struct数组中是否存在strng值,arrays,c,loops,struct,Arrays,C,Loops,Struct,我有一个结构,用户可以在其中输入他们选择的多个坐标并标记每个坐标。因此,如果他们选择输入3个坐标,他们需要标记所有3个坐标并给出坐标。我可以遍历所有数据并验证标签是否存在,但如果找不到,则无法为struct标签赋值 这是我的密码 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> /*Stuct link list */ struct node
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/*Stuct link list */
struct node
{
double xCoord;
double yCoord;
char label[30];
};
int checkLabel(char* receivedlabel, int coordSize, struct node coord[coordSize])
{
int i,j;
if (coordSize == 0)
{
}
else
{
for(i = 0; i<coordSize; i++)
{
if(strcmp(coord[i].label, receivedlabel)==0)
{
j = 0;
break;
}
else
{
j = 1;
}
}
}
return j;
}
int main() {
int i, numOfCoords, j, k;
double distance;
double closest = 0;
double farthest = 0;
char label[30];
do //Get the amount of coordinates to enter greater than 2
{
printf("How many coords do you want to enter?\nPlease enter more than 2:");
scanf("%d", &numOfCoords);
}while(numOfCoords <=2);
struct node coords[numOfCoords];
for(i = 0; i < numOfCoords; i++ )
{
do
{
printf("Please Enter a X Coordinate:");
scanf("%lf", &coords[i].xCoord);
printf("Please Enter a y coordinate:");
scanf("%lf", &coords[i].yCoord);
}while(coords[i].xCoord<0 || coords[i].yCoord<0);
if(i==0)
{
printf("Please Enter a Label:");
scanf("%s", &coords[i].label);
}
else
{
do
{
printf("Please Enter a Label:");
scanf("%s", &label);
k = checkLabel(label,i, coords);
if(k == 1)
{
*coords[i].label = label;
}
}while(k==0);
}
}
printf("\n");
for(i = 0; i < numOfCoords-1; i++ )
{
for(j = 1; j < numOfCoords; j++)
{
distance = sqrt(pow(coords[i].xCoord - coords[j].xCoord,2) + pow(coords[i].yCoord - coords[j].yCoord,2)); //Calculate Distance
if(distance == 0) //If distance equal 0 output no info
{
}
else
{
//Print out the distance of all coordinates entered
printf("The distance between %s:[%lf,%lf] and %s:[%lf,%lf] is %lf\n", coords[i].label, coords[i].xCoord, coords[i].yCoord,coords[j].label, coords[j].xCoord, coords[j].yCoord, distance);
if(j==1 && i==0)
{
//Set the closet and furthest distance to the first calculated distance
closest = distance;
farthest = distance;
}
else
{
if(distance > farthest)//If distance is larger than current furthest set furthest equal to current distance
{
farthest = distance;
}
if(distance < closest) //If distance is less than closet set closet equal to current distance
{
closest = distance;
}
}
}
}
}
printf("The closet coordinate is %lf and the furthest is %lf\n", closest, farthest);//Display closest and farthest distance
return 0;
}
我发现问题可能在于您直接从读取的scanf动态使用numofoords声明节点的方式 无论如何,我写了一个例子,用你的代码作为起点,我想我可以做你想做的 如果我还能帮上什么忙,请告诉我
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/*Stuct link list */
struct node
{
double xCoord;
double yCoord;
char label[30];
};
int checkLabel(char* receivedlabel, int coordSize, struct node coord[coordSize])
{
int i,j;
if (coordSize > 0)
{
for(i = 0; i<coordSize; i++)
{
if(strcmp(coord[i].label, receivedlabel)==0)
{
return 0;
}
}
}
return 1;
}
int main() {
int i = 0, numOfCoords = 0, j = 0, k = 0;
double distance;
double closest = 0;
double farthest = 0;
char label[30];
struct node coords[5];
while(numOfCoords <=2) //Get the amount of coordinates to enter greater than 2
{
printf("How many coords do you want to enter?\nPlease enter more than 2:");
scanf("%d", &numOfCoords);
}
for(i = 0; i < numOfCoords; i++)
{
do
{
printf("Please Enter a X Coordinate:");
scanf("%lf", &coords[i].xCoord);
printf("Please Enter a y coordinate:");
scanf("%lf", &coords[i].yCoord);
}while(coords[i].xCoord<0 || coords[i].yCoord<0);
do{
printf("Please Enter a Label:");
scanf("%s", label);strtok(label, "\n");
if ( i > 0 && checkLabel(label, i, coords) == 0 ){
printf("\n");
continue;
}
strcpy(coords[i].label,label);
break;
}
while(1);
}
printf("\n");
for(i = 0; i < numOfCoords; i++ )
{
printf("i=%d", i);
for(j = 0; j < numOfCoords; j++)
{
distance = sqrt(pow(coords[i].xCoord - coords[j].xCoord,2) + pow(coords[i].yCoord - coords[j].yCoord,2)); //Calculate Distance
if(distance == 0) //If distance equal 0 output no info
{
}
else
{
//Print out the distance of all coordinates entered
printf("The distance between %s:[%lf,%lf] and %s:[%lf,%lf] is %lf\n", coords[i].label, coords[i].xCoord, coords[i].yCoord,coords[j].label, coords[j].xCoord, coords[j].yCoord, distance);
if(j==1 && i==0)
{
//Set the closet and furthest distance to the first calculated distance
closest = distance;
farthest = distance;
}
else
{
if(distance > farthest)//If distance is larger than current furthest set furthest equal to current distance
{
farthest = distance;
}
if(distance < closest) //If distance is less than closet set closet equal to current distance
{
closest = distance;
}
}
}
}
}
printf("The closet coordinate is %lf and the furthest is %lf\n", closest, farthest);//Display closest and farthest distance
return 0;
}
所以我能找到答案。我选择使用&coords[I].label,而不是让用户以字符的形式输入字符串,然后我将该值传递给函数,函数可以循环使用现有值,如果没有找到,则将该值分配给coords[I].label 这是我的新代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/*Stuct link list */
struct node
{
double xCoord;
double yCoord;
char label[30];
};
int checkLabel(char* receivedlabel, int coordSize, struct node coord[coordSize])
{
int i,j;
if (coordSize == 0)
{
}
else
{
for(i = 0; i<coordSize; i++)
{
if(strcmp(coord[i].label, receivedlabel)==0)
{
j = 0;
break;
}
else
{
j = 1;
}
}
}
return j;
}
int main() {
int i, numOfCoords, j, k;
double distance;
double closest = 0;
double farthest = 0;
char label[30];
do //Get the amount of coordinates to enter greater than 2
{
printf("How many coords do you want to enter?\nPlease enter more than 2:");
scanf("%d", &numOfCoords);
}while(numOfCoords <=2);
struct node coords[numOfCoords];
for(i = 0; i < numOfCoords; i++ )
{
do
{
printf("Please Enter a X Coordinate:");
scanf("%lf", &coords[i].xCoord);
printf("Please Enter a y coordinate:");
scanf("%lf", &coords[i].yCoord);
}while(coords[i].xCoord<0 || coords[i].yCoord<0);
if(i==0)
{
printf("Please Enter a Label:");
scanf("%s", &coords[i].label);
}
else
{
do
{
printf("Please Enter a Label:");
scanf("%s", &coords[i].label);
k = checkLabel(coords[i].label,i, coords);
}while(k==0);
}
}
printf("\n");
for(i = 0; i < numOfCoords-1; i++ )
{
for(j = 1; j < numOfCoords; j++)
{
distance = sqrt(pow(coords[i].xCoord - coords[j].xCoord,2) + pow(coords[i].yCoord - coords[j].yCoord,2)); //Calculate Distance
if(distance == 0) //If distance equal 0 output no info
{
}
else
{
//Print out the distance of all coordinates entered
printf("The distance between %s:[%lf,%lf] and %s:[%lf,%lf] is %lf\n", coords[i].label, coords[i].xCoord, coords[i].yCoord,coords[j].label, coords[j].xCoord, coords[j].yCoord, distance);
if(j==1 && i==0)
{
//Set the closet and furthest distance to the first calculated distance
closest = distance;
farthest = distance;
}
else
{
if(distance > farthest)//If distance is larger than current furthest set furthest equal to current distance
{
farthest = distance;
}
if(distance < closest) //If distance is less than closet set closet equal to current distance
{
closest = distance;
}
}
}
}
}
printf("The closet coordinate is %lf and the furthest is %lf\n", closest, farthest);//Display closest and farthest distance
return 0;
}
了解您是否接收到编译错误、它是什么以及它所指的行会有所帮助*坐标[i]。标签=标签;看起来很不对。使用strcpy复制字符串。除非检查返回值,否则无法开始正确使用任何输入函数。一次键的失误导致一个无效的双精度值,您将进入一个无限循环。您的代码中有4个警告转化为指针错误。始终在启用警告的情况下编译,并且在编译代码时不接受警告。您的问题出现在第71、78、83行,第42行返回了一个可能未初始化的j。让编译器帮助您编写代码