检查结构数组中是否存在strng值

时间:2021-04-16 02:41:04

标签: arrays c loops struct

我有一个结构体,用户可以在其中输入他们选择的多个坐标并标记每个坐标。因此,如果他们选择输入 3 个坐标,则需要标记所有 3 个坐标并给出坐标。我可以遍历所有数据并验证标签是否存在,但如果没有找到,我无法为结构标签分配该值。

这是我的代码

#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;
}

2 个答案:

答案 0 :(得分:0)

我发现问题可能在于您声明节点的方式,直接从读取 scanf 动态使用 numofcoords。

无论如何,我已经写了一个示例,使用您的代码作为起点,我想我可以做您想做的事。

如果我能帮上什么忙,请告诉我!

#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;
}

答案 1 :(得分:0)

所以我能够弄清楚。我没有让用户输入字符串作为字符,我选择使用 &coords[i].label 然后我将该值传递给函数,它能够遍历现有值,如果没有找到,则将值分配给 coords[i ].标签

这是我的新代码

#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;
}