Total members 11895 |It is currently Sat Dec 21, 2024 6:12 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Calculator simple implementation using C# code, four operations only (+,-,/,*) , the showAnswer function is called to print the operation results , the process is done as (FirstParameter Operation SecondParameter) . For numbers value you will notice that we defined button actions button1_Click,button2_Click,... button11_Click to take the number's values from the button text.
csharp code
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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private double FirstParameter;

private double SecondParameter;

private string cal;

private bool inputstatus = true;
private void button1_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button1.Text;
}
else
{
textBox1.Text = button1.Text;
inputstatus = true;
}
}

private void button2_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button2.Text;
}
else
{
textBox1.Text = button2.Text;
inputstatus = true;
}
}

private void button3_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button3.Text;
}
else
{
textBox1.Text = button3.Text;
inputstatus = true;
}
}

private void button4_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button4.Text;
}
else
{
textBox1.Text = button4.Text;
inputstatus = true;
}
}

private void button5_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button5.Text;
}
else
{
textBox1.Text = button5.Text;
inputstatus = true;
}
}

private void button6_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button6.Text;
}
else
{
textBox1.Text = button6.Text;
inputstatus = true;
}
}

private void button7_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button7.Text;
}
else
{
textBox1.Text = button7.Text;
inputstatus = true;
}
}

private void button8_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button8.Text;
}
else
{
textBox1.Text = button8.Text;
inputstatus = true;
}
}

private void button9_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button9.Text;
}
else
{
textBox1.Text = button9.Text;
inputstatus = true;
}
}

private void button10_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button10.Text;
}
else
{
textBox1.Text = button10.Text;
inputstatus = true;
}
}

private void button11_Click(object sender, EventArgs e)
{
if (inputstatus)
{
textBox1.Text += button11.Text;
}
else
{
textBox1.Text = button11.Text;
inputstatus = true;
}
}

private void AddButton_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

FirstParameter = System.Double.Parse(textBox1.Text);

showAnswer();

cal = "+";

}
}

private void SubButton_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

FirstParameter = System.Double.Parse(textBox1.Text);

showAnswer();

cal = "-";

}
}

private void MutliButton_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

FirstParameter = System.Double.Parse(textBox1.Text);

showAnswer();

cal = "*";

}
}

private void DivideButton_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{

FirstParameter = System.Double.Parse(textBox1.Text);

showAnswer();

cal = "/";

}
}

private void ClearField_Click(object sender, EventArgs e)
{
showAnswer();

cal = string.Empty;

}

private void ClearAll_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;

FirstParameter = 0;

SecondParameter = 0;

cal = string.Empty;
}
// Here we shoud results based on the selected operation
private void showAnswer()
{

SecondParameter = System.Double.Parse(textBox1.Text);

switch (cal)
{

case "+":

FirstParameter = FirstParameter + SecondParameter;

break;

case "-":

FirstParameter = FirstParameter - SecondParameter;

break;

case "*":

FirstParameter = FirstParameter * SecondParameter;

break;

case "/":

FirstParameter = FirstParameter / SecondParameter;

break;

}

textBox1.Text = FirstParameter.ToString();

inputstatus = false;

}
}
}




_________________
Please recommend my post if you found it helpful


Author:
Newbie
User avatar Posts: 9
Have thanks: 0 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Calculator
 Calculator     -  
 C++ Calculator     -  
 C++ calculator with modification.     -  
 Simple Calculator     -  
 Scientific Calculator     -  
 Making calculator in JAVA     -  
 Build Calculator in OpenGL     -  
 Complex numbers calculator (C++)     -  
 Simple JavaScript calculator     -  
 how to program a windows standard calculator     -  



Topic Tags

C# Projects
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com