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