表达式求值(堆栈)

思路:

(1)建立两个字符型的堆栈和一个浮点数的堆栈

(2)先输入表达式,将表达式转换为后缀表达式

(3)求出后缀表达式的结果

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
//存放字符的堆栈 
struct Node{
    char data;
    struct Node* next;
};
typedef struct Node* Stack;
char e;
void Init(Stack &s)
{
    s=NULL;
}
bool Empty(Stack &s)
{
    return s==NULL;
}
void Push(Stack &s,char x)
{
    Stack tmp=new Node;
    tmp->data=x;
    tmp->next=s;
    s=tmp; 
}
void Pop(Stack &s)
{
    Stack tmp=s;
    e=tmp->data;
    s=tmp->next;
    delete tmp;
}
char Top(Stack &s)
{
    return s->data;
}

//存放浮点数的堆栈 
struct N{
    double data;
    struct N* next;
};
typedef struct N* STK;
double e1;
void Init1(STK &s)
{
    s=NULL;
}
void Push1(STK &s,double x)
{
    STK tmp=new N;
    tmp->data=x;
    tmp->next=s;
    s=tmp;
}
void Pop1(STK &s)
{
    if(s==NULL) return ;
    STK tmp=s;
    e1=tmp->data;
    s=tmp->next;
    delete tmp; 
}
double Top1(STK &s)
{
    return s->data;
}
bool Empty1(Stack &s)
{
    return s==NULL;
}

void f()
{
    Stack s1,s2;
    //从78-133是将中缀表达式转换为后缀表达式 
    Init(s1);Init(s2); //s1记录操作符,s2记录数字 
    char str[120],a[120];
    printf("请输入一行不带空格的,完整的,整数表达式(并且每个整数小于10):\n");
    scanf("%s",str);
    int i,j,len=strlen(str);
    for(i=0;i<len;i++)
    {
        if(str[i]>='0'&&str[i]<='9') Push(s2,str[i]);
        else if(str[i]=='(') Push(s1,str[i]);
        else if(str[i]==')')
        {
            while(!Empty(s1)&&Top(s1)!='(')
            {
                Push(s2,Top(s1));
                Pop(s1);
            }
            Pop(s1);
        }
        else if(str[i]=='-'||str[i]=='+')
        {
            if(Empty(s1)||Top(s1)=='(') Push(s1,str[i]);
            else
            {
                while(!Empty(s1)&&Top(s1)!='(')
                {
                    Push(s2,Top(s1));
                    Pop(s1);
                }
                Push(s1,str[i]);
            }
        }
        else if(str[i]=='*'||str[i]=='/')
        {
            if(Empty(s1)||Top(s1)=='('||Top(s1)=='+'||Top(s1)=='-') Push(s1,str[i]);
            else
            {
                while(!Empty(s1)&&Top(s1)!='('&&Top(s1)!='+'&&Top(s1)!='-')
                {
                    Push(s2,Top(s1));
                    Pop(s1);
                }
                Push(s1,str[i]);
            }
        }
    }
    while(!Empty(s1))
    {
        Push(s2,Top(s1));
        Pop(s1);
    }
    j=0;
    while(!Empty(s2)) //用a数组存储后缀表达式 
    {
        //cout<<Top(s2);
        a[j++]=Top(s2);
        Pop(s2);
    }
    //for(i=j-1;i>=0;i--) cout<<a[i];
    //cout<<endl; 
    
    //从138-154是将求出后缀表达式的结果 
    STK s3;
    Init1(s3);
    for(i=j-1;i>=0;i--)
    {
        if(a[i]>='0'&&a[i]<='9') Push1(s3,(double)(a[i]-'0'));
        else
        {
            double t1=Top1(s3); Pop1(s3);
            double t2=Top1(s3); Pop1(s3);
            //cout<<t1<<" "<<t2<<endl;
            if(a[i]=='+') Push1(s3,t1+t2);
            else if(a[i]=='-') Push1(s3,t2-t1);
            else if(a[i]=='*') Push1(s3,t1*t2);
            else if(a[i]=='/') Push1(s3,t2/t1);
        }
    }
    printf("%lf",Top1(s3));
}
int main(void)
{
    f();
    return 0;
}

//测试数据:1+((2+3)*4)-5 

猜你喜欢

转载自www.cnblogs.com/2018zxy/p/9900585.html