如何删除struct数组中的重复元素并打印输出
我试图从从文件读取并存储在struct数组中的数据中删除重复项,并将新的唯一值打印到文件中 我有一个具有x、y值和索引的文件,我已从文件中读取数据并将其存储在struct数组中,我希望遍历该数组并删除重复元素(x和y匹配),最后将新的唯一值打印到文件中。我正在读取的文件包含以下数据如何删除struct数组中的重复元素并打印输出,c,data-structures,C,Data Structures,我试图从从文件读取并存储在struct数组中的数据中删除重复项,并将新的唯一值打印到文件中 我有一个具有x、y值和索引的文件,我已从文件中读取数据并将其存储在struct数组中,我希望遍历该数组并删除重复元素(x和y匹配),最后将新的唯一值打印到文件中。我正在读取的文件包含以下数据 12 20 20 29 68 87 20 29 我的目标是将文件读入一个struct数组,删除重复项并将它们打印到另一个文件中 int store_fixation_point(FILE *fp,int id,str
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;
int-store\u-fixation\u-point(文件*fp,int-id,结构固定点\u-point\u-type固定点[],int-x,int-y);
结构固定点类型{
国际身份证号码;
int x;
int-y;
};
int main()
{
int N_列表,i,N,j;
int temp1=0;
浮动temp2、temp3;
int id_N0=0;
文件*fp_输入,*fp_输出;
整数=0,x=0,y=0;
char ch;
固定点类型固定点[最大固定点数量];
fp_in=fopen(“../data/input.txt”,“r”);
if(fp_in==NULL)
{
printf(“无法打开输入文件\n”);
出口(0);
}
fp_out=fopen(“../data/output.txt”,“w”);
如果(fp_out==NULL)
{
printf(“无法打开输出文件\n”);
出口(0);
}
fscanf(fp_在、%d和N_列表中);
i=0;
做
{
i++;
N=0;
temp2=0;
temp3=0;
做
{
如果(fscanf(fp_in,“%d%f%f”、&temp1、&temp2和&temp3)!=EOF){
if(temp2==-1&&temp2==-1)
{
打破
}
其他的
{
固定点[N]。id_编号=temp1;
固定点[N].x=temp2;
固定点[N].y=temp3;
N++;
}
}
}而(1),;
存储固定点(fp out,N,固定点,x,y);
fscanf(fp_in,“%d%d%d”、&x和&y);
N++;
对于(j=0;j来说,最好将程序的各个部分分离为单独的函数或过程。这样,每个部分都充当一个“黑匣子”其行为被封装。第一个函数是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));
}
}
/*如果结构在成员方面相等,则返回0;否则返回false*/
int cmp(常数结构固定点固定类型*常数a,常数结构固定点固定点固定类型*常数b)
{
返回(a->id\u number==b->id\u number&&a->x==b->x&&a->y==b->y);
}
/*删除阵列arr中的重复项*/
无效remDups(结构固定点类型arr[],尺寸n)
{
断言(n>=0);
if(n<2)
返回;
尺寸长度=n;
而(--n>0){
/*如果此元素与前一元素相等,则将元素移到后面
arr[n]进入它的位置*/
如果(cmp(arr[n],arr[n-1])==0)
memmove(arr+n-1,arr+n,sizeof(*arr)*(len-n));
}
}
remDups
函数使用memmove
移动元素,覆盖重复的元素。表达式sizeof(*arr)*(len-n)
是元素中超出arr[n]的字节数
我的评论是错误的,但请纠正一致的缩进。这就是我犯错误的原因。实际上,如果在读取过程中检查重复项,并且只存储唯一值,而不是将所有x、y值读取到数组中,然后从arr中删除它们,则可以节省大量的元素复制和乱序是的,我该怎么做大卫,我认为这比我谁的DV:d更有效,但我敢打赌,这是因为这是一个只有代码的答案。让投票人失望的是:添加了评论,修复了代码中的打字错误