读取文本文件并将列存储在数组中 [英] Reading text file and storing columns in an array

查看:82
本文介绍了读取文本文件并将列存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的文件:

I have file that looks like this:

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

我试图读取每一行并将每一列数据存储到单独的数组中。像这样:

I am trying to read each line and store each column of data into separate arrays. Something like this:

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}

到目前为止,我有这个:

So far I have this:

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

我的问题是如何使用以下命令在每个数组中存储数据列正确的索引?我还一直在使用输入文件重定向:

My issue is how to do I store my columns of data in each array with the correct index? I also have been using input file redirection:

program.exe < txtfile.txt

这是因为我希望能够读取任何类型的具有像上面的数据。

This is because I want to be able to read any sort of text file with columns of data like the one above.

赏金信息,因为发布时看起来并不好:

Bounty information, since it didn't look nice when i posted it:

好吧,我想能够查看第一列和第二列是否同时具有重复数字。例如,在第一列中,重复出现的 03下降,而在第二列中,则重复出现的 01下降。两者都在同一阶段发生,例如:

Okay so I want to be able to see if the first column and second column have recurring digits at the same time. For example, in the first column, there are recurring '03' going down, and in the second column, there are recurring '01' going down. Both happen at the same stage, like this:

    03 01
    03 01
    03 01

我希望能够弄清楚这一点,如果是这样,我希望能够将双精度值相加在第五列(最右边)。我希望它看起来像这样:

I want to be able to recongize that and if that is true, I want to be able to sum the doubles on the fifth column(far right). I want it to look like this:

    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

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

other wise, I want the program to continue searching for the first/second column occurrence. A bit of extra background info, The first column is a room type, the second column is a room instance, and the fifth column is the total area of those rooms. This is why I want to sum the areas on the far right, because the room types are the same as the instances.

推荐答案

不知道我是否正确理解了您的问题,但这是一个主意:

Not sure if I understood your question correctly but here's an idea:

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

}

这篇关于读取文本文件并将列存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