#include <stdio.h>
#include <ctype.h>
void
push(char *s, int *top, int c)
{
s[*top] = c;
*top++;
}
int
pop(char *s, int *top)
{
*top--;
return s[*top];
}
void
infix_to_postfix(char *in, char *post, char *stk, int *tp, int *ts)
{
char operator[256] = {0};
operator['('] = 0;
operator['+'] = 1;
operator['-'] = 1;
operator['*'] = 2;
operator['/'] = 2;
/* This program stop here */
while (*in) {
push(post, tp, *in);
} else { /* Operator */
while (1) {
switch (*in) {
case '(':
push(stk, ts, *in);
break;
case ')':
while (stk[*ts - 1] != '(') {
push(post, tp, pop(stk, ts));
}
pop(stk, ts);
break;
default:
break;
}
if (*in == '(' && *in == ')') {
break;
}
if (!stk[*ts - 1]) {
while (operator[stk[*ts - 1]] >= operator[*in]) {
push(post, tp, pop(stk, ts));
if (stk[*ts - 1]) {
break;
}
}
}
break;
}
if (*in != '(' && *in != ')') {
push(stk, ts, *in);
}
}
in++;
}
}
void
stack_empty(char *post, char *stk, int *ts)
{
while (stk[*ts - 1]) {
push(post, ts, pop(stk, ts));
}
}
int
main(int argc, char **argv)
{
char infix[] = "2*((3-5)*2)";
char postfix[256] = {0};
char stack[256] = {0};
int top_p = 0;
int top_s = 0;
infix_to_postfix(infix, postfix, stack, &top_p, &top_s);
stack_empty(postfix, stack, &top_s);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjdHlwZS5oPgoKdm9pZApwdXNoKGNoYXIgKnMsIGludCAqdG9wLCBpbnQgYykKewogICAgc1sqdG9wXSA9IGM7CiAgICAqdG9wKys7Cn0KCmludApwb3AoY2hhciAqcywgaW50ICp0b3ApCnsKICAgICp0b3AtLTsKICAgIHJldHVybiBzWyp0b3BdOwp9Cgp2b2lkCmluZml4X3RvX3Bvc3RmaXgoY2hhciAqaW4sIGNoYXIgKnBvc3QsIGNoYXIgKnN0aywgaW50ICp0cCwgaW50ICp0cykKewogICAgY2hhciBvcGVyYXRvclsyNTZdID0gezB9OwoKICAgIG9wZXJhdG9yWycoJ10gPSAwOwogICAgb3BlcmF0b3JbJysnXSA9IDE7CiAgICBvcGVyYXRvclsnLSddID0gMTsKICAgIG9wZXJhdG9yWycqJ10gPSAyOwogICAgb3BlcmF0b3JbJy8nXSA9IDI7Ci8qIFRoaXMgcHJvZ3JhbSBzdG9wIGhlcmUgKi8KICAgIHdoaWxlICgqaW4pIHsKICAgICAgICBpZiAoaXNkaWdpdCgqaW4pKSB7CiAgICAgICAgICAgIHB1c2gocG9zdCwgdHAsICppbik7CiAgICAgICAgfSBlbHNlIHsgLyogT3BlcmF0b3IgKi8KICAgICAgICAgICAgd2hpbGUgKDEpIHsKICAgICAgICAgICAgICAgIHN3aXRjaCAoKmluKSB7CiAgICAgICAgICAgICAgICAgICAgY2FzZSAnKCc6CiAgICAgICAgICAgICAgICAgICAgICAgIHB1c2goc3RrLCB0cywgKmluKTsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgY2FzZSAnKSc6CiAgICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIChzdGtbKnRzIC0gMV0gIT0gJygnKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwdXNoKHBvc3QsIHRwLCBwb3Aoc3RrLCB0cykpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIHBvcChzdGssIHRzKTsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKmluID09ICcoJyAmJiAqaW4gPT0gJyknKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoIXN0a1sqdHMgLSAxXSkgewogICAgICAgICAgICAgICAgICAgIHdoaWxlIChvcGVyYXRvcltzdGtbKnRzIC0gMV1dID49IG9wZXJhdG9yWyppbl0pIHsKICAgICAgICAgICAgICAgICAgICAgICAgcHVzaChwb3N0LCB0cCwgcG9wKHN0aywgdHMpKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHN0a1sqdHMgLSAxXSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoKmluICE9ICcoJyAmJiAqaW4gIT0gJyknKSB7CiAgICAgICAgICAgICAgICBwdXNoKHN0aywgdHMsICppbik7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW4rKzsKICAgIH0KfQoKdm9pZApzdGFja19lbXB0eShjaGFyICpwb3N0LCBjaGFyICpzdGssIGludCAqdHMpCnsKICAgIHdoaWxlIChzdGtbKnRzIC0gMV0pIHsKICAgICAgICBwdXNoKHBvc3QsIHRzLCBwb3Aoc3RrLCB0cykpOwogICAgfQp9CgppbnQKbWFpbihpbnQgYXJnYywgY2hhciAqKmFyZ3YpCnsKICAgIGNoYXIgaW5maXhbXSA9ICIyKigoMy01KSoyKSI7CiAgICBjaGFyIHBvc3RmaXhbMjU2XSA9IHswfTsKICAgIGNoYXIgc3RhY2tbMjU2XSA9IHswfTsKICAgIGludCB0b3BfcCA9IDA7CiAgICBpbnQgdG9wX3MgPSAwOwoKICAgIGluZml4X3RvX3Bvc3RmaXgoaW5maXgsIHBvc3RmaXgsIHN0YWNrLCAmdG9wX3AsICZ0b3Bfcyk7CiAgICBzdGFja19lbXB0eShwb3N0Zml4LCBzdGFjaywgJnRvcF9zKTsKICAgIHByaW50ZigiJXNcbiIsIHBvc3RmaXgpOwogICAgcmV0dXJuIDA7Cn0=