Question

In: Computer Science

USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub...

USING C++:

Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest):

5 *, /, %
4 +, -
3 <, <=, >, >=, !=, ==
2 not
1 and
0 or

1.  Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the postfix expression and the result.

  • (19 + 4) * 2 < (32 * 2 + 1) and (16 / 4) == 4

Solutions

Expert Solution

The Given Infix expression is:

(19 + 4) * 2 < (32 * 2 + 1) AND (16 / 4) == 4

To convert Infix to Postfix expression, we use stack, called 'Operator stack'. For coversion, we use precedence and associatiativity rules of operators.

Infix Expression is: A operand B

Potfix Expresion is: A B operand

As precedence is already given in quetion, following is generally followed associativity:

Precedence Operator Associativity
5 *, /, % Left to right
4 +, - Left to right
3 <, <=, >, >=, !=, == Left to right
2 not Left to right
1 and Left to right
0 or Left to right

Rules:

  1. scan infix expession left to right.
  2. whenever you see the operand, add it to postfix expression.
  3. whenever you see the operator, push it into the stack.
  4. if priority of operator is higher than operator present on top of stack, then push operator on the stack. If priority is low, pop the top of stack and it to prefix expression and add the low priority operator on to the stack.
  5. If operator is of same precedence to the operator on the top of stack, then there are two cases according to associativity: if associativity is Left to right then pop top of stack and add to postfix expression; if associativity is right to left then push operator on top of stack.
  6. if closing bracket is seen, pop operators one by one from top of the stack until you see opening bracket on the stack, and add to postfix expression.

now, for conversion: see the image

The postfix expression is:

19 4 + 2 * 32 2 * 1 + < 16 4 / 4 == AND

Postfix Evaluation

For evaluation, we use stack called, operand stack.

Rules:

  1. scan postfix expression left to right.
  2. whenever you se operand, push it into stack.
  3. whenever you see operator, pop 1 top operand, it is B; pop 2nd top operator, it is A. Then perform: A operator B
  4. push the result into top of stack

Now, for evaluation: see image


Related Solutions

Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at...
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at least 2 examples of relational expressions with enumeration types. Explain your examples
Introduction to java: Gambling Objectives: •Performing decisions using if and switch statements. •Using relational and logical...
Introduction to java: Gambling Objectives: •Performing decisions using if and switch statements. •Using relational and logical operators •Working with random numbers Write an application that simulates a gambling slot machine. In the application, perform the following tasks: 1. Prompt the player to enter the amount of money that the player is willing to gamble. 2. Create three random numbers in range from one to four to represent three of the following four objects (Apple, Orange, Cherry and Pineapple). 3. Compare...
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that...
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that sum up to 'sum' by inserting the operators between digits in 'number'. example for 'number=123456789' and 'sum = 0' Print the output using the terminal: Output should be exactly like this from 1 - 22 1 : +1+2-34-56+78+9=0 2 : +1-2-34+5+6+7+8+9=0 3 : +1-23-4-56-7+89=0 ... 12 : -1+2+34-5-6-7-8-9=0 13 : -1+23+4+56+7-89=0 14 : -1-2+34+56-78-9=0 ... 22 : -12-34+56+7-8-9=0
Show using a direct proof and logical operators the following set equality.A−BC= A∩B
Show using a direct proof and logical operators the following set equality.A−BC= A∩B
Using the descriptions below, write a scanner for numbers, symbols, comments, arithmetic operators, and parenthesis in...
Using the descriptions below, write a scanner for numbers, symbols, comments, arithmetic operators, and parenthesis in Racket or scheme. - a number is one or more digits | zero of more digits followed by a decimal point followed by one or more digits | one or more digits followed by a decimal point followed by zero or more digits - a symbol is      one or more characters from the set [_A-Za-z] followed by zero or more characters from the...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token in the expression is an operator, If the operator stack is empty or the priority of the operator is greater...
Check ambiguity, operators’ precedence and associativity ( 2) <E> → id | <E> - id |...
Check ambiguity, operators’ precedence and associativity ( 2) <E> → id | <E> - id | <E> * id
create an expression using appropriate logical operators (AND,OR or NOT) 1. You can have only one...
create an expression using appropriate logical operators (AND,OR or NOT) 1. You can have only one meal a. pizza b. spaghetti 2. You need one of the following as proof of identity to get driver's license a. passport b. two of the following (a) birth certificate (b) ID CARD (c) Bill statement with your name and address
Your textbook mentions that relational operators can be used to compare letters and words, thanks to...
Your textbook mentions that relational operators can be used to compare letters and words, thanks to ASCII. This simple program will ask the user to enter two letters and then two words to see if we can really "sort" these entries alphabetically with relational operators. Prompts: This is a Letter and Word sorting program Enter two different letters separated by a space: [assume user inputs: b a] Enter two different words separated by a space: [assume user inputs: bob alex]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT