

/**//*
中序表达转换成后序表达式的方法:
从左到右读取该中弱序表达式:
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]=...{'
本文介绍了一种将中序表达式转换为后序表达式的算法实现,详细阐述了扫描中序表达式并利用栈进行转换的具体步骤,包括如何处理括号、不同运算符的优先级等。
5639

被折叠的 条评论
为什么被折叠?



