簡體   English   中英

讀取文本文件並將列存儲在數組中

[英]Reading text file and storing columns in an array

我有看起來像這樣的文件:

01 01 5.00 1.50 7.50
02 01 4.00 3.00 12.00
02 02 3.00 4.00 12.00
03 01 4.50 3.00 13.50
03 01 7.50 2.50 18.75
03 01 6.00 0.50 3.00 
04 01 2.00 3.00 6.00 
04 02 2.00 3.00 6.00
05 01 1.50 3.00 4.50
07 01 5.00 1.00 5.00
09 01 1.50 6.00 9.00

我正在嘗試讀取每一行並將每一列數據存儲到單獨的數組中。 像這樣的東西:

int A[100] = {1, 2, 2, 3, 3, 3, 4, 4, 5, 7, 9}
int B[100] = {1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1}
double C[100] = {5.00, 4.00, 3.00, 4.50, 7.50, 6.00, 2.00, 2.00, 1.50, 5.00, 1.50}
double D[100] = {1.50, 3.00, 4.00, 3.00, 2.50, 0.50, 3.00, 3.00, 3.00, 1.00, 6.00}

到目前為止,我有這個:

#include <stdio.h>
#inlcude <stdlib.h>

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
    int i = 0;
    int a, b;
    double c, d;
    int A[MAXSIZE], B[MAXSIZE];
    double C[MAXSIZE], D[MAXSIZE]

    while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {

        A[i] = a;
        B[i] = b;
        C[i] = c;
        D[i] = d;
    }
    return 0;
}

我的問題是如何使用正確的索引將數據列存儲在每個數組中? 我也一直在使用輸入文件重定向:

program.exe < txtfile.txt

這是因為我希望能夠讀取任何類型的文本文件,其中包含像上面那樣的數據列。

賞金信息,因為我發布時它看起來不太好:

好的,所以我希望能夠查看第一列和第二列是否同時具有重復數字。 例如,在第一列中,重復出現“03”下降,而在第二列中,重復出現“01”下降。 兩者都發生在同一階段,如下所示:

    03 01
    03 01
    03 01

我希望能夠認識到這一點,如果這是真的,我希望能夠對第五列(最右邊)的雙打進行求和。 我希望它看起來像這樣:

    01 01 5.00 1.50 7.50
    02 01 4.00 3.00 12.00
    02 02 3.00 4.00 12.00
    03 01 4.50 3.00  ---
    03 01 7.50 2.50  ---
    03 01 6.00 0.50 35.25 
    04 01 2.00 3.00 6.00 
    04 02 2.00 3.00 6.00
    05 01 1.50 3.00 4.50
    07 01 5.00 1.00 5.00
    09 01 1.50 6.00 9.00

否則,我希望程序繼續搜索第一/第二列出現。 一些額外的背景信息,第一列是房間類型,第二列是房間實例,第五列是這些房間的總面積。 這就是為什么我要對最右側的區域求和,因為房間類型與實例相同。

#include <stdio.h>
#inlcude <stdlib.h>

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
    int i = 0;
    int a, b;
    double c, d;
    int A[MAXSIZE], B[MAXSIZE];
    double C[MAXSIZE], D[MAXSIZE]

    while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {

        A[i] = a;
        B[i] = b;
        C[i] = c;
        D[i] = d;
        i++; // your saviour  
    }
    return 0;
}

以下代碼:

cleanly compiles
does not leave trash in unused entries in arrays
eliminates unneeded variables
checks to assure that arrays are not overflowed
removes ~1/2 the statements in the code as they are not needed
corrects the spelling of the word `include`

現在的代碼:

#include <stdio.h>
#include <stdlib.h> // size_t

#define MAXSIZE (100)

int main( void )
{
    int    A[MAXSIZE] = {0};
    int    B[MAXSIZE] = {0};
    double C[MAXSIZE] = {0.0};
    double D[MAXSIZE] = {0.0};

    for ( size_t i=0;
          i< MAXSIZE && (scanf("%d %d %lf %lf", 
                               &A[i], &B[i], &C[i], &D[i]) == 4);
          i++);

    return 0;
}

從已經批准的答案中修改代碼以適應賞金的要求 -

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

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
    int i = 0;
    int a, b;
    double c, d, e;
    int A[MAXSIZE], B[MAXSIZE];
    double C[MAXSIZE], D[MAXSIZE], E[MAXSIZE];

    while (scanf("%d %d %lf %lf %lf", &a, &b, &c, &d, &e) == 5) {

        A[i] = a;
        B[i] = b;
        C[i] = c;
        D[i] = d;
        if (i > 0 && A[i] == A[i-1] && B[i] == B[i-1])
        {
          E[i] = E[i-1] + e;
          E[i-1] = -1;
        }
        else
          E[i] = e;
        i++;  
    }
    for (int j = 0; j < i; j++)
    {
      printf("%d %d %.2lf %.2lf %.2lf\n", A[j], B[j], C[j], D[j], E[j]);
    }
    return 0;
}

對於給定的輸入,它將打印 -

1 1 5.00 1.50 7.50
2 1 4.00 3.00 12.00
2 2 3.00 4.00 12.00
3 1 4.50 3.00 -1.00
3 1 7.50 2.50 -1.00
3 1 6.00 0.50 35.25
4 1 2.00 3.00 6.00
4 2 2.00 3.00 6.00
5 1 1.50 3.00 4.50
7 1 5.00 1.00 5.00
9 1 1.50 6.00 9.00

不確定我是否正確理解你的問題,但這里有一個想法:

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

#define MAXSIZE 100    

int main(int argc, char *argv[]) {
int i = 0;
int a, b;
double c, d;
int A[MAXSIZE], B[MAXSIZE];
double C[MAXSIZE], D[MAXSIZE];
int previousA=-1, previousB=-1;
double tempsum=0.0;

while (scanf("%d %d %lf %lf", &a, &b, &c, &d) == 4) {

    A[i] = a;
    B[i] = b;
    C[i] = c;
    D[i] = d;
    if (previousA==A[i] && previousB==B[i]) { //to check for recurrent values
        tempsum+=(C[i]*D[i]);
    }
    else {
        //do whatever you want with the last stored tempsum value, which is the result of the multiplication of the C and D elements needed
        tempsum=(C[i]*D[i]); //then tempsum is reset to the current multiplication
    }
    i++;
}
return 0;

}

暫無
暫無

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

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