Assignment 5
by leiperte
HTML
<input type="textbox" id="input" value="Enter a number" />
<input type="button" value="Push to Stack" id="btnStack" onClick="btnStack()" />
<br />
<br /> Math Problem:
<div id="output">
</div>
JavaScript
//Assignemnt 5 Redo
var Node = function(input) {
this.next = null;
this.last = null;
this.input = input;
}
var Stack = function() {
this.bottom = null;
this.top = null;
this.push = function(input) {
var node = new Node();
node.input = input;
node.next = null;
if (this.bottom == null) {
this.bottom = new Node(input);
this.top = this.bottom;
return this;
}
var addedNode = new Node(input);
addedNode.last = this.top;
this.top.next = addedNode;
this.top = addedNode;
return this;
}
this.pop = function() {
if (this.bottom == null) {
//alert("The Stack is Empty");
return null;
}
if (this.bottom == this.top) {
this.top = this.bottom;
this.top.last = null;
return this;
}
var c = this.top;
this.top = this.top.last;
this.top.next = null;
return c;
}
this.toString = function() {
var str = "";
var node = this.bottom;
while (node != null) {
str += node.input + " : ";
node = node.next;
}
return str;
}
}
function btnStack(str) {
var input = document.getElementById("input").value;
if (!isNaN(input)) {
stack.push(input);
} else {
if (input == "+") {
var n1 = stack.pop();
var n2 = stack.pop();
var n1n = parseInt(n1.input);
var n2n = parseInt(n2.input);
var answer = parseInt(n1.input) + parseInt(n2.input);
stack.push(answer);
document.getElementById("output").innerHTML = n1n + " + " + n2n + " = " + answer;
} else if (input == "-") {
var n3 = stack.pop();
var n4 = stack.pop();
var n3n = parseInt(n3.input);
var n4n = parseInt(n4.input);
var answerm = parseInt(n4.input) - parseInt(n3.input);
stack.push(answerm);
document.getElementById("output").innerHTML = n4n + " - " + n3n + " = " + answerm;
} else if (input == "*") {
var n5 = stack.pop();
var n6 = stack.pop();
var n5n =...