【问题标题】:Failing to construct a stack using array in C无法在 C 中使用数组构造堆栈
【发布时间】:2021-01-30 17:26:42
【问题描述】:

现在我有一个输入由带有“(”和“)”的随机字符串组成。我想将这些括号推入一个名为“balance”的堆栈中。但是在我输入后,程序会终止,堆栈中没有任何内容。我该如何解决这个问题?

这是我的代码:

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

#define STACKSIZE 1000

struct stack  
{
    int top;
    char items[STACKSIZE];
};

void push(struct stack *pb, char x)
{
    if(pb->top==STACKSIZE)
        printf("The stack is full\n");
    else
        pb->items[pb->top++]=x;
}


int main()
{
    while(1)
    {
        struct stack balance;     //the stack called "balance"
        struct stack *b;          //the pointer of stack
        char line[STACKSIZE];     //input
        scanf("%s", line);

        if(!strcmp(line, "-1"))   //program stops when only "-1"
            break;

        b->top=0;                 //initializing top value

        for(int i=0;i<STACKSIZE-1;i++)        
        {
            if(line[i]=='(' || line[i]==')')      //push '(' and ')' in the input to the stack
                push(b, line[i]);
        }
        printf("test");           //can't reach this line
        printf("%s\n", b->items);

    }

    return 0;
}

【问题讨论】:

    标签: arrays c stack


    【解决方案1】:

    出于某种原因,您使用 b 作为指向 stack 的指针,而不是 &balance 本身。这没有错,但您必须初始化b,使其真正指向结构。所以,而不是:

    struct stack *b;
    

    你应该有:

    struct stack *b = &balance;
    

    另一个潜在的问题是您一直在阅读,直到 i 达到 STACKSIZE。这是错误的,因为您不知道输入是否会达到那个长度,如果没有,那么您就是在 line 末尾之前阅读。

    所以,而不是:

    for(int i=0;i<STACKSIZE-1;i++)        
    {
    

    你应该有:

    for(int i=0;line[i] != '\0';i++)        
    {
    

    顺便说一句,您不需要在循环中定义(“为每次迭代重新定义”)所有本地人。这实际上不会发生,因为优化器会将它们移出,但无论如何。

    谈到潜在问题,无限循环总是一个坏主意。准确定义退出条件是什么,您将处于安全的一边。在这种情况下,您甚至可以定义要发生的异常情况并调用break,但这不是一般规则。

    您正在读取输入,在这种情况下,真正常见的是预读技术。

    int input_status = scanf("%s", line);
        
    while(input_status != EOF
       && strcmp(line, "-1"))
    {
        // more things...
    
        input_status = scanf("%s", line);
    }
    

    这样,每次您测试退出条件时,您只需读取并检查结束条件(输入时为“-1”)还是没有更多输入(scanf 返回 EOF)。

    完整代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define STACKSIZE 1000
    
    struct stack  
    {
        int top;
        char items[STACKSIZE];
    };
    
    void push(struct stack *pb, char x)
    {
        if(pb->top==STACKSIZE)
            printf("The stack is full\n");
        else
            pb->items[pb->top++]=x;
    }
    
    
    int main()
    {
        struct stack balance;     //the stack called "balance"
        struct stack *b = &balance;          //the pointer of stack
        char line[STACKSIZE];     //input
        int input_status = scanf("%s", line);
            
        while(input_status != EOF
           && strcmp(line, "-1"))
        {
            b->top=0;                 //initializing top value
    
            for(int i=0;line[i] != '\0';i++)        
            {
                if(line[i]=='(' || line[i]==')')      //push '(' and ')' in the input to the stack
                    push(b, line[i]);
            }
            printf("test");           //can't reach this line
            printf("%s\n", b->items);
    
            input_status = scanf("%s", line);
        }
    
        return 0;
    }
    

    【讨论】:

    • 只有一件事there is simply not more input (scanf returns EOF) - 嗯..,最好明确检查扫描元素的数量,scanf 可能会返回0。做while (input_status == 1 &amp;&amp;..
    猜你喜欢
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2013-04-29
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    相关资源
    最近更新 更多