学过数据结构的同学对堆栈一定不陌生吧,本人在此为学过数据结构或者正在学习数据结构的同学们提供一个堆栈的应用实例:表达式求值。在严尉敏编写的数据结构中提到过,也给出了伪码,本人在理解的基础上给出了具体的代码。在此先申明:由于此代码主要从堆栈的应用角度出发,所以只考虑输入正确的情况,各种异常情况可能比正常情况更复杂,例如:除数为0,数据运算溢出,括号不匹配等等,我觉得比较繁琐,也不是堆栈的主要内容,在此就不细究了。运算附主要包括:+,-,*,/,(,)。

#define NULL 0
#define ERROR -1
#include "iostream.h"
struct char_stack{
    char *base;
    char *top;
    int stacksize;
};
struct int_stack{
    int *base;
    int *top;
    int stacksize;
};
struct char_stack init_char_stack(struct char_stack s)  //¹¹Ôì²Ù×÷·ûÕ»
{
    s.base=new char;
    s.top=s.base;    
    s.stacksize=0;
    return(s);
}
struct int_stack init_int_stack(struct int_stack s)  //¹¹Ôì²Ù×÷ÊýÕ»
{
    s.base=new int;
    s.top=s.base;
    s.stacksize=0;
    return(s);
}
struct char_stack push_char_stack(char e,struct char_stack s)
{
    *s.top=e;
    s.top=s.top+1;
    s.stacksize=s.stacksize+1;
    return(s);
}
struct int_stack push_int_stack(int e,struct int_stack s)
{
    *s.top=e;
    s.top=s.top+1;
    s.stacksize=s.stacksize+1;
    return(s);
}
struct char_stack pop_char_stack(struct char_stack s)
{
    char e;
    e=*--s.top;
    s.stacksize=s.stacksize-1;
    return(s);
}
struct int_stack pop_int_stack(struct int_stack s)
{
    int e;
    e=*--s.top;
    s.stacksize=s.stacksize-1;
    return(s);
}
IsOPND(char OPND)  //Èç¹û²ÎÊýÊÇÊý×Ö£¬Ôò·µ»Ø1£¬·ñÔò£¬·µ»Ø0
{
    if(OPND>='0' && OPND<='9')return(1);
    return(0);
}
IsOPTR(char OPTR)  //&Egrave;&ccedil;&sup1;&ucirc;&sup2;&Icirc;&Ecirc;&yacute;&Ecirc;&Ccedil;&ordm;&Iuml;·¨&Ocirc;&Euml;&Euml;&atilde;·&ucirc;&pound;&not;&Ocirc;ò·&micro;&raquo;&Oslash;1&pound;&not;·&ntilde;&Ocirc;ò&pound;&not;·&micro;&raquo;&Oslash;0
{
    if(OPTR=='(' || OPTR=='+' || OPTR=='-' || OPTR=='*' || OPTR=='/' || OPTR==')' || OPTR=='^')return(1);
    return(0);
}
int getpower(int OPND1,int OPND2)  //·&micro;&raquo;&Oslash;OPND1&micro;&Auml;OPND2&acute;&Icirc;·&frac12;
{
    int result=1;
    if(OPND1==0 && OPND2==0)
    {
        cout<<"&Ecirc;&yacute;&frac34;&Yacute;·&Ccedil;·¨!";
        return(-1);    
    }
    if(OPND2==1)return(OPND1);
    result=OPND1*getpower(OPND1,OPND2-1);
    return(result);
}
int compute(int OPND1,int OPND2,char OPTR1)    //&frac14;&AElig;&Euml;&atilde;&Aacute;&frac12;&cedil;&ouml;&Otilde;&ucirc;&Ecirc;&yacute;&Oacute;&euml;&Ograve;&raquo;&cedil;&ouml;&Ocirc;&Euml;&Euml;&atilde;·&ucirc;&micro;&Auml;&frac14;&AElig;&Euml;&atilde;&frac12;á&sup1;&ucirc;
{
    switch(OPTR1)
    {
        case'+':return(OPND1+OPND2);
        case'-':return(OPND1-OPND2);
        case'*':return(OPND1*OPND2);
        case'/':return(OPND1/OPND2);
        case'^':return(getpower(OPND1,OPND2));
        return(NULL);
    }
}
int priority(char OPTR1,char OPTR2)   //±&Egrave;&frac12;&Iuml;&Ocirc;&Euml;&Euml;&atilde;·&ucirc;&micro;&Auml;&Oacute;&Aring;&Iuml;&Egrave;&frac14;&para;
{
    if((OPTR1=='+' || OPTR1=='-') && OPTR2=='+')return(1);
    if((OPTR1=='+' || OPTR1=='-') && OPTR2=='-')return(1);
    if((OPTR1=='+' || OPTR1=='-') && OPTR2=='*')return(-1);
    if((OPTR1=='+' || OPTR1=='-') && OPTR2=='/')return(-1);
    if((OPTR1=='+' || OPTR1=='-') && OPTR2=='(')return(-1);
    if((OPTR1=='+' || OPTR1=='-') && OPTR2==')')return(1);
    if((OPTR1=='+' || OPTR1=='-') && OPTR2=='\n')return(1);
    if((OPTR1=='*' || OPTR1=='/') && OPTR2=='+')return(1);
    if((OPTR1=='*' || OPTR1=='/') && OPTR2=='-')return(1);
    if((OPTR1=='*' || OPTR1=='/') && OPTR2=='*')return(1);
    if((OPTR1=='*' || OPTR1=='/') && OPTR2=='/')return(1);
    if((OPTR1=='*' || OPTR1=='/') && OPTR2=='(')return(-1);
    if((OPTR1=='*' || OPTR1=='/') && OPTR2==')')return(1);
    if((OPTR1=='*' || OPTR1=='/') && OPTR2=='\n')return(1);
}
int from_char_to_int(char c) //×&Ouml;·&ucirc;&acute;&reg;&Ouml;&ETH;&micro;&Auml;&Ecirc;&yacute;×&Ouml;×&Ouml;·&ucirc;×&ordf;&raquo;&macr;&Icirc;&ordf;&Otilde;&ucirc;&ETH;&Iacute;&Ecirc;&yacute;
{
    return(c-48);
}(未完)