#include <stdio.h>
#include <ctype.h>
char postfix[64] = {0};
char stack[64] = {0};
int top_p = 0;
int top_s = 0;
void
push_s(char d)
{
stack[top_s] = d;
top_s++;
}
void
push_p(char d)
{
postfix[top_p] = d;
top_p++;
}
int
_pop_s(void)
{
top_s--;
return stack[top_s];
}
void
stack_init(void)
{
stack[top_s] = 0x00;
}
int
pop_s(void)
{
char d;
d = _pop_s();
stack_init();
return d;
}
int
_pop_p()
{
top_p--;
return postfix[top_p];
}
void
postfix_init(void)
{
postfix[top_p] = 0x00;
}
int
pop_p(void)
{
char d;
d = _pop_p();
postfix_init();
return d;
}
void
infix_to_postfix(char *in)
{
char operator[256] = {0};
operator['('] = 0;
operator['+'] = 1;
operator['-'] = 1;
operator['*'] = 2;
operator['/'] = 2;
while (*in) {
push_p(*in);
} else { /* operator */
while (1) {
switch (*in) {
case '(':
push_s(*in);
break;
case ')':
while (stack[top_s -1] != '(') {
push_p(pop_s());
}
pop_s();
break;
default:
break;
}
if (*in == '(' || *in == ')') {
break;
}
if (!stack[top_s]) {
while (operator[stack[top_s - 1]] >= operator[*in]) {
push_p(pop_s());
if (stack[top_s - 1]) {
break;
}
}
}
break;
}
if (*in != '(' || *in != ')') {
push_s(*in);
}
}
in++;
}
}
void
stack_empty()
{
while (stack[top_s - 1]) {
push_p(pop_s());
}
}
int
main(int argc, char **argv)
{
char infix[] = "1+2-3*4/3";
infix_to_postfix(infix);
stack_empty();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjdHlwZS5oPgoKY2hhciBwb3N0Zml4WzY0XSA9IHswfTsKY2hhciBzdGFja1s2NF0gPSB7MH07CmludCB0b3BfcCA9IDA7CmludCB0b3BfcyA9IDA7Cgp2b2lkCnB1c2hfcyhjaGFyIGQpCnsKICAgIHN0YWNrW3RvcF9zXSA9IGQ7CiAgICB0b3BfcysrOwp9Cgp2b2lkCnB1c2hfcChjaGFyIGQpCnsKICAgIHBvc3RmaXhbdG9wX3BdID0gZDsKICAgIHRvcF9wKys7Cn0KCmludApfcG9wX3Modm9pZCkKewogICAgdG9wX3MtLTsKICAgIHJldHVybiBzdGFja1t0b3Bfc107Cn0KCnZvaWQKc3RhY2tfaW5pdCh2b2lkKQp7CiAgICBzdGFja1t0b3Bfc10gPSAweDAwOwp9CgppbnQKcG9wX3Modm9pZCkKewogICAgY2hhciBkOwogICAgZCA9IF9wb3BfcygpOwogICAgc3RhY2tfaW5pdCgpOwogICAgcmV0dXJuIGQ7Cn0KCmludApfcG9wX3AoKQp7CiAgICB0b3BfcC0tOwogICAgcmV0dXJuIHBvc3RmaXhbdG9wX3BdOwp9Cgp2b2lkCnBvc3RmaXhfaW5pdCh2b2lkKQp7CiAgICBwb3N0Zml4W3RvcF9wXSA9IDB4MDA7Cn0KCmludApwb3BfcCh2b2lkKQp7CiAgICBjaGFyIGQ7CiAgICBkID0gX3BvcF9wKCk7CiAgICBwb3N0Zml4X2luaXQoKTsKICAgIHJldHVybiBkOwp9Cgp2b2lkCmluZml4X3RvX3Bvc3RmaXgoY2hhciAqaW4pCnsKICAgIGNoYXIgb3BlcmF0b3JbMjU2XSA9IHswfTsKCiAgICBvcGVyYXRvclsnKCddID0gMDsKICAgIG9wZXJhdG9yWycrJ10gPSAxOwogICAgb3BlcmF0b3JbJy0nXSA9IDE7CiAgICBvcGVyYXRvclsnKiddID0gMjsKICAgIG9wZXJhdG9yWycvJ10gPSAyOwogICAgd2hpbGUgKCppbikgewogICAgICAgIGlmIChpc2RpZ2l0KCppbikpIHsKICAgICAgICAgICAgcHVzaF9wKCppbik7CiAgICAgICAgfSBlbHNlIHsgLyogb3BlcmF0b3IgKi8KICAgICAgICAgICAgd2hpbGUgKDEpIHsKICAgICAgICAgICAgICAgIHN3aXRjaCAoKmluKSB7CiAgICAgICAgICAgICAgICBjYXNlICcoJzoKICAgICAgICAgICAgICAgICAgICBwdXNoX3MoKmluKTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIGNhc2UgJyknOgogICAgICAgICAgICAgICAgICAgIHdoaWxlIChzdGFja1t0b3BfcyAtMV0gIT0gJygnKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHB1c2hfcChwb3BfcygpKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgcG9wX3MoKTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKmluID09ICcoJyB8fCAqaW4gPT0gJyknKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoIXN0YWNrW3RvcF9zXSkgewogICAgICAgICAgICAgICAgICAgIHdoaWxlIChvcGVyYXRvcltzdGFja1t0b3BfcyAtIDFdXSA+PSBvcGVyYXRvclsqaW5dKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHB1c2hfcChwb3BfcygpKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHN0YWNrW3RvcF9zIC0gMV0pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKCppbiAhPSAnKCcgfHwgKmluICE9ICcpJykgewogICAgICAgICAgICAgICAgcHVzaF9zKCppbik7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW4rKzsKICAgIH0KfQoKdm9pZApzdGFja19lbXB0eSgpCnsKICAgIHdoaWxlIChzdGFja1t0b3BfcyAtIDFdKSB7CiAgICAgICAgcHVzaF9wKHBvcF9zKCkpOwogICAgfQp9CgppbnQKbWFpbihpbnQgYXJnYywgY2hhciAqKmFyZ3YpCnsKICAgIGNoYXIgaW5maXhbXSA9ICIxKzItMyo0LzMiOwoKICAgIGluZml4X3RvX3Bvc3RmaXgoaW5maXgpOwogICAgc3RhY2tfZW1wdHkoKTsKICAgIHByaW50ZigiJXNcbiIsIHBvc3RmaXgpOwogICAgcmV0dXJuIDA7Cn0K