Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int priority(char op)
- {
- switch (op)
- {
- case '+':
- case '-':
- return 1;
- case '*':
- case '/':
- return 2;
- default:
- return 0;
- }
- };
- void inToPost(char* input, char* post)
- {
- char stack[100] = { NULL };
- int i, j, top;
- for (i = 0, j = 0, top = 0; input[i] != '\0'; i++)
- {
- switch (input[i])
- {
- case '(':
- stack[++top] = input[i];
- break;
- case '+':
- case '-':
- case '*':
- case '/':
- while (priority(stack[top]) >= priority(input[i]))
- post[j++] = stack[top--];
- stack[++top] = input[i];
- break;
- case')':
- while (stack[top] != '(')
- post[j++] = stack[top--];
- top--;
- break;
- default:
- post[j++] = input[i];
- }
- }
- while (top > 0)
- post[j++] = stack[top--];
- };
- int main()
- {
- char post[100] = { NULL };
- char input[100] = { NULL };
- while (1)
- {
- scanf("%s", input);
- inToPost(input, post);
- for (int i = 0; post[i] != '\0'; i++)
- cout << post[i];
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement