Building a calculator in C#, stuck on doing the math stuff.

Started by
49 comments, last by eFoDay 11 years, 9 months ago

[quote name='Oberon_Command' timestamp='1341033093' post='4954206']
[quote name='Bill Fountaine' timestamp='1341032033' post='4954197']
Like, getting the calculator to add multiple numbers.


So, my attempted summary of the situation.

You have a textbox containing a string which represents the expression to be calculated.
You want to take the equation represented in string in the textbox and compute the output of the expression.
But the string representation of the expression isn't very useful to you - at least, not yet.

Am I correct so far?
[/quote]

yes

I want to have the calculator able to deal with multiple numbers instead of just 2.

97 + 576 + 46, etc.

I originally thought storing each number in an array/list somehow would do the trick, but dealing with taking the numbers out of the string after each math symbol is what has me stuck atm.
[/quote]

Okay.

Just so we're on the same page here: leaving aside the problem of "taking the numbers out of the string" (there's a specific term for this - expression parsing - which we'll get to in a moment), have you determined how to deal with the numbers once they're "out of the string?" If so, can you please post the code so that we can see where you're going with it? If not, let's tackle that problem, first since it's easier - write code that takes each number in an array (or a list, if you prefer - this is what I would prefer, for reasons that will become clear later) and adds them together.

As an aside: I often find it helpful to write a program in chunks according to what I already know how to do. If I know how one particular step works, and I know exactly what sort of thing I'm going to get from the steps before it but not how that predecessor step will work, then I can write that particular step in isolation with some "dummy" test data in the format that I think that particular step will consume once the one before it is done. That's useful for unit testing, as well - if I can verify that each part of my program works on its own when given data that I know should work, then I can track down bugs more easily.
Advertisement
What I had in mind was adding a List of ints. Then whenever the user presses a math button, store the preceding substring of numbers in the list, but right now I'm having trouble cutting the substring of numbers out of the string.
While some people on here are being quite rude, they are right on one thing, being a programmer is not about reading a tutorial, and then knowing how to do that 1 thing. Its about being able to solve problems on your own. Anyway.

This is probably the simplest way to approach the issue, though not necessarily the best:

Your user enters a string along the lines of 10 + 20 - 5. Parse that string and store each number in one array. Also store the symbols in a separate array.

Now you have array A = {10, 20, 5} and B = {"+"," -"} (note this is just pseudocode)

Now parse the two arrays and perform the operations. Pretty basic. If you need an actual example:


while (A.length > 1)
{
if (B[0] == "+")
{
A[1] = A[0] + A[1];
remove first element of A;
remove first element of B;
}
//add other symbol options here
}


Now A[0] will contain the solution.

Of course for things like multiplication you'll have to take into account order of operations, but I won't solve the whole thing for you.

What I had in mind was adding a List of ints. Then whenever the user presses a math button, store the preceding string of numbers in the list, but right now I'm having trouble cutting the substring of numbers out of the string.


I realize that. You seem very insistent on this. But work with me for a bit - try writing the "adding a list of ints" bit before you write the code that cuts the numbers out of the string. In essence, expand AdrianC's example into actual, working code. Post the code here when you're done.
I think I am just going to stop while I'm ahead so I don't make myself look any more incompetent than I already have. Obviously the concept of "program, don't just look at tutorials that teaches you how to use stuff." isn't doing anything for me. If I could find something similar to http://www.cplusplus.com/forum/articles/12974/ using C#, in terms of setup (working from the ground up, making you do exercises using stuff you've learned, etc). That would be great. The tutorials I have been watching lately, which have me jumping straight into windows forms, are the ones from http://thenewboston.org/list.php?cat=15

Problem solving is easy. I just need to learn the ins and outs of the language to know what syntax I can use to solve said problems.

I think I am just going to stop while I'm ahead so I don't make myself look any more incompetent than I already have.


I must say, your attitude seems very defeatist.

Obviously the concept of "program, don't just look at tutorials that teaches you how to use stuff." isn't doing anything for me.[/quote]

