Question

Use C++ Your program should expect as input from (possibly re-directed) stdin a series of space-...

Use C++

Your program should expect as input from (possibly re-directed) stdin a series of space- separated strings. If you read a1 (no space) this is the name of the variable a1 and not "a" followed by "1". Similarly, if you read "bb 12", this is a variable "bb" followed by the number "12" and not "b" ,"b", "12" or "bb", "1" ,"2". Your program should convert all Infix expressions to Postfix expressions, including expressions that contain variable names. The resulting Postfix expression should be printed to stdout. Your program should evaluate the computed Postfix expressions that contain only numeric operands, using the above algorithm, and print the results to stdout.

(5 + 3) * 12 - 7 is an infix arithmetic expression that evaluates to 89

5 + 3 * 12 – 7 is an infix arithmetic expression that evaluates to 34

Postfix arithmetic expressions (also known as reverse Polish notation) equivalent to the above examples are:

5 3 + 12 * 7 –

5 3 12 * + 7 –   

Homework Answers

Answer #1

#include<stdio.h>

char stack[20]:

int top=-1:

void push(char x)

{

stack[++top] =x;

}

char pop()

{

if( top==-1)

return -1;

else

return stack[top--];

}

int priority(char x)

{

if (x=='(')

return o;

if(x=='+' ||x=='-')

return 1;

if(x==||x=='/')

return 2;

}

int main()

{

char exp[20];

char *e x;

printf("enter the expression :: ");

scanf("%s",exp);

e=exp;

while(*e !='\0)

{

if isalnum(*e))

printf("%c",*e);

else if(*e=='(')

push(*e);

else if(*e=='))

{

while((x=pop()) !='(')

printf("%c",x);

}

else

{while (priority(stack[top]) >=priority(*e))

printf("%c",pop());

push(*e);

}

e++;

while (top !=-1)

{printf("%c",pop());

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT