Creating a Calculator in VB.Net | Beginners
(This tutorial will include PseudoCode and actual code for better understanding)
Creating a Calculator in VB.Net has got to be one of the most simple logic-based application one could ever make in their time with VB.net. Logically speaking, a calculator’s sole task is to take one input (Number1) and either Add/Subtract/Multiply/Divide it by a second input (Number2). Now sure, there are more further tasks that could be done: SquareRoots, Squaring, and use of Parenthesis, but for now this Calculator will be just basic.
One thing I’ve noticed, which our calculator will address, is that most people who create these calculators to show off on Youtube, do so with one flaw. They can’t continue with their operation after utilizing a second Number.
For instance: They can do 1 + 4 = 5. Then hit clear 5 + 5 = 10. Hit clear 10 / 5 = 2. Hit clear 2 / 2 = 1.
From the example above, the point I’m trying to make is they have to hit clear to wipe out the values and reinput what the next operation will be. One guy even went as far as inputing the M+ M- and MRC (that’s Memory+ Memory- Memory Clear for those that never knew what the M’s were on a calculator) just so they could store their last output and use it in the new input.
So what we want to do is create it so that you could type in the first number, hit your operator, then second number, then again hit your operator and continue on until you hit Equals.
Purpose: Create a calculator that accepts a number, can +-/* it by a second number and output to Textbox. Also create ability to keep going with operation to allow 1+ 3 – 2 * 5 = 10.Â ORDER OF OPERATIONS WILL FOLLOW LEFT TO RIGHT, WILL ONLY CALCULATE TWO NUMBERS AT A TIME. (((1 + 3) – 2) * 5) IS HOW THE CALCULATOR WILL READ
Logical Design andÂ PseudoCode:
Well to do this we’re going to want to set up our design. Of course for this simple calculator we want an input and an output. Our input will be buttons that represent numbers 0-9, buttons that represent mathematical operators +-/*=, and an output for our results. For this we’re use a Textbox. Lastly, we’ll also want to have a way to clear out the values that were previously inputted. While normal calculators have a Clear and ClearEverything button, we’re just implementing a ClearEverything at this time.
Buttons (btn + LogicalName):
btnZero-btnNine when Clicked()
txtOutput.text = btnValue
btnAdd-btnMultply when Clicked()
if numSec isEmpty then
Set operator to “+” or “-” or “/” or “*”
numFirst = numFirst operator numSec
Set operatorÂ “+” or “-” or “/” or “*”
btnEquals when Clicked()
numFirst =Â numFirst operator numSec
txtOutput.text = numFirst