Calculator

by Peyton Hessler

HTML

<div class="wrapper">
<H4>Enter at least two operands to manipulate and then an operation</H4>


<p>Operations available: min , ^ , - , * , + , /</p>
</div>

<div calss="textbox">
<input type="textbox" id="value" value="" />
</div>

<br/>
<input type="button" value="Add Node" onClick="AddToStack();" />
<br/>
<input type="button" value="+" onClick="Add">
<br/>
<input type="button" value="-" onClick="Minus">
<p id='output1'></p>
<p id='output2'></p>

CSS

.wrapper{
  text-align: center;
}

.textbox{
  background-color:#FFF;
  min-height:400px;
}

JavaScript

// In this code I am trying to make a stack calculator. It is how a calculator actually works within a computer. In those regards I mean that two numbers are placed within the stack and then an operator is used with those two numbers. Calculators the way they are usually used are fixed with post predocution of the code. Here I do not have it that way just to simply show how a stack works.

var Node = function(_content) {		// Here is where I create my node properties for the stack.
    this.next = null;
    this.last = null;
    this.content = _content;

  }

  var Stack = function() {		// Here is where I create the stack properties.
    this.bottom = null;
    this.top = null;
    
    this.push = function(_content) {		// Here is the push function
      
      if (this.bottom == null) {
        this.bottom = new Node(_content);
        this.top = this.bottom;
        return this;
      }
      
      var addedNode = new Node(_content);
      addedNode.last = this.top;   // pointer to previous node
      this.top.next = addedNode;   // current top points to new
      this.top = addedNode;        // which becomes new top
      return this;
    }

    this.pop = function() {		// Here is the pop function
      if (this.bottom == null) {
        alert("The Stack has less than 2 nodes, add more to the stack");
        return null;
      }
      // Case of one node
      if (this.bottom == this.top) {
      	var a = this.top.content;
      	this.bottom = null;
        this.top = null;
      return a;
      }
      
      // Now remove top Node
      var a = this.top.content;   // hold value for return
      this.top = this.top.last;
      this.top.next = null;
      
      return a;
    }

    this.toString = function() {		// Here is the function that will create the string shown on the sceen.
      var str = "";
      var node = this.bottom;

      while (node != null) {
        str += node.content + " | ";
        node = node.next;
      }
      return str;
    }
   ...