Question

Write a C program which evaluate and calculate prefix expression notation (normal polish notation). In this...

Write a C program which evaluate and calculate prefix expression notation (normal polish notation). In this notation, the operators come first, so ++ 2 3 4 should evaluate as 9. Instead of using any loop, write it using recursion.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <cstdio>

int eval(const char *s, const char **outptr)
{
int a, b, y;
const char *out;

switch (*s) {
case '+':
a = eval(s + 1, &out);
b = eval(out, &out);
y = a + b;
*outptr = out;
break;
case '-':
a = eval(s + 1, &out);
b = eval(out, &out);
y = a - b;
*outptr = out;
break;
case '*':
a = eval(s + 1, &out);
b = eval(out, &out);
y = a * b;
*outptr = out;
break;
case '/':
a = eval(s + 1, &out);
b = eval(out, &out);
y = a / b;
*outptr = out;
break;
default: /* '0'...'9'assumed */
y = *s - '0';
*outptr = s + 1;
break;
}

return y;
}

int main(int argc, char *argv[])
{
const char *end;
int x;
   printf("Enter without spaces :");
   char c[100];
   scanf("%s",c);
x = eval(c, &end);
printf("%d\n", x);

return 0;
}

output:

Enter without spaces :++234
9


Process exited normally.
Press any key to continue . . .


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.

Add a comment
Know the answer?
Add Answer to:
Write a C program which evaluate and calculate prefix expression notation (normal polish notation). In this...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to write a program that implements a Reverse Polish Notation Calculator in C using...

    You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files. Link to the files to start and have a general view of the program: https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0 Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed...

  • Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...

    Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...

  • write a C program that evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN)

    Your program must evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN). + (addition), - (subtraction), * (multiplication) and / (division) should be treated as arithmetic operations. Depending on the selection, numerical values must be entered and displayed in decimal, hexadecimal or binary form. Example: The expression ((3+4) *(7+1.6-12) -5) / (3-8.7)Will be calculated on your RPN calculator as 3 4 + 7 1.6 + 12 - * 5 - 3 8.7 - / Your RPN computer's user...

  • Write in C++ Implement a program that can input an expression in postfix notation (see Exercise...

    Write in C++ Implement a program that can input an expression in postfix notation (see Exercise C-5.8) and output its value. As you can see, you will need to read Exercise C-5.8 to complete this programming task. Exercise C-5.8 Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if “(exp1) o (exp2)” is a normal fully parenthesized expression whose operation is “o”, then the postfix version of this is “pexp1pexp2o”, where...

  • For this assignment, you are to write a program, which will calculate the results of Reverse...

    For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user. You must use a linked list to maintain the stack for this program. You must handle the following situations (errors): Too many operators (+ - / *) Too many operands (doubles) Division by zero The program will take in a Polish expression that separates the operators and operands by a single space, and terminates the expression...

  • Write a program in c++ to convert an expression written in infix notation to be converted...

    Write a program in c++ to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b....

  • Create a C program Evaluate an expression from console input and print its result. -The format...

    Create a C program Evaluate an expression from console input and print its result. -The format of expressions: Number Symbol Number Symbol ... Symbol Number = Number -Number refers to an integer and Symbol refers to either + , -, *, or / - * and / have higher priority over + and -. E.g., given input “1+2*3+4=”, the result should be “11”; Hint: Use Polish Notation

  • Write a C++ program that reads string expressions from an input file "prefix.txt" containing prefix expressions...

    Write a C++ program that reads string expressions from an input file "prefix.txt" containing prefix expressions (one expression per input line). For each prefix expression read from the input file, the program should: (1) convert the expression to a fully parenthesized infix expression and (2) find the value of the expression. Use a stack to solve the problem. Assume the expressions contain only integer numbers and the *,/, +, - operators. Generate the results in an output file "results.txt" in...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT