Assignment 5

Stack Calculator

by Jeremy Boss

HTML

Enter a number (Enter two numbers before operation)
<br/>
<br/>
<input type="textbox" id="val" />
<input type="button" value="Enter" onClick="Check()" />
<br/>
<br/> Operation
<br/>
<input type="button" value="+" onClick="add()" />
<input type="button" value="-" onClick="subtract()" />
<br/>
<input type="button" value="*" onClick="multiply()" />
<input type="button" value="/" onClick="divide()" />
<br/>
<br/>
<input type="button" value="Clear" onClick="clearStack()" />
<br/>
<br/>
<div id="output">

</div>

JavaScript

var aStack = new stackList();

function stackList() {
  this.head = null;
  this.tail = null;
  this.length = 0;
}

function Node() {
  this.val = null;
  this.next = null;
  this.prev = null;
  return this;
}

stackList.prototype.add = function(val) {
  var node = new Node();
  node.val = val;

  if (this.head === null) {
    this.head = node;
    this.length = 1;
    return node;
  }
  if (this.tail === null) {
    this.tail = this.head;
    this.head = node;
    this.tail.prev = this.head;
    this.head.next = this.tail;
    this.length = 2;
    return node;
  } else {
    alert(document.getElementById("output").innerHTML = "Only use of 2 numbers at a time! Choose an operation or clear");
  }
}


function addNode() {
  var val = parseFloat(document.getElementById("val").value);
  aStack.add(val);
  aStack.print();
}

stackList.prototype.print = function() {
  if (this.head === null) return "Stack is Empty!";
  var display = " ";
  var counter = 0;
  var node = this.head;
  while (node !== null) {
    counter = counter + 1;
    display += "Node " + counter + " Value = " + node.val + "</br>";
    node = node.next;
  }
  document.getElementById("output").innerHTML = display;
}

function Check() {
  var val = document.getElementById("val").value;
  if (isNaN(val)) {
    document.getElementById("output").innerHTML = "Invalid Input";
  } else {
    addNode();
  }
}

stackList.prototype.sum = function() {
  var x = this.head.val;
  var y = this.tail.val;
  var z = this.head.val + this.tail.val;
  aStack = new stackList();
  aStack.add(z);
}

stackList.prototype.difference = function() {
  var x = this.head.val;
  var y = this.tail.val;
  var z = this.tail.val - this.head.val;
  aStack = new stackList();
  aStack.add(z);
}

stackList.prototype.product = function() {
  var x = this.head.val;
  var y = this.tail.val;
  var z = this.head.val * this.tail.val;
  aStack = new stackList();
  aStack.add(z);
}

stackList.prototype.quotient = function() {
  var x =...