计算算术表达式

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define ElementType int
#define EmptyTOS (-1)
#define MinStackSize (5)

struct StackRecord{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
};

typedef struct StackRecord *Stack;

Stack CreateStack(int MaxElements);
void DisposeStack( Stack S );
int IsEmpty( Stack S );
void MakeEmpty( Stack S );
void Push(ElementType X, Stack S);
ElementType Top( Stack S );
void Pop( Stack S );


Stack CreateStack(int MaxElements)
{
    Stack S;

    S = (Stack)malloc( sizeof( struct StackRecord) );
    S->Array = (ElementType *)malloc(sizeof(ElementType)*MaxElements);
    S->Capacity = MaxElements;
    MakeEmpty(S);
    return S;

}

void DisposeStack( Stack S )
{
    if( S != NULL)
    {
        free(S->Array);
        free(S);
    }
}

int IsEmpty( Stack S )
{
    return S->TopOfStack == EmptyTOS;
}

int IsFull( Stack S )
{
    return S->TopOfStack == S->Capacity;
}

void MakeEmpty( Stack S )
{
    S->TopOfStack = EmptyTOS;
}

void Push(ElementType X, Stack S)
{
    if(IsFull(S))
    {

    }
    else
    {
        S->Array[++S->TopOfStack] = X;
    }
}

ElementType Top( Stack S )
{
    if(!IsEmpty(S))
        return S->Array[S->TopOfStack];
}

void Pop( Stack S )
{
    if(IsEmpty( S ))
    {

    }
    else
    {
        S->TopOfStack--;
    }
}

char s[105];
int priority[255];

int main()
{
    scanf("%s", s);
    priority['+'] = 1;
    priority['-'] = 1;
    priority['*'] = 2;
    priority['/'] = 2;
    priority['^'] = 2;
    priority['('] = 3;
    priority[')'] = 3;

    Stack number = CreateStack(105);
    Stack punct = CreateStack(105);

    int len = strlen(s);
    int num = 0;
    for(int i=0; i<len; i++)
    {
        if(s[i]<='9' && s[i]>='0')
        {
            num = num*10 + (s[i]-'0');
            if(i+1>=len ||  !(s[i+1]<='9' && s[i+1]>='0') )
            {
                Push(num, number);
                num = 0;
            }
        }
        else
        {
            if(s[i] == ')')
            {
                while(Top(punct) != '(')
                {
                    char p = Top(punct);

                    int a1=0,a2=0,ans=0;
                    a2 = Top(number);
                    Pop(number);
                    a1 = Top(number);
                    Pop(number);

                    switch(p){
                        case '+':
                            ans = a1 + a2;
                            break;
                        case '-':
                            ans = a1 - a2;
                            break;
                        case '*':
                            ans = a1 * a2;
                            break;
                        case '/':
                            ans = a1 / a2;
                            break;
                        case '^':
                            ans = pow(a1,a2);
                        default:
                            break;

                    }
                    Push(ans, number);
                    Pop(punct);
                    if(IsEmpty(punct)) break;
                }
                Pop(punct);
                continue;
            }
            else if(!IsEmpty(punct) &&
               priority[Top(punct)] >= priority[s[i]])
            {

                char tmp = s[i];

                while(Top(punct)!='(' && priority[Top(punct)] >= priority[tmp])
                {
                    char p = Top(punct);

                    int a1=0,a2=0,ans=0;
                    a2 = Top(number);
                    Pop(number);
                    a1 = Top(number);
                    Pop(number);

                    switch(p){
                        case '+':
                            ans = a1 + a2;
                            break;
                        case '-':
                            ans = a1 - a2;
                            break;
                        case '*':
                            ans = a1 * a2;
                            break;
                        case '/':
                            ans = a1 / a2;
                            break;
                        case '^':
                            ans = pow(a1,a2);
                        default:
                            break;

                    }
                    Push(ans, number);
                    Pop(punct);
                    if(IsEmpty(punct)) break;
                }

            }

            Push(s[i], punct);

        }
    }

    while(!IsEmpty(punct))
    {
        char p = Top(punct);

        int a1=0,a2=0,ans=0;
        a2 = Top(number);
        Pop(number);
        a1 = Top(number);
        Pop(number);

        switch(p){
            case '+':
                ans = a1 + a2;
                break;
            case '-':
                ans = a1 - a2;
                break;
            case '*':
                ans = a1 * a2;
                break;
            case '/':
                ans = a1 / a2;
                break;
            case '^':
                ans = pow(a1,a2);
            default:
                break;

            }

        Push(ans, number);
        Pop(punct);
    }

    int ans = Top(number);
    Pop(number);
    printf("%d\n", ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值