Assignment 5

by Kristy Bond

HTML

<html>

  <body>
    <h1>
      RPN Push/ Pop Calculator
    </h1>

    <p>
      A stack should have at least two numbers for an operation to successfully occur. Reverse Polish notation is a mathematical notation in which operators follow their operands. So if you wanted to calculate 1 + 1 you would Stack 1 and stack 1 then Add.
    </p>
    <p>
      <input type='number' id='value' min='1' max='1000'>
      <input type='button' id='add' value='Add to Stack' onclick='PushNumber()'>
      <input type='button' id='remove' value='Pop/Remove from Stack' onclick='PopNumber()'>
    </p>
    <p>

      <input type='button' id='multiplication' value='Multiply ' onclick='Multiply()' disabled/>
      <input type='button' id='division' value='Divide ' onclick='Divide()' disabled/>
      <input type='button' id='addition' value='Add ' onclick='Add()' disabled/>
      <input type='button' id='subtraction' value='Subtract ' onclick='Subtract()' disabled/>
    </p>
    <p id='output'>
    </p>
  </body>

</html>

JavaScript

var string;
var Node = function(foo) {
  this.next = null;
  this.last = null;
  this.content = foo;
  this.length = 0;
}
var Stack = function() {
  this.head = null;
  this.top = null;
}

Stack.prototype.push = function(foo) {
  if (this.top == null) {
    this.top = new Node(foo);
    this.head = this.top;
    this.length = 1;
    return this;
  }
  var newNode = new Node(foo);
  newNode.last = this.head;
  this.head.next = newNode;
  this.head = newNode;
  Enable();
  this.length++;
  return this;
}

Stack.prototype.pop = function() {
  if (this.top == null) {
    alert(document.getElementById('output').innerHTML = "Please enter at least two numbers to calculate.");
    return null;
  } else if (this.top == this.head) {
    this.top = null;
    this.head = null;
    return this.head;
  }

  var A = this.head;
  this.head = this.head.last;
  this.head.next = null;
  return A;
}

Stack.prototype.toString = function() {
  string = "";
  var node = this.head;

  while (node != null) {
    string += node.content + "<br>";
    node = node.last;
  }
  return string;
}

var numberStack = new Stack();

function PushNumber() {
  var number = document.getElementById('value').value;
  numberStack.push(number);
  document.getElementById('output').innerHTML = numberStack.toString();
}

function PopNumber() {
  numberStack.pop();
  document.getElementById('output').innerHTML = numberStack.toString();
}

function Add() {
  var B = parseFloat(numberStack.head.last.content);
  var C = parseFloat(numberStack.head.content);
  var D = (B + C);
  numberStack.pop();
  numberStack.pop();
  numberStack.push(D);
  parseFloat(document.getElementById('output').innerHTML = "Result: " + numberStack.toString());
}

function Subtract() {
  var B = parseFloat(numberStack.head.last.content);
  var C = parseFloat(numberStack.head.content);
  var D = (B - C);
  numberStack.pop();
  numberStack.pop();
  numberStack.push(D);
  parseFloat(document.getElementById('output').innerHTML = "Result: " +...