[英]Char array in multiple test cases
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char item)
{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
stack[top] = item;
}
}
char pop()
{
char item;
if(top <0)
{
printf("stack under flow: invalid infix expression");
}
else
{
item = stack[top];
top = top-1;
return(item);
}
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(5);
}
else if(symbol == '/')
{
return(4);
}
else if(symbol == '*')
{
return(3);
}
else if(symbol == '+' )
{
return(2);
}
else if(symbol == '-' )
{
return(1);
}
else
{
return(0);
}
}
void InfixToPostfix(char infix_exp[], char postfix_exp[])
{
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item; /* add operand symbol to postfix expr */
j++;
}
else if(is_operator(item) == 1) /* means symbol is operator */
{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x; /* so pop all higher precendence operator and */
j++;
x = pop(); /* add them to postfix expresion */
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
}
i++;
item = infix_exp[i];
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char infix[SIZE], postfix[SIZE]; /* declare infix string and postfix string */
scanf("\n%[^\n]s",infix);
InfixToPostfix(infix,postfix); /* call to convert */
//printf("Postfix Expression: ");
printf("%s\n",postfix);
/* print postfix expression */
}
return 0;
}
This is the code to convert infix to postfix expression I am using multiple test cases the problem is the second time(second test case the postfix_exp is printing more than it should) Input 2 a+b*(c^de)^(f+g h)-ia (b+c)/d Output abcd^e-fgh*+^*+i- abc+d/ fgh +^*+i-这是将中缀转换为后缀表达式的代码我正在使用多个测试用例问题是第二次(第二个测试用例 postfix_exp 打印的比它应该多)输入 2 a+b*(c^de)^(f+ g h)-ia (b+c)/d 输出 abcd^e-fgh*+^*+i- abc+d/ fgh +^*+i-
Can you tell me what is wrong because of which " fgh +^*+i-" the extra expression from the previous expression is being concatenated at the end of the output?你能告诉我什么是错误的,因为哪个“ fgh +^*+i-”在输出的末尾连接了前一个表达式中的额外表达式?
The postfix_exp seems to be overwritten but I was expecting a new assigned postfix expression. postfix_exp 似乎被覆盖,但我期待一个新的分配后缀表达式。 Why is it that earlier data(data from previous test case) is saved?
为什么要保存较早的数据(来自先前测试用例的数据)?
Please ask any further query in the comments section请在评论部分提出任何进一步的问题
You did not closed postfix_exp
with the terminating '\\0'
.您没有使用终止
'\\0'
关闭postfix_exp
。 After the first iteration postfix
will look like something like this在第一次迭代后,
postfix
看起来像这样
XXXXXXXXXXX0
After the shorter 2nd input it will look something like在较短的第二个输入之后,它看起来像
YYYYYYYXXXX0
Where X
represent the characters written in the first iteration and Y
the ones which in the second.其中
X
代表在第一次迭代中写入的字符, Y
代表在第二次迭代中写入的字符。 So when you print it it will contain the last iteration results too.因此,当您打印它时,它也会包含最后一次迭代结果。
Add this postfix_exp[j] = '\\0';
添加这个
postfix_exp[j] = '\\0';
before the if (top>0)
line.在
if (top>0)
行之前。
Note that this is still undefined behavior since postfix
array is unitialized and there is no guarantee that it will be zeroed out.请注意,这仍然是未定义的行为,因为
postfix
数组是未初始化的,并且不能保证它会被清零。
you should clos postfix_exp with the terminating '\\0' by adding您应该通过添加终止 '\\0' 来关闭 postfix_exp
Add this postfix_exp[j] = '\0';
before the if (top>0) line.在 if (top>0) 行之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.