表达式求值实现c语言

#include<bits/stdc++.h>
#define STACK_INIT_SIZE 100
using namespace std;
typedef struct
{
    char date[STACK_INIT_SIZE];
    int top;
}OptrStack;
typedef struct
{
    double date[STACK_INIT_SIZE];
    int top;
}OpndStack;
OptrStack *Init_OptrStack();
int Empty_OptrStack(OptrStack *s);
int Push_OptrStack(OptrStack *s, char x);
char Pop_OptrStack(OptrStack *s, char x);
char GetTop_OptrStack(OptrStack *s, char x);

OpndStack *Init_OpndStack();
int Empty_OpndStack(OpndStack *t);
int  Push_OpndStack(OpndStack *t, double y);
double Pop_OpndStack(OpndStack *t, double y);
double GetTop_OpndStack(OpndStack *t, double y);

void Error(char *s);
int Judge_optr(char ch);
int Operate(int a, int b, char top);
void Jsbds_operate(char str[]);

OptrStack *Init_OptrStack()
{
    OptrStack *s;
    s = (OptrStack *)malloc(sizeof(OptrStack));
    s->top = -1;
    return s;
}
int Empty_OptrStack(OptrStack *s)
{
    if (s->top != -1) return 1;
    else return 0;
}
int  Push_OptrStack(OptrStack *s, char x)
{
    if (s->top == (STACK_INIT_SIZE - 1)) return 0;
    else s->date[++s->top] = x;
    return 1;

}
char Pop_OptrStack(OptrStack *s, char x)
{
    if (!Empty_OptrStack(s)) return 0;
    else
    x = s->date[s->top];
    s->top--;
    return x;
}
char GetTop_OptrStack(OptrStack *s, char x)
{
    if (!Empty_OptrStack(s)) return 0;
    else x = s->date[s->top];
    return x;
}
OpndStack *Init_OpndStack()
{
    OpndStack *t;
    t = (OpndStack*)malloc(sizeof(OpndStack));
    t->top = -1;
    return t;
}
int Empty_OpndStack(OpndStack *t)
{
    if (t->top != -1) return 1;
    else return 0;
}
int  Push_OpndStack(OpndStack *t, double y)
{
    if (t->top == (STACK_INIT_SIZE - 1)) return 0;
    else t->date[++t->top] = y;
    return 1;
}
double Pop_OpndStack(OpndStack *t, double y)
{
    if (!Empty_OpndStack(t)) return 0;
    else
    y = t->date[t->top];
    t->top--;
    return y;
}
double GetTop_OpndStack(OpndStack *t, double y)
{
    if (!Empty_OpndStack(t)) return 0;
    y = t->date[t->top];
    return y;
}

void Error(char *s)
{
    cout << s << endl;
    exit(1);
}
int Judge_optr(char top)
{
    int x;
    //cout << top << "test" << endl;
    switch (top)
    {
    case '+':
    case '-':
        x = 1; break;
    case '*':
    case '/':
        x = 2; break;
    }
    return x;
}
double Operate(double b, double a, char top)
{
    double c = 0;
    switch (top)
    {
    case '+':
        c = b + a;
        break;
    case '-':
        c = b - a;
        break;
    case '*':
        c = b * a;
        break;
    case '/':
        if (a == 0)
        {
            printf("wrong\n");
            return 0;
        }
        else
            c = b / a;
        break;
    default:
        printf("wrong\n");
        break;
    }
    return c;
}
void Jsbds_operate(char str[])
{
    OptrStack *optr = Init_OptrStack();
    OpndStack *opnd = Init_OpndStack();
    int i, j;
    double f;
    double a = 0;
    double b = 0;
    double c = 0;
    char d[100];
    char top = 0;
    for (i = 0; str[i]; i++)
    {
        switch (str[i])
        {
        case '+':
        case '-':
            if (!Empty_OptrStack(optr))
                Push_OptrStack(optr, str[i]);
            else
            {
                a = Pop_OpndStack(opnd, a);
                b = Pop_OpndStack(opnd, b);
                top = Pop_OptrStack(optr, top);
                c = Operate(b, a, top);
                Push_OpndStack(opnd, c);
                Push_OptrStack(optr, str[i]);
            }
            break;
        case '*':
        case '/':
            if ((!Empty_OptrStack(optr))||(Judge_optr(str[i]) > Judge_optr(GetTop_OptrStack(optr, top))))
                Push_OptrStack(optr, str[i]);
            else
            {
                a = Pop_OpndStack(opnd, a);
                b = Pop_OpndStack(opnd, b);
                top = Pop_OptrStack(optr, top);
                c = Operate(b, a, top);
                Push_OpndStack(opnd, c);
                Push_OptrStack(optr, str[i]);
            }
        case '\0':
            break;
        default:
            j = 0;
            do
            {
                d[j++] = str[i];
                i++;
            } while (str[i] >= '0' && str[i] <= '9');
            d[j] = '\0';
            i--;
            f = atof(d);
            Push_OpndStack(opnd, f);
            break;
        }
    }
    while (Empty_OptrStack(optr))
    {
        a = Pop_OpndStack(opnd, a);
        b = Pop_OpndStack(opnd, b);
        top = Pop_OptrStack(optr, top);
        c = Operate(b, a, top);
        Push_OpndStack(opnd, c);
    }
    cout << "the answer is:";
    cout << GetTop_OpndStack(opnd, c) << endl;
}

int main()
{
    char str[100];
    cout << "puts your text:" << endl;
    cin >> str;
    Jsbds_operate(str);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值