1. 程式人生 > >表示式求值實現c語言

表示式求值實現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;
}