【发布时间】: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;
}
【问题讨论】: