Basic Calculator in C#
#31
Posted 31 March 2010 - 12:42 PM
#32
Posted 04 April 2010 - 05:34 AM
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?
#34
Posted 31 May 2010 - 02:04 PM
lesPaul456, on 17 June 2009 - 08:52 AM, said:
Irish18, on 16 Jun, 2009 - 08:40 AM, said:
papuccino1, on 15 Jun, 2009 - 03:15 PM, said:
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...
#35 Guest_shonababy*
Posted 04 July 2010 - 07:12 AM
#36
Posted 04 July 2010 - 07:25 AM
#37 Guest_shonababy*
Posted 04 July 2010 - 11:39 PM
#38
Posted 05 July 2010 - 12:34 AM
Quote
#39 Guest_SJF*
Posted 31 July 2010 - 01:42 PM
The first discussion of a calculator that I have been able to understand.
SJF
#40 Guest_MF.cs*
Posted 04 December 2010 - 01:49 AM
equal button in my code looks like the following :
private void button5_Click(object sender, EventArgs e)
{
}
#41 Guest_mm*
Posted 04 December 2010 - 04:36 AM
#42 Guest_Exodus*
Posted 21 December 2010 - 09:30 AM
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.
#43
Posted 19 January 2011 - 03:14 PM
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
#44 Guest_C#Starter*
Posted 03 February 2011 - 04:55 AM
Thanks again and I hope I helped!
#45 Guest_balamurugan*
Posted 18 February 2011 - 12:22 AM
WorkingC#, on 21 October 2008 - 08:20 PM, said:
PsychoCoder, on 31 Jan, 2008 - 08:49 AM, said:
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!
|
|





MultiQuote




|