HJ50 四则运算(想了两天)

描述
输入一个表达式(用字符串表示),求这个表达式的值。
保证字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。且表达式一定合法。



输入描述:
输入一个算术表达式

输出描述:
得到计算结果

示例1
输入:
3+2*{1+2*[-4/(8-6)+7]}

输出:
25
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <stdlib.h>

#define    INIT_STACK_SIZE    128

typedef struct
{
    char *base;
    char *top;
    int size;
} stack_op;

typedef struct
{
    int *base;
    int *top;
    int size;
} stack_num;

void push_op(stack_op *a, char b)
{
    int len = a->top-a->base;
    if(len >= a->size)
    {
        a->base = (char* )realloc(a->base, a->size+1024);
        a->top = a->base+len;
        a->size = a->size+1024;
    }
    *(a->top) = b;
    a->top++;
}


char pop_op(stack_op* a)
{
    if(a->top > a->base)
    {
        a->top--;
        return (*(a->top));
    }
    else
    {
        return 0;
    }
}

void push_num(stack_num *a, int b)
{
    int len = a->top-a->base;
    if(len >= a->size)
    {
        a->base = (int* )realloc(a->base, a->size+1024*sizeof(int));
        a->top = a->base+len;
        a->size = a->size+1024;
    }
    *(a->top) = b;
    a->top++;
}

int pop_num(stack_num *a)
{
    if(a->top > a->base)
    {
        a->top--;
        return (*(a->top));
    }
    else
    {
        return 0;
    }
}

int is_digit(char a)
{
    if(a>='0' && a<='9')
    {
        return 1;
    }
    return 0;
} 

int cmp(char a, char b)
{
    if(a=='+' || a=='-')
    {
        if(b=='+' || b=='-')
        {
            return 1;
        }
        else if(b=='*' || b=='/' || b=='(')
        {
            return -1;
        }
        else if(b==')' || b=='#')
        {
            return 1;
        }
    }
    else if(a=='*' || a=='/')
    {
        if(b=='+' || b=='-' || b=='*' || b=='/' || b==')' || b=='#')
        {
            return 1;
        }
        else if(b=='(')
        {
            return -1;
        }
    }
    else if(a == '(')
    {
        if(b == ')')
        {
            return 0;
        }
        else
        {
            return -1;
        }
    }
    return 0;
}

int calcu(int a, int b, char opt)
{
    switch (opt)
    {
        case '+':
			return (a+b);
			break;
		case '-':
			return (b-a);
			break;
		case '*':
			return (a*b);
			break;
		case '/':
			return (b/a);
			break;
    }
	return 0;
}

int main(void)
{
    stack_num a;
    stack_op b;
    char str[64] = {0};
    int i, j;
    int len;
    int *p;
    char *q;
    
    a.base = (int* )calloc(INIT_STACK_SIZE, sizeof(int));
    a.top = a.base;
    a.size = INIT_STACK_SIZE;
    b.base = (char* )calloc(INIT_STACK_SIZE, sizeof(char));
    b.top = b.base;
    b.size = INIT_STACK_SIZE;
    
    scanf("%s", str);
    // 括号统一替换为 '(', ')'
    for(i=0; i<strlen(str); i++)
    {
        if(str[i]=='[' || str[i]=='{')
        {
            str[i] = '(';
        }
        if(str[i]==']' || str[i]=='}')
        {
            str[i] = ')';
        }
    }
    // 最后面加上终止符
    str[strlen(str)] = '#';
    
    // 负号前面补 0
    for(i=0; i<strlen(str); i++)
    {
        if((i==0&&(str[i]=='+'||str[i]=='-')) || (i>0&&str[i-1]=='('&&(str[i]=='+'||str[i]=='-')))
        {
            len = strlen(str);
            for(j=strlen(str)-1; j>=i; j--)
            {
                str[j+1] = str[j];
            }
            str[i] = '0';
        }
    }
#if 0
    //printf("%s\n", str);
    for(i=0; i<strlen(str); i++)
    {
        if(is_digit(str[i]) == 1)
        {
            push_num(&a, str[i]-'0');
        }
        else
        {
            push_op(&b, str[i]);
        }
    }
    p = a.base;
    while(p < a.top)
    {
        printf("%d ", *p);
        p++;
    }
    printf("\n");
    q = b.base;
    while(q < b.top)
    {
        printf("%c ", *q);
        q++;
    }
    printf("\n");
#endif
    
    i = 0;
    //printf("%s\n", str);
    while(i < strlen(str))
    {
        if(is_digit(str[i]) == 1)
        {
            j = 0;
            char val[10] = {0};
            int con;
            
            while(is_digit(str[i]) == 1)
            {
                val[j] = str[i];
                i++;
                j++;
            }
            con = atoi(val);
            //printf("(%d):%d\n", __LINE__, con);
            push_num(&a, con);
        }
        else
        {
            if(b.top > b.base)
            {
                if(str[i]==')' && *(b.top-1)=='(')
                {
                    pop_op(&b);
                    i++;
                }
                else
                {
                    if(cmp(*(b.top-1), str[i]) < 0)
                    {
                        push_op(&b, str[i]);
                        i++;
                    }
                    else
                    {
                        //printf("(%d):%c, %c\n", __LINE__, str[i], *(b.top-1));
                        int tmp_1 = pop_num(&a);
                        int tmp_2 = pop_num(&a);
                        char tmp_3 = pop_op(&b);
                        //printf("tmp_1=%d, tmp_2=%d, tmp_3=%c\n", tmp_1, tmp_2, tmp_3);
                        int tmp_4 = calcu(tmp_1, tmp_2, tmp_3);
                        //printf("tmp_4=%d\n", tmp_4);
                        push_num(&a, tmp_4);
                    }
                }
            }
            else
            {
                push_op(&b, str[i]);
                i++;
            }
        }
    }
    //printf("(%d):\n", __LINE__);
    p = a.base;
    while(p != a.top)
    {
        printf("%d\n", *p);
        p++;
    }
    
    free(a.base);
    free(b.base);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值