leetcode-224
前言
逆波兰表达式,经典的栈应用。
一、题目leetcode-224
题目描述非常简洁,所以难免会有些小坑。
二、代码
#define MAX_STACK_LEN (1<<20)
typedef int Status;
typedef enum
{
V__STATUS_FALSE = 0,
V__STATUS_TRUE = 1
}ENU_STATUS_DEF;
typedef union
{
int num;
char mark;
}UNI_DATA_DEF;
typedef enum
{
V__TYPE_NUM = 0,
V__TYPE_MAK
}ENU_TYPE_DEF;
typedef struct
{
int type;
UNI_DATA_DEF data;
}ST_INFO_DEF;
typedef struct
{
ST_INFO_DEF bufStack[MAX_STACK_LEN];
int sizeStack;
int top;
}ST_STACK_DEF;
static ST_STACK_DEF s_StackRes = { 0 };
static ST_STACK_DEF s_StackTmp = { 0 };
static void clearStack(ST_STACK_DEF* stack)
{
stack->sizeStack = 0;
stack->top = 0;
return;
}
static Status isEmptyStack(ST_STACK_DEF* stack)
{
if (0 == stack->sizeStack)
return V__STATUS_TRUE;
return V__STATUS_FALSE;
}
static Status isFullStack(ST_STACK_DEF* stack)
{
if(MAX_STACK_LEN - 1 <= stack->sizeStack)
return V__STATUS_TRUE;
return V__STATUS_FALSE;
}
static Status popStack(ST_STACK_DEF* stack)
{
if (isEmptyStack(stack))
return V__STATUS_FALSE;
stack->sizeStack--;
stack->top--;
return V__STATUS_TRUE;
}
static Status pushStack(ST_STACK_DEF* stack, ST_INFO_DEF *e)
{
if (isFullStack(stack))
return V__STATUS_FALSE;
stack->bufStack[stack->top] = *e;
stack->sizeStack++;
stack->top++;
return V__STATUS_TRUE;
}
static Status topStack(ST_STACK_DEF* stack, ST_INFO_DEF* e)
{
if (isEmptyStack(stack))
return V__STATUS_FALSE;
*e = stack->bufStack[stack->top - 1];
return V__STATUS_TRUE;
}
static char* procNumb(char* str)
{
int sum = 0;
ST_INFO_DEF t;
while ('9' >= *str && '0' <= *str)
{
sum = sum * 10 + (*str - '0');
str++;
}
t.type = V__TYPE_NUM;
t.data.num = sum;
pushStack(&s_StackRes, &t);
return str;
}
static char* procChar(char* str)
{
ST_INFO_DEF t;
switch (*str)
{
case '(':
t.type = V__TYPE_MAK;
t.data.mark = *str;
pushStack(&s_StackTmp, &t);
break;
case ')':
topStack(&s_StackTmp, &t);
if ('(' != t.data.mark)
{
pushStack(&s_StackRes, &t);
popStack(&s_StackTmp);
}
popStack(&s_StackTmp);
break;
case '+':
case '-':
if (V__STATUS_TRUE == (topStack(&s_StackTmp, &t)) && ('(' != t.data.mark))
{
topStack(&s_StackTmp, &t);
pushStack(&s_StackRes, &t);
popStack(&s_StackTmp);
}
t.type = V__TYPE_MAK;
t.data.mark = *str;
pushStack(&s_StackTmp, &t);
if (V__STATUS_TRUE == isEmptyStack(&s_StackRes))
{
t.type = V__TYPE_NUM;
t.data.num = 0;
pushStack(&s_StackRes, &t);
}
break;
default:
break;
}
return str + 1;
}
static void procInput(char *str)
{
ST_INFO_DEF t;
while ('\0' != *str)
{
if (' ' == *str)
{
str++;
continue;
}
else if ('9' >= *str && '0' <= *str)
{
str = procNumb(str);
}
else
{
str = procChar(str);
}
}
if (V__STATUS_FALSE == isEmptyStack(&s_StackTmp))
{
topStack(&s_StackTmp, &t);
pushStack(&s_StackRes, &t);
popStack(&s_StackTmp);
}
}
static int calcResult(void)
{
char mark;
int a, b;
ST_INFO_DEF t;
clearStack(&s_StackTmp);
while (V__STATUS_FALSE == isEmptyStack(&s_StackRes))
{
topStack(&s_StackRes, &t);
pushStack(&s_StackTmp, &t);
popStack(&s_StackRes);
}
while (V__STATUS_FALSE == isEmptyStack(&s_StackTmp))
{
topStack(&s_StackTmp, &t);
switch (t.type)
{
case V__TYPE_MAK:
mark = t.data.mark;
topStack(&s_StackRes, &t);
a = t.data.num;
popStack(&s_StackRes);
topStack(&s_StackRes, &t);
b = t.data.num;
popStack(&s_StackRes);
t.type = V__TYPE_NUM;
t.data.num = ('+' == mark) ? b + a : b - a;
pushStack(&s_StackRes, &t);
break;
case V__TYPE_NUM:
pushStack(&s_StackRes, &t);
break;
default:
break;
}
popStack(&s_StackTmp);
}
topStack(&s_StackRes, &t);
return t.data.num;
}
int calculate(char* s) {
clearStack(&s_StackRes);
clearStack(&s_StackTmp);
procInput(s);
return calcResult();
}
int main()
{
char* Str = "(9-(10-(10-0-(3+(8+(0+(8-(10-8-(7-(2+(5+(6+(10+(3+(8+(3-(9+(1+(10+(1-(1+(6-2+0+(10-(9-(3-(3-9-(1-(7+(4-(2+(2-(10+(3+(7-(1-(4+(1+(1-(10-(5-(9+(9-4-(5-(1+8-(2-(1+(1-10-(4-(1+(4-(7)-(3-(8)+(5+5-(5-(9-(8+(8-4-1+(0-(1+(1+(10-(7+(2-(5-(4-(6+(2+(1-(2-(9+8+(2+(9-(9-(7+(10+1+(5)))-(2-(8+3+(5-(7-(3+(9)+(10+(0+(8-(1-(9)-(0+10-(3+(9-(0-(5-(7-(4-4+1+(7)-(10+(5+(9-(3+(5+(6-(0-(7-(1-(4+(6+(4-2-(4+(9-(6+9-8+1+(5+(7-9+3)+(10-(10+(2+(0-(5-(2+(10-(4-5-(7-(4-(7+(4)+6+10+(2-(7+(2))+(1)+(5-(7)-(10-(5+(7-(6-(2+(1-4)+(10-(5)+(4+(10+(4+(0+(10+(8-(8+(6+5-(1-(6-(1-(2+(4+(9-(3+(1+(10+(4)+(0+(3-(2-(9-(2-(3-(4-(2+(7-(6-(5+(7+(5+(5-(4+(0-(7+(2-(7+(9)-(6-(10)+(7+(2-(9-(9)+(4+(1-(8+(2-0-(2+(2+10)-(7-9-(9+(8-(5-8-(5)+(6+(10-(3-(2-(2+(7-2+(9+(3+(9+(2-(8+(5-(4+(4-(1-(9+(0+(6-(4-(3+(5-(2-(4-(6+(0+(4+3)-(8-(6+(9+(1+(2)-(8-(1+1+(5+(4-(3-(1-(7-4+(6+(9+(1+(4)+(6+(4+(2+(7-(1+(4-(8+(6+(8-(9-(2)-3-(0-(0)+(5+(7-(8)+(8-(2+(1)+1+(3+(6-(10-(2-4-(2-(2)+(8)+(3-(1-(1)+(6+(1+(9+(9+(5)-(4+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+(10)-(7-(0-(2)-(6))-(2+(10)))+(7)+(3))+(8-(8+(10)-(8)+(0+(6-(2)-(1))+(3+(10+(10-(4+(7-(2)-(9-(2+(8))))+(7)-(7+10+(9-(2)+(0))-(6+(1)))+(10)+(2)-(7)-(4)-(10+(3-(6))))+(8-(1))-(10)))+(5+(3-(0-(1-(2+(3-(6-(4)-(1)+(4+(7+(3)-(7)+(4-(9))+(0-(4)+(9+(3-(9)+(4-(10+(6+(4)))+(4))+(10+(0-3-(8+(0-(6))-(5))-(9))-(6))))+2))+6+(6)+(1-(6))-(7-(1))-(8)+(9-(8))+(4)))-(0+7-(1)))-(2))+(0)))+(4-(7))-(5)-(8)-4+(1-(3-(8+(2+0)+(7)))))))-(4-(2))+(9))))+(7)-(2-(10+(4)-(8+(7)+(5-(4)-(6+6))-(2+(6)-(2+(4-(2-(8-(4)-(7+(5)-(10-(7)))))-(10+(9+(8)-(10)+(3-(7+(4+(2+(5)-(10+7+(2-(10)-(10+(3))+(0-(10+(8+(4+(7-(2)+(3+9))))+(7-(6+(2)-(2)+7+(5+(7+(10+(5-(4)-2+(5)+(1))+(0))))-(9))-5-(8)-(9-(4)-(10))-(8-(5)-(10)-7)+(5))-(4)))))+6+3+(3+(6+(9)))-10+(6)+(0)))))+(7)))+(1-(5)+(3-(3+6))+(5)+(7)-(9-(1))+(4+(1))+(2)))-(3))-(10)+(1)))))))))+(3)+2+(8-(4)))-(1))+(6-(8-(0)-(8-(0))-(2-(4+2)))-(9+1)))-(8-(8+(1-8-(7))))+7-(5+(5+(6+(10)+(8)))))))-(4))))-(4)-(6)+(10)-(5)))+(0+(2+(4))-(4-(2)+(0-(10-(4))))))+3-(10)))-(9+(9-(8-(7)-4))))+(6))-(4-(9))))-(1))+(10))))-(0+(9+7-(1)))))-(7)-(4)))-(9))))-7))))+(9))+(10))-(8-(9)))+(8))-(6)-(4)-(8)))))))))))))-(7)))))+(2-(6)-(0))))-(0)-(5+(9)+(9))+(3-(9))))+(8))))))))-(0-(0))+(7-(2))))))))-(6))-(8+(9))-(9+(2))-(2)+(9))-(4))+(7)-(1)-(6))-(2-0)))))))-(0)))))-(8+(0-(5))))+(9)-(1-(0)-(3)))-(3)-(0)))+(4)+(6))))-(5)+(1-(5)))))+(10))))-(5)+(0))))))-(6)))))))+(1))))))))-(5)))))))))+(8))))))))))))))))))-(7)+(10)))))))))))))-(4))))))-(10)-(4))+(1)+(3))-(1))))+(9))))))))+(2-(7-(4-(3+(0))))-(10)))))+0))))+(10)))))+(4)))))))))))+(3)))))))-(5)))))+(3)))))))))))))-(7)-(5-(2+(9))-(0))+(4)))+(10)))))-(1)))-(0))+(1))-(8+(10))))))-(10)-(10+(9)+(2))))-(1)))-(2))))+(4+(5))))))+(8))))))))))))))))))))))-(7)))-(3)))))))+(1))))-(7)-(3)+(4))))))-(6)))))))-(9-(3)))))))))))+(8))))))))))+(6))))))))))))))))))))))))))))))))))+(5))+(7))))))))))))))))))))))))-(10))))))+9)))))))";
int ans = calculate(Str);
return 0;
}