PLEASE HOW DO I PUT THIS C PROGRAM INTO A SWITCH STATEMENT E

PLEASE HOW DO I PUT THIS C++ PROGRAM INTO A SWITCH STATEMENT

EXAMPLE:     switch (ch)
                      {
                          case \'a\';
                         (statement )
                          break;

                           case \'a\';
                         (statement )
                          break;
                       }


#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 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
            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] = \'\\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

Here is the modified code for you, which uses the switch case:

#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]);
}

PLEASE HOW DO I PUT THIS C++ PROGRAM INTO A SWITCH STATEMENT EXAMPLE: switch (ch) { case \'a\'; (statement ) break; case \'a\'; (statement ) break; } #include &
PLEASE HOW DO I PUT THIS C++ PROGRAM INTO A SWITCH STATEMENT EXAMPLE: switch (ch) { case \'a\'; (statement ) break; case \'a\'; (statement ) break; } #include &
PLEASE HOW DO I PUT THIS C++ PROGRAM INTO A SWITCH STATEMENT EXAMPLE: switch (ch) { case \'a\'; (statement ) break; case \'a\'; (statement ) break; } #include &
PLEASE HOW DO I PUT THIS C++ PROGRAM INTO A SWITCH STATEMENT EXAMPLE: switch (ch) { case \'a\'; (statement ) break; case \'a\'; (statement ) break; } #include &
PLEASE HOW DO I PUT THIS C++ PROGRAM INTO A SWITCH STATEMENT EXAMPLE: switch (ch) { case \'a\'; (statement ) break; case \'a\'; (statement ) break; } #include &

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site