PLEASE HOW DO I APPLY THE FOLLOWING RULES TO THE C PROGRAM G

PLEASE HOW DO I APPLY THE FOLLOWING RULES TO THE C++ PROGRAM GIVEN.

RULE 1: scan infix string

do
{
    get ch from postfix
    if (ch is a letter)
RULE 2: append ch to postfix
    else
    if ch is \'(\'
RULE 4: push into stack
    else
RULE 5: if it is \')\'
     keep popping and appending to postfix until \'(\'
    else
RULE 6: if ch is a \'$\'
     keep popping and appending to postfix until $ sign add \'$\'
     add \'\\0\'
    else
     (statement)
    (append ->push) while (ch != \'$\')
{
RULE 3: If next symbol is an operand,
(a) Pop and append to the postfix string every operator on the stack that
(i) Is above the most recently scanned left parenthesis, and
(ii) has precedence higher than or equal to that of the new operator symbol
(b) Then push the new operator symbol onto the stack.


#include <stdio.h>
#include <ctype.h>
#define MAX 40
typedef struct stack
{
int dat [MAX];
int top;
}stack;
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 is : %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]!=\'\\0\'; i++)
{
token = infix[i];
if (isalnum (token))
postfix[j++] = token;
else
{
switch(token)
{
case \'(\':
push (&s, \'(\'); break;
case \')\':
while ((x = pop (&s)) !=\'(\')
postfix[j++] = x;
break;  
default:
{
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] = \'\\0\';
}
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]);
}

Solution

What is wrong with yur code? Which part of it is not working. Please specify.

Meanwhile, here is what you need to do.

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;

// The following function is to get the precedence of the operators. High value means high priority. For non operators, //zero is returned.

int getPrecedence(char ch) {
switch (ch) {
case \'/\':
case \'*\': return 2;
case \'+\':
case \'-\': return 1;
default : return 0;
}
}

// Following function converts the infix expression to postfix expression
void infix_to_postfix(char infix[], char postfix[], int size) {
stack<char> s;
int weight;
int i = 0;
int k = 0;
char ch;
// iterate over the array containing the infix expression   
while (i < size) {
ch = infix[i];
if (ch == \'(\') {
// If opening parenthesis is encountered, simply push
s.push(ch);
i++;
continue;
}
if (ch == \')\') {
// if a closing parenthesis is encountered, the pop all elements and append it to the postfix expression till we //encounter an opening parenthesis

while (!s.empty() && s.top() != \'(\') {
postfix[k++] = s.top();
s.pop();

}
// pop off the opening parenthesis also
if (!s.empty()) {
s.pop();
}
i++;
continue;
}
weight = getPrecedence(ch);
if (weight == 0) {
// zero precedence value means an operand. so, simply append it to postfix expression
postfix[k++] = ch;

}
else {
// If it is an operator
if (s.empty()) {
// If stack is empty, simply push the operator onto stack
s.push(ch);
}
else {
// pop of all the operators from the stack and
// append it to the postfix expression till we
// see an operator with a lower precedence that
// the current operator
while (!s.empty() && s.top() != \'(\' &&
weight <= getWeight(s.top())) {
postfix[k++] = s.top();
s.pop();

}
// push the current operator onto stack
s.push(ch);
}
}
i++;
}
// pop of the remaining operators present in the stack
// and append it to postfix expression
while (!s.empty()) {
postfix[k++] = s.top();
s.pop();
}
postfix[k] = 0; // null terminate the postfix expression
}

// main
int main() {
char infix[] = \"A*(B+C)/D\";
int size = strlen(infix);
char postfix[size];
infix_to_postfix(infix,postfix,size);
cout<<\"\ Infix Expression :: \"<<infix;
cout<<\"\ Postfix Expression :: \"<<postfix;
cout<<endl;
return 0;
}

PLEASE HOW DO I APPLY THE FOLLOWING RULES TO THE C++ PROGRAM GIVEN. RULE 1: scan infix string do { get ch from postfix if (ch is a letter) RULE 2: append ch to
PLEASE HOW DO I APPLY THE FOLLOWING RULES TO THE C++ PROGRAM GIVEN. RULE 1: scan infix string do { get ch from postfix if (ch is a letter) RULE 2: append ch to
PLEASE HOW DO I APPLY THE FOLLOWING RULES TO THE C++ PROGRAM GIVEN. RULE 1: scan infix string do { get ch from postfix if (ch is a letter) RULE 2: append ch to
PLEASE HOW DO I APPLY THE FOLLOWING RULES TO THE C++ PROGRAM GIVEN. RULE 1: scan infix string do { get ch from postfix if (ch is a letter) RULE 2: append ch to
PLEASE HOW DO I APPLY THE FOLLOWING RULES TO THE C++ PROGRAM GIVEN. RULE 1: scan infix string do { get ch from postfix if (ch is a letter) RULE 2: append ch to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site