cal_stack

#include <stdio.h>
#define MAX 100
struct operand
{
    int data[MAX];
    int top;
};
struct operator_ch
{
    int top;
    char data[MAX];
};

typedef struct operand OPND;
typedef struct operator_ch OPCH;
void init_OPND_stack(OPND *stack)
{
    stack->top = -1;
}
void init_OPCH_stack(OPCH *stack)
{
    stack->top = -1;
}
void is_empty_OPND(OPND *stack)
{
    if(stack->top == -1)
    {
        return -1;
    }
    return 0;
}
void is_empty_OPCH(OPCH *stack)
{
    if(stack->top == -1)
    {
        return -1;
    }
    return 0;
}
int get_OPND_top(OPND *stack)
{
    if(is_empty_OPND(stack) == -1)
    {
        return -1;
    }
    return stack->data[stack->top];
}
int get_OPCH_top(OPCH *stack)
{
    if(is_empty_OPCH(stack) == -1)
    {
        return -1;
    }
    return stack->data[stack->top];
}
int push_OPND(OPND *stack, int num)
{
    stack->data[++(stack->top)] = num;
}
int push_OPCH(OPND *stack, char ch)
{
    stack->data[++(stack->top)] = ch;
}
int pop_OPND(OPND *stack)
{
    if(is_empty_OPND(stack) == -1)
    {
        return -1;
    }
   
    int num = stack->data[stack->top];
    (stack->top)--;
    return num;
}
char pop_OPCH(OPCH *stack)
{
    if(is_empty_OPCH(stack) == -1)
    {
        return -1;
    }
   
    char ch = stack->data[stack->top];
    (stack->top)--;
    return ch;
}
int get_priority(char ch)
{
    if(ch == '+' || ch == '-')
    {
        return 1;
    }
    if(ch == '*' || ch == '/')
    {
        return 2;
    }
}
int compare_priority(char op_ch, char ch)
{
    if(get_priority(op_ch) > get_priority(ch))
    {
        return 1;
    }
    return -1;
}
int count(int num1, int num2, char ch)
{
    int result;
    switch(ch)
    {
        case '+':
        {
            result = a + b;
            break;
        }
        case '-':
        {
            result = a - b;
            break;
        }
       
        case '*':
        {
            result = a * b;
            break;
        }
       
        case '-':
        {
            result = a / b;
            break;
        }
    }
    return result;
}
int main()
{
    char ch;
    char op_ch;
    while((ch = getchar()) != '\n')
    {
        if(ch >= '0' && ch <= '9')
        {
            push_stack1();
        }
        if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
        {
            op_ch = pop_stack2();
            if(compare(op_ch,ch) > 0)
            {
                push_stack2(ch);
            }
            else
            {
                count(a,b,op_ch);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值