【问题标题】:How to determine height and length of a 2D array from stdin?如何从标准输入确定二维数组的高度和长度?
【发布时间】:2013-05-30 14:40:44
【问题描述】:

我的程序必须像这样读取文件的内容:

name_of_program < Test.txt

Test.txt 是一个二维整数数组(整数由空格分隔,有时是多个整数,整数行由 EOL 分隔),但长度和高度未知。我如何确定它们? 我计划将链表的节点中的每一行存储为整数数组。

编辑:我的程序大致如下:

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

struct node {
    int *val; //stores the row
    struct node *next;
};

struct node *addnode(int *val, struct node *next);
struct node *mergesort(struct node *head, int column); //column by which I'll sort it
struct node *merge(struct node *head_one, struct node *head_two, int column);

int main(int column) //number of column to sort by, should run like that "name_of_program column < Test.txt" in Unix systems
{
    struct node *head;
    struct node *current;
    struct node *next;
    head = NULL;

    /* Reading from stdin line by line, writing it to list and linking nodes - I have only the last bit done */

    head = mergesort(head, column);

    /* Writing to stdout, row by row of sorted list, I can't do that without the previous bit */

    for(current = head; current != NULL; current = next)
        next = current->next, free(current);
    return 0;
};


struct node *addnode(int *val, struct node *next)
{
    struct node *tnode;
    tnode = (struct node*)malloc(sizeof(*tnode));
    if(tnode != NULL) {
        tnode->val = val; //not sure about this line, would it write whole array, or just one element?
        tnode->next = next;
    };
    return tnode;
}

struct node *mergesort(struct node *head, int column)
{
    struct node *head_one;
    struct node *head_two;
    if((head == NULL) || (head->next == NULL))
        return head;
    head_one = head;
    head_two = head->next;
    while((head_two != NULL) && (head_two->next != NULL)) {
        head = head->next;
        head_two = head->next->next;
    };
    head_two = head->next;
    head->next = NULL;
    return merge(mergesort(head_one, column), mergesort(head_two, column), column);
}

struct node *merge(struct node *head_one, struct node *head_two, int column)
{
    struct node *head_combined;
    if(head_one == NULL)
        return head_two;
    if(head_two == NULL)
        return head_one;
    if(head_one->val[column] < head_two->val[column]) {
        head_combined = head_one;
        head_combined->next = merge(head_one->next, head_two, column);
    } else {
        head_combined = head_two;
        head_combined->next = merge(head_one, head_two->next, column);
    };
    return head_combined;
}

【问题讨论】:

  • 通过读入文本,判断一行的值个数和总行数?
  • 如果输入文件是整数的文本表示,各个条目之间的分隔符与行之间的分隔符不同(例如,空格分隔的行),您可以遵循 Oli 的建议。如果输入文件只是一堆整数,你会很难过。 (如果给您 4 个值,是 2x2、1x4 还是 4x1?)如何确定尺寸完全取决于您。
  • @WilliamPursell 每行的整数之间有空格(有时是多个),每行之间有 EOL。我对 C 完全陌生,虽然我有点理解排序算法(因为它更像是数学而不是编程),但剩下的就是黑魔法。

标签: c arrays list integer stdin


【解决方案1】:

您可以从 C 中逐行读取文件开始,您可以使用 getline 函数。 考虑到行的最大长度,您将需要分配足够大的缓冲区。 处理行,从行中检索整数,这可以通过循环行缓冲区来完成 并跳过空格。整数可以存储在您可以创建的二维数组或链表中。

【讨论】:

  • 我更新了问题。我在理解英语语言规范方面遇到问题,据我所知,我的母语没有。你能告诉我它到底应该是什么样子吗?
猜你喜欢
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
相关资源
最近更新 更多