【问题标题】:Aligning dataset columns in C在 C 中对齐数据集列
【发布时间】: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()程序,不继续程序就好像打开成功一样
  • 代码实际上只需要包含每列的当前值(假设AC 是按升序排列的)加上正在保存/测试的当前值。我建议,当每一对都匹配时,将一行附加到一个包含匹配值的新文件中。然后在匹配完成后,读取匹配的值进行处理/显示
  • 1024 大小的数组不太可能容纳 200,000 行输入的所有匹配项。
  • 这段代码:int check = colA[j] - colC[j]; if check &gt; 0有点乱,建议if( colA[j] == colB[j] )
  • 如果我要在内存中存储大约 200,000 个条目(乘以 2),我不会将它们放在堆栈中。如果您确定200,000 号码,那么我建议将它们放在file global 内存中。但是,如果不确定条目的确切数量,我建议使用heaprealloc() 函数来创建所需的存储区域。

标签: c arrays excel alignment


【解决方案1】:

在数组中存储 200,000 个值是否可行?

可以,只要您不将这些数组放入堆栈即可。在函数内声明变量会将变量放入堆栈(1)。现代(2016 年)桌面通常将堆栈大小限制为几兆字节,而主内存为几千兆字节。

所以最好将大数组放入主存。这可以通过多种方式完成:

  • 使用全局数组,即在任何函数之外声明数组
  • 使用静态数组,即用static关键字声明数组
  • 使用动态分配的数组,即使用malloc来分配数组

(您也可以使用链表。链表的优点是它可以根据需要增长;您无需提前知道空间需求。)

在您的情况下,我会将比率存储在由红色/绿色值给出的索引处的数组中。假设比率始终为正数,我将使用-1.0 初始化数组中的所有条目。然后,当您阅读文件时,将比率存储在两个数组中的适当位置。例如,当您阅读该行时

6   0.156   5   0.146

0.156 存储在红色数组的索引6,并将0.146 存储在绿色数组的索引5

从文件中读取所有值后,您可以简单地扫描两个数组,并打印两个数组都具有非负值的值。

(1) 忽略没有正常堆栈的古怪系统(例如小型嵌入式系统)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2018-12-02
    • 2017-08-25
    • 2012-06-17
    • 2023-01-31
    相关资源
    最近更新 更多