Input: a sequence of numbers and operators (tokens), in RPN.
(Each number is just a single digit.)
The algorithm uses the Stack class to hold doubles
Algorithm:
For (each token in the sequence, from left to right)
if (the token is an number) push it on the stack as a double;
if (the token is an operator) {
pop the top two items from the stack (two doubles);
perform the operation on the two items;
push the result (a double) back on the stack;
}
Return the double that is on the stack. (Only one item should be there.)
Here are some hints and help with various things.
Double dWrap = new Double(3.14159265358979); double op2 = ((Double)(dWrap)).doubleValue();(Here the extra parens are needed because the cast (double) has precedence over the dot operator.)
char ch = '8'; Double dWrap = new Double((double)(ch - '0'));(Here ch holds the Ascii code for the character '8' (the code is 56), while '0' is the character zero (the code is 48). The difference is 8, the actual integer. This is then converted to a double) and that value inserted into a new Double.)
// Eval: evaluate RPN string
// single digits (treated as doubles), and operators (+, -, *, /, and ^)
import java.util.*;
public class Eval {
Stack stack; // java library stack of objects
public Eval() {
stack = new Stack();
}
// evaluateRPN: evaluate the RPN in an input string as doubles
public double evaluateRPN(String s) {
// insert code here to carry out the evaluation
// by implementing the algorithm above
// using stack to hold wrapped doubles
// given two doubles from the stack and an operator
// you can use the function below to produce a value
}
// evaluate: actually perform operator
private double evaluate(char ch, double op1, double op2) {
switch(ch) {
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '^': return Math.pow(op1, op2);
default: error(3); return 0;
}
}
// There is no need for a separate main class
public static void main(String[] args) {
GetNext getNext = new GetNext(); // input class
String s = getNext.getNextString(); // fetch string
System.out.println("Input: " + s); // print input string
Eval eval = new Eval(); // new evaluator
double res = eval.evaluateRPN(s); // produce result
System.out.println("Result: " + res); // print result
}
}
// GetNext: fetch next char or unsigned integer
import java.io.*;
public class GetNext {
private Reader in; // internal file name for input stream
// GetNext: constructor
public GetNext () {
in = new InputStreamReader(System.in);
}
// getNextChar: fetches next char
private char getNextChar() {
char ch = ' '; // = ' ' to keep compiler happy
try {
ch = (char)in.read();
} catch (IOException e) {
System.out.println("Exception reading character");
}
return ch;
}
// getNextString: fetch String
public String getNextString() {
String s ;
char ch = getNextChar();
while (ch == ' ' || ch == '\n')
ch = getNextChar();
s = "" + ch;
while ((ch = getNextChar()) != ' ' && ch != '\n')
s += ch;
return s;
}
}
234*+ Input: 234*+ Result: 14.023+4* Input: 23+4* Result: 20.0
23-4- Input: 23-4- Result: -5.0
234^^ Input: 234^^ Value: 2.4178516392292583E24
317153*111498*1+*/+/+/+/+ Input: 317153*111498*1+*/+/+/+/+ Value: 3.1415926530119025 (10 significant digits of pi)