常规表达式转换为后缀表达式

来源:互联网 发布:sql server2012教程 编辑:程序博客网 时间:2024/05/23 12:35
/*常规表达式转换为后缀表达式*/#include<stdio.h>char op[20],postexp[50];int r=-1;char pop(){return op[r--];}void push(char c){r++;op[r]=c;}int main(){int i,front=0,flag=0,flag2=0;char c;r++;op[r]='=';while((c=getchar())!='\n'&&flag==0){while(c>='0'&&c<='9'){postexp[front++]=c;c=getchar();flag2=1;}if(flag2==1){flag2=0;postexp[front++]='#';}if(c=='\n')flag=1;else if(c==')')     {     while((c=pop())!='(')     {     postexp[front++]=c;    }    }    else if(c=='('||c=='-'||c=='+'||c=='*'||c=='/')        {        push(c);        }}while((c=pop())!='=')postexp[front++]=c;for(i=0;i<front;i++)printf("%c",postexp[i]);printf("\n");return 0;} 

0 0
原创粉丝点击