Line: 2 3 4 + + Result: 9 Line: 12 6 / 4 * 4 + Result: 12 Line: 2 2 3 3 * ** Result: 36 Line: 7 5 / 5 + 17 - Result: -11 Line: 4 3 - 5 6 + 4

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 10PE
icon
Related questions
Question
100%

VERY EASY RPN PROBLEM.

read the directions

please answer thank you


public class Node <T>
{

    private T data;
    private Node<T> next;
    
    public Node(T data, Node<T> next)
    {
        this.data = data;
        this.next = next;
    }
    
    public T getData()
    {
        return data;
    }
    
    public Node<T> getNext()
    {
        return next;
    }
    
}

 

import java.util.Stack;

public class StackArray<T> implements Stack<T> {
    private T [] elements;
    private int top;
    private int size;
    
    public StackArray (int size)
    {
        elements = (T[]) new Object[size];
        top = -1;
        this.size = size;
    }
    
    public boolean empty()
    {
        return top == -1;
    }
    
    public boolean full()
    {
        return top == size - 1;
    }
    
    public boolean push(T el)
    {
        if (full())
            return false;
        else
        {
            top++;
            elements[top] = el;
            return true;
        }
    }
    
    public T pop()
    {
        T el = elements[top];
        top--;
        return el; 
    }

}

 

// Generic Stack Reference Implementation

public class StackRef<T> implements Stack<T>
{

    private Node<T> top;
    
    public StackRef (int size)
    {
        top = null;
    }
    
    public StackRef()
    {
        top = null;
    }
    
    public boolean empty()
    {
        return top == null;
    }
    
    public boolean full()
    {
        return false;
    }
    
    public boolean push(T el)
    {
        Node<T> node = new Node<>(el, top);
        top = node;
        return true;
    }
    
    public T pop()
    {
        if (empty())
            return null;
        
        T el = top.getData();
        top = top.getNext();
        return el; 
    }
    
    
}

 

// Generic Stack Interface

public interface Stack<T>
{
    public boolean empty();
    public boolean full();
    public boolean push(T el);
    public T pop();
    
}

 

import java.util.Scanner;
import java.util.Stack;
import java.util.Set;
import java.io.*;
import java.util.*;

public class RPN {

    private static Stack<String> stack;
    private static FileReader file;
    private static Scanner in;
    private static Set<String> operators;
        
}

 

In class we developed code for a Generic Stack. The classes were Node, StackArray, StackRefas wel
as the Stack interface. All were generic and use the <T>type parameter. We also developed the
algorithm (logic) to process reverse polish notation (RPN) expressions, such as 23 + or 234 +* 2*
The operators +, -, and * are will always returninteger values gives integer expressions. Your / and ^
operators should also return integer expressions. You are likely to use the Math.pow method to
implement the ^ operator. However, be aware that it returns a result of type double.
Use the Stack interface and the Node and StackRef classes in a new project called RPN. You will add
a client program to read the data file below which achieves the output as specified below. You will need
to determine a stack parameter<T>to use. Since you are reading a file (line by line), you will readeach
line as a String. You will need to break up that line into a series of tokens. The String split method is
an easyway to create anarray of String tokens. For example, after reading the line "12 6/4 4
split will produce anarray of 7 String elements (7 tokens). For each token you will need to determine
if it is an operator or an operand, and act according to your RPN processing algorithm. Todetermine if
the token is an operand, you may want to create a Set<String>of all possible operators, then use the
contains method to determine if the token is an operator. If you determine that the token is an operand
(integer), you will want to push it on the stack. Since the token is always a String, you can push the
operand as a String (on your Stack<String>) or convert it to an integer value using the
Integer.parseInt()method and push the operand as an int (on your Stack<Integer>). Whichever
Stack parameter <T> you use, you will need to convert the String-stored integer value to an int-
before pushing or after popping it off.
Create a project and demonstrate that you can process a file of RPN expressions. All lines in this sample
Data File are correct. You do not have to try to catch bad input in this lab.
Data File
234 ++
12 6/4 * 4 +
2233 ***
75/5+ 17 -
43 -5 6+ 4 * -
11+3 * 4 - 10 ^
Transcribed Image Text:In class we developed code for a Generic Stack. The classes were Node, StackArray, StackRefas wel as the Stack interface. All were generic and use the <T>type parameter. We also developed the algorithm (logic) to process reverse polish notation (RPN) expressions, such as 23 + or 234 +* 2* The operators +, -, and * are will always returninteger values gives integer expressions. Your / and ^ operators should also return integer expressions. You are likely to use the Math.pow method to implement the ^ operator. However, be aware that it returns a result of type double. Use the Stack interface and the Node and StackRef classes in a new project called RPN. You will add a client program to read the data file below which achieves the output as specified below. You will need to determine a stack parameter<T>to use. Since you are reading a file (line by line), you will readeach line as a String. You will need to break up that line into a series of tokens. The String split method is an easyway to create anarray of String tokens. For example, after reading the line "12 6/4 4 split will produce anarray of 7 String elements (7 tokens). For each token you will need to determine if it is an operator or an operand, and act according to your RPN processing algorithm. Todetermine if the token is an operand, you may want to create a Set<String>of all possible operators, then use the contains method to determine if the token is an operator. If you determine that the token is an operand (integer), you will want to push it on the stack. Since the token is always a String, you can push the operand as a String (on your Stack<String>) or convert it to an integer value using the Integer.parseInt()method and push the operand as an int (on your Stack<Integer>). Whichever Stack parameter <T> you use, you will need to convert the String-stored integer value to an int- before pushing or after popping it off. Create a project and demonstrate that you can process a file of RPN expressions. All lines in this sample Data File are correct. You do not have to try to catch bad input in this lab. Data File 234 ++ 12 6/4 * 4 + 2233 *** 75/5+ 17 - 43 -5 6+ 4 * - 11+3 * 4 - 10 ^
Program Output
Line: 2 3 4 + +
Result: 9
Line: 12 6 / 4 * 4 +
Result: 12
Line: 2 2 3 3 ** *
Result: 36
Line: 7 5 / 5 + 17 -
Result: -11
Line: 4 3 - 5 6 + 4 * -
Result: -43
Line: 1 1 + 3 * 4 - 10 ^
Result: 1024
Transcribed Image Text:Program Output Line: 2 3 4 + + Result: 9 Line: 12 6 / 4 * 4 + Result: 12 Line: 2 2 3 3 ** * Result: 36 Line: 7 5 / 5 + 17 - Result: -11 Line: 4 3 - 5 6 + 4 * - Result: -43 Line: 1 1 + 3 * 4 - 10 ^ Result: 1024
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 4 images

Blurred answer
Knowledge Booster
Public key encryption
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning