无法在 C 中使用数组构造堆栈
Failing to construct a stack using array in C
现在我有一个输入,其中包含带有“(”和“)”的随机字符串。我想将这些括号推入一个名为“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;
}
出于某种原因,您使用 b 作为指向 堆栈 的指针,而不是 &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] != '[=13=]';i++)
{
顺便说一句,您不需要定义(“为每次迭代重新定义”)循环内的所有局部变量。这实际上不会发生,因为优化器会将它们移出,但无论如何。
谈到潜在问题,无限循环总是一个坏主意。准确定义什么是退出条件,这样你就安全了。您甚至可以定义要发生的异常情况并在这种情况下调用 break
,但不是一般规则。
您正在读取输入,在这种情况下,真正常见的是 read-ahead 技术。
int input_status = scanf("%s", line);
while(input_status != EOF
&& strcmp(line, "-1"))
{
// more things...
input_status = scanf("%s", line);
}
这样,每次测试退出条件时,您只需阅读并检查结束条件(输入时为“-1”)还是根本没有更多输入(scanf
returns 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] != '[=15=]';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;
}
现在我有一个输入,其中包含带有“(”和“)”的随机字符串。我想将这些括号推入一个名为“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;
}
出于某种原因,您使用 b 作为指向 堆栈 的指针,而不是 &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] != '[=13=]';i++)
{
顺便说一句,您不需要定义(“为每次迭代重新定义”)循环内的所有局部变量。这实际上不会发生,因为优化器会将它们移出,但无论如何。
谈到潜在问题,无限循环总是一个坏主意。准确定义什么是退出条件,这样你就安全了。您甚至可以定义要发生的异常情况并在这种情况下调用 break
,但不是一般规则。
您正在读取输入,在这种情况下,真正常见的是 read-ahead 技术。
int input_status = scanf("%s", line);
while(input_status != EOF
&& strcmp(line, "-1"))
{
// more things...
input_status = scanf("%s", line);
}
这样,每次测试退出条件时,您只需阅读并检查结束条件(输入时为“-1”)还是根本没有更多输入(scanf
returns 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] != '[=15=]';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;
}