- #include<stdio.h>
- #include<ctype.h>
- #include<string.h>
- char stack[100];
- int top = -1;
- void push(char x)
- {
- stack[++top] = x;
- }
- char pop()
- {
- if(top == -1)
- return -1;
- else
- return stack[top--];
- }
- int priority(char x)
- {
- if(x=='=')
- return 1;
- if(x == '(')
- return 0;
- if(x == '+' || x == '-')
- return 2;
- if(x == '*' || x == '/')
- return 3;
- return 0;
- }
- int main()
- {
- char exp[100];
- char *e, x;
- printf("Enter the expression : ");
- scanf("%s",exp);
- printf("\n");
- e = exp;
- char post[100];
- int ppointer=0;
- while(*e != '\0')
- {
- if(isalnum(*e))
- post[ppointer++] = *e;
- else if(*e == '(')
- push(*e);
- else if(*e == ')')
- {
- while((x = pop()) != '(')
- post[ppointer++] = x;
- }
- else
- {
- while(priority(stack[top]) >= priority(*e))
- post[ppointer++] = pop();
- push(*e);
- }
- e++;
- }
- while(top != -1)
- {
- post[ppointer++] = pop();
- }
- post[ppointer] = '\0';
- printf("postfix expression is: %s\n",post);
- printf("REGISTER OP1 OP2 OPERATOR\n");
- for(int i = 0; i<ppointer ; i++ )
- {
- if(isalpha(post[i]))
- {
- printf(" %d %c MOV \n",i,post[i]);
- push(48+i);
- }
- else
- {
- char x2 = pop();
- char x1 = pop();
- if(post[i]=='+')
- printf(" %c %c %c ADD \n",x1,x1,x2);
- else if(post[i]=='-')
- printf(" %c %c %c SUB \n",x1,x1,x2);
- else if(post[i]=='*')
- printf(" %c %c %c MUL \n",x1,x1,x2);
- else if(post[i]=='/')
- printf(" %c %c %c DIV \n",x1,x1,x2);
- else
- printf(" %c %c %c LOAD \n",x1,x1,x2);
- push(x1);
- }
- }
- return 0;
- }
[text] 3address
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: