如何删除结构数组中的重复元素并打印输出

时间:2019-02-04 22:40:24

标签: c data-structures

我正在尝试从文件读取的数据中删除重复数据,并将其存储在struct数组中,并将新的唯一值打印到文件中

我有一个带有x,y值和索引的文件,我已经从文件中读取了数据并将其存储在struct数组中,我想遍历该数组并最终删除重复的元素(x和y匹配)将新的唯一值打印到文件中。我正在读取的文件具有以下数据

  1. 12 20
  2. 20 29
  3. 68 87
  4. 20 29

我的目标是将文件读取到一个struct数组中,删除重复项并将其打印到另一个文件中。下面是我的代码

int store_fixation_point(FILE *fp,int id,struct fixation_point_type fixation_point[],int x, int y);
struct fixation_point_type {
    int id_number;
    int x;
    int y;
};
int main()
{
    int N_list,i,N,j;
    int temp1 = 0;
    float temp2, temp3;

    int id_N0=0;
    FILE *fp_in, *fp_out;
    int fixation_number = 0, x =0, y=0;
    char ch;
    fixation_point_type fixationPoint [MAX_NUMBER_OF_POINTS];
fp_in = fopen("../data/input.txt", "r");
if (fp_in == NULL)
{
    printf("Cannot open the input file \n");
    exit(0);
}
fp_out = fopen("../data/output.txt", "w");
if (fp_out == NULL)
{
    printf("Cannot open the output file \n");
    exit(0);
}

fscanf(fp_in,"%d",&N_list);
i = 0;
do
{
    i++;
    N = 0;
    temp2 = 0;
    temp3 = 0;
    do
    {
        if (fscanf(fp_in, "%d %f %f ", &temp1,&temp2,&temp3) != EOF){
        if (temp2 == -1 && temp2 == -1)
        {
            break;
        }
        else
        {
            fixationPoint[N].id_number = temp1;
            fixationPoint[N].x = temp2;
            fixationPoint[N].y = temp3;
            N++;
        }
        }
    }while(1);
    store_fixation_point(fp_out,N,fixationPoint,x,y);
            fscanf(fp_in, "%d %d %d", &fixation_number, &x, &y);
            N++;
  for (j=0;j<N;j++)
 {
     printf("%3d %d %d\n",fixationPoint[j].id_number,(int)fixationPoint[j].x,(int)fixationPoint[j].y);
      fprintf(fp_out,"%3d %d %d\n",fixationPoint[j].id_number,(int)fixationPoint[j].x,(int)fixationPoint[j].y);
   }
    printf("------------------------\n");
    fprintf(fp_out,"------------------------\n");
}while(i<N_list);
fclose(fp_in);
fclose(fp_out);
return 0;
}
//function to remove duplicates
int store_fixation_point(FILE *fp,int indexID,struct fixation_point_type fixation_point[],int x, int y)
{
    int i;
    int a =0;
    int index=0;
    fixation_point_type temp[1000];
    if(indexID == 0){
        printf("%d %d %d\n",fixation_point[indexID].id_number, fixation_point[indexID].x,fixation_point[indexID].y);    
    }
    else
    {
        for (i =0; i < indexID; i++){
            if ( (fixation_point[i].x ==x)  && (fixation_point[i].y==y) ){
                a=a+1;
            }   
        }
        if (a==0)
        {
            printf("%d %d %d\n",fixation_point[indexID].id_number, fixation_point[indexID].x,fixation_point[indexID].y);
            fprintf(fp,"%d %d %d \n",fixation_point[indexID].id_number, fixation_point[indexID].x,fixation_point[indexID].y);   
        }
        return indexID;

我希望输出唯一值,例如

  1. 12 20
  2. 20 29
  3. 68 87

1 个答案:

答案 0 :(得分:0)

将程序的各个部分分成单独的函数或过程是一个好主意。这样,每个都充当一个“黑匣子”,其行为被封装。第一个函数是{% url 'product-list' %},它将按成员比较两个结构。基于此基本功能,我们可以构建更大的功能cmp,以删除重复项。

remDups

/* return 0 if structures are memberwise equal; false otherwise */ int cmp(const struct fixation_point_type *const a, const struct fixation_point_type *const b) { return (a->id_number == b->id_number && a->x == b->x && a->y == b->y); } /* removes duplicates in the array arr */ void remDups(struct fixation_point_type arr[], size_t n) { assert(n >= 0); if (n < 2) return; size_t len = n; while (--n > 0) { /* if this elem. and the previous elem. are equal, shift elements beyond arr[n] into its position */ if (cmp(arr[n], arr[n-1]) == 0) memmove(arr + n - 1, arr + n, sizeof (*arr) * (len-n)); } } 函数使用remDups移动元素,覆盖重复元素。表达式memmovesizeof (*arr) * (len-n)之后的元素中的字节数。

相关问题