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 infix expressions would appear in postfix notation as 3 4 +and 7 9 / respectively. To evaluate a complex infix expression, a compiler would first convert the expression to post-fix notation, and then evaluate the postfix version of the expression. Each of these algorithms requires only a single left-to-right pass of the expression. Each algorithm uses a stack in support of its operation, and in each the stack is used for a different purpose. In this exercise, you’ll write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single digit integers such as (6 + 2) * 5 -8 / 4 to a postfix expression. The postfix version of the preceding infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into character array infix and use modified versions of the stack functions you have implemented so farto help create the postfix expression in character array postfix. The algorithm for creating a postfix expression is as follows: 1. Push a left parenthesis '(' onto the stack. 2. Append a right parenthesis ')' to the end of infix. 3. While the stack is not empty, read infix from left to right and do the following: 3.1. If the current character in infix is a digit, copy it to the next element of postfix. 3.2. If the current character in infix is a left parenthesis, push it onto the stack. 3.3. If the current character in infix is an operator, 3.3.1. Pop operators (if there are any) at the top of the stack while they have equal or higher precedence than the current operator and insert the popped operators in postfix. 3.3.2. Push the current character in infix onto the stack. 3.4. If the current character in infix is a right parenthesis 3.4.1. Pop operators from the top of the stack and insert them in postfix until a left parenthesis is at the top of the stack. 3.4.2. Pop (and discard) the left parenthesis from the stack. The following arithmetic operations are allowed in an expression: + addition - subtraction * multiplication / division ^ exponentiation % remainder The stack should be maintained with the following declarations: struct stackNode { char data; struct stackNode *nextPtr; }; typedef struct stackNode StackNode; typedef StackNode *StackNodePtr; The program should consist of main and eight other functions with the following function headers: void convertToPostfix( char infix[], char postfix[] ) Convert the infix expression to postfix notation. int isOperator( char c ) Determine if c is an operator. int precedence( char operator1, char operator2 ) Determine if the precedence of operator1 is less than, equal to, or greater than the precedence of operator2. The function returns -1, 0 and 1, respectively. void push( StackNodePtr *topPtr, char value ) Push a value on the stack. char pop( StackNodePtr *topPtr ) Pop a value off the stack. char stackTop( StackNodePtr topPtr ) Return the top value of the stack without popping the stack. int isEmpty( StackNodePtr topPtr ) Determine if the stack is empty. void printStack( StackNodePtr topPtr ) Print the stack.
Working code implemented in C and appropriate comments provided for better understanding:
Source code for main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
struct stackNode {
char data;
struct stackNode *nextPtr;
};
void convertToPostfix( char infix[], char postfix[] ); //Convert
the infix expression to postfix notation.
int isOperator( char c ); //Determine if c is an operator.
int precedence( char operator1, char operator2 );//Determine if the
precedence of operator1 is less than, equal to, or greater than the
precedence of operator2. The function returns -1, 0 and 1,
respectively.
void push( StackNodePtr *topPtr, char value ); //Push a value on
the stack.
char pop( StackNodePtr *topPtr ); // Pop a value off the
stack.
char stackTop( StackNodePtr topPtr ); //Return the top value of the
stack without popping the stack.
int isEmpty( StackNodePtr topPtr ); //Determine if the stack is
empty.
void printStack( StackNodePtr topPtr );// print stack
int main(void) {
char infix[256]; //initialise empty infix array
char postfix[256]; //initialise empty postfix
array
printf("Enter an infix expression: ");
scanf("%s", &infix);
printf("The original infix expression is: \n");
printf("%s\n", infix);
convertToPostfix(infix, postfix);
printf("The expression in postfix notation is:
%s\n",postfix); //answer
}
void convertToPostfix( char infix[], char postfix[] )
{
int infixIndex=0; //iterates through infix
int postfixIndex=0;//iterates through postfix
StackNodePtr head = NULL; //initialise empty
stack
push(&head, '(');
printStack(head);
strcat(infix,")"); //append ")" onto infix
while(isEmpty(head) == 1) { //stack not empty
if(isdigit(infix[infixIndex])){
//is digit
postfix[postfixIndex++] = infix[infixIndex++];
}
if(infix[infixIndex]=='(') { //is
"("
push(&head,
infix[infixIndex++]);
printStack(head);
}
if(isOperator(infix[infixIndex])==1) {//is ^,/,*,%,+,-
while((isOperator(stackTop(head)) == 1) &&
((precedence(stackTop(head), infix[infixIndex]) != -1))) { //is
operator with higher precedence than current infix value
postfix[postfixIndex++] = pop(&head);
//insert operator in postfix
}
push(&head
,infix[infixIndex++]);
printStack(head);
}
if(infix[infixIndex]==')') { //is
")"
infixIndex++;
while(stackTop(head) != '(') {
postfix[postfixIndex++] = pop(&head);
printStack(head);
}
pop(&head);
printStack(head);
}
}
}
int isEmpty(StackNodePtr topPtr) {
if(topPtr == NULL){
return 0; // empty, return 0
}else{
return 1; //not empty
}
}
void push (StackNodePtr *topPtr, char value) {
StackNodePtr snp;
snp = malloc(sizeof(StackNode)); //allocate memory for
snp
if(snp != NULL) { //memory allocation succeeded
snp->data = value;
snp->nextPtr = *topPtr;
*topPtr = snp;
}
}
char pop( StackNodePtr *topPtr ) {
StackNodePtr snp = *topPtr;
char data = snp->data;
*topPtr = (*topPtr)->nextPtr; //move topPtr to new
top
free(snp);
return data;
}
int isOperator(char c) {
if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^' ||
c=='%') {
return 1; //is operator
}
return -1; //is not an operator
}
int precedence(char operator1, char operator2) {
//precedence: ^ < / <= *<= % < + <=
-
if(operator1 == '+' || operator1 == '-'){ //compare +
and -, they equal precedence
if(operator2 == '+' || operator2 ==
'-'){
return
0;
}else{
return
-1;
}
}else if(operator1 == '*' || operator1 == '/' ||
operator1 == '%'){ //compare * , / and %, they equal
precedence
if(operator2 == '+' || operator2 ==
'-'){
return
1;
}else if(operator2 == '*' ||
operator2 == '/' || operator2 == '%'){
return
-0;
}else{
return
-1;
}
}else if(operator1 == '^'){ //^ has the greatest
precedence
if(operator2 == '^'){
return
0;
}else{
return
1;
}
}
}
char stackTop( StackNodePtr topPtr ) {
if(topPtr != NULL){
return topPtr -> data; //peek at
top
}
}
void printStack( StackNodePtr topPtr ) {
while(topPtr != NULL) {
printf("%c\t",
topPtr->data);
topPtr=topPtr->nextPtr; //update
topPtr
}
printf("NULL \n");
}
Sample Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.
Stacks are used by compilers to help in the process of evaluating expressions and generating machine...
In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is 62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...
In c++ Section 1. Stack ADT – Overview Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack. Operations Constructor. Creates an empty stack. Copy constructor....
We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...
Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...
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;...
EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation; in it, operators are written between their operands: X + Y. Such expressions can be ambiguous; do we add or multiply first in the expression 5 + 3 * 2? Parentheses and rules of precedence and association clarify such ambiguities: multiplication and division take precedence over addition and subtraction, and operators associate from left to right. This project implements and exercises a stack-based algorithm that evaluates...
Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...
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...
Code should be written in java Write a program to convert an infix expression to postfix expression using stack. Algorithm: a) Create a stack b) For each character t in the input stream If(t is an operand) append t to the output. Else if (t is a right parenthesis) Pop and append to the output until a left parenthesis is popped (but do not append this parenthesis to the output). Else if(t is an operator or left parenthesis) Pop and...
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...