Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The conventional notation in w

ID: 3598506 • Letter: E

Question

EVALUATING GENERAL INFIX EXPRESSIONS

INTRODUCTION

The conventional 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 infix expressions according to the conventional rules of precedence and association.

DESCRIPTION

Design, implement, document, and test a program that reads infix expressions from a file, one expression per line. The program echoes the expressions, evaluates them using the stack-based algorithm described in class, and reports their values.

INPUT

The program reads the name of a file, then from the file reads syntactically correct general infix expressions involving single-digit integers and binary arithmetic operators: +, -, *, and /.

OUTPUT

The program prompts for the input file name and prints to the terminal each expression in the file and its value.

ERRORS

The program can assume that its input is as described; it need not detect any errors.

EXAMPLE

If a data file infix.dat is this:

then a run of the program might look like this:

OTHER REQUIREMENTS

Implement a stack abstract data type in a class. Use a linked (pointer-based) stack implementation.

Explanation / Answer

**Note : Here is a C++ program to implement the given question

#include <iostream>

#include <conio.h>

#include <string.h>

using namespace std;

struct node

{

    int data;

    node *next;

}*p = NULL, *top = NULL, *save = NULL, *ptr;

void push(int x)

{

    p = new node;

    p->data = x;

    p->next = NULL;

    if (top == NULL)

    {

        top = p;

    }

    else

    {

        save = top;

        top = p;

        p->next = save;

    }

}

char pop()

{

    if (top == NULL)

    {

        cout<<"underflow!!";

    }

    else

    {

        ptr = top;

        top = top->next;

        return(ptr->data);

        delete ptr;

    }

}

int main()

{

    char x[30];

    int a, b;

    cout<<"enter the balanced expression ";

    cin>>x;

    for (int i = 0; i < strlen(x); i++)

    {

        if (x[i] >= 48 && x[i] <= 57)

            push(x[i]-'0');

        else if (x[i] >= 42 && x[i] <= 47)

        {

            a=pop();

            b=pop();

            switch(x[i])

            {

            case '+':

                push(a+b);

                break;

            case '-':

                push(a-b);

                break;

            case '*':

                push(a*b);

                break;

            case '/':     

                push(a/b);

                break;

            }

        }

    }

    cout<<"ans is "<<pop()<<endl;

    getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote