其他分享
首页 > 其他分享> > 后缀表达式

后缀表达式

作者:互联网

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define Error 0
typedef int Status;
//栈
typedef struct StackNode
{
    int data;
    struct StackNode *next;
}StackNode,*LinkStack;

Status InistStack(LinkStack &S)
{
    S=NULL;
    return OK;
}

Status Push(LinkStack &S,int e)
{
    LinkStack p;
    p=new StackNode;
    p->data=e;
    p->next=S;
    S=p;
    return OK;
}
Status Pop(LinkStack &S,int &e)
{
    LinkStack p;
    if(S==NULL)
        return Error;
    e=S->data;
    p=S;
    S=S->next;
    free(p);
    return OK;
}
Status GetTop(LinkStack S)
{
    if(S!=NULL)
        return S->data;
    return Error;
}

int main()
{

    char ass[1000];
    while(gets(ass)!=NULL)
    {
        int i,num=0;
        int flag=0;
        int left,right;
        LinkStack Q;
        InistStack(Q);
        for(i=0;;i++)
        {
            if(ass[i]=='@')
                break;
            else if(ass[i]>='0'&&ass[i]<='9')
            {
                flag=1;
                num=num*10+ass[i]-'0';
            }
            else
            {
                if(flag==1)
                {
                    Push(Q,num);
                    num=0;
                }
                if(ass[i]==' ')
                {
                    flag=0;
                    continue;
                }
                Pop(Q,right);
                Pop(Q,left);
                switch(ass[i])
                {
                    case '+':
                        Push(Q,left+right);
                        break;
                    case '-':
                        Push(Q,left-right);
                        break;
                    case '/':
                        Push(Q,left/right);
                        break;
                    case '*':
                        Push(Q,left*right);
                        break;
                }
                flag=0;
            }
        }
        Pop(Q,i);
        printf("%d",i);
    }
    return 0;
}

 

标签:Status,LinkStack,return,后缀,int,NULL,ass,表达式
来源: https://www.cnblogs.com/1328497946TS/p/11057492.html