利用栈进行表达式求值

利用栈进行表达式求值

实验步骤

1)构建操作数栈和运算符栈
2)确定优先级
3)括弧匹配
4)利用运算符栈转换成后缀式
5)利用操作数栈解出结果
源码:

#include 
#include 
#include              
#include               
#include              
#include              
#define STACK_INIT_SIZE 100     
#define STACK_INCREMENT 10      
 
typedef float DATATYPE;
typedef char SYMBOLTYPE;
 
typedef struct stack
{
    int *base;         
    int *top;          
    int stackSize;          
}*Stack;
 
// 栈的初始化
Stack Init_Stack(Stack S)
{
    S=(Stack)malloc(sizeof(Stack));
    if(!S)
        exit(0);
    S->base = (int*)malloc(STACK_INIT_SIZE*sizeof(DATATYPE));
    if(!S->base)
        exit(0);
    S->top = S->base;
    S->stackSize = STACK_INIT_SIZE;
    return S;
}
 
// 判栈空
int IsEmpty(Stack S)
{
    if (S->top == S->base)
    {
        return 1;
    } else
    {
        return 0;
    }
}
 
// 判栈满
int IsFull(Stack S)
{
    if (S->top - S->base == S->stackSize)
    {
        return 1;
    } else
    {
        return 0;
    }
}
void Push(Stack S, DATATYPE e)
{
    assert(S);
    if (IsFull(S))
    {
        S->base = (int*)malloc((STACK_INIT_SIZE+STACK_INCREMENT)*sizeof(DATATYPE));
        if (!S->base)
            exit(0);        
        S->top = S->base + S->stackSize;
        S->stackSize += STACK_INCREMENT;
    }
    *S->top++ = e;
}
void PushSymbol(Stack S, SYMBOLTYPE e)
{
    assert(S);
    if (IsFull(S))
    {
        S->base = (int*)malloc((STACK_INIT_SIZE+STACK_INCREMENT)*sizeof(DATATYPE));
        if (!S->base)
            exit(0);        
        S->top = S->base + S->stackSize;
        S->stackSize += STACK_INCREMENT;
    }
    *S->top++ = e;
}
DATATYPE Pop(Stack S)
{
    assert(S);
    if (S->top == S->base)
        return 0;              
    else
    {
        return *--S->top;     
    }
}
SYMBOLTYPE PopSymbol(Stack S)
{
    assert(S);
    if (S->top == S->base)
        return 0;
    else
    {
        return *--S->top;
    }
}
void DestroyStack(Stack S) {
      free(S->base);
      free(S);
}
char Priority[7][7] =
{      
   {/*'+'*/'>','>','<','<','<','>','>'},
    {/*'-'*/'>','>','<','<','<','>','>'},
    {/*'*'*/'>','>','>','>','<','>','>'},
    {/*'/'*/'>','>','>','>','<','>','>'},
    {/*'('*/'<','<','<','<','<','=','0'},
    {/*')'*/'>','>','>','>','0','>','>'},
   {/*'#'*/'<','<','<','<','<','0','='}
};
 
int Operator(char c)
{
    switch(c)
    {
        case '+': return 0;
        case '-': return 1;
        case '*': return 2;
        case '/': return 3;
        case '(': return 4;
        case ')': return 5;
        case '#': return 6;
        default:  return -1;
    }
}
float Calculation(float a, char op, float b)
{
    switch(op)
    {
        case '+': return a+b;
        case '-': return a-b;
        case '*': return a*b;
        case '/': return a/b;
        default:  return -1;
    }
}
float CalculatingExpression(char *s)
{
    int i;
    strcat(s, "#");                 // 为表达式s串接"#"
    Stack OPND=NULL;
    OPND = Init_Stack(OPND);        // 操作数栈
    Stack OPTR=NULL;
    OPTR = Init_Stack(OPTR);        // 运算符栈
    PushSymbol(OPTR, '#');          //"#"压栈作为运算符栈的栈底元素
    for (i=0; itop-1)), col = Operator(s[i]);       // 确定栈顶运算符的行数,入栈运算符的列数
            switch(Priority[row][col])              // 确定优先级
            {
                case '<': PushSymbol(OPTR, s[i]); break;
                case '>': Push(OPND, Calculation(Pop(OPND), PopSymbol(OPTR), Pop(OPND))); --i; break;       
                             //Push()参数里右边的Pop先执行;--i是为了下次继续对当前入栈运算符s[i]进行判断
                case '=': PopSymbol(OPTR); break;
                default: 
                        DestroyStack(OPTR);
                          DestroyStack(OPND);
                          return -4294967296;           
            }
        }
    }
    DestroyStack(OPTR);
    return Pop(OPND);       
}
 
int main()
{
    char s[100];
    printf("请输入要计算的表达式:\n");
    gets(s);
    printf("%1.2f", CalculatingExpression(s));
    return 0;
}

from nuister kjr

你可能感兴趣的:(数据结构)