<html>
<head>
<h2>
Juan Alban Franco
</h2>
<h4>
Assignment 5
</h4>
</head>
<body>
Input into calculator
<br>
<input type = textbox id = input_txt readonly >
<br>
<br>
<input type=button id="Add" value="Add to Stack" onclick="addtostack()">
<br>
================
<br>
<input type = button value = "1" id = "One" onclick = calculator(1) >
<input type = button value = "2" id = "two" onclick = calculator(2) >
<input type = button value = "3" id = "three" onclick = calculator(3) >
<input type = button value = "-" id = "minus" onclick = calculate(1) >
<br><br>
<input type = button value = "4" id = "four" onclick = calculator(4) >
<input type = button value = "5" id = "five" onclick = calculator(5) >
<input type = button value = "6" id = "six" onclick = calculator(6) >
<input type = button value = "+" id = "add" onclick = calculate(2)>
<br><br>
<input type = button value = "7" id = "seven" onclick = calculator(7) >
<input type = button value = "8" id = "eight" onclick = calculator(8) >
<input type = button value = "9" id = "nine" onclick = calculator(9) >
<input type = button value = "x" id = "multi" onclick = calculate(3) >
<br><br>
<input type = button value = "Ce" id = "clear" onclick = Clear() >
<input type = button value = "0" id = "zero" onclick = calculator(0) >
<input type = button value = "Ă·" id = "divide" onclick = calculate(4)>
<br>
================
Answer so far:
<p id = test></p>
</body>
</html>
JavaScript
var stack = new Stack();
var number = "";
//stack definition
function Stack() {
this.head = null;
this.tail = null;
this.length = 0;
}
// node definition
function Node() {
this.content = null;
this.next = null;
this.prev = null;
}
//resets the list to null and clears answer text.
Stack.prototype.clear = function() {
this.head = null;
this.tail = null;
this.lenght = 0;
document.getElementById("test").innerHTML = " ";
}
// pushes values into the stack.
Stack.prototype.push = function(data) {
var node = new Node();
node.content = data;
console.log(node);
if (this.tail == null) {
this.length = 0;
this.head = node;
this.tail = node;
this.length++;
console.log(this);
} else {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
console.log(this);
return this;
}
}
//pops out nodes from the stack.
Stack.prototype.pop = function(data) {
if (this.head == null) {
alert("Stack is currently empty, there is nothing to pop");
return null;
}
// checks to see if the selected nonde to pop is the last one in the stack. resets head and tail to point to null until an push() is called.
else if (this.tail === this.head) {
var val = this.head.content;
this.length = 1;
this.head = null;
this.tail = null;
return val;
} else {
var holder_node = this.tail;
this.tail = this.tail.prev;
this.tail.next = null;
this.length--;
return holder_node.content;
}
}
// operation part of the calculator.
Stack.prototype.calculate = function(operator) {
var traveler = new Node();
var s = [];
var i = 0;
var answer = 0;
// this is just a cool name!
traveler = this.tail;
while (traveler) {
s[i] = this.pop();
i++;
traveler = traveler.prev;
}
if (operator == 3) {
while ((i - 1) >= 0) {
if (i == s.length) {
answer = s[i - 1];
} else {
answer = answer * s[i - 1];
}
i--;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.