• (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

Basic Calculator in C#

#31 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 31 March 2010 - 12:42 PM

I'll be starting to work on Part 2 in the next couple days which will cover some of the items requested in this thread, so keep your eyes open (might take a few days due to work and such but will get it written)
Was This Post Helpful? 0
  • +
  • -

#32 AnzeBarle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-April 10

Posted 04 April 2010 - 05:34 AM

I decided to make a graphical calculator (like one in Windows) for my seminary assigment. But, as a beginner, i don't know, how to start.

I must first form a graphic, and write a code. I found some instructions about the code, but non about graphic.

Can You help me?
Was This Post Helpful? 0
  • +
  • -

#33 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 773
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Posted 08 April 2010 - 12:27 AM

It woudld better for you to ask your question in the forums. This comment area is mainly for tutorial feedback.
Was This Post Helpful? 0
  • +
  • -

#34 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Posted 31 May 2010 - 02:04 PM

View PostlesPaul456, on 17 June 2009 - 08:52 AM, said:

View PostIrish18, on 16 Jun, 2009 - 08:40 AM, said:

View Postpapuccino1, on 15 Jun, 2009 - 03:15 PM, said:

Irish18, you probably DO NOT have a TextBox called "txtInput" on your form. Triple-check for that. I'm 999% percent sure that's the problem.


Gyargh, I found the name of my TextBox, it was called 'textBox1'; so i put that infront of '.Text' and its giving the same error message

This is what i have so far;


public class Program
	{
		// variables to hold operands
		private double valHolder1;
		private double valHolder2;
		
		// variable to hold temporary values
		private double tmpValue;

		// True if '.' is use else false
		private bool hasDecimal = false;
		private bool inputStatus = true;

		// variable to hold Operator
		private string calcFunc;
	   
		[STAThread]
		public void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//Check the inputStatus
			if (inputStatus)
			{
				//Its True
				//Append values to the value
				//in the input box
				textBox1.Text += button1.Text;
			}
			else
			{
				//Value is False
				//Set the value to the value of the button
				textBox1.Text = button1.Text;
				//Toggle inputStatus to True
				inputStatus = true;
			}
		}




In the code you pasted, the button1 click event looks like it's in the "Program" class. If that's right, then there's your problem. This method should be in the "Form1" class.


Anyone can tell me which program to use to open the .zip file in this example? I tried just about everything now...much appreciated!

Can you please tell me which program to use to open the .zip file (with the Calculator code)? I tried just about everything...
Was This Post Helpful? 0
  • +
  • -

#35 Guest_shonababy*


Reputation:

Posted 04 July 2010 - 07:12 AM

after pressing enter it doesnot give any answer.
Was This Post Helpful? 0

#36 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 04 July 2010 - 07:25 AM

This is a very basic example of a calculator. It doesn't accept any key inputs, when I get around to a newer version it will contain that functionality but this one does not
Was This Post Helpful? 0
  • +
  • -

#37 Guest_shonababy*


Reputation:

Posted 04 July 2010 - 11:39 PM

whenever i press enter it doesnot gives me any answer.
Was This Post Helpful? 0

#38 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 05 July 2010 - 12:34 AM

Maybe you didn't see my last response to this statement:

Quote

This is a very basic example of a calculator. It doesn't accept any key inputs, when I get around to a newer version it will contain that functionality but this one does not

Was This Post Helpful? 0
  • +
  • -

#39 Guest_SJF*


Reputation:

Posted 31 July 2010 - 01:42 PM

Excellent.
The first discussion of a calculator that I have been able to understand.
SJF
Was This Post Helpful? 0

#40 Guest_MF.cs*


Reputation:

Posted 04 December 2010 - 01:49 AM

how can I put CalculateTotals()method in equal button ??
equal button in my code looks like the following :

private void button5_Click(object sender, EventArgs e)
{
}
Was This Post Helpful? 0

#41 Guest_mm*


Reputation:

Posted 04 December 2010 - 04:36 AM

View PostMF.cs, on 04 December 2010 - 12:49 AM, said:

how can I put CalculateTotals()method in equal button ??
equal button in my code looks like the following :

private void button5_Click(object sender, EventArgs e)
{
CalculateTotals();
}


simple like this .
Was This Post Helpful? 0

#42 Guest_Exodus*


Reputation:

Posted 21 December 2010 - 09:30 AM

First it is nice tutorial.

But some very fundamental things are missen:

The equals button and the constructor!!

the equals function goes:

  if((output.Text.Length != 0) && (valHolder1 != 0))
            {
            CalculateTotals();
            calcFunc = string.Empty;
            hasDecimal = false;
            }



And in the constructor:

public Form1()
        {
            valHolder1 = 0;
            valHolder2 = 0;
            calcFunc = string.Empty;
            InitializeComponent();
           // valHolder1 = "";            
        }



And I saw an other comment about that u cant put the function: CalculateTotals() in an seperate Class. And that is ofcourse Correct, because it is private!!

It doenst belong in a form such method. and anyway u have to make it static. but oke. U can try it by urself.
Was This Post Helpful? 0

#43 Janitor  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 56
  • Joined: 30-July 10

Posted 19 January 2011 - 03:14 PM

I've created a simplified version, not as many IF statements and uses maths class...within a console application though!

main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
{
    public class Program
    {
        double Value = 0;
        double Value2 = 0;
        double answer = 0;

        private static void Main(string[] args)
        {
            int userChoice = 0;

           do
           {
                Program display = new Program();

                display.displayOptions();
                display.menu(userChoice);

                Console.ReadLine();
                Console.Clear();

            } while (userChoice != 5);
        }

        private void displayOptions()
        {       
            Console.WriteLine();
            Console.WriteLine("Calculator");
            Console.WriteLine("------------");

            Console.Write("Enter a number: ");
            Value = double.Parse(Console.ReadLine());

            Console.Write("Please Enter another number: ");
            Value2 = double.Parse(Console.ReadLine());
            Console.WriteLine();
        }

        private void menu(int choice)
        {

            MathsFunctions sum = new MathsFunctions();

            Console.WriteLine("------------");
            Console.WriteLine("1 - Add");
            Console.WriteLine("2 - Multiply");
            Console.WriteLine("3 - Divide");
            Console.WriteLine("4 - Subtract");
            Console.WriteLine("5 - Exit");
            Console.WriteLine("------------");
            Console.Write("Please select a value 1 - 4: ");
            
            choice = int.Parse(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        answer = sum.add(Value, Value2);
                        Console.WriteLine();
                        Console.WriteLine(Value + " + " + Value2 + " = " + answer);
                        break;
                    case 2:
                        answer = sum.multiply(Value, Value2);
                        Console.WriteLine();
                        Console.WriteLine(Value + " * " + Value2 + " = " + answer);
                        break;
                    case 3:
                        answer = sum.divide(Value, Value2);
                        Console.WriteLine();
                        Console.WriteLine(Value + " / " + Value2 + " = " + answer);
                        break;
                    case 4:
                        answer = sum.subtract(Value, Value2);
                        Console.WriteLine();
                        Console.WriteLine(Value + " - " + Value2 + " = " + answer);
                        break;
                    case 5:
                        Environment.Exit(0);
                        break;
                    default:
                        break;
                }
        }
    }
}



mathsfunction.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
{
    public class MathsFunctions
    {
        public double add(double x, double y)
        {
            return  x + y;
        }

        public double subtract(double x, double y)
        {
            return x - y;
        }

        public double multiply(double x, double y)
        {
            return x * y;
        }

        public double divide(double x, double y)
        {
            return x / y;
        }
    }
}



Doesn't yet include powers, and square-root, though would be easy to implement...

This post has been edited by Janitor: 19 January 2011 - 03:16 PM

Was This Post Helpful? 0
  • +
  • -

#44 Guest_C#Starter*


Reputation:

Posted 03 February 2011 - 04:55 AM

This is a really nice setup to start learning C# with. Only I might found BUG, try to press: ",". According to your code you do some checks(if's) and when they pass you do set the hasDecimal to false . But forgot to do it after the else. So when you start with pressing the decimal button. You get the input of "0," on your screen. then I randomly press some numbers again and again I'm able to press the decimal button.

Thanks again and I hope I helped!
Was This Post Helpful? 0

#45 Guest_balamurugan*


Reputation:

Posted 18 February 2011 - 12:22 AM

View PostWorkingC#, on 21 October 2008 - 08:20 PM, said:

View PostPsychoCoder, on 31 Jan, 2008 - 08:49 AM, said:

This is the code for the equals button, which is listed at the end of the tutorial:


CalculateTotals()
{
private void 
	valHolder2 = System.Double.Parse(txtInput.Text);
	//determine which calculation we're going to execute
	//by checking the value of calcFunc
	switch (calcFunc)
	{
			//addition
		case "Add":
			valHolder1 = valHolder1 + valHolder2;
			break;
			//subtraction
		case "Subtract":
			valHolder1 = valHolder1 - valHolder2;
			break;
			//division
		case "Divide":
			valHolder1 = valHolder1 / valHolder2;
			break;
			//multiplication
		case "Multiply":
			valHolder1 = valHolder1 * valHolder2;
			break;
			//exponents (power of)
		case "PowerOf":
			valHolder1 = System.Math.Pow(valHolder1, valHolder2);
			break;
	}
	//set our input area to the value of the calculation
	txtInput.Text = valHolder1.ToString();
	inputStatus = false;
}



So what if we had to place CalculateTotals() in a separate class? I have tried but it gives me errors since I cannot access the variables and textbox created in my forms class. Any help would be great! :^:

Was This Post Helpful? 0

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4