Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node {
- char data;
- struct node * next;
- };
- void push (struct node **top, char c) {
- struct node *temp = (struct node*)malloc(sizeof(struct node));
- temp -> next = NULL;
- temp->data = c;
- temp -> next = *top;
- *top = temp;
- }
- int pop (struct node **top) {
- struct node *p = *top;
- if(p!=NULL) {
- int popped = p->data;
- *top = p->next;
- return popped;
- }
- else {
- printf("Empty Stack");
- return -999;
- }
- }
- int peak(struct node **top) {
- return ((*top)->data);
- }
- int main() {
- struct node * top = NULL;
- int x=0;
- printf("Enter the FPE Infix expression: ");
- char exp[100];
- char out[100];
- char rev[100];
- gets(rev);
- strcpy(exp,strrev(rev));
- for(int i=0; i<strlen(exp); i++) {
- if(exp[i]=='(')
- exp[i]=')';
- else if(exp[i]==')')
- exp[i]='(';
- else
- continue;
- }
- for(int i=0; i< strlen(exp); i++) {
- if(exp[i]=='(')
- push(&top, '(');
- else if (exp[i]>=65 && exp[i]<=90) {
- out[x++] = exp[i];
- }
- else if(exp[i]==')') {
- while(peak(&top)!='(') {
- out[x++] = pop(&top);
- }
- char temp = pop(&top);
- }
- else {
- if(exp[i]=='+'||exp[i]=='-') {
- if(peak(&top)=='*'||peak(&top)=='/'||peak(&top)=='^') {
- out[x++] = pop(&top);
- push(&top, exp[i]);
- }
- else {
- push(&top, exp[i]);
- }
- }
- else if(exp[i]=='/'||exp[i]=='*') {
- if(peak(&top)=='^') {
- out[x++] = pop(&top);
- push(&top, exp[i]);
- }
- else {
- push(&top, exp[i]);
- }
- }
- else {
- push(&top, exp[i]);
- }
- }
- }
- strcpy(out, strrev(out));
- printf("Output: %s", out);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement