在中缀到后缀转换的输出中用未知字符代替运算符
Unkown characters in place of operators in the output for Infix to Postfix conversion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define max 10
void eval(char []);
struct stack
{
int top;
int info[max];
};
int main()
{
char inf[20];
printf("\n Enter the string ");
scanf("%s",inf);
printf("%s",inf);
eval(inf);
return 0;
}
void eval(char inf[])
{
struct stack s; s.top=-1;
int instack(char);
int incoming(char);
void push(struct stack *, char);
char pop(struct stack *);
int i,j=0,ip,is,k;
char pst[20],ch,x;
for(i=0;i<strlen(inf);i++)
{
ch=inf[i];
if(isdigit(ch))
{
pst[j]=ch;
j++;
}
else if(ch==')')
{
while(x=pop(&s)!='(')
{
pst[j]=x; j++;
}
}
else if(s.top==-1)
{
push(&s,ch);
}
else
{
ip=incoming(ch);
is=instack(s.top);
if(ip>is)
{
push(&s,ch);
}
else
{
while((incoming(k=pop(&s))<(instack(s.top))))
{
pst[j]=pop(&s);
j++;
}
push(&s,ch);
}
}
}
while(s.top!=-1)
{
pst[j]=pop(&s);
j++;
}
pst[j]='[=10=]';
printf("\n%s",pst);
}
void push(struct stack *s,char ch)
{
s->top=s->top+1;
s->info[s->top]=ch;
}
char pop(struct stack *s)
{
char ch;
ch=s->info[s->top];
s->top=s->top-1;
return ch;
}
int incoming(char ch)
{
switch(ch)
{
case '+':
case '-': return 1; break;
case '*':
case '/': return 2; break;
case '(': return 4; break;
}
}
int instack(char ch)
{
switch(ch)
{
case '+':
case '-': return 1;break;
case '*':
case '/': return 2; break;
case '(': return 0; break;
}
}
我得到的 i/p- (5+6)*(3-2) 的输出是 56?32?(asterix)。我在 linux 机器上使用 GCC 编译器。输出包括?和 ?代替 + 和 - 运算符。像 5+6 这样的简单输入被正确转换。我得到的输出是 56+。只有带括号的输入才能使这些 ?出现。
一个非常小的错误,while(x=pop(&s)!='(')
改成while( (x=pop(&s)) !='(')
。现在,它正在提供您想要的输出。
:)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define max 10
void eval(char []);
struct stack
{
int top;
int info[max];
};
int main()
{
char inf[20];
printf("\n Enter the string ");
scanf("%s",inf);
printf("%s",inf);
eval(inf);
return 0;
}
void eval(char inf[])
{
struct stack s; s.top=-1;
int instack(char);
int incoming(char);
void push(struct stack *, char);
char pop(struct stack *);
int i,j=0,ip,is,k;
char pst[20],ch,x;
for(i=0;i<strlen(inf);i++)
{
ch=inf[i];
if(isdigit(ch))
{
pst[j]=ch;
j++;
}
else if(ch==')')
{
while(x=pop(&s)!='(')
{
pst[j]=x; j++;
}
}
else if(s.top==-1)
{
push(&s,ch);
}
else
{
ip=incoming(ch);
is=instack(s.top);
if(ip>is)
{
push(&s,ch);
}
else
{
while((incoming(k=pop(&s))<(instack(s.top))))
{
pst[j]=pop(&s);
j++;
}
push(&s,ch);
}
}
}
while(s.top!=-1)
{
pst[j]=pop(&s);
j++;
}
pst[j]='[=10=]';
printf("\n%s",pst);
}
void push(struct stack *s,char ch)
{
s->top=s->top+1;
s->info[s->top]=ch;
}
char pop(struct stack *s)
{
char ch;
ch=s->info[s->top];
s->top=s->top-1;
return ch;
}
int incoming(char ch)
{
switch(ch)
{
case '+':
case '-': return 1; break;
case '*':
case '/': return 2; break;
case '(': return 4; break;
}
}
int instack(char ch)
{
switch(ch)
{
case '+':
case '-': return 1;break;
case '*':
case '/': return 2; break;
case '(': return 0; break;
}
}
我得到的 i/p- (5+6)*(3-2) 的输出是 56?32?(asterix)。我在 linux 机器上使用 GCC 编译器。输出包括?和 ?代替 + 和 - 运算符。像 5+6 这样的简单输入被正确转换。我得到的输出是 56+。只有带括号的输入才能使这些 ?出现。
一个非常小的错误,while(x=pop(&s)!='(')
改成while( (x=pop(&s)) !='(')
。现在,它正在提供您想要的输出。
:)