Question

In: Computer Science

C++ Use BinaryNodeTree to solve the following questions in a program: 1) What is the value...

C++ Use BinaryNodeTree to solve the following questions in a program:

1) What is the value of the prefix expression +−∗ 235/↑ 2 3 4?

2) What is the postfix form of the expression ((x + y) ↑ 2) + ((x − 4)/3)?

3) What is the value of the postfix expression723 ∗ − 4 ↑ 9 3/+?

Solutions

Expert Solution

QUE : 1) What is the value of the prefix expression +−∗ 235/↑ 2 3 4?

Symbol Opnd 1 Opnd 2 Value Opndstack
4 4
3 4,3
2 4,3,2
2 3 8 4
4,8
/ 8 4 2
2
5 2,5
3 2,5,3
2 2,5,3,2
* 2 3 6 2,5
2,5,6
- 6 5 1 2
2,1
+ 1 2 3
VALUE = 3

VALUE For given prefix expression  +−∗ 235/↑ 2 3 4 is 3.

_______________________________________________________________________

QUE: 2) What is the postfix form of the expression ((x + y) ↑ 2) + ((x − 4)/3)?

ANS:

Input String Output Stack Operator Stack
((x + y) ↑ 2) + ((x − 4)/3) (
((x + y) ↑ 2) + ((x − 4)/3) ((
((x + y) ↑ 2) + ((x − 4)/3) x ((
((x + y) ↑ 2) + ((x − 4)/3) x ((
((x + y) ↑ 2) + ((x − 4)/3) x ((+
((x + y) ↑ 2) + ((x − 4)/3) x ((+
((x + y) ↑ 2) + ((x − 4)/3) x y ((+
((x + y) ↑ 2) + ((x − 4)/3) x y+ (
((x + y) ↑ 2) + ((x − 4)/3) x y+ (
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ (
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ (
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 (
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 +
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 +
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 +(
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 +((
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x +((
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x +((
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − +((
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − +((
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − 4 +((
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − 4 +(
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − 4 +(/
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − 43 +(/
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − 43/ +
((x + y) ↑ 2) + ((x − 4)/3) x y+ ↑ 2 x − 43/+

Que : 3) What is the value of the postfix expression723 ∗ − 4 ↑ 9 3/+?

ANS :

723 ∗ − 4 ↑ 9 3/+

The first character scanned is "723", which is an operand, so push it to the stack.

723

  

Stack Expression

___________________________________________________________________________________________

723 − 4 ↑ 9 3/+

The next character scanned is "", which is an operator, so pop its two operands from the stack. Pop 723 from the stack for the right operand and then pop undefined from the stack to make the left operand.

undefined ∗ 723 = 0

Stack Expression

Next, push the result of undefined ∗ 723 (0) to the stack.

0

  

Stack Expression

__________________________________________________________________________________________

723 ∗ 4 ↑ 9 3/+

The next character scanned is "", which is an operator, so pop its two operands from the stack. Pop 0 from the stack for the right operand and then pop undefined from the stack to make the left operand.

undefined − 0 = 0

Stack Expression

Next, push the result of undefined − 0 (0) to the stack.

0

  

Stack Expression

_______________________________________________________________________________________

723 ∗ − 4 ↑ 9 3/+

The next character scanned is "4", which is an operand, so push it to the stack.

4
0

  

Stack Expression

_______________________________________________________________________________________

723 ∗ − 4 9 3/+

The next character scanned is "", which is an operator, so pop its two operands from the stack. Pop 4 from the stack for the right operand and then pop 0 from the stack to make the left operand.

0 ↑ 4 = 0

Stack Expression

Next, push the result of 0 ↑ 4 (0) to the stack.

0

  

Stack Expression

_________________________________________________________________________________________

723 ∗ − 4 ↑ 9 3/+

The next character scanned is "9", which is an operand, so push it to the stack.

9
0

  

Stack Expression

_________________________________________________________________________________________

723 ∗ − 4 ↑ 9 3/+

The next character scanned is "3/+", which is an operator, so pop its two operands from the stack. Pop 9 from the stack for the right operand and then pop 0 from the stack to make the left operand.

0 3/+ 9 = 0

Stack Expression

Next, push the result of 0 3/+ 9 (0) to the stack.

0

  

Stack Expression

Since we are done scanning characters, the remaining element in the stack (0) becomes the result of the postfix evaluation.

Postfix notation: 723 ∗ − 4 ↑ 9 3/+
Result: 0


Related Solutions

(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Use the table below to answer the following questions: Present Value of 1 Factor Present Value...
Use the table below to answer the following questions: Present Value of 1 Factor Present Value of an Annuity of 1 Factor Period 1/2 Yr Full-Yr 1/2 Yr Full-Yr 1 0.9578 0.9174 0.9578 0.9174 2 0.9174 0.8417 1.8753 1.7591 3 0.8787 0.7722 2.7540 2.5313 4 0.8417 0.7084 3.5957 3.2397 5 0.8062 0.6499 4.4019 3.8897 6 0.7722 0.5963 5.1740 4.4859 Assumption: Required annual effective rate (EPR) of return is 9%. If an investment pays you $54,000 every 6 months for 3...
Use the information from the following Income Statement to solve for the questions at bottom of...
Use the information from the following Income Statement to solve for the questions at bottom of the page.      Complete the grey sections Income Statement Sales Revenue $2,250,000 Variable Costs Purchases $425,000 Direct labor $395,000 $820,000 $1,430,000 Fixed Costs Selling $175,000 Administrative $110,000 $285,000 The above is based on sales of 20,000 units. 1. Contribution Margin 2. Selling price per unit 3. Variable labor cost per unit 4. Variable purchases cost per unit 5. Breakeven point in units and dollars
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Please tell me how to solve the following questions a) to c) using a graph. In...
Please tell me how to solve the following questions a) to c) using a graph. In particular, please elaborate on c). Cellwave is a cellular phone company. Answer the following questions relating to its pricing policies: a) When Cellwave started, it sold to a group of homogeneous retail customers. Each person’s monthly demand for cell phone minutes was given by P = $2 - 0.02Q , where P = the price per minute and Q = the quantity of minutes...
Using R to solve these questions: 1.Consider the following dataset: fuel <- c(0.95, 0.52, 0.82, 0.89,...
Using R to solve these questions: 1.Consider the following dataset: fuel <- c(0.95, 0.52, 0.82, 0.89, 0.81) The numbers correspond to the amount of fuel burnt by a new type of high-efficiency engine under a randomised test load. A value of 1 corresponds to the same fuel efficiency as the old engine, values greater than one correspond to more fuel burned (hence lower efficiency) and values less than one correspond to greater efficiency. (a) One-sided or two-sided test? Justify. (b)...
Using R to solve these questions: 1.Consider the following dataset: fuel <- c(0.95, 0.52, 0.82, 0.89,...
Using R to solve these questions: 1.Consider the following dataset: fuel <- c(0.95, 0.52, 0.82, 0.89, 0.81) The numbers correspond to the amount of fuel burnt by a new type of high-efficiency engine under a randomised test load. A value of 1 corresponds to the same fuel efficiency as the old engine, values greater than one correspond to more fuel burned (hence lower efficiency) and values less than one correspond to greater efficiency. (a) One-sided or two-sided test? Justify. (b)...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT