I've been working on a calculator program for a couple weeks now, and I'm stuck on how I can get the program to actually do the calculations.

The short story is this:

Let's say I have a string from the calculator that is "3 + 3"
I can already convert it to int and have it display a result, but my result is not 6... it gives me 33!

This is the code for my "add" button

``````private void uxAdd_Click(object sender, EventArgs e)
{
input = false;
display = true;
decimalpoint = false;
calc = calc + " + ";
Event_Handler(this, null);
}``````

And this is the code for my "equals" button

``````private void uxEquals_Click(object sender, EventArgs e)
{
int calcparse = Int32.Parse(calc);
int calcint = Convert.ToInt32(calc);
String numdisplay = calcint.ToString();
Event_Handler(this, numdisplay);
}``````

My professor isn't being much help (as many of them aren't) so any help from here would be great. I've been searching numerous forums for an answer and can't find much (unless I'm completely oblivious to an obvious answer)

4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by Prof.Mendl

look at string.split

``````string dig1, oper, dig2;
string[] calcs = calc.Split(' ');
dig1 = calcs[0];
oper = calcs[1];
dig2 = calcs[2];
int result;
switch(oper)
{
case "+":
result = Convert.ToInt32(dig1) + Convert.ToInt32(dig2);
break;
default:
break;
}

String numdisplay = result.ToString();
Event_Handler(this, numdisplay);``````

you'll need to do more than this for each operator, but this is the concept

As long as you are clicking on an add, subtract button etc with a number click.

It would also be easy to store two arrays, one of the numbers, one of the operators. And when equals is pressed loop through number then operator then number etc. I don't know which method you are using, let me know if you need more clarification based upon which method you are using.

Long as you add the items in turn to a list/array so you end up with

1
+
2
=

then you can work through easily.

I made it before, one as windows applications and the other as console application but with graphical interface

you can see them and may be useful for you

Attachments
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.