中序表达式转换成后序表达式问题(栈的应用)


/*
中序表达转换成后序表达式的方法:

从左到右读取该中弱序表达式:

1.若是操作数,则直接输出.

2.若是运算符:
(1)若该运算符为"(",则直接入栈.
(2)若该运算符为")",则取出堆栈中的运算符,直到"("时.
(3)其它: 按优先级比较,如果大于或等于堆栈中当前的运算则压入栈中,否则直接输出.
(4)检查栈是否非空,如果非空,则输出所有值,直到空为止.

*/


#include 
< stdio.h >
#include 
< stdlib.h >
#include 
< string .h >
#include 
< ctype.h >

#define  MAX 101 /*表达式的最大长度*/

typedef 
struct  optor
{
        
char ch ;
        
struct optor *next ;
}
OPT ;

/*typedef struct data
{
        int num ;
        struct data *next ;
}DAT ;
*/

   OPT 
* opttop  =  NULL ;  /*初始堆栈*/
 
/*  DAT *dattop = NULL ;*/

void  push_opt(  int  cx)
{
     OPT 
*q ;
     q 
= (OPT *)malloc(sizeof(OPT)) ;
     q
->ch = cx ;
     q
->next= opttop;
     opttop 
= q ;
}




char  top_opt( void )
{
     
if(opttop)
     
return opttop->ch ;
     
return '#' ;
}




void  pop_opt()
{
     OPT 
*q ;
     q 
= opttop ;
     opttop 
= opttop->next ;
     free(q);

}



int  rank( char  cr)
{
    
if( cr == '-' || cr == '+')
    
return 1 ;
    
else if( cr == '*' || cr == '/')
    
return 2 ;
    
else if( cr == '^')
    
return 3 ;
}



     
int  main( void )
{
    
char st[MAX]={'
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值