Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<ctype.h>
- char stack[100];
- int top = -1;
- void push(char ch) {
- top++;
- stack[top] = ch;
- }
- void pop () {
- top--;
- }
- int priority (char ch) {
- if(ch=='^') {
- return 3;
- }
- else if(ch=='*' || ch == '/' || ch == '%') {
- return 2;
- }
- else if(ch=='+' || ch=='-') {
- return 1;
- }
- else {
- return -1;
- }
- }
- int main () {
- char s[100];
- scanf("%s",s);
- int i,l;
- l = strlen(s);
- for(i=0;i<l;i++) {
- if(isalpha(s[i])) {
- printf("%c",s[i]);
- }
- else if(s[i] == '(') {
- push(s[i]);
- }
- else if(s[i] == ')') {
- while(stack[top]!='(') {
- printf("%c",stack[top]);
- pop();
- }
- top--;
- }
- else {
- while(priority(s[i]) <= priority(stack[top])) {
- printf("%c",stack[top]);
- pop();
- }
- push(s[i]);
- }
- }
- for(int i=top;i>=0;i--) {
- printf("%c",stack[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment