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

Started by
49 comments, last by eFoDay 11 years, 9 months ago
Considering I have a million books/tutorials, with no real direction. Meh

I wish I could find something similar to this for C#

http://www.cplusplus...articles/12974/

Working from the ground up with pretty much everything you need to learn, with exercises/sample programs to make on your own.

Almost every tutorial/book I've come across, claims to be for beginners, but throws 20 pages of code at you for like the 2nd or 3rd concepts, when it could just be something simple. But no, they have to throw a bunch of overly complicated code in to confuse people, instead of just showing how to do that one small concept.

Maybe I should just stop asking for help. Whenever I do I get chewed out by people.
Advertisement
Building an expression parser, using Winforms, is not what I would consider a "beginner project to learn C#".

My advice: start with console applications for now, trying to learn Winforms + C# + general programming all at once is a bit much.

As Telastyn said, make a console application that takes input from the user and adds them together first.

Building an expression parser, using Winforms, is not what I would consider a "beginner project to learn C#".

My advice: start with console applications for now, trying to learn Winforms + C# + general programming all at once is a bit much.

As Telastyn said, make a console application that takes input from the user and adds them together first.


That's easy to do..

[quote name='laztrezort' timestamp='1341006566' post='4954109']
Building an expression parser, using Winforms, is not what I would consider a "beginner project to learn C#".

My advice: start with console applications for now, trying to learn Winforms + C# + general programming all at once is a bit much.

As Telastyn said, make a console application that takes input from the user and adds them together first.


That's easy to do..
[/quote]

Then do it. Personally, I'm a bit skeptical that you can accomplish that without needing someone to hold your hand.

Once you've done that, have the user enter the input via form rather than console.

Once you've done that, have the user input the operation as well.

Once you've done that, have the user able to input multiple inputs and operations to perform.

There's your direction. No more excuses.
My problem isn't knowing how to solve the solution.

The problem is not knowing the syntax to solve specific problems.

Stop treating me like I'm an idiot. Thats why I barely use this damn site.
Almost every tutorial/book I've come across, claims to be for beginners, but throws 20 pages of code at you for like the 2nd or 3rd concepts, when it could just be something simple. But no, they have to throw a bunch of overly complicated code in to confuse people, instead of just showing how to do that one small concept.[/quote]

That's because much of the time, that "one small concept" cannot really be demonstrated in isolation. To demonstrate virtual functions, for instance, one would need at least three classes plus an entry-point function (possibly a third class to retain this entry-point method, if you're using C#). To demonstrate the real power thereof, one would need something much more substantial. An extreme example: to demonstrate the basic opening of a window in the Win32 API from C or C++, one would need several dozens of lines of code just to open the window, and then another few lines just to keep the window open!

Besides showing the syntax of the feature, to properly show off a concept I would argue that one needs to actually demonstrate the practical use of that feature, which means showing a non-trivial program that actually uses it to accomplish something. While I understand your "20 pages of code" to be hyperbole, I believe this also has a different purpose unrelated to merely demonstrating non-trivially the concepts being taught - consider that as you code more and more, your programs will grow larger, and it's highly unlikely that you'll ever be dealing with "one small concept" at a time - you'll be dealing with pages upon pages of code all on your screen at once. In my experience as an intern/co-op alone I've come across source code files that are literally THOUSANDS of lines of code in length! If it were me, I would be putting those "20 pages of code" there in part to get you used to looking at (and understanding) lots and lots of code at once - if you want to be a programmer, the reality is that you are going to need to be able to a) read other people's code b) read and understand LOTS of other people's code.


My problem isn't knowing how to solve the solution.

The problem is not knowing the syntax to solve specific problems.


In that case, let's do it this way. You provide the pseudocode (or a list of steps in plain English, whichever you'd prefer) to the solution you have in mind, and someone here (possibly me) can show you the C# syntax for those specific tasks which will do what you have in mind. I personally will not give you the whole source code to the solution; knowing the solution, and having acquired the syntax you need, you should be able to put the pieces together yourself.
OK, first.

1. Object Oriented programming ... break your programming down into "classes", related to logical sets of data, and the methods that manipulate that data.
2. Focus on building the LOGICAL classes, separate from your UI.

So, don't write a dozen input hanlders doing all your math ... write a dozen input handlers calling methods, which do the math ... now in a real program there is nothing wrong with event handlers the size of yours (3 lines long) ... but since this is half the logic of your program, you are not solving your problem cleanly.

There are at least 5-10 reasonable and very different ways to solve the problem you are asking (and millions of smaller varieties). Programming is about thinking through problems and we all think different (like musicians or painters) ... so all we can do is either show you how we would do it and hope you can generalize, or show you general techniques and hope you can apply them specifically.

But to help you out a bit ... first a thought, try to store your variables in a "natural" manner completely not realated to your UI. what is "natural" depends on you point of view. If you were implementing a more simple calculator "natural" might be:

a variable called something like "accumulator", "currentValue", "register" or the like ... which holds the total so far ... ie "0" when reset, and the number that is displayed after users press the "=" etc.

a variable called "operator", "pendingOperation", "state" or some such .... which holds the operation the user has requested after the press something like "+". and would be able to detect the when there isn't one (ie if the user types "3+3=" after the "=" the pending operation is null, so that you can tell if the user then types "53" then are doing the "set value to ..." operation, which doesn't care what the previous value was ... wheras if they type "+" then "53" they are doing the "add value ..." operation which does use the previous / current value.

now of course, i've only hinted at posibilities ... over about 17 years of programming, I've written calculators for examples, for fun, and for trying out new programming languages or ideas probably 7 or 8 times ... none are just like I described, but that's the point, its just a program to think up however you like ... and then work through it tell it makes since AND works.

Good luck.
I have no problem learning, it's just finding the stuff I need to learn to accomplish what I need to.

Like, getting the calculator to add multiple numbers. I'd have no problem if I found something that teaches me how to do it.

I just need to find the right learning materials for certain things, thats all.

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 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.

This topic is closed to new replies.

Advertisement