C# Calcuator Help

Started by
10 comments, last by raidzero 12 years, 9 months ago
It's only been a few days since I started learning C#, but it doesn't seem like much progress.
So I'm trying to make a simple Windows Calculator application, but I can't exactly figure it out.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;


public Calculator()
{
InitializeComponent();
}

private void Addition_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
double answer = variableOne + variableTwo;
}

private void Equals_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
string a = answer.ToString();
FieldOne.Text = a;
}

private void Clear_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
}

private void FieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(FieldOne.Text, out variableOne);
double.TryParse(FieldOne.Text, out variableTwo);
}
}
}


Any help is much appreciated.
Advertisement

It's only been a few days since I started learning C#, but it doesn't seem like much progress.
So I'm trying to make a simple Windows Calculator application, but I can't exactly figure it out.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;


public Calculator()
{
InitializeComponent();
}

private void Addition_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
double answer = variableOne + variableTwo;
}

private void Equals_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
string a = answer.ToString();
FieldOne.Text = a;
}

private void Clear_Click(object sender, EventArgs e)
{
FieldOne.Clear();
FieldOne.Focus();
}

private void FieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(FieldOne.Text, out variableOne);
double.TryParse(FieldOne.Text, out variableTwo);
}
}
}


Any help is much appreciated.



What problems are you having?

From the looks of the code, I would say that the OP is having trouble separating/parsing the input values into variableOne and variableTwo from one fieldbox.

OP, consider the actions you do when you type in something like 1 + 2 =

The user types in 1, presses add, types in 2 and presses equal.

Your code changes both variableOne and variableTwo at the same time with the same value which is incorrect.


private void FieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(FieldOne.Text, out variableOne);
double.TryParse(FieldOne.Text, out variableTwo);
}

Steven Yau
[Blog] [Portfolio]

So I split up variableOne and variableTwo. But when I attempt to get an answer the result is always 0.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;

public Calculator()
{
InitializeComponent();
}

private void fieldOne_TextChanged(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
}

private void Addition_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
double.TryParse(fieldOne.Text, out variableTwo);
answer = variableOne + variableTwo;
}

private void Equals_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
string a = answer.ToString();
fieldOne.Text = a;
}

private void Clear_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
}
}
}

private void Addition_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
double.TryParse(fieldOne.Text, out variableTwo);
answer = variableOne + variableTwo;
}


[/quote]

I've not done much winforms work, but looks to me like you are clearing the value in fieldone before you read it into variableTwo.


Second - I believe the TextChanged event is fired whenever the text is changed - either by the user or programmatically. There are many peices of code in your example which change the value of fieldOne - this TextChanged event will be fired after each of these occurences. It would seem that the final time this event is fired, fieldone has a value of 0.
And to add to my previous post

I'm not too clear on your logic.
When do you expect variableOne to be read from the text box?

The way I would do it:

User insert value into fieldOne.
User clicks addition button
program reads value from fieldOne, puts into variableOne, clears fieldOne
User inserts second value into fieldOne
User clicks Equals button
program reads value from fieldOne, puts into variableTwo, clears fieldOne
program calculates answer
program inserts answer into fieldOne
Thanks Dave, that really helped.
So I got it to calculate correctly, but what if I were to do subtraction?
The way I currently have it setup is that when the user presses equal the two variables are added. How can I make it so that if the addition sign was clicked it does addition and same for subtraction?

-Edit-
So I attempted it myself, but now I'm wondering is there a more efficient way of doing this?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
bool addition, subtraction, multiplication, division;

public Calculator()
{
InitializeComponent();
}

private void fieldOne_TextChanged(object sender, EventArgs e)
{

}

private void Clear_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
}

private void Addition_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
addition = true;
}

private void Subtraction_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
subtraction = true;
}

private void Multiplication_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
multiplication = true;
}

private void Division_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
division = true;
}

private void Equals_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableTwo);
fieldOne.Clear();
fieldOne.Focus();
if (addition == true)
{
answer = variableOne + variableTwo;
addition = false;
}
if (subtraction == true)
{
answer = variableOne - variableTwo;
subtraction = false;
}
if (multiplication == true)
{
answer = variableOne * variableTwo;
multiplication = false;
}
if (division == true)
{
answer = variableOne / variableTwo;
division = false;
}
string a = answer.ToString();
fieldOne.Text = a;
}
}
}

