Jump to content

  • Log In with Google      Sign In   
  • Create Account


C# Calcuator Help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Key   Members   -  Reputation: 112

Like
0Likes
Like

Posted 18 July 2011 - 01:08 AM

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.

Sponsor:

#2 DaveMS   Members   -  Reputation: 185

Like
0Likes
Like

Posted 18 July 2011 - 02:20 AM

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?



#3 yaustar   Members   -  Reputation: 530

Like
0Likes
Like

Posted 18 July 2011 - 02:46 AM

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);
        }


#4 Key   Members   -  Reputation: 112

Like
0Likes
Like

Posted 18 July 2011 - 11:59 AM

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();
    	}
	}
}


#5 DaveMS   Members   -  Reputation: 185

Like
0Likes
Like

Posted 18 July 2011 - 12:58 PM

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


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.

#6 DaveMS   Members   -  Reputation: 185

Like
0Likes
Like

Posted 18 July 2011 - 01:12 PM

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

#7 Key   Members   -  Reputation: 112

Like
0Likes
Like

Posted 18 July 2011 - 03:38 PM

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;
    	}
	}
}


#8 DaveMS   Members   -  Reputation: 185

Like
1Likes
Like

Posted 19 July 2011 - 10:05 AM

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();
    	}
	}
}



#9 DaveMS   Members   -  Reputation: 185

Like
0Likes
Like

Posted 19 July 2011 - 12:05 PM

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 :)

#10 Key   Members   -  Reputation: 112

Like
0Likes
Like

Posted 20 July 2011 - 10:10 PM

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?

#11 falcon93   Members   -  Reputation: 121

Like
1Likes
Like

Posted 21 July 2011 - 02:58 AM

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?


I havn't read through all replies, but I assume you use Visual Studio?

In properties window, click on the yellow thunder icon. On the right of the field "Click", check if the method name for your button click is correct. I'm not sure if it will solve your problem, but it's worth checking out.

For your second question; If you add a button on your form without creating a click event method for it, your program will not crash, and it will neither crash if you leave the click event method empety. However, it's pointless to have a button that doesn't do anything, right? Posted Image

#12 Key   Members   -  Reputation: 112

Like
0Likes
Like

Posted 21 July 2011 - 10:35 AM

Thank you so much.
I added button for the actual numbers on them, but I wanted to test if the program still worked before proceeding.
But it all works now, so I can move on.
Thanks again.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS