<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jords</title>
	<atom:link href="http://www.shadowservices.biz/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shadowservices.biz/blog</link>
	<description></description>
	<lastBuildDate>Sat, 10 Oct 2009 21:28:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rpn Calculator</title>
		<link>http://www.shadowservices.biz/blog/2009/10/rpn-calculator/</link>
		<comments>http://www.shadowservices.biz/blog/2009/10/rpn-calculator/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 08:09:10 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[hp]]></category>
		<category><![CDATA[rpn]]></category>

		<guid isPermaLink="false">http://www.shadowservices.biz/blog/?p=152</guid>
		<description><![CDATA[This is a reverse polish notation (ie stack-based) calculator program written in ANSI C. It uses a dynamically allocated stack, so it can hold any amount of data in the stack up to the memory limits of the computer you are running on. It is currently using double-precision floating point operations although I might write [...]]]></description>
			<content:encoded><![CDATA[<p>This is a reverse polish notation (ie stack-based) calculator program written in ANSI C. It uses a dynamically allocated stack, so it can hold any amount of data in the stack up to the memory limits of the computer you are running on. It is currently using double-precision floating point operations although I might write a version using GMP (GNU multi-precision library) instead which will be much more accurate.</p>
<p><span id="more-152"></span></p>
<p><strong>Overview:</strong></p>
<p>The source code is split up into several files &#8211; here is a description of the functionality handled by each of these files:</p>
<p>rpn.h &#8211; Header file with function prototypes  and a  few #DEFINES.</p>
<p>main.c &#8211; Contains the main loop, which reads input from the keyboard, does the requested operation, and then loops.</p>
<p>stack.c &#8211; This is my implementation of a LIFO (last-in-first-out) stack. It provides functions for pushing data onto the stack and poping data off it. It also handles  growing, shrinking, printing and duplicating  the stack. Internally the stack is represented by a dynamically allocated array, and a stack pointer to store the current size of the stack.</p>
<p>getop.c &#8211; This file contains the getop() function, which reads data from the keyboard, and interprets it. It is called from the main loop.</p>
<p>inputbuf.c &#8211; This file handles input buffering. Since this program will use the getche() function if available to read unbuffered input from the keyboard (so operations happen immediately without needing to push enter), we need to provide our own input buffer for situations where a operator is entered as the terminating character for a entered number.</p>
<p>help.c &#8211; This function provides a function to print the help for the program.</p>
<p>util.c &#8211; A few miscellaneous functions &#8211; clear() for clearing the screen,  radToDegrees() and degreesToRad() for converting angles.</p>
<p>This program provides access to much of the standard libraries&#8217; math functions, like sin, cos, tan and sqrt. </p>
<p>The biggest challenge in writing this was in the input handling.  I wanted operations to happen immediately as on my Hp 50g, rather than having to be typed and then enter pushed to get the calculator to read the input. This is not provided for by the ANSI standard, so my program currently detects if it is being compiled on windows, and if it is, uses the getche() function which does allow for unbuffered input. Also handling multiple character function names was tricky, since when the user types the first letter of, say &#8217;sin&#8217; you don&#8217;t know weather they meant to swap the elements on the stack, or are wanting to do the sine function. So, all multiple-character functions must be entered starting with a exclamation mark.</p>
<p>Another thing not provided for in the ANSI standard is clearing the screen &#8211; so I have written a  clear() command which tries to clear the screen by running the cls or clear commands through system(). This will work for windows and unix platforms, which is fairly comprehensive but not ideal.</p>
<p><strong>Screenshot:</strong><br />
<a href="http://www.shadowservices.biz/blog/wp-content/uploads/2009/10/rpn.jpg"><img src="http://www.shadowservices.biz/blog/wp-content/uploads/2009/10/rpn.jpg" alt="Reverse Polish notation screenshot" title="Reverse Polish notation screenshot" width="677" height="594" class="alignnone size-full wp-image-163" /></a></p>
<p><strong>Documentation: </strong></p>
<blockquote><p>Operators: +, &#8211; , * , /<br />
% &#8211; Modulus. Works on floating points.<br />
Enter &#8211; duplicate item at bottom of stack<br />
n &#8211; Make number on bottom of stack negative<br />
b or backspace &#8211; Remove number on bottom of stack<br />
c &#8211; Clear stack<br />
w &#8211; Print current  size of stack array<br />
s &#8211; Swap bottom 2 elements<br />
Math Functions:<br />
!sin &#8211; Sine (Radians)<br />
!cos &#8211; Cosine (Radians)<br />
!tan &#8211; Tangent (Radians)<br />
Add an a to the beginning of trigonometric function for the inverse.<br />
Add an d to the end to do a trigonometric function in degrees.<br />
^ &#8211; Power function.<br />
sqrt &#8211; Square Root.<br />
!! &#8211; Factorial.<br />
q &#8211; Quit</p></blockquote>
<p><strong>Abridged source code: (the dotted lines are where repetitive code has been removed)</strong><br />
main.c:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;rpn.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> type<span style="color: #339933;">;</span>
	<span style="color: #993333;">double</span> op1<span style="color: #339933;">;</span>
	<span style="color: #993333;">double</span> op2<span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> s<span style="color: #009900;">&#91;</span>MAXOP<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	allocateStack<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Type h and press enter for help.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span> getop<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">case</span> NUMBER<span style="color: #339933;">:</span>
				push <span style="color: #009900;">&#40;</span>atof<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> FUNCTION<span style="color: #339933;">:</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;sin&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					push<span style="color: #009900;">&#40;</span>sin<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;cos&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					push<span style="color: #009900;">&#40;</span>cos<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;tan&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					push<span style="color: #009900;">&#40;</span>tan<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			        ..............................
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;!&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					op1 <span style="color: #339933;">=</span> floor<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>op2<span style="color: #339933;">=</span>op1<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> op2<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> op2<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						op1 <span style="color: #339933;">=</span> op1 <span style="color: #339933;">*</span> op2<span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					push<span style="color: #009900;">&#40;</span>op1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'+'</span><span style="color: #339933;">:</span>
				push<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'*'</span><span style="color: #339933;">:</span>
				push<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'-'</span><span style="color: #339933;">:</span>
				op2 <span style="color: #339933;">=</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				push<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> op2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'^'</span><span style="color: #339933;">:</span>
				op1 <span style="color: #339933;">=</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				op2 <span style="color: #339933;">=</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				push<span style="color: #009900;">&#40;</span>pow<span style="color: #009900;">&#40;</span>op2<span style="color: #339933;">,</span>op1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'/'</span><span style="color: #339933;">:</span>
				op2<span style="color: #339933;">=</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>op2 <span style="color: #339933;">!=</span> <span style="color:#800080;">0.0</span><span style="color: #009900;">&#41;</span>
					push<span style="color: #009900;">&#40;</span>pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>op2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">else</span>
					<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Zero devisor error, yo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'n'</span><span style="color: #339933;">:</span>
				push<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">-</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'b'</span><span style="color: #339933;">:</span>
				pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'%'</span><span style="color: #339933;">:</span>
				op1 <span style="color: #339933;">=</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				op2 <span style="color: #339933;">=</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				push<span style="color: #009900;">&#40;</span>op2<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>op2<span style="color: #339933;">/</span> op1<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>op1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">' '</span><span style="color: #339933;">:</span>
				dup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'q'</span><span style="color: #339933;">:</span>
				exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'s'</span><span style="color: #339933;">:</span>
				<span style="color: #666666; font-style: italic;">//Swap first 2 elements</span>
				op1 <span style="color: #339933;">=</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				op2 <span style="color: #339933;">=</span> pop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				push<span style="color: #009900;">&#40;</span>op1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				push<span style="color: #009900;">&#40;</span>op2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'w'</span><span style="color: #339933;">:</span>
				<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Stack Size: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> getStackSize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'c'</span><span style="color: #339933;">:</span>
				clearStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				printStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'f'</span><span style="color: #339933;">:</span>
				setClear<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'g'</span><span style="color: #339933;">:</span>
				setClear<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'h'</span><span style="color: #339933;">:</span>
				printHelp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
				<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;unknown command %s.&quot;</span><span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>stack.c:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;rpn.h&quot;</span>
<span style="color: #993333;">void</span> freeStack<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> stackPosition <span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> stackSize <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">double</span><span style="color: #339933;">*</span> stack<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> doClear <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> print <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> allocateStack<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">double</span><span style="color: #339933;">*</span> stacknew<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stackSize <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//We are allocating the initial stack.</span>
			stacknew <span style="color: #339933;">=</span> malloc<span style="color: #009900;">&#40;</span>size <span style="color: #339933;">*</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//We are growing/shrinking the stack</span>
		stacknew <span style="color: #339933;">=</span> realloc<span style="color: #009900;">&#40;</span>stack<span style="color: #339933;">,</span> size <span style="color: #339933;">*</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stackPosition <span style="color: #339933;">&gt;</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			stackPosition <span style="color: #339933;">=</span> size<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>stacknew <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>stackSize <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			free<span style="color: #009900;">&#40;</span>stack<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error: Could not allocate memory for stack of size %d&quot;</span><span style="color: #339933;">,</span>size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Memory allocation succeded</span>
		stack <span style="color: #339933;">=</span> stacknew<span style="color: #339933;">;</span>
		stackSize <span style="color: #339933;">=</span> size<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* push value onto stack */</span>
<span style="color: #993333;">void</span> push<span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> newValue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stackPosition  <span style="color: #339933;">&lt;</span> stackSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
                  stack<span style="color: #009900;">&#91;</span>stackPosition<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> newValue<span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> 
                  allocateStack<span style="color: #009900;">&#40;</span>stackSize<span style="color: #339933;">+</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  stack<span style="color: #009900;">&#91;</span>stackPosition<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> newValue<span style="color: #339933;">;</span> 
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">double</span> pop<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 	<span style="color: #993333;">double</span> value<span style="color: #339933;">;</span> 	
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stackPosition  <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">// If there is data on the stack.</span>
		stackPosition<span style="color: #339933;">--;</span>
		value <span style="color: #339933;">=</span> stack<span style="color: #009900;">&#91;</span>stackPosition<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		freeStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;error : pop() called, but stack empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">void</span> dup<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>stackPosition <span style="color: #339933;">&gt;</span>  <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">// If there is data on the stack.</span>
		push<span style="color: #009900;">&#40;</span>stack<span style="color: #009900;">&#91;</span>stackPosition<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">void</span> clearStack<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	stackPosition <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	freeStack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">int</span> getStackSize<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> stackSize<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">void</span> setClear<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> newDoClear<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	doClear <span style="color: #339933;">=</span> newDoClear<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">void</span> setPrint<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> newPrint<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	print <span style="color: #339933;">=</span> newPrint<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">void</span> freeStack<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>stackSize <span style="color: #339933;">-</span> stackPosition<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">15</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Reduce the size of the stack to free some memory.</span>
		allocateStack<span style="color: #009900;">&#40;</span>stackPosition<span style="color: #339933;">+</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">void</span> printStack<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> i<span style="color: #339933;">,</span>position<span style="color: #339933;">=</span>stackPosition<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>doClear<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		clear<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>print<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i</pre></div></div>

<p>getop.c:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;rpn.h&quot;</span>
<span style="color: #993333;">int</span> getop<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> s<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>c<span style="color: #339933;">,</span>d<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> c <span style="color: #339933;">=</span> getchb<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">' '</span> <span style="color: #339933;">||</span> c <span style="color: #339933;">==</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #339933;">;</span>
	s<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isdigit<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> c <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'.'</span> <span style="color: #339933;">&amp;&amp;</span> c <span style="color: #339933;">!=</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">&amp;&amp;</span> c <span style="color: #339933;">!=</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">==</span> <span style="color: #0000dd;">33</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			i<span style="color: #339933;">=-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span><span style="color: #339933;">++</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> d <span style="color: #339933;">=</span>  getchb<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>d <span style="color: #339933;">!=</span> <span style="color: #0000dd;">13</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #339933;">;</span>
			ungetchb<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> FUNCTION<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> c<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">==</span> <span style="color: #0000dd;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #ff0000;">' '</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">==</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #ff0000;">'b'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>isdigit<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>isdigit<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span><span style="color: #339933;">++</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> c <span style="color: #339933;">=</span> getchb<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">==</span> <span style="color: #ff0000;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>isdigit<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span><span style="color: #339933;">++</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> c <span style="color: #339933;">=</span> getchb<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		ungetchb<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> NUMBER<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>inputbuf.c:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;rpn.h&quot;</span>
<span style="color: #666666; font-style: italic;">// If using the VC++ compiler, use unbuffered input. Otherwise, use normal buffered input.</span>
<span style="color: #666666; font-style: italic;">// Ideally should write some ncurses code to allow for unbuffered input on unix platforms.</span>
<span style="color: #339933;">#if defined(_WIN32)</span>
   <span style="color: #666666; font-style: italic;">// Use VC++ input.</span>
	<span style="color: #339933;">#define INPUT_FUNCTION getche()</span>
<span style="color: #339933;">#else</span>
	<span style="color: #339933;">#define INPUT_FUNCTION getchar()</span>
<span style="color: #339933;">#endif</span>
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> buffer<span style="color: #009900;">&#91;</span>BUFSIZE<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> bufPos<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> getchb<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>bufPos <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> buffer<span style="color: #009900;">&#91;</span><span style="color: #339933;">--</span>bufPos<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> INPUT_FUNCTION<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">void</span> ungetchb<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>bufPos <span style="color: #339933;">&lt;</span> BUFSIZE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>input <span style="color: #339933;">!=</span> <span style="color: #0000dd;">13</span> <span style="color: #339933;">&amp;&amp;</span> input <span style="color: #339933;">!=</span> <span style="color: #0000dd;">46</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			buffer<span style="color: #009900;">&#91;</span>bufPos<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> input<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Input buffer overrun... Not cool, guys!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>util.c:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;rpn.h&quot;</span>
<span style="color: #993333;">void</span> clear<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Clear the screen. Hopefully this works on every os, not ANSI compliant.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>system<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cls&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> system<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;clear&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">double</span> radToDegrees<span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> radians<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>radians<span style="color: #339933;">/</span>PI<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #0000dd;">180</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #993333;">double</span> degreesToRad<span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> degrees<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>degrees<span style="color: #339933;">/</span><span style="color: #0000dd;">180</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>PI<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Code download: I have posted a .zip file of this code along with a visual studio project that can be used to compile it <a href="http://www.shadowservices.biz/blog/wp-content/uploads/2009/10/rpncalc.zip">here</a>. If you just want to run it and your on windows, there is a binary in the Release directory that should work <img src='http://www.shadowservices.biz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><strong>Acknowledgements:</strong></p>
<p>The basic idea and some of the basic code for this came from Kernigan and Ritchie 2nd edition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2009/10/rpn-calculator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Wordpress 2.7!!!</title>
		<link>http://www.shadowservices.biz/blog/2008/12/wordpress-27/</link>
		<comments>http://www.shadowservices.biz/blog/2008/12/wordpress-27/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 07:32:33 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.shadowservices.biz/blog/?p=149</guid>
		<description><![CDATA[Is awesome.
and yes i am WAY behind on updating this, basically ive finished school now and going to uni to do software engineering next year, cant really be bothered writing much more now
]]></description>
			<content:encoded><![CDATA[<p>Is awesome.<br />
and yes i am WAY behind on updating this, basically ive finished school now and going to uni to do software engineering next year, cant really be bothered writing much more now</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2008/12/wordpress-27/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Server (Again!)</title>
		<link>http://www.shadowservices.biz/blog/2008/03/new-server-again/</link>
		<comments>http://www.shadowservices.biz/blog/2008/03/new-server-again/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 20:39:42 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.shadowservices.biz/blog/?p=148</guid>
		<description><![CDATA[This site is now on slicehost! Got a slice yesterday and have been setting it up. Will use it for all my sites from now on, unlimited configurability and its wicked fast even though i&#8217;ve only got the 256mb slice at the moment.
]]></description>
			<content:encoded><![CDATA[<p>This site is now on slicehost! Got a slice yesterday and have been setting it up. Will use it for all my sites from now on, unlimited configurability and its wicked fast even though i&#8217;ve only got the 256mb slice at the moment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2008/03/new-server-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Server</title>
		<link>http://www.shadowservices.biz/blog/2008/01/new-server/</link>
		<comments>http://www.shadowservices.biz/blog/2008/01/new-server/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 11:49:53 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.shadowservices.biz/blog/?p=147</guid>
		<description><![CDATA[Well I setup a new server that&#8217;s running at home a few days ago, and i have moved this site to it which will result in it actually being up most of the time.
I&#8217;ve also used zoneedit, which is really cool, to point www.shadowservices.biz to the server.
Holidays right now, back to school in a few [...]]]></description>
			<content:encoded><![CDATA[<p>Well I setup a new server that&#8217;s running at home a few days ago, and i have moved this site to it which will result in it actually being up most of the time.</p>
<p>I&#8217;ve also used zoneedit, which is really cool, to point www.shadowservices.biz to the server.</p>
<p>Holidays right now, back to school in a few weeks. Looking forward to it actually, just drifting and playing games atm&#8230;</p>
<p>P.S: Server is a Celeron 2.4ghz, 256mb ram. The ram is constraining really but the processor is nice and fast for a web server&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2008/01/new-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lol&#8230; Hav&#8217;nt posted in AAAGES</title>
		<link>http://www.shadowservices.biz/blog/2007/11/lol-havnt-posted-in-aaages/</link>
		<comments>http://www.shadowservices.biz/blog/2007/11/lol-havnt-posted-in-aaages/#comments</comments>
		<pubDate>Thu, 01 Nov 2007 22:47:49 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jordanthoms.no-ip.info/blog/?p=146</guid>
		<description><![CDATA[Ok:
1) I have a pretty good laptop now, spend ages lying in bed reading my rss feeds  
2) Half Way through my final exams, going pretty good.
3) Should really write more, but can&#8217;t be bothered right now&#8230; got study to do :&#60;
]]></description>
			<content:encoded><![CDATA[<p>Ok:</p>
<p>1) I have a pretty good laptop now, spend ages lying in bed reading my rss feeds <img src='http://www.shadowservices.biz/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>2) Half Way through my final exams, going pretty good.</p>
<p>3) Should really write more, but can&#8217;t be bothered right now&#8230; got study to do :&lt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2007/11/lol-havnt-posted-in-aaages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I have a quad-core now.</title>
		<link>http://www.shadowservices.biz/blog/2007/06/i-have-a-quad-core-now/</link>
		<comments>http://www.shadowservices.biz/blog/2007/06/i-have-a-quad-core-now/#comments</comments>
		<pubDate>Tue, 05 Jun 2007 08:56:01 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jordanthoms.no-ip.info/blog/?p=144</guid>
		<description><![CDATA[Or at least, top seems to think so:
top &#8211; 20:49:15 up  2:57,  1 user,  load average: 2.36, 2.40, 2.29
Tasks: 124 total,   2 running, 122 sleeping,   0 stopped,   0 zombie
Cpu(s):  9.2%us,  2.2%sy, 88.1%ni,  0.0%id,  0.0%wa,  0.1%hi,  0.4%si,  0.0%st
Mem:   [...]]]></description>
			<content:encoded><![CDATA[<p>Or at least, top seems to think so:<br />
top &#8211; 20:49:15 up  2:57,  1 user,  load average: 2.36, 2.40, 2.29<br />
Tasks: 124 total,   2 running, 122 sleeping,   0 stopped,   0 zombie<br />
Cpu(s):  9.2%us,  2.2%sy, 88.1%ni,  0.0%id,  0.0%wa,  0.1%hi,  0.4%si,  0.0%st<br />
Mem:   2074032k total,  1804764k used,   269268k free,   101544k buffers<br />
Swap:  1004052k total,        0k used,  1004052k free,   985588k cached</p>
<p>  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND<br />
 6593 boinc     39  19 83216  63m 4556 R  179  3.1 302:33.14 wcg_hpf2_rosett<br />
 6594 boinc     34  19 63064  40m 3600 S  174  2.0 302:54.56 setiathome-5.12</p>
<p>Here&#8217;s a screenshot for authenticity:<br />
<a href="http://jords.servebeer.com/berylscreenshots/wtf400%25usageonadualcore.png">http://jords.servebeer.com/berylscreenshots/wtf400%25usageonadualcore.png</a><br />
Just ignore the opera playing in the background and my new beryl theme/background/kde theme <img src='http://www.shadowservices.biz/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2007/06/i-have-a-quad-core-now/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Photos of new computer!</title>
		<link>http://www.shadowservices.biz/blog/2007/04/photos-of-new-computer/</link>
		<comments>http://www.shadowservices.biz/blog/2007/04/photos-of-new-computer/#comments</comments>
		<pubDate>Thu, 19 Apr 2007 00:17:11 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Photography]]></category>

		<guid isPermaLink="false">http://jordanthoms.no-ip.info/blog/?p=141</guid>
		<description><![CDATA[Here they are, sorry about the bad lighting&#8230;:

]]></description>
			<content:encoded><![CDATA[<p>Here they are, sorry about the bad lighting&#8230;:</p>
<p><a href="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3325.jpg" title="Photo1"><img src="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3325.jpg" alt="Photo1" /></a><a href="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3327.jpg" title="Photo2"><img src="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3327.jpg" alt="Photo2" /></a><a href="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3328.jpg" title="Photo3"><img src="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3328.jpg" alt="Photo3" /></a><a href="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3329.jpg" title="Photo4"><img src="http://jordanthoms.no-ip.info/blog/wp-content/uploads/2007/04/_mg_3329.jpg" alt="Photo4" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2007/04/photos-of-new-computer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Computer!</title>
		<link>http://www.shadowservices.biz/blog/2007/04/new-computer-2/</link>
		<comments>http://www.shadowservices.biz/blog/2007/04/new-computer-2/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 04:17:24 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jordanthoms.no-ip.info/blog/?p=137</guid>
		<description><![CDATA[But I can&#8217;t be bothered writing anything about it yet, still setting it up.
Blah.
P.S: Linux drivers awsome, apart from the NIC but got the drivers working for it now &#8211; They&#8217;re not in the kernel yet.
]]></description>
			<content:encoded><![CDATA[<p>But I can&#8217;t be bothered writing anything about it yet, still setting it up.</p>
<p>Blah.</p>
<p>P.S: Linux drivers awsome, apart from the NIC but got the drivers working for it now &#8211; They&#8217;re not in the kernel yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2007/04/new-computer-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Just got back from tramp at Mt Taranaki&#8230;.</title>
		<link>http://www.shadowservices.biz/blog/2007/04/just-got-back-from-tramp-at-mt-taranaki/</link>
		<comments>http://www.shadowservices.biz/blog/2007/04/just-got-back-from-tramp-at-mt-taranaki/#comments</comments>
		<pubDate>Sat, 14 Apr 2007 09:12:48 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://jordanthoms.no-ip.info/blog/?p=136</guid>
		<description><![CDATA[Wednesday: Drive down from Auckland to North Egmont, Stayed at the Camphouse.
Thursday: VERY wet, lots of ice and snow blowing around and mostly into us.  Walked from North Egmont to Holly Hut. (3 hrs), but they were a interesting 3 hours!  Could&#8217;nt see any views though.
Friday: Planned to get going in the  [...]]]></description>
			<content:encoded><![CDATA[<p>Wednesday: Drive down from Auckland to North Egmont, Stayed at the Camphouse.</p>
<p>Thursday: VERY wet, lots of ice and snow blowing around and mostly into us.  Walked from North Egmont to Holly Hut. (3 hrs), but they were a interesting 3 hours!  Could&#8217;nt see any views though.</p>
<p>Friday: Planned to get going in the  morning, but little bits of cloud whistling around the hut at insane speeds &#8211; The whole hut would shake. Managed to do the 2 hour walk through the swamp and along some ridges in a break in the weather though, to Pouakai hut. Wind shooting around the hut with serious hail coming down the windows, so we cowered there for the rest of the day.</p>
<p>Saturday: STILL Blowing, so we changed our plans from the track completing the Pouakai  Circuit back to North Egmont to taking the Mangorei track straight down and got a ride to north egmont to pick up our car and get changed into some dry(!) clothes, and drive back up to Auckland.</p>
<p>Did&#8217;nt get much walking done and spent ages  in the huts barely able to go outside, but Thursday we did get frozen which always helps.</p>
<p>Note: Pictures are here: <a href="http://www.dubdubdub.org.nz/WALKS/TaranakiApr07/TaranakiApr07.html" target="_blank">http://www.dubdubdub.org.nz/WALKS/TaranakiApr07/TaranakiApr07.html</a></p>
<p>Should get my computer and my new Genius Ergo 525 2000dpi gaming mouse on Monday!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2007/04/just-got-back-from-tramp-at-mt-taranaki/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Close to getting my new computer?</title>
		<link>http://www.shadowservices.biz/blog/2007/04/close-to-getting-my-new-computer/</link>
		<comments>http://www.shadowservices.biz/blog/2007/04/close-to-getting-my-new-computer/#comments</comments>
		<pubDate>Sun, 08 Apr 2007 10:32:08 +0000</pubDate>
		<dc:creator>Jordan Thoms</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">http://jordanthoms.no-ip.info/blog/?p=135</guid>
		<description><![CDATA[There is&#8217;nt actually anything I&#8217;m waiting on stock-wise now, (except possibly a cheap nvidia pci-express gfx card) so If it was&#8217;nt for easter I might have had my computer by now! :]]></description>
			<content:encoded><![CDATA[<p>There is&#8217;nt actually anything I&#8217;m waiting on stock-wise now, (except possibly a cheap nvidia pci-express gfx card) so If it was&#8217;nt for easter I might have had my computer by now! :<</p>
<p>Hope I get it Tuesday or Wednesday&#8230; But Then I do have study to do :<</p>
<p>IN UNRELATED NEWS:</p>
<p>Running Beryl with SuperKaramba now. Pretty Awsome, I&#8217;ve setup a screenshot repo on http://jordanthoms.no-ip.info/berylscreenshots. (I&#8217;ve only just setup superkaramba, some of them have my older gkrellm system monitor on them)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shadowservices.biz/blog/2007/04/close-to-getting-my-new-computer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
