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

PLEASE HOW CAN I PUT THE FOLLOWING PROGRAM IN A LOOP: #include <iostream> #inclu

ID: 3922082 • Letter: P

Question

PLEASE HOW CAN I PUT THE FOLLOWING PROGRAM IN A LOOP:

#include <iostream>
#include <stdio.h>
#include <ctype.h>
#define MAX 40

using namespace std;

struct stack
{
    int dat [MAX];
    int top;
};

int precedence (char);
void init (stack *);
int empty (stack *);
int full (stack *);
int pop (stack *);
void push (stack *, int);
int top (stack *);
void infix_to_postfix (char infix[], char postfix[]);
void eval_postfix (char postfix[]);
int evaluate (char x, int op1, int op2);

int main()
{
    char infix [30], postfix [30];
    printf (" Enter an infix expression : ");
    scanf ("%s", infix);
    infix_to_postfix (infix, postfix);
    printf (" Postfix expression :%s ", postfix);
    printf (" ");
    return 0;
}

void infix_to_postfix (char infix[], char postfix[])
{
    stack s;
    char x;
    int i, j; // i-index for infix[], j-index for postfix
    char token;
    init (&s);
    j = 0;
    for (i = 0; infix[i]!=''; i++)
    {
        token = infix[i];
        if (isalnum (token))
            postfix[j++] = token;
        else
            if (token == '(')
                push (&s, '(');
        else
            if (token == ')')
                while ((x = pop (&s)) !='(')
                    postfix[j++] = x;
        else
        {
            while (precedence (token) <= precedence (top (&s)) && !empty (&s))
            {
                x = pop (&s);
                postfix[j++] = x;
            }
            push (&s, token);
        }
    }
    while (!empty (&s))
    {
        x = pop (&s);
        postfix[j++] = x;
    }
    postfix[j] = '';
}

int precedence (char x)
{
    if (x == '(') return (0);
    if (x == '+' || x == '-') return (1);
    if (x == '*' || x == '/' || x == '%') return (2);
    return (3);
}

void init (stack *s)
{
    s->top = -1;
}

int empty (stack *s)
{
    if (s->top == -1) return (1);
    return (0);
}

int full (stack *s)
{
    if (s->top == MAX - 1) return (1);
    return (0);
}

void push (stack *s, int x)
{
    s->top = s->top + 1;
    s->dat [s->top] = x;
}

int pop (stack *s)
{
    int x;
    x = s->dat [s->top];
    s->top = s->top - 1;
    return (x);
}

int top (stack * p)
{
    return (p->dat [p->top]);
}

Explanation / Answer

#include <iostream>
#include <stdio.h>
#include <ctype.h>
#define MAX 40
using namespace std;
struct stack
{
int dat [MAX];
int top;
};
int precedence (char);
void init (stack *);
int empty (stack *);
int full (stack *);
int pop (stack *);
void push (stack *, int);
int top (stack *);
void infix_to_postfix (char infix[], char postfix[]);
void eval_postfix (char postfix[]);
int evaluate (char x, int op1, int op2);
int main()
{
char infix [30], postfix [30];
int option= 1;
while(option == 1) //while loop condition option =1 initially
{
printf (" Enter an infix expression : ");
scanf ("%s", infix);
infix_to_postfix (infix, postfix);
printf (" Postfix expression :%s ", postfix);
printf (" ");
printf("Do you want to continue? enter 1 for yes 0 for no"); //set loop condition
scanf("%d",&option);
if(option != 1)
break;
}
  
return 0;
}
void infix_to_postfix (char infix[], char postfix[])
{
stack s;
char x;
int i, j; // i-index for infix[], j-index for postfix
char token;
init (&s);
j = 0;
for (i = 0; infix[i]!=''; i++)
{
token = infix[i];
if (isalnum (token))
postfix[j++] = token;
else
if (token == '(')
push (&s, '(');
else
if (token == ')')
while ((x = pop (&s)) !='(')
postfix[j++] = x;
else
{
while (precedence (token) <= precedence (top (&s)) && !empty (&s))
{
x = pop (&s);
postfix[j++] = x;
}
push (&s, token);
}
}
while (!empty (&s))
{
x = pop (&s);
postfix[j++] = x;
}
postfix[j] = '';
}
int precedence (char x)
{
if (x == '(') return (0);
if (x == '+' || x == '-') return (1);
if (x == '*' || x == '/' || x == '%') return (2);
return (3);
}
void init (stack *s)
{
s->top = -1;
}
int empty (stack *s)
{
if (s->top == -1) return (1);
return (0);
}
int full (stack *s)
{
if (s->top == MAX - 1) return (1);
return (0);
}
void push (stack *s, int x)
{
s->top = s->top + 1;
s->dat [s->top] = x;
}
int pop (stack *s)
{
int x;
x = s->dat [s->top];
s->top = s->top - 1;
return (x);
}
int top (stack * p)
{
return (p->dat [p->top]);
}

output:

Success time: 0 memory: 3472 signal:0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote