利用栈 做一个简单的计算器系统

#include <stdio.h>
#include <stdlib.h>

#define OK    1000001
#define ERROR 1000002

struct node 
{
    int data;
    struct node *next;
};
typedef struct node Node;

struct stack
{
    Node *top;
    int count;
};
typedef struct stack Stack;

int InitStack(Stack *S)
{
    S->top = NULL;
    S->count = 0;

    return OK;
}

int EmptyStack(Stack *S)
{
    return (S->count == 0) ? OK : ERROR;
}

int PUSH(Stack *S,int e)
{
    Node *p = (Node *)malloc(sizeof(Node));
    if(p == NULL)
    {
        return ERROR;
    }
    p->data = e;
    p->next = S->top;
    S->top = p;
    S->count++;

    return OK;
}

int GetTop(Stack *S)
{
    if(NULL == S->top)
    {
        return ERROR;
    }

    return (S->top->data);
}

int Priority(char s)
{
    switch(s)
    {
        case '(':
            return 3;
        case '*':
        case '/':
            return 2;
        case '+':
        case '-':
            return 1;
        default:
            return 0;
    }
}

int Pop(Stack *S)
{
    int e;

    if(NULL == S->top)
    {
        return ERROR;
    }

    Node *p = S->top;
    e = p->data;
    S->top = p->next;
    free(p);
    S->count--;

    return e;
}

int main()
{
    Stack num,opt;
    char str[100] = {0};
    int i = 0, tmp = 0, j;

    if(InitStack(&num) != OK || InitStack(&opt) != OK)
    {
        printf("Init Failure!\n");
        exit(1);
    }

    printf("Please Input Operator :\n");
    scanf("%s",str);

    while(str[i] != '\0' || EmptyStack(&opt) != OK)
    {
        if(str[i] >= '0' && str[i] <= '9')
        {
            tmp = tmp * 10 + str[i] - '0';
            i++;
            if(str[i] < '0' || str[i] > '9')
            {
                PUSH(&num,tmp);
                tmp = 0;
            }
        }
        else 
        {
            if((EmptyStack(&opt) == OK) || (GetTop(&opt) == '(' && str[i] != ')') || Priority(str[i]) > Priority(GetTop(&opt)))
            {
                PUSH(&opt,str[i]);
                i++;
                continue;
            }

            if(GetTop(&opt) == '(' && str[i] == ')')
            {
                Pop(&opt);
                i++;
                continue;
            }

            if((str[i] == '\0' && EmptyStack(&opt) != OK) || (str[i] == ')' && GetTop(&opt) != '(') || Priority(str[i]) <= Priority(GetTop(&opt)))
            {
                switch(Pop(&opt))
                {
                    case '+':
                        PUSH(&num, Pop(&num) + Pop(&num));
                        break;
                    case '-':
                        j = Pop(&num);
                        PUSH(&num,Pop(&num) - j);
                        break;
                    case '*':
                        PUSH(&num,Pop(&num) * Pop(&num));
                        break;
                    case '/':
                        j = Pop(&num);
                        PUSH(&num,Pop(&num) / j);
                        break;
                }
                continue;
            }

        }
    }
    printf("result is %d\n",Pop(&num));
    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值