Thanks Dave, that really helped.
So I got it to calculate correctly, but what if I were to do subtraction?
The way I currently have it setup is that when the user presses equal the two variables are added. How can I make it so that if the addition sign was clicked it does addition and same for subtraction?

-Edit-
So I attempted it myself, but now I'm wondering is there a more efficient way of doing this?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
bool addition, subtraction, multiplication, division;

public Calculator()
{
InitializeComponent();
}

private void fieldOne_TextChanged(object sender, EventArgs e)
{

}

private void Clear_Click(object sender, EventArgs e)
{
fieldOne.Clear();
fieldOne.Focus();
}

private void Addition_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
addition = true;
}

private void Subtraction_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
subtraction = true;
}

private void Multiplication_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
multiplication = true;
}

private void Division_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
fieldOne.Clear();
fieldOne.Focus();
division = true;
}

private void Equals_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableTwo);
fieldOne.Clear();
fieldOne.Focus();
if (addition == true)
{
answer = variableOne + variableTwo;
addition = false;
}
if (subtraction == true)
{
answer = variableOne - variableTwo;
subtraction = false;
}
if (multiplication == true)
{
answer = variableOne * variableTwo;
multiplication = false;
}
if (division == true)
{
answer = variableOne / variableTwo;
division = false;
}
string a = answer.ToString();
fieldOne.Text = a;
}
}
}



One thing i've learnt in the few years i'v been programming - there are always more efficient ways of doing it. The most important thing is that it works.

If you want to try it though - you can use a delegate as a function pointer, ie - the function to be called can be set at runtime.

Code incoming - not tested.



namespace Calculator
{


public class OperatorFunctions
{

public delegate double OperatorDelegate(double n1, double n2); // here we declare a delegate function, which has a return type of double, and requires 2 doubles as parameters. We will use it to reference one of the functions below.

public static double Addition(double num1, double num2)
{
return num1 + num2;
}

public static double Subtraction(double num1, double num2)
{
return num1 - num2;
}

public static double Multiplication(double num1, double num2)
{
return num1 * num2;
}

public static double Division(double num1, double num2)
{
return num1 / num2;
}
}


public partial class Calculator : Form
{
double variableOne, variableTwo, answer;
OperatorFunctions.OperatorDelegate op; // here is our delegate object. it will hold a reference to one of the operator methods, although we don't know which one yet.

public Calculator()
{
InitializeComponent();
}


private void ClearAndFocus()
{
fieldOne.Clear();
fieldOne.Focus();
}

private void Clear_Click(object sender, EventArgs e)
{
ClearAndFocus();
}

private void Addition_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Addition; // the delegate object op now stores a reference to the addition method
}

private void Subtraction_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Subtraction; // the delegate object op now stores a reference to the subtraction method
}

private void Multiplication_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Multiplication; // the delegate object op now stores a reference to the multiplication method
}

private void Division_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableOne);
ClearAndFocus();
op = OperatorFunctions.Division; // the delegate object op now stores a reference to the division method
}

private void Equals_Click(object sender, EventArgs e)
{
double.TryParse(fieldOne.Text, out variableTwo);
ClearAndFocus();
answer = op(variableOne, variableTwo); // we are now going to invoke the method referenced by this delegate object. But which method is it? Beats me, but we know it takes 2 doubles as parameters, and returns a double.
fieldOne.Text = answer.ToString();
}
}
}

I should also point out in the above code, there is a possible problem - if the user clicks the equals button without choosing an operation to perform - op will be unassigned. But hey, I did warn you it was untested :)
Haha, thanks for the code though :)

I was adding some new button like the actual numbers 0 through 9, and I also renamed some of the button. I also made sure to rename them inside my class file.
After doing this the button aren't doing what they're suppose to, in fact they do nothing at all.

If I add buttons and don't define what they do in my class file will that cause my whole program to break?

This topic is closed to new replies.

Advertisement