But that's what programming is. You'll never learn if you don't think for yourself.

I reiterate - post the exact sequence of steps that you want to do and we'll help you find the syntax to carry them out. If you know what you want to do and it's only the syntax that escapes you, then you should have no trouble doing this. If you're having trouble doing that, then clearly it is not just the syntax you're really having trouble with. What little you've told us is not enough - you're on the right track, but you need to break the problem down further before you get to the level where syntax matters. With that said, I'm going to give you a hint you may find useful in the form of some sample code.


// starting expression
string expr = "1 + 2";

// see http://msdn.microsoft.com/en-us/library/b873y76a.aspx for documentation on precisely how Split() works
string[] tokens = expr.Split(' ');

// exercise: what will get printed to the screen?
foreach (string t in tokens)
System.Console.WriteLine(t);


The tutorials I have been watching lately, which have me jumping straight into windows forms, are the ones from http://thenewboston....list.php?cat=15[/quote]

Which of those tutorials have you watched?


Problem solving is easy. I just need to learn the ins and outs of the language to know what syntax I can use to solve said problems.
[/quote]

You will find as you program more and more complicated things that the syntax is actually the easy part, and the problem solving the hard part.
TextBox.Text
int.TryParse
MessageBox.Show

Google each of them for examples. Try the examples out in your code. Watch what they do.


Here is your quiz:


string s = ???; // get it from your textbox.
int value;
???; // replace the ??? to convert the string to an integer. Show a messagebox with an error if it fails.
value = value * 5;
// put the resulting value back in the textbox.

[quote name='Bill Fountaine' timestamp='1341036908' post='4954218']
I think I am just going to stop while I'm ahead so I don't make myself look any more incompetent than I already have.


I must say, your attitude seems very defeatist.

Obviously the concept of "program, don't just look at tutorials that teaches you how to use stuff." isn't doing anything for me.[/quote]

But that's what programming is. You'll never learn if you don't think for yourself.

I reiterate - post the exact sequence of steps that you want to do and we'll help you find the syntax to carry them out. If you know what you want to do and it's only the syntax that escapes you, then you should have no trouble doing this. If you're having trouble doing that, then clearly it is not just the syntax you're really having trouble with. What little you've told us is not enough - you're on the right track, but you need to break the problem down further before you get to the level where syntax matters. With that said, I'm going to give you a hint you may find useful in the form of some sample code.


// starting expression
string expr = "1 + 2";

// see http://msdn.microsoft.com/en-us/library/b873y76a.aspx for documentation on precisely how Split() works
string[] tokens = expr.Split(' ');

// exercise: what will get printed to the screen?
foreach (string t in tokens)
System.Console.WriteLine(t);


The tutorials I have been watching lately, which have me jumping straight into windows forms, are the ones from http://thenewboston....list.php?cat=15[/quote]

Which of those tutorials have you watched?


Problem solving is easy. I just need to learn the ins and outs of the language to know what syntax I can use to solve said problems.
[/quote]

You will find as you program more and more complicated things that the syntax is actually the easy part, and the problem solving the hard part.
[/quote]

I've watched up to vid 18, but I know about some of the other stuff in the other videos.

As for the code itself, I'll get back to that in a bit. I need to get some coffee in me and wake up fully first.
The exact steps I want to do:

Once the user presses a math operator button, store the string of numbers prior to that in an array.
Do math with contents from array
Hit equals button to display result
If your not acquainted with the idea of reading books on the subject of C# such as in this case. I recommend you start, it would give you exactly what you want if you can find the right book. Examples along with "problem sets" that you can solve to further your skills which is what it sounds like your looking for. Such as Head First C#, 2E: A Learner's Guide to Real-World Programming with Visual C# and .NET


Links that might help:
http://www.c-sharpcorner.com/Beginners/
http://www.homeandlearn.co.uk/csharp/csharp.html
http://www.fincher.org/tips/Languages/csharp.shtml

This topic is closed to new replies.

Advertisement