pathterminuspages/projects/aboutcontactabout me

Simple Calculator - Preface

ComCalc #1 :: 25-08-2018

Contents
 @Simple Calculator - Preface
 -Simple Calculator - Syntax
 -Simple Calculator - Generating parser
 -Simple Calculator - The application

This is actually a project I started when I discovered parse generators. But I shifted to other projects, and now I have decided to finish this. Let's build a calculator. First a history tour.

I started programming (started again) maybe 6 years ago. I borrowed a pamphlet about writing PHP from the library. By reading this I learned the basics of MySql in PHP. And I learned the basics of web-forms. With this I created my first application: a PHP calculator. Most aspiring programmers probably start their career building a calculator. Mine, as can be seen, isn't very useful. You can only work two operands which means that in order to calculate more complex expressions you have to write the result down.

I like to look back at things I have programmad. To see that I have evolved. That PHP calculator is, probably as most beginner applicatoins, constructed purely with a chain of statements. See for yourself:

function forsk($a, $b) { if($a > $b) { print $a - $b; } elseif($a < $b) { print $b - $a; } elseif($a == $b) { print $b - $a; } } if ($radio == 'add') { echo "$a + $b = "; echo $a + $b; } elseif ($radio == 'sub') { echo "$a - $b = "; echo $a - $b; } elseif ($radio == 'multi') { echo "$a * $b = " . $a * $b; } elseif ($radio == 'div') { echo "$a / $b = " . $a / $b; } elseif ($radio == 'pow') { echo "$a ^ $b = " . pow($a ,$b); } elseif($radio == 'sqrt') { echo "sqrt($a) = " . sqrt($a); } elseif ($radio == 'ln') { echo "ln($a) = " . log($a); } elseif ($radio == 'log') { echo "log($a) = " . log10($a); } elseif ($radio == 'pi') { echo "pi = " . pi(); } elseif ($radio == 'bin') { echo "binary($a) = " . decbin($a); } elseif ($radio == 'forsk') { echo "Forskellen mellem $a og $b = "; echo forsk($a, $b); }

Not purely, I see now. I have included a function. Beautiful. I had forgotten that you can put variables inside strings in PHP. As you can with bash. The simplicity comes from the fact that any programming language I have ever heard of, can do arithmetic. But I must admit: even then I wasn't very impressed. I wanted to build a calculator that functioned like my TI89 calculator

My TI89 calculator - Terminus is inspired by the interface of this

I have build various calculators since. But since I have started studying Computer Science, I have been introduced to parsers, and using one of those a calculator is straight forward to build.

CommentsGuest Name:Comment: