簡體   English   中英

幾乎相同的代碼不打印。 為什么不?

[英]Almost identical code not printing. Why not?

我的任務是從 .txt 文件中讀取數據,並對 .txt 文件中的數據進行各種處理。 這是我將使用的 a.txt 文件的示例;

N 51.0 751.0 50.0
N 101.0 501.0 70.0
N 351.0 751.0 90.0
N 501.0 201.0 110.0
P 280.0 110.0
P 730.0 190.0

現在,我有一個 function 類似的事情重復了兩次。 不同之處在於scanf function 正在掃描然后打印到 2 個不同的 arrays 結構中。 最后的for循環本質上應該做同樣的事情,只是針對不同的 arrays。 這是代碼;

void agg_noise_level_p(){
    rewind(stdin);
    noise_n_t n[MAX_ARRAY];
    noise_p_t p[MAX_ARRAY];
    int i = 0, j = 0, count1 = 0, count2 = 0;
    double xn[MAX_ARRAY], yn[MAX_ARRAY], out[MAX_ARRAY];
    double xp[MAX_ARRAY], yp[MAX_ARRAY];

    while((scanf("%s %lf %lf %lf", &n[i].np, &n[i].x, &n[i].y, &n[i].output) == 4)){
        if(n[i].np == 'N'){
            xn[i] = n[i].x;
            yn[i] = n[i].y;
            out[i] = n[i].output;
            i++;
            count1++;
        }
    }
    while((scanf("%s %lf %lf", &p[i].np, &p[i].x, &p[i].y) == 3)){
        if(p[j].np == 'P'){
            xp[j] = p[j].x;
            yp[j] = p[j].y;
            j++;
            count2++;
        }
    }
    for(i = 0; i < count1; i++){
        printf("%f %f %f\n", xn[i], yn[i], out[i]);
    }
    for(j = 0; j < count2; j++){
         printf("%f %f\n", xp[j], yp[j]);
    }
}

現在 output 只顯示;

51.000000 751.000000 50.000000
101.000000 501.000000 70.000000
351.000000 751.000000 90.000000
501.000000 201.000000 110.000000

xpyp arrays 沒有。 output 應顯示;

51.000000 751.000000 50.000000
101.000000 501.000000 70.000000
351.000000 751.000000 90.000000
501.000000 201.000000 110.000000
280.000000 110.000000
730.000000 190.000000

為什么會這樣? 我做錯了什么,我需要解決什么? 我猜它與最終for循環有關,但不確定。

如果你想使用scanf ,我建議實現一個可以處理N行 4 個字段和P行 3 個字段的單個循環。

在簡化的示例中,我用printf output 替換了計算數據並將它們存儲到n[i]p[j]等。

#include <stdio.h>

int main(void)
{
        char np;
        double x;
        double y;
        double output;
        int res;
        int error = 0;
        int end = 0;

        do {
                /* The space before %c is important. */
                res = scanf(" %c %lf %lf %lf", &np, &x, &y, &output);

                if((res == 4) && (np == 'N')) {
                        /* if necessary check for N line after P line and
                         * set error flag instead of storing data*/

                        /* store data for N case */
                        printf("%d: %c %lf %lf %lf\n", res, np, x, y, output);
                } else if ((res == 3) && (np == 'P')) {
                        /* store data for P case */
                        printf("%d: %c %lf %lf\n", res, np, x, y);
                } else if (res < 0) {
                        if(feof(stdin)) {
                                end = 1;
                        } else {
                                error = 1;
                        }
                } else {
                        error = 1;
                }
        } while(!end && !error);

        printf("end %d error %d\n", end, error);
        return 0;
}

暫無
暫無

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

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