#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); /*! push_p() */
} else { /* Operator */
while (1) {
switch (*in) {
case '(':
push(stk, ts, *in); /*! push_s() */
break;
case ')':
while (stk[*ts - 1] != '(') {
push(post, tp, pop(stk, ts)); /*! push_p( pop_s() ) */
}
pop(stk, ts); /*! pop_s() */
break;
default:
break;
}
if (*in == '(' && *in == ')') {
break;
}
if (!stk[*ts - 1]) {
while (operator[stk[*ts - 1]] >= operator[*in]) {
push(post, tp, pop(stk, ts)); /*! push_p( pop_s() ) */
if (stk[*ts - 1]) {
break;
}
}
}
break;
}
if (*in != '(' && *in != ')') {
push(stk, ts, *in); /* !push_s() */
}
}
in++;
}
}
void
stack_empty(char *post, char *stk, int *ts, int *tp)
{
while (stk[*ts - 1]) {
push(post, tp, pop(stk, ts)); /*! push_p( pop_s() ) */
}
}
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_p, &top_s);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjdHlwZS5oPgoKdm9pZApwdXNoKGNoYXIgKnMsIGludCAqdG9wLCBpbnQgYykKewogICAgc1sqdG9wXSA9IGM7CiAgICAoKnRvcCkrKzsKfQoKaW50CnBvcChjaGFyICpzLCBpbnQgKnRvcCkKewogICAgKCp0b3ApLS07CiAgICByZXR1cm4gc1sqdG9wXTsKfQoKdm9pZAppbmZpeF90b19wb3N0Zml4KGNoYXIgKmluLCBjaGFyICpwb3N0LCBjaGFyICpzdGssIGludCAqdHAsIGludCAqdHMpCnsKICAgIGNoYXIgb3BlcmF0b3JbMjU2XSA9IHswfTsKCiAgICBvcGVyYXRvclsnKCddID0gMDsKICAgIG9wZXJhdG9yWycrJ10gPSAxOwogICAgb3BlcmF0b3JbJy0nXSA9IDE7CiAgICBvcGVyYXRvclsnKiddID0gMjsKICAgIG9wZXJhdG9yWycvJ10gPSAyOwovKiBUaGlzIHByb2dyYW0gc3RvcCBoZXJlICovCiAgICB3aGlsZSAoKmluKSB7CiAgICAgICAgaWYgKGlzZGlnaXQoKmluKSkgewogICAgICAgICAgICBwdXNoKHBvc3QsIHRwLCAqaW4pOyAvKiEgcHVzaF9wKCkgKi8KICAgICAgICB9IGVsc2UgeyAvKiBPcGVyYXRvciAqLwogICAgICAgICAgICB3aGlsZSAoMSkgewogICAgICAgICAgICAgICAgc3dpdGNoICgqaW4pIHsKICAgICAgICAgICAgICAgICAgICBjYXNlICcoJzoKICAgICAgICAgICAgICAgICAgICAgICAgcHVzaChzdGssIHRzLCAqaW4pOyAvKiEgcHVzaF9zKCkgKi8KICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgY2FzZSAnKSc6CiAgICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIChzdGtbKnRzIC0gMV0gIT0gJygnKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwdXNoKHBvc3QsIHRwLCBwb3Aoc3RrLCB0cykpOyAvKiEgcHVzaF9wKCBwb3BfcygpICkgKi8KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBwb3Aoc3RrLCB0cyk7IC8qISBwb3BfcygpICovCiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKCppbiA9PSAnKCcgJiYgKmluID09ICcpJykgewogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKCFzdGtbKnRzIC0gMV0pIHsKICAgICAgICAgICAgICAgICAgICB3aGlsZSAob3BlcmF0b3Jbc3RrWyp0cyAtIDFdXSA+PSBvcGVyYXRvclsqaW5dKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHB1c2gocG9zdCwgdHAsIHBvcChzdGssIHRzKSk7CS8qISBwdXNoX3AoIHBvcF9zKCkgKSAqLwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoc3RrWyp0cyAtIDFdKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICgqaW4gIT0gJygnICYmICppbiAhPSAnKScpIHsKICAgICAgICAgICAgICAgIHB1c2goc3RrLCB0cywgKmluKTsJLyogIXB1c2hfcygpICovCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW4rKzsKICAgIH0KfQoKdm9pZApzdGFja19lbXB0eShjaGFyICpwb3N0LCBjaGFyICpzdGssIGludCAqdHMsIGludCAqdHApCnsKICAgIHdoaWxlIChzdGtbKnRzIC0gMV0pIHsKICAgICAgICBwdXNoKHBvc3QsIHRwLCBwb3Aoc3RrLCB0cykpOwkvKiEgcHVzaF9wKCBwb3BfcygpICkgKi8KICAgIH0KfQoKaW50Cm1haW4oaW50IGFyZ2MsIGNoYXIgKiphcmd2KQp7CiAgICBjaGFyIGluZml4W10gPSAiMiooKDMtNSkqMikiOwogICAgY2hhciBwb3N0Zml4WzI1Nl0gPSB7MH07CiAgICBjaGFyIHN0YWNrWzI1Nl0gPSB7MH07CiAgICBpbnQgdG9wX3AgPSAwOwogICAgaW50IHRvcF9zID0gMDsKCglpbmZpeF90b19wb3N0Zml4KGluZml4LCBwb3N0Zml4LCBzdGFjaywgJnRvcF9wLCAmdG9wX3MpOwoJc3RhY2tfZW1wdHkocG9zdGZpeCwgc3RhY2ssICZ0b3BfcCwgJnRvcF9zKTsKICAgIHByaW50ZigiJXNcbiIsIHBvc3RmaXgpOwogICAgcmV0dXJuIDA7Cn0K