【问题标题】:Reading text file and storing columns in an array读取文本文件并将列存储在数组中
【发布时间】:2016-04-17 06:22:29
【问题描述】:

我的文件如下所示:

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

否则,我希望程序继续搜索第一/第二列出现。一些额外的背景信息,第一列是房间类型,第二列是房间实例,第五列是这些房间的总面积。这就是我想对最右边的区域求和的原因,因为房间类型与实例相同。

【问题讨论】:

  • 为什么要存储在每个带有索引的数组中?
  • 添加一个i++ 作为while 循环内的最后一个语句?
  • 发布的代码根本无法编译。编译时,始终启用所有警告,然后修复这些警告。 (对于gcc 至少使用:-Wall -Wextra -pedantic 我也使用:-Wconversion -std=gnu99
  • 正如@kaylum 所说,在循环中递增 I,代码的其他部分看起来不错,您已经将它们存储在数组中。
  • 为了便于我们人类阅读和理解:1) 遵循公理:每行只有一个语句,并且(最多)每个语句一个变量声明。

标签: c arrays readfile


【解决方案1】:
#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;
}

【讨论】:

    【解决方案2】:

    以下代码:

    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;
    }
    

    【讨论】:

      【解决方案3】:

      根据已批准的答案修改代码以符合赏金的要求 -

      #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
      

      【讨论】:

        【解决方案4】:

        不确定我是否正确理解了您的问题,但这是一个想法:

        #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;
        

        }

        【讨论】:

          猜你喜欢
          • 2013-11-19
          • 1970-01-01
          • 2020-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多