簡體   English   中英

比較兩個結構中的浮點數

[英]comparing floats in two structs

我有一個讀取兩個.txt 文件並將它們放入結構的代碼。 然后,我想比較每個文件中的 x 值並找到匹配項。 這些值的類型是浮點數。 我不斷收到不正確的匹配項和行號,我不知道該怎么辦。 它適用於前幾個,但它只是說當它只是一個文件中的一個值時存在匹配。 其他一切都很好,所以如果有人對比較值有任何建議,將不勝感激。 謝謝你。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define FLT_EPSILON 1.19209290E-07F

typedef struct fcat_s {
    float x;
    float y;
    float a_j2000;
    float b_j2000;
    float mag;
} fcat_s;

typedef struct cat_s {
    float num;
    float x;
    float y;
    float xworld;
    float yworld;
    float flux_auto;
    float mag_auto;
    float awin;
    float bwin;
} cat_s;

int main(void) {
    
    float exptime = 0;
    float F = 0;
    float Mi = 0;
    float Mcat = 0;
    float FLUX_AUTO = 0;
    float ZP = 0;
    char fcatname[50];
    char catname[50];
    int fcatcount = 0;
    int catcount = 0;
    char fcat_c;
    char cat_c;
    float x;
    char str[100];
    int i = 0;
    int j = 0;
    float temp;
    int match = 0;
    
    printf("Please input the .fcat file name:\n");
    scanf("%str", fcatname);

    printf("Please input the .cat file name:\n");
    scanf("%str", catname);

    printf("Please input the exposure time:\n");
    scanf("%f", &exptime);
    
    fcat_s *f = (fcat_s *)malloc(sizeof(fcat_s));
    cat_s *c = (cat_s *)malloc(sizeof(cat_s));

    // .fcat file
    
    FILE *fcat;
    fcat = fopen(fcatname, "r");

    if (fcat == NULL) {
        printf("The input file does not exist\n");
    } else {
        for (fcat_c = getc(fcat); fcat_c != EOF; fcat_c = getc(fcat)) {
            if (fcat_c == '\n')
                fcatcount++;
            if (fcatcount > 4) {
                fscanf(fcat, "%f", &f[fcatcount-5].x);
                fscanf(fcat, "%f", &f[fcatcount-5].y);
                fscanf(fcat, "%f", &f[fcatcount-5].a_j2000);
                fscanf(fcat, "%f", &f[fcatcount-5].b_j2000);
                fscanf(fcat, "%f", &f[fcatcount-5].mag);
            }
        }
    }
    
    printf("\n");
    printf("The .fcat file has %d lines. \n", fcatcount);
    
    printf("\n");
    printf("\n");
    printf("FCAT CONTENTS\n");
    for (i = 0; i < (fcatcount-5); i++) {
        printf("%lf\t %lf\t %lf\t %lf\t %lf\n", 
               f[i].x, f[i].y, f[i].a_j2000, f[i].b_j2000, f[i].mag);
    }
    
    printf("\n");
    printf("\n");
    
    // .cat file
    
    FILE *cat;
    cat = fopen(catname, "r");

    if (cat == NULL) {
        printf("The input file does not exist\n");
    } else {
        for (cat_c = getc(cat); cat_c != EOF; cat_c = getc(cat)) {
            if (cat_c == '\n')
                catcount++;
            if (catcount > 8) {
                fscanf(cat, "%f", &c[catcount-9].num);
                fscanf(cat, "%f", &c[catcount-9].x);
                fscanf(cat, "%f", &c[catcount-9].y);
                fscanf(cat, "%f", &c[catcount-9].xworld);
                fscanf(cat, "%f", &c[catcount-9].yworld);
                fscanf(cat, "%f", &c[catcount-9].flux_auto);
                fscanf(cat, "%f", &c[catcount-9].mag_auto);
                fscanf(cat, "%f", &c[catcount-9].awin);
                fscanf(cat, "%f", &c[catcount-9].bwin);
            }
        }
    }
    
    printf("\n");
    printf("The .cat file has %d lines. \n", catcount);
    
    printf("\n");
    printf("\n");
    printf("CAT CONTENTS\n");
    for (i = 0; (i < catcount-9); i++) {
        printf("%lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\n", 
               c[i].num, c[i].x, c[i].y, c[i].xworld, c[i].yworld,
               c[i].flux_auto, c[i].mag_auto, c[i].awin, c[i].bwin);
    }
    
    printf("\n");
    printf("\n");
    
    // searching in the files for a match
    
    for (i = 0; (i <= (fcatcount-5)); i++){
        for (j = 0; (j <= (catcount-9)); j++) {
            if (fabs(f[i].x - c[j].x) < FLT_EPSILON && fabs(f[i].y - c[j].y) < FLT_EPSILON) {
                printf("%f\t .fcat line: %d\t .cat line: %d\n", c[j].x, i, j);
                match++;
            }
        }
    }
    
    printf("\n");
    printf("\n");
    
    printf("The files have %d matches. \n", match);
    
    fclose(fcat);
    fclose(cat);
    free(f);
    free(c);
    
    return 0;
}

您只為一個fcat_s分配了空間:

fcat_s *f = (fcat_s*)malloc(sizeof(fcat_s));

但是在您的第一個循環中,您已經將f視為一個數組:

...
fscanf(fcat, "%f", &f[fcatcount-5].x);
...

除非我遺漏了什么,否則您似乎正在讀寫未分配的 memory。 我很驚訝你的程序沒有段錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM