TL;DR:
Here’s the code: GitHub: Simple Calculator
The video is down below.
What are we doing this time?
I’m glad you asked. We’re going to write a simple calculator app. Why you ask? Because it’s a good introduction to a lot of the tools within VS (Visual Studio) and it means that I can shorten the length of the other videos in the RSS Aggregator series. That’s kind of a win-win right?
Video time:
… and here’s the code.
It’s really not that bad, I promise
Ok, it’s a ~35 minute video, but there’s really nothing too difficult in there. What I really wanted to focus on in this one was navigating through the work space. I’ll be honest with you, once we get into the Web App stuff, you’ll probably want to MVC land and won’t need the WinForms stuff anyway. However, doing simple projects like this will make you much more comfortable within VS and it will help reduce the learning curve for MVC Web Apps.
You’ll notice that Visual Studio is, unsurprisingly, very visual when it comes to creating a desktop app. This makes building any sort of GUI (Graphical User Interface) really easy, once you figure out where everything is. For that part, just watch the video. I mean, I could tell you to move your mouse to the left-hand side of the screen, about 2/3rds of the way up, click on Toolbox, open All Windows Forms, click and drag the control over but that would get as confusing for me to write as it would for you to follow along.
The Meaty Bits:
The Operator class
class Operator { private char op; public Operator(char op) { this.op = op; } public double apply(double before, double after) { double b = before; switch (op) { case '-': b -= after; break; case '+': b += after; break; case '*': b *= after; break; case '/': b /= after; break; default: break; } return b; } }
The important part about this class is the apply
method. In a nutshell, all we are doing is getting the value of the number before we make the change, figuring out which operation we need to do, and then combining the before number with the after number using that operation. Using this method, you could actually create your own operators and do whatever you want. You would only have to add a case for the custom operator, add a button for it on the calculator GUI, and there you have it.
Haven’t seen a switch statement before? That’s ok! Here’s what it does:
//Looks like we need a switch statement for //a variable. In this case, op. switch (op) { //Is this the case where op = '-'? case '-': //Let's do this to the b variable then b -= after; //Ok, now we're done. Let's break //on through to the other side. break; //op wasn't '-'? Ok, is it a '+'? case '+': //Let's do this if it is: b += after; break; //so on and so forth: case '*': b *= after; break; case '/': b /= after; break; //Wait! None of our cases matched. //Ok, lets just break out by default then default: break; }
Make sense?
This one’s beef:
private void UpdateTextBox(string str, TextBox tb) { tb.Text = str; } private double ConvertToDouble(string str) { return Double.Parse(str); }
Why did I do it this way? Yes, both of these functions hardly do anything and aren’t necessary. However, if you wanted to add further functionality in regards to how the TextBox control is updated or how the numbers are converted, you only have to change these functions. This is an over simplified example of functional programming. I built functions to do a single, simple task and made it as good at that task as possible. I might cover functional programming in another post someday, but for the scope of this one, I figured I would scratch the surface.
Now the other white meat:
private string solve() { double before = numbers[0]; for (int i = 1; i < this.numbers.Count; i++) { before = ops[i - 1].apply(before, numbers[i]); } return before.ToString(); }
This is the function that brings it all together. I sort of explained it in the video but I’ll try to explain it a little better here. The thought process is that you have to start with the first number the user pushed in. Then, you need to loop through the rest of the numbers and apply the correct operator after each number. We start our increment at 1 because numbers[0] has already been spoken for. this.numbers.Count
is a built-in property of any List instance and it tells use how many numbers there are for us to count up. Because our operators DO start at ops[0] when it’s time to look at those, we have to account for that 1 increment offset at the beginning of the loop. If you wanted to test this, you could make the op variable inside your operator class public, and then call MessageBox.Show(before+"_"+ops[i-1].op +"_"+numbers[i]);
inside the loop to verify that the operators are getting applied in the correct order. At any rate, I would be happy to answer any other questions about this. Just post a comment somewhere, anywhere.
Final Thoughts on the Calculator:
Thanks for reading, and remember, this is a SIMPLE Calculator. There are still a couple of bugs in it and it could use quite a few more features, but hopefully this has given you enough information to dig in and implement some new things on your own. Challenges are fun right?