我正在嘗試製作一個C程序來評估後綴表達式,並且在輸入45+時在屏幕上打印不需要的符號。 P.S.請告訴我的錯誤(除非那得到()我現在如何用fgets正在學習())評估後綴表達式時不需要的符號結果
// to Evaluate a postfix expression
#include<stdio.h>
#include<conio.h>
int is_operator(char);
void answer();
char stack[100];
int top =-1;
void push(char);
char pop();
void main()
{
char postfix[100],item;
int i=0;
clrscr();
printf("Enter Postfix Expression");
gets(postfix);
while(postfix[i]!='\0')
{
item=postfix[i];
if(is_operator(item)==2)
{
push(item);
}
if(is_operator(item)==1)
{
char op;
int n1,n2,n3;
op=item;
n1=pop();
n2=pop();
switch(op)
{
case '+':
n3=n1+n2;
case '-':
n3=n1-n2;
case '*':
n3=n1*n2;
case '/':
n3=n1/n2;
}
push(n3);
}
i++;
}//end while
answer();
getch();
}
void push(char c)
{
top++;
stack[top]=c;
}
char pop()
{
char c;
c=stack[top];
top--;
return(c);
}
int is_operator(char i)
{
char ch=i;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
return(1);
}
else
{
return(2);
}
}
void answer()
{
char ans;
ans=stack[top];
printf("Answere is %c",ans);
}
是否也能在此附上電流輸出和期望的輸出? (通過[編輯]你的問題) –
如果你以前從未使用調試器,現在是開始的最佳時機。使用調試器,您可以逐行瀏覽代碼,同時監控變量及其值,以及它們如何變化。通過這樣做,您應該能夠輕鬆地找出發生的地點,並希望爲什麼。 –
當前輸出是像smilley面的不需要的符號而通緝輸出是9 @JonnyHenly – user6547375