Hi
Anyone know Calculator programme as i formatted my pc last week so that program also deleted know i need C# calculator programe it's urgent if anyone know plz provide me calculator programme
Thanks
Printable View
Hi
Anyone know Calculator programme as i formatted my pc last week so that program also deleted know i need C# calculator programe it's urgent if anyone know plz provide me calculator programme
Thanks
Code:using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
class Calculator:Form
{
const int BUTTON_VERTICALSPACING=5;
const int BUTTON_HORIZONTALSPACING=3;
const int MEMORY_BUTTON_WIDTH=35;
const int MEMORY_BUTTON_HEIGHT=35;
const int MEMORY_BUTTONY=70;
const int MEMORY_BUTTONX=5;
const int DIGIT_BUTTON_WIDTH=35;
const int DIGIT_BUTTON_HEIGHT=35;
const int DIGIT_BUTTONY=MEMORY_BUTTONY;
const int DIGIT_BUTTONX=MEMORY_BUTTONX+MEMORY_BUTTON_WIDTH+BUTTON_HORIZONTALSPACING+5;
const int DISPLAY_HEIGHT=25;
const int OTHER_BUTTON_HEIGHT=DIGIT_BUTTON_HEIGHT-10;
const int OTHER_BUTTON_WIDTH=60;
const int OTHER_BUTTONY=5+DISPLAY_HEIGHT+10;
const int OTHER_BUTTONX=DIGIT_BUTTONX;
const int FORM_HEIGHT=260;
const int FORM_WIDTH=250;
Button[] btnDigitOperator=new Button[20];
Button[] btnMemory=new Button[4];
Button[] btnOther=new Button[3];
Label lblDisplay;
Label lblMemory;
string[,] DigitCaption=new String[5,4]{{"7","4","1","0"},{"8","5","2","+/-"},{"9","6","3","."},{"/","*","-","+"},{"sqrt","%","1/x","="}};
string[] MemoryCaption={"MC","MR","MS","M+"};
string[] OtherCaption={"BackSpace","CE","C"};
static double BufferValue=0.0;
static double Operand1=0.0;
static double Operand2=0.0;
static string Operator="";
//if true then the number does not have integral part.
static bool IsFraction=false;
//if digit pressed just after operator then clear display and
//display only the clicked digit otherwise do concatenation
static bool IsOperatorClicked=false;
public Calculator()
{
lblDisplay=new Label();
lblDisplay.Text="0.";
lblDisplay.BackColor=Color.White;
lblDisplay.Location=new Point(5,5);
lblDisplay.Size=new Size(FORM_WIDTH-20,DISPLAY_HEIGHT);
lblDisplay.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
lblDisplay.TextAlign=ContentAlignment.MiddleRight;
this.Controls.Add(lblDisplay);
lblMemory=new Label();
lblMemory.Text="";
lblMemory.BackColor=this.BackColor;
lblMemory.Location=new Point(MEMORY_BUTTONX,OTHER_BUTTONY);
lblMemory.Size=new Size(MEMORY_BUTTON_WIDTH,OTHER_BUTTON_HEIGHT);
lblMemory.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
lblMemory.TextAlign=ContentAlignment.MiddleCenter;
this.Controls.Add(lblMemory);
// Adding Memory Buttons
for(int i=0;i<btnMemory.Length;i++)
{
btnMemory[i]=new Button();
btnMemory[i].ForeColor=Color.Red;
btnMemory[i].Text=MemoryCaption[i];
btnMemory[i].Size=new Size(MEMORY_BUTTON_WIDTH,MEMORY_BUTTON_HEIGHT);
if(i==0)
{
btnMemory[i].Location=new Point(MEMORY_BUTTONX,MEMORY_BUTTONY);
}
else
{
btnMemory[i].Location=new Point(MEMORY_BUTTONX,MEMORY_BUTTONY + i*MEMORY_BUTTON_HEIGHT + i*BUTTON_VERTICALSPACING);
}
btnMemory[i].Click+=new EventHandler(Memory_Click);
this.Controls.Add(btnMemory[i]);
}
// Adding Digit/Operator Buttons
int btnIndex=0;
for(int i=DigitCaption.GetLowerBound(0);i<=DigitCaption.GetUpperBound(0);i++)
{
for(int j=DigitCaption.GetLowerBound(1);j<=DigitCaption.GetUpperBound(1);j++)
{
btnDigitOperator[btnIndex]=new Button();
btnDigitOperator[btnIndex].Text=DigitCaption[i,j];
btnDigitOperator[btnIndex].ForeColor=Color.Blue;
btnDigitOperator[btnIndex].Size=new Size(DIGIT_BUTTON_WIDTH,DIGIT_BUTTON_HEIGHT);
int buttonX=DIGIT_BUTTONX+i*DIGIT_BUTTON_WIDTH+i*BUTTON_HORIZONTALSPACING;
if(j==0)
{
btnDigitOperator[btnIndex].Location=new Point(buttonX,DIGIT_BUTTONY);
}
else
{
btnDigitOperator[btnIndex].Location=new Point(buttonX,DIGIT_BUTTONY + j*DIGIT_BUTTON_HEIGHT+j*BUTTON_VERTICALSPACING);
}
if(i==DigitCaption.GetUpperBound(0)-1)
btnDigitOperator[btnIndex].ForeColor=Color.Red;
if(i==DigitCaption.GetUpperBound(0) && j==DigitCaption.GetUpperBound(1))
btnDigitOperator[btnIndex].ForeColor=Color.Red;
btnDigitOperator[btnIndex].Click+=new EventHandler(DigitOpertor_Click);
this.Controls.Add(btnDigitOperator[btnIndex++]);
}
}
// Adding Other Buttons
for(int i=0;i<btnOther.Length;i++)
{
btnOther[i]=new Button();
btnOther[i].ForeColor=Color.Red;
btnOther[i].Text=OtherCaption[i];
if(i==0)
{
btnOther[i].Location=new Point(OTHER_BUTTONX,OTHER_BUTTONY);
btnOther[i].Size=new Size(OTHER_BUTTON_WIDTH+10,OTHER_BUTTON_HEIGHT);
}
else
{
btnOther[i].Location=new Point((2-i)*5+10+OTHER_BUTTONX + i*OTHER_BUTTON_WIDTH + i*BUTTON_HORIZONTALSPACING,OTHER_BUTTONY);
btnOther[i].Size=new Size(OTHER_BUTTON_WIDTH-10,OTHER_BUTTON_HEIGHT);
}
btnOther[i].Click+=new EventHandler(OtherButton_Click);
this.Controls.Add(btnOther[i]);
}
this.Size=new Size(FORM_WIDTH,FORM_HEIGHT);
this.MaximizeBox=false;
this.Text="Calculator";
}
public void Memory_Click(object sender,EventArgs eArgs)
{
switch(((Button)sender).Text)
{
case "MR":
lblDisplay.Text=BufferValue.ToString()+".";
break;
case "MS":
BufferValue=Double.Parse(lblDisplay.Text);
lblMemory.Text="M";
break;
case "M+":
try
{
BufferValue+=Double.Parse(lblDisplay.Text);
}
catch(Exception)
{
BufferValue=0.0;
}
break;
case "MC":
BufferValue=0.0;
lblMemory.Text="";
break;
}
}
public void DigitOpertor_Click(object sender,EventArgs eArgs)
{
string Caption=((Button)sender).Text;
bool IsError=false;
switch(Caption)
{
case "+":
case "-":
case "*":
case "/":
Operand1=Double.Parse(lblDisplay.Text);
Operator=Caption;
IsOperatorClicked=true;
IsFraction=false;
break;
case "=":
Operand2=Double.Parse(lblDisplay.Text);
IsOperatorClicked=false;
switch(Operator)
{
case "+":
try
{
lblDisplay.Text=(Operand1+Operand2).ToString();
}
catch(Exception)
{
IsError=true;
lblDisplay.Text="Error.";
}
break;
case "-":
try
{
lblDisplay.Text=(Operand1-Operand2).ToString();
}
catch(Exception)
{
IsError=true;
lblDisplay.Text="Error.";
}
break;
case "*":
try
{
lblDisplay.Text=(Operand1*Operand2).ToString();
}
catch(Exception)
{
IsError=true;
lblDisplay.Text="Error.";
}
break;
case "/":
try
{
lblDisplay.Text=(Operand1/Operand2).ToString();
}
catch(Exception)
{
IsError=true;
if(Operand2==0)
lblDisplay.Text="Cannot Divide by Zero.";
else
lblDisplay.Text="Error.";
}
break;
}
if(!IsError && lblDisplay.Text.IndexOf(".")<0)
{
lblDisplay.Text+=".";
}
break;
case "+/-":
try
{
if(lblDisplay.Text!="0.")
{
if(lblDisplay.Text.Substring(lblDisplay.Text.Length-1)==".")
lblDisplay.Text=(-Double.Parse(lblDisplay.Text)).ToString()+".";
else
lblDisplay.Text=(-Double.Parse(lblDisplay.Text)).ToString();
}
}
catch(Exception)
{
lblDisplay.Text="Error.";
}
break;
case ".":
IsFraction=true;
break;
case "1/x":
try
{
if(lblDisplay.Text!="0.")
lblDisplay.Text=(1.0/Double.Parse(lblDisplay.Text)).ToString();
if(lblDisplay.Text.IndexOf(".")<0)
lblDisplay.Text+=".";
}
catch(Exception)
{
lblDisplay.Text="Error.";
}
break;
case "%":
break;
case "sqrt":
try
{
lblDisplay.Text=Math.Sqrt(Double.Parse(lblDisplay.Text)).ToString();
if(lblDisplay.Text.IndexOf(".")<0)
lblDisplay.Text+=".";
}
catch(Exception)
{
lblDisplay.Text="Error.";
}
break;
default:
if(IsOperatorClicked)
{
lblDisplay.Text="0.";
}
if(lblDisplay.Text!="0.")
{
if(!IsFraction)
{
lblDisplay.Text=lblDisplay.Text.Substring(0,lblDisplay.Text.Length-1) + Caption + ".";
}
else
{
lblDisplay.Text=lblDisplay.Text.Substring(0,lblDisplay.Text.Length) + Caption;
}
}
else
{
if(!IsFraction)
{
lblDisplay.Text=Caption + ".";
}
else
{
lblDisplay.Text=lblDisplay.Text+Caption;
}
}
IsOperatorClicked=false;
break;
}
}
public void OtherButton_Click(object sender,EventArgs eArgs)
{
switch(((Button)sender).Text)
{
case "CE":
case "C":
lblDisplay.Text="0.";
IsFraction=false;
break;
default:
if(lblDisplay.Text.Length>2)
{
if(lblDisplay.Text.Substring(lblDisplay.Text.Length-1)==".")
{
lblDisplay.Text=lblDisplay.Text.Substring(0,lblDisplay.Text.Length-2) + ".";
}
else
{
lblDisplay.Text=lblDisplay.Text.Substring(0,lblDisplay.Text.Length-1);
}
}
else
{
IsFraction=false;
lblDisplay.Text="0.";
}
break;
}
}
public static void Main()
{
Application.Run(new Calculator());
}
}
Code:rivate void bttnD2_Click(object sender, System.EventArgs e)
{
AddToArray(bttnD2);
}
private void AddToArray(Button bttn)
{
//Step[1]
m_value += bttn.Text;
//Step[2]
lbRes.Text += bttn.Text;
//Step[3]
SetEnableOperatorBttns(true);
}
private void bttnPlus_Click(object sender, System.EventArgs e)
{
AddOperatorToArray(bttnPlus);
}
private void AddOperatorToArray(Button bttn)
{
//[1]
m_store.Add( m_value );
//[2]
lbRes.Text += bttn.Text;
//[3]
m_value = "";
//[4]
m_store.Add( bttn.Text );
//[5]
bttnDot.Enabled = true;
//[6]
SetEnableOperatorBttns(false);
}
private void bttnDot_Click(object sender, System.EventArgs e)
{
//[1]
AddToArray(bttnDot);
//[2]
bttnDot.Enabled = false;
}
private void bttnEqual_Click(object sender, System.EventArgs e)
{
try
{
//[1]
m_store.Add(m_value);
//[2]
m_store.Add( bttnEqual.Text );
/* m_res here take the first element in m_store if this element is
* operator like +*-/ Exception give us Error else it will take the
* Digits or dot point
*/
//[3]
float m_res = float.Parse ( m_store[0].ToString() );
/* this loop extract all element in Array then check it
* if the element is operator it will calculate the prefix
* and postfix and give us the result
* Example 1+2/3*5
* 1) 1+2 = 3
* 2) 3/3 = 1
* 3) 1*5 = 5
* note:- no periority operator and no brakets so it's basic calculator:)
*/
//[4]
for( int i = 0 ; i{
if( m_store[i].ToString() == "+" )
{
lbRes.Text="";//(1)
m_res += float.Parse (m_store[i+1].ToString());//(2)
lbRes.Text = m_res.ToString();//(3)
}
else if( m_store[i].ToString() == "-" )
{
lbRes.Text="";//(1)
m_res -= float.Parse (m_store[i+1].ToString());//(2)
lbRes.Text= m_res.ToString();//(3)
}
else if( m_store[i].ToString() == "*" )
{
lbRes.Text="";//(1)
m_res *= float.Parse ( m_store[i+1].ToString() );//(2)
lbRes.Text= m_res.ToString();//(3)
}
else if( m_store[i].ToString() == "/" )
{
lbRes.Text= "";//(1)
m_res /= float.Parse ( m_store[i+1].ToString() );//(2)
lbRes.Text= m_res.ToString();//(3)
}
}
m_store.Clear();//[5]
m_value = lbRes.Text;//[6]
for( int i = 0 ; i < m_value.Length ; i++ )//[7]
{
if( m_value[i].ToString() == "." )
{
bttnDot.Enabled = false;
break;
}
else
{
bttnDot.Enabled = true;
}
}
bttnEqual.Enabled = false;//[8]
}
catch(Exception exception)
{
MessageBox.Show(exception.Message,"Error");
}
}