描述
输入一个表达式(用字符串表示),求这个表达式的值。
保证字符串中的有效字符包括[‘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)] = '#';
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
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;
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);
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
{
int tmp_1 = pop_num(&a);
int tmp_2 = pop_num(&a);
char tmp_3 = pop_op(&b);
int tmp_4 = calcu(tmp_1, tmp_2, tmp_3);
push_num(&a, tmp_4);
}
}
}
else
{
push_op(&b, str[i]);
i++;
}
}
}
p = a.base;
while(p != a.top)
{
printf("%d\n", *p);
p++;
}
free(a.base);
free(b.base);
return 0;
}