HHH++--

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX  100
enum link { PUSH, PUSH_NO };

typedef struct      // 运算数
{
    int num[MAX];
    int top;
}OP_num;

typedef struct      // 运算符
{
    char str[MAX];
    int top;
}OP_ch;

// 运算数置空栈
void SETNULL_num(OP_num* s)
{
    s->top = -1;
}

// 运算符置空栈
void SETNULL_ch(OP_ch* s)
{
    s->top = -1;
}

// 判断是否是数字,是返回1 不是返回0
int is_num(char ch)
{
    if (ch >= '0' && ch <= '9')
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

// 数字入栈
int PUSH_num(OP_num* s, int data)
{
    if ((MAX - 1) == s->top)
    {
        return 0;
    }
    else
    {
        s->num[++s->top] = data;
    }
}

// 运算符入栈
int PUSH_ch(OP_ch* s, char ch)
{
    if ((MAX - 1) == s->top)
    {
        return 0;
    }
    else
    {
        s->str[++s->top] = ch;
    }
}
// 判断是否将运算符入栈
int jud(OP_ch* s, char ch)
{
    if (-1 == s->top)   // 判断是否是空栈
    {
        return PUSH;
    }
    else
    {
        switch (s->str[s->top])     // 根据栈顶运算判断是否进栈
        {
        case '+':           //  当栈顶是'+-'时,只有‘+-)’不进栈
        case '-':
        {
            if (ch == '+' || ch == '-' )
            {
                return PUSH_NO;
            }
            else
            {
                return PUSH;
            }
            break;
        }
        }
    }
}

// 数字出栈
int Pop_num(OP_num* s)
{
    return (s->num[s->top--]);
}

// 运算符出栈
void Pop_ch(OP_ch* s)
{
    s->top--;
}

// 进行运算
void operate(OP_ch* s_ch, OP_num* s_sum)
{
    int a = Pop_num(s_sum);                     // 取第一个数
    int b = Pop_num(s_sum);                     // 取第二个数
    int result;

    // 根据当前运算符栈顶的符号来判断进行何种运算
    switch (s_ch->str[s_ch->top])
    {
    case '+':
        result = a + b;
        break;
    case '-':
        result = b - a;
        break;
    }
    PUSH_num(s_sum, result);                   // 将运算结果入栈
}
int main()
{
    OP_num sdata;  //运算数
    OP_ch  soper;  //运算符

    SETNULL_num(&sdata);
    SETNULL_ch(&soper);

    int i = 0, len_str, t;

    char str[MAX];
    char str_num[MAX];          // 存放要转化的数字
    gets(str);                 // 输入表达式
    len_str = strlen(str);     // 获取表达式长度

    while (str[i] != '\0')      // 遍历表达式
    {
        if (is_num(str[i]))     // 判断是否是数字
        {
            t = 0;
            while (is_num(str[i]))
            {
                str_num[t++] = str[i++];
                //将表达式中的数字进行保存,用于转化为对应的整形数
            }
            str_num[t] = '\0';
            PUSH_num(&sdata, atoi(str_num));
            // 遇到算数符号的时候让符号前面的数进栈
        }
        else
        {
    PUSH_ch(&soper, str[i]);
         i++;
        }
    }
    while (soper.top != -1)
    {
        operate(&soper, &sdata);
        Pop_ch(&soper);
    }
    printf("%d\n", sdata.num[sdata.top]);

    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值