栈的应用之表达式求值

前言:运用栈对后缀表达式求值比较简单,在日常应用中有必要将中缀表达式转换为后缀表达式来处理。

代码展示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include<iostream>
using namespace std;

struct stacknode
{
    int top;
    char data[100];
};//用来存取运算符的栈
struct stacknode1
{
    int top;
    int data[100];
};//用于存取数字的栈
typedef stacknode *stack;
typedef stacknode1 *nstack;

int priority(char);     //用来确定运算符的优先级
void pop(stack);        //运算符栈的出栈
int pop1(nstack s);     //数字栈的出栈
char top(stack);        //获取运算符栈的栈顶元素
int top1(nstack s);     //获取数字栈的栈顶元素
void push(stack,char);      //将运算符压入运算符栈
void push1(nstack,char);    //将字符转换为数字后压入数字栈
void push2(nstack s,int c); //直接将数字压入数字栈
bool isnumber(char);        //判断字符是否为数字
bool isempty(stack);        //判断栈是否为空

int main()
{
    int i,j,len,count=0;
    stack s=new stacknode;
    nstack result=new stacknode1;
    char instr[100];        //定义输入的字符数组
    char outstr[100];       //定义输出的数组
    cout<<"please input the test:";
    scanf("%s",instr);
    len=strlen(instr);
    s->top=-1;
    result->top=-1;
    for(int i=0;i<len;i++)
    {
        if(isnumber(instr[i]))
            outstr[count++]=instr[i];
        else
        {
            if(instr[i]=='('||isempty(s))
            {
                push(s,instr[i]);
                continue;
            }
            if(instr[i]==')')
            {
                while(top(s)!='(')
                {
                    outstr[count++]=top(s);
                    pop(s);
                }
                pop(s);
                continue;
            }
            while(!isempty(s)&&top(s)!='('&&priority(instr[i])<=priority(top(s)))
            {
                outstr[count++]=top(s);
                pop(s);
            }
            push(s,instr[i]);
        }
    }

    while(!isempty(s))
    {
        outstr[count++]=top(s);
        pop(s);
    }
    outstr[count]='\0';
    for(int i=0;i<count;i++)
    {
        printf("%c ",outstr[i]);
    }
    cout<<endl;
    int a=0,b=0;
    for(int i=0;i<count;i++)
    {

        if(isnumber(outstr[i]))
        {
            push1(result,outstr[i]);
        }
        else if(outstr[i]=='+')
        {
            a=pop1(result);
            b=pop1(result);
            push2(result,b+a);
            //result->data[result->top-1]+=top1(result);
            //pop1(result);
        }
        else if(outstr[i]=='-')
        {
            a=pop1(result);
            b=pop1(result);
            push2(result,b-a);
        }
        else if(outstr[i]=='*')
        {
            a=pop1(result);
            b=pop1(result);
            push2(result,b*a);
        }
        else if(outstr[i]=='/')
        {
            a=pop1(result);
            b=pop1(result);
            push2(result,b/a);
        }

    }

    printf("the test result is:%d",top1(result));
}

bool isnumber(char c)
{
    if(c>='0'&&c<='9')
        return true;
    else

        return false;
}
bool isempty(stack s)
{
    if(s->top==-1)
        return true;
    else
        return false;
}

int priority(char c)
{
    if(c=='(')
       return 0;
    if(c=='+'||c=='-')
        return 1;
    if(c=='*'||c=='/')
        return 2;
    return 0;
}

void push(stack s,char c)
{
    s->data[++s->top]=c;
}
void push1(nstack s,char c)
{
    s->data[++s->top]=c-'0';       //将字符转换为数字并压入数字栈
}
void push2(nstack s,int c)
{
    s->data[++s->top]=c;
}
char top(stack s)
{
    return s->data[s->top];
}
int top1(nstack s)
{
    return s->data[s->top];
}
int pop1(nstack s)
{
    return s->data[s->top--];
}
void pop(stack s)
{
    s->top--;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏代有工的玉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值