大学期间写的表达式求值的算法,有空再修改

#include <iostream>
#include <string>
#include "stdlib.h"
#include <sstream>

using namespace std;
#define MAXSIZE 1024
typedef char DataType;
DataType Exp[8][8]={' ', '+', '-', '*', '/', '(', ')', '#',
                    '+', '>', '>', '<', '<', '<', '>', '>',
                    '-', '>', '>', '<', '<', '<', '>', '>',
                    '*', '>', '>', '>', '>', '<', '>', '>',
                    '/', '>', '>', '>', '>', '<', '>', '>',
                    '(', '<', '<', '<', '<', '<', '=', ' ',
                    ')', '>', '>', '>', '>', ' ', '>', '>',
                    '#', '<', '<', '<', '<', '<', ' ', '='
};

//操作符栈
typedef struct
{
    DataType data[MAXSIZE];        //栈的数据元素
    int top;                    //top指向栈顶
}SqStack;

typedef struct
{
    double data[MAXSIZE];
    int top;
}NumStack;

//初始化操作符栈
SqStack StackInit()
{
    SqStack S;
    S.top=-1;
    return S;
}
NumStack NumStackInit()
{
    NumStack S;
    S.top=-1;
    return S;
}

/*
//检查顺序站是否为空,栈空返回1,不空返回0
int Empty(SeqStack S)
{
    if(S.top==-1) return 1;
    else return 0;
}*/

//把栈置空
SqStack StackClear(SqStack S)
{
    S.top=-1;
    return S;
}


//把元素放入栈中,使其成为栈顶元素
void Push(SqStack &S,DataType x)
{
    S.top++;
    S.data[S.top]=x;
}


//弹出栈顶元素
DataType Pop(SqStack &S)
{
    S.top--;    //栈顶减一
    return (S.data[S.top+1]);    //返回栈顶元素
}

 

//取栈顶元素
DataType GetTop(SqStack S)
{
    return(S.data[S.top]);
}


//判断优先关系
DataType Precede(DataType b1,DataType b2)
{
    int i,j;
    for(i=0;i<=7;i++)
    {
        if(b1==Exp[i][0])
        break;
    }
    for(j=0;j<8;j++)
    {
        if(b2==Exp[0][j])
        break;
    }
    return Exp[i][j];
}

//判断是否为运算符,是返回ture
bool In(DataType ch)
{
    int i;
    for(i=0;i<=7;i++)
    {
        if(ch==Exp[i][0])
            return true;
    }
    if(i==8) return false;
    else return true;
}

//运算
double Operate(DataType ch,double a,double b)
{
    switch(ch)
    {
    case '+':return a+b;break;
    case '-':return b-a;break;
    case '*':return a*b;break;
    case '/':return b/a;break;
    default:break;
    }
}

//获得数字
double OpndNum(SqStack &S)
{   
    string s="";
    double a=1;
    while(GetTop(S)!='#'||a>0){
        if(GetTop(S)!='#'){
            s=Pop(S)+s;
            a--;
        }
        if(GetTop(S)=='#'&&a==1)
            Pop(S);
    }
    istringstream iss(s);
    iss>>a;
    return a;
}


//运算操作
double EvaluateExpression()
{
    cout<<"*******************************************\n";
    cout<<"\t\t四则运算求值\n";
    cout<<"*******************************************";
    SqStack OPTD;                //OPTD运算符栈
    SqStack OPND;                 //OPND操作数栈
    NumStack Num;
    OPTD=StackInit();
    OPND=StackInit();
    Num=NumStackInit();

    Push(OPTD,'#');
    Push(OPND,'#');    //运算符栈底为#,退出时遇到#结束运算
   

    DataType t;
    int sLengh=0,i=0;
    double a,b;

    cout<<"\n输入表达式:";
   
    string exp;
    cin>>exp;
    exp+='#';
   
    //获取字符串长度
    while(exp[sLengh]!='#')
    {
        sLengh++;
    }
   
    while(!(exp[i]=='#'&&GetTop(OPTD)=='#'))
    {
        if(!In(exp[i])||(GetTop(OPTD)=='('&&exp[i]=='-')||(GetTop(OPTD)=='#'&&exp[i]=='-'&&i==0))//当当前字符不在运算符集合里面时
        {
            Push(OPND,exp[i]);
            i++;
        }
       
        else//为运算符时
        {
            switch(Precede(GetTop(OPTD),exp[i]))
            {
            case '<':
                Push(OPTD,exp[i]);
                i++;
                break;
            case '=':
                Pop(OPTD);
                i++;
                break;
            case '>':
                t=Pop(OPTD);
                if(Num.top==-1)
                {
                a=OpndNum(OPND);
                }
                else a=Num.data[Num.top--];
                b=OpndNum(OPND);
                a=Operate(t,a,b);
                Num.data[++Num.top]=a;
                break;
            default:break;
            }
            Push(OPND,'#');
        }
    }
        StackClear(OPND);
        StackClear(OPTD);

    return a;
   
}

int main()
{
    double re;
    re=EvaluateExpression();
    cout<<"\n运算结果为:";
    cout<<re;
    cout<<"\n\n";
    return 0;
}

posted @ 2015-09-08 12:57  qiangwushuang  阅读(161)  评论(0编辑  收藏  举报