【发布时间】:2016-09-08 18:09:20
【问题描述】:
我有两组数据集:红色和绿色,我想比较比率之间的差异,但必须先对齐它们。
原始文件(前几行 200,000 个条目)
A B C D
Red Ratio Green Ratio
1 0.35 1 0.21
2 0.45 2 0.235
3 0.45 3 0.154
4 0.235 4 0.156
6 0.156 5 0.146
7 0.668 6 0.154
8 0.44 7 0.148
9 0.446 8 0.148
10 0.354 9 0.199
11 0.154 10 0.143
12 0.49 12 0.148
使用代码后,值对齐并删除“附加项”,并将列向上移动。
A B C D
Red Ratio Green Ratio
1 0.35 1 0.21
2 0.45 2 0.235
3 0.45 3 0.154
4 0.235 4 0.156
6 0.156 6 0.154
7 0.668 7 0.148
8 0.44 8 0.148
9 0.446 9 0.199
10 0.354 10 0.143
12 0.49 12 0.148
15 0.146 15 0.87
17 0.113 17 0.113
19 0.44 19 0.448
这是我到目前为止的代码:我正在获取 A 和 C 之间的差异来检查它们是否为 0,如果不是则调整它们。
#include <stdio.h>
int deletemove(char column, int row)
{
// This script would delete the positions mentioned in the arguments, and shift the other values up.
}
int main(void)
{
//Opening input file for read/write
FILE *input;
input=fopen("/full/path/file.xlsx", "r");
if (input == NULL) {printf("error opening input file\n");}
//Store the values from file into an array
int colA[1024];
int colC[1024];
// read contents of columns A and C and store in an array
int ai;
for(ai=1; ai<1024; ai++)
{ fseek(input,ai,SEEK_SET);
colA[ai]=fgetc(input);
}
int ci;
for(ci=1; ci<1024; ci++)
{ fseek(input,ci,SEEK_SET);
colC[ci]=fgetc(input);
}
//Take difference between value of Column A and C to check if they are identical.
int j;
char A,B;
for (j = 1; j < 1024; j++)
{
int check = colA[j] - colC[j]; // check difference between two values in a column
if (check > 0)
deletemove(A,j); //delete values from column C and D
else if (check < 0)
deletemove(B,j); // delete values from column A and B
}
fclose(input); // close files
}
我需要帮助实现删除行/列函数并读取数组中的值。
另外,将 200,000 个值存储在一个数组中是否可行?
谢谢。
【问题讨论】:
-
关于这一行:
if (input == NULL) {printf("error opening input file\n");}也需要exit()程序,不继续程序就好像打开成功一样 -
代码实际上只需要包含每列的当前值(假设
A和C是按升序排列的)加上正在保存/测试的当前值。我建议,当每一对都匹配时,将一行附加到一个包含匹配值的新文件中。然后在匹配完成后,读取匹配的值进行处理/显示 -
1024 大小的数组不太可能容纳 200,000 行输入的所有匹配项。
-
这段代码:
int check = colA[j] - colC[j]; if check > 0有点乱,建议if( colA[j] == colB[j] ) -
如果我要在内存中存储大约 200,000 个条目(乘以 2),我不会将它们放在堆栈中。如果您确定
200,000号码,那么我建议将它们放在file global内存中。但是,如果不确定条目的确切数量,我建议使用heap和realloc()函数来创建所需的存储区域。