Question

Given the following C code. Develop a evaluate post fix function that calculates the converted post...

Given the following C code. Develop a evaluate post fix function that calculates the converted post fix.

#include <stdio.h>
#include <ctype.h>
#define max 10
char s[100];
int top=-1;
void push(char);
char pop();
int precede(char);
main(){
   char infix[100],postfix[100],ch;
   int i=0,j=0;
   // Read infix expression from user
   printf("Enter infix expression:");
   scanf("%s",infix);
   // evaluate each char in infix expression
   while((ch=infix[i++])!='\0'){
       // if it is number, add it to postfix expression
       if(isalnum(ch)){
           postfix[j++]=ch;
       }else if(ch=='('){
           // if it open paranthesis, push it to stack
           push(ch);
       }else if(ch==')'){
           // if it is closing paranthesis, pop all the
           // numbers from stack and add it postfix
           while(s[top]!='('){
               postfix[j++]=pop();
           }
           top--;
       }else{
           /*
               if if is operator, if stack is empty or precedence of current operator is
               greater than precedence of top of the stack, then push it into stack.
               otherwise pop all the operators from stack until precedence of top of the stack is
               less than precedence of the current operator.
           */
           if(top==-1||precede(ch)>precede(s[top])){
               push(ch);
           }else{
               while(precede(ch)<=precede(s[top])&&top!=-1){
                   postfix[j++]=pop();
               }
               push(ch);
           }
       }
   }
   while(top!=-1){
       postfix[j++]=pop();
   }
   postfix[j]='\0';
   printf("Infix expression = %s\nPostfix expression = %s",infix,postfix);
}
void push(char a)
{
   s[++top]=a;
}
char pop(){
   return s[top--];
}
int precede(char a){
   if(a=='('||a==')'){
       return 1;
   }else if(a=='-'||a=='+'){
       return 2;
   }else if(a=='%'||a=='/'||a=='*'){
       return 3;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
struct Stack
{
int top;
unsigned capacity;
int* array;
};
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
  
if (!stack) return NULL;
  
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
  
if (!stack->array) return NULL;
  
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
int evaluatePostfix(char* postfix)
{

struct Stack* stack = createStack(strlen(postfix));
int i;
  
  
if (!stack) return -1;
  
for (i = 0; postfix[i]; ++i)
{
  
if (isdigit(postfix[i]))
push(stack, postfix[i] - '0');
  
  
else
{
int val1 = pop(stack);
int val2 = pop(stack);
switch (postfix[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2/val1); break;
case '%': push(stack, val2%val1); break;
}
}
}
return pop(stack);
}


int main()
{
char postfix[100];
scanf("%s",postfix);
printf ("postfix evaluation: %d", evaluatePostfix(postfix));
}

Please upvote if you found this solution useful and comment for any doubts.

Add a comment
Know the answer?
Add Answer to:
Given the following C code. Develop a evaluate post fix function that calculates the converted post...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • This code in C converts infix to postfix and evaluates it. The problem is that it...

    This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...

  • i want similar for this code to solve two questions : 1- Write a program to...

    i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c;...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a...

    Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a postfix expression, a binary operation follows its two opperands. The order of the operands in a infix expression is the same as in the corresponding postfix expression but the order of the operators might change based on the precedence of the operators and the existing of paranthses. Infix Postfix a + b a b + (a + b) * c a b + c...

  • Help with c++ program. The following code takes an infix expression and converts it to postfix....

    Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this. #include #include #include using namespace std; template class Stack { public: Stack();//creates the stack bool isempty(); // returns true if the stack is empty T gettop();//returns the front of the list void push(T entry);//add entry to the top of the stack void pop();//remove the top of the stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT