表达式求值(单位运算)--栈

本文介绍了一种使用栈的数据结构来解析并计算数学表达式的算法。通过定义运算符优先级和运算规则,该算法能正确处理包含加减乘除及括号的表达式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef int Status;
typedef struct StackNode
{
    char date;
    struct StackNode *next;
}StackNode,*LinkStack;
Status Initstack(LinkStack &S)
{
    S=NULL;  //栈顶指针置为空 
    return true;
}
Status xiaohui(LinkStack &S)
{
    delete S;
    return true;
}
Status Push(LinkStack &S,char e)//入栈 
{
    LinkStack p;
    p = new StackNode;
    p->date = e;
    p->next = S; //将新结点插入栈顶 
    S = p;  //将栈顶置为p 
    return true;
}
Status Pop(LinkStack &S,char &e)//出栈 
{
    LinkStack p;
    if(S==NULL)
    return false;
    e = S->date;
    p=S;
    S=S->next;
    delete p; 
    return true;
}
char GetTop(LinkStack S)
{
    if(S!=NULL)
    return  S->date;
}
Status In(char a) //判断是否为运算符 
{
    if(a == '+' || a == '-' || a == '*' || a == '/'||a == '#'||a == '('||a == ')')
    return true;
    else
    return false;
}
int Operate(char a, char b, char c)//运算函数 
{
    if(b == '+')
    return ((a-'0')+(c-'0')+'0');
    else if(b == '-')
    return ((a-'0')-(c-'0')+'0');
    else if(b == '*')
    return ((a-'0')*(c-'0')+'0');
    else if(b == '/')
    return ((a-'0')/(c-'0')+'0');

}
char Precede(char a, char b) //返回运算符优先级 
{
    if(a == '+')
    {
        if(b == '+')
        return '>';
        else if(b == '-')
        return '>';
        else if(b == '*')
        return '<';
        else if(b == '/')
        return '<';
        else if(b == '(')
        return '<';
        else if(b == ')')
        return '>';
        else if(b == '#')
        return '>'; 
    }
    else if(a == '-')
    {
        if(b == '+')
        return '>';
        else if(b == '-')
        return '>';
        else if(b == '*')
        return '<';
        else if(b == '/')
        return '<';
        else if(b == '(')
        return '<';
        else if(b == ')')
        return '>';
        else if(b == '#')
        return '>'; 
    }
    else if(a == '*')
    {
        if(b == '+')
        return '>';
        else if(b == '-')
        return '>';
        else if(b == '*')
        return '>';
        else if(b == '/')
        return '>';
        else if(b == '(')
        return '<';
        else if(b == ')')
        return '>';
        else if(b == '#')
        return '>'; 
    }
    else if(a == '/')
    {
        if(b == '+')
        return '>';
        else if(b == '-')
        return '>';
        else if(b == '*')
        return '>';
        else if(b == '/')
        return '>';
        else if(b == '(')
        return '<';
        else if(b == ')')
        return '>';
        else if(b == '#')
        return '>'; 
    }
    else if(a == '(')
    {
        if(b == '+')
        return '<';
        else if(b == '-')
        return '<';
        else if(b == '*')
        return '<';
        else if(b == '/')
        return '<';
        else if(b == '(')
        return '<';
        else if(b == ')')
        return '=';
        else if(b == '#')
        return ' '; 
    }
    else if(a == ')')
    {
        if(b == '+')
        return '>';
        else if(b == '-')
        return '>';
        else if(b == '*')
        return '>';
        else if(b == '/')
        return '>';
        else if(b == '(')
        return ' ';
        else if(b == ')')
        return '>';
        else if(b == '#')
        return '>'; 
    }
    else if(a == '#')
    {
        if(b == '+')
        return '<';
        else if(b == '-')
        return '<';
        else if(b == '*')
        return '<';
        else if(b == '/')
        return '<';
        else if(b == '(')
        return '<';
        else if(b == ')')
        return ' ';
        else if(b == '#')
        return '='; 
    }

}
char EvaluateExpression()
{
    LinkStack OPND,OPTR;
    Initstack(OPND);
    Initstack(OPTR);
    Push(OPTR,'#');
    char ch;
    cin>>ch;
    while(ch!='#'||GetTop(OPTR)!='#')//表达式没有扫描完毕,或OPTR的栈顶元素不为'#' 
    {
        if(!In(ch))
        {
            Push(OPND,ch);
            cin>>ch;
        }
        else
        {
            switch(Precede(GetTop(OPTR),ch))
            {
                case '<':
                    {
                        Push(OPTR,ch);
                        cin>>ch;
                        break;
                    }
                case '>':
                    {
                        char a,b;
                        char theta;
                        Pop(OPTR,theta);
                        Pop(OPND,b);
                        Pop(OPND,a);
                        Push(OPND,Operate(a,theta,b));
                        break;
                    }   
                case '=':
                    {
                        char x;
                        Pop(OPTR,x);
                        cin>>ch;
                        break;
                    }   
            }
        }
    }
    return GetTop(OPND) ;
}
int main()
{
    cout<<"请输入表达式,以#结尾"<<endl;
    cout<<"括号请用英文输入法下的括号"<<endl; 
    char sum=EvaluateExpression();
    cout<<sum-'0'<<endl;
    return 0;
 } 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值