Trying to make a Reverse Polish Calculator with a double linked list but I am so
ID: 3757186 • Letter: T
Question
Trying to make a Reverse Polish Calculator with a double linked list but I am so lost and nothing will display. What I am trying to do is have user input value into a linked list and display the number while doing the calculate function.
HTML
RPN Calculator
<input type="textbox" id="value" value=""/>
<br />
<br />
<input type='button' id = "enterbtn" value="Calculate"/>
<br />
<br />
<div id="output">
</div>
Javascript
function LinkedList() {
this.head = null;
this.tail = null
this.length = 0
}
function Node(_item) {
this.prev = null;
this.next = null;
this.item = _item;
}
LinkedList.prototype.add = function(_data) {
var node = new Node();
node.item = _data
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.previous = this.tail;
this.tail.next = node;
this.tail = node;
}
this.values++;
}
var output = document.getElementById("output").innerHTML;
var list = new LinkedList();
function enter() {
var input = document.getElementById(" value").value;
list.add(input);
if(input == '+' || input == '-' || input == '*' || input == '/') {
var top = stack.pop();
if(top != undefined)
{
var top2 = stack.pop();
if(top2 != undefined)
{
var answer = 0;
switch(input)
{
case '+':
answer = parseInt(top) + parseInt(top2);
break;
case '-':
answer = parseInt(top) - parseInt(top2);
break;
case '*':
answer = parseInt(top) * parseInt(top2);
break;
case '/':
answer = parseInt(top) / parseInt(top2);
break;
}
}
else
{
stack.push(top);
}
}
stack.push(ans);
}
else if(isNaN(parseFloat(input)))
{
}
else
{
stack.push(input);
}
}
}
);
Explanation / Answer
import java.util.Scanner;
public class ReversePolish
{
public static final String[] OPS = { "+", "-", "*", "/" };
public static boolean isOp(String s)
{
for(String op : OPS)
if(op.equals(s)) return true;
return false;
}
public static Double operation(String op, Double num1, Double num2)
{
if(op.equals("+")) return num1+num2;
else if(op.equals("-")) return num2-num1;
else if(op.equals("*")) return num1*num2;
else if(op.equals("/")) return num2/num1;
else System.out.println("Invalid operator");
return -999.999;
}
public static Double evaluate(String[] input)
{
LinkedList<Double> stack = new LinkedList<Double>();
for(String s : input)
{
if(isOp(s))
{
double num1 = stack.get(1);
double num2 = stack.get(2);
stack.remove(1);
stack.remove(2);
stack.add(operation(s, num1, num2));
}
else
stack.add(Double.parseDouble(s));
}
return stack.get(1);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter numbers and operations: ");
String in = sc.nextLine();
System.out.println("Result: " + evaluate(in.split(" ")));
sc.close();
}
}
javascript
<html>
<head>
<title>RPN Calculator</title>
<style type="text/css">
body {
color: white;
background-color: blue;
font-weight: bold;
font-size: 24px;
text-align: center;
margin-top: 200px;
}
</style>
<script type="text/javascript">
function evaluate() {
var input = prompt("Please enter your input string Examples of input strings: 1. 10 4 5 + * 2. 10 4 5 + * 2 + 3. 10 8 *");
var values = input.split(" ");
var array = new Array();
for (i in values) {
if (values[i] != "+" && values[i] != "*" && values[i] != "-" && values[i] != "/") {
array.push(parseInt(values[i]));
} else {
var operator = values[i];
var val2 = array.pop();
var val1 = array.pop();
switch (operator) {
case "+":
array.push(eval("val1 + val2"));
break;
case "*":
array.push(eval("val1 * val2"));
break;
case "-":
array.push(eval("val1 - val2"));
break;
case "/":
array.push(eval("val1 / val2"));
break;
}
}
}
if (input == "" || input == null) {
document.writeln("Oops, based on your input we have nothing to calculate for you!");
} else {
document.writeln("Your RPN calculation is: ");
document.writeln(array + " with a starting input string of: " + input);
}
}
</script>
</head>
<body>
<h2>Online RPN Calculator</h2>
<script type="text/javascript">
evaluate();
</script>
</body>
</html>