using System; using System.Drawing; using System.Windows.Forms; class calculator :Form { TextBox t; Button b,b1,b2,b3,b4,b5,b6,b7,b8,b9,bp,bs,bm,bd,be,bc,ba,pi,sqr,dec; bool clr=false; bool deci=false; decimal opd1,opd2; string op; int ope,op1,op2; calculator() { ClientSize = new Size(155,173); Text="CALCULATOR"; Label l4 =new Label(); l4.Text="CALCULATOR"; l4.Location=new Point(5,5); l4.ForeColor=Color.Yellow; t=new TextBox(); t.Location=new Point(5,20); t.Size=new Size(150,20); b=new Button(); b.Text ="1"; b.Location = new Point(5,50); b.Click += new EventHandler(b_Click); b.Size=new Size(30,30); b1=new Button(); b1.Text ="2"; b1.Location = new Point(35,50); b1.Click += new EventHandler(b1_Click); b1.Size=new Size(30,30); b2=new Button(); b2.Text="3"; b2.Location = new Point(65,50); b2.Click += new EventHandler(b2_Click); b2.Size=new Size(30,30); b3=new Button(); b3.Text="4"; b3.Location = new Point(95,50); b3.Click += new EventHandler(b3_Click); b3.Size=new Size(30,30); b4=new Button(); b4.Text="5"; b4.Location = new Point(125,50); b4.Click += new EventHandler(b4_Click); b4.Size=new Size(30,30); b5=new Button(); b5.Text="6"; b5.Location = new Point(5,80); b5.Click += new EventHandler(b5_Click); b5.Size=new Size(30,30); b6=new Button(); b6.Text="7"; b6.Location = new Point(35,80); b6.Click += new EventHandler(b6_Click); b6.Size=new Size(30,30); b7=new Button(); b7.Text="8"; b7.Location = new Point(65,80); b7.Click += new EventHandler(b7_Click); b7.Size=new Size(30,30); b8=new Button(); b8.Text="9"; b8.Location = new Point(95,80); b8.Click += new EventHandler(b8_Click); b8.Size=new Size(30,30); b9=new Button(); b9.Text="0"; b9.Location = new Point(125,80); b9.Click += new EventHandler(b9_Click); b9.Size=new Size(30,30); bp=new Button(); bp.Text="+"; bp.Location = new Point(5,110); bp.Click += new EventHandler(bp_Click); bp.Size=new Size(30,30); bs=new Button(); bs.Text="-"; bs.Location = new Point(35,110); bs.Click += new EventHandler(bs_Click); bs.Size=new Size(30,30); bm=new Button(); bm.Text="*"; bm.Location = new Point(65,110); bm.Click += new EventHandler(bm_Click); bm.Size=new Size(30,30); bd=new Button(); bd.Text="/"; bd.Location = new Point(95,110); bd.Click += new EventHandler(bd_Click); bd.Size=new Size(30,30); be=new Button(); be.Text="="; be.Location = new Point(125,110); be.Click += new EventHandler(be_Click); be.Size=new Size(30,30); bc=new Button(); bc.Text="CLR"; bc.Location = new Point(5,140); bc.Click += new EventHandler(bc_Click); bc.Size=new Size(30,30); ba=new Button(); ba.Text="AC"; ba.Location = new Point(35,140); ba.Click += new EventHandler(ba_Click); ba.Size=new Size(30,30); pi=new Button(); pi.Text="pi"; pi.Location = new Point(65,140); pi.Click += new EventHandler(pi_Click); pi.Size=new Size(30,30); sqr=new Button(); sqr.Text="sqr"; sqr.Location = new Point(95,140); sqr.Click += new EventHandler(sqr_Click); sqr.Size=new Size(30,30); dec=new Button(); dec.Text="."; dec.Location = new Point(125,140); dec.Click += new EventHandler(dec_Click); dec.Size=new Size(30,30); Controls.AddRange(new Control[]{ t,b,b1,b2,b3,b4,b5,b6,b7,b7,b8,b9,bp,bs,bm,bd,be,bc,ba,pi,sqr,dec} ); } private void b_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b.Text); } private void b1_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b1.Text); } private void b2_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b2.Text); } private void b3_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b3.Text); } private void b4_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b4.Text); } private void b5_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b5.Text); } private void b6_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b6.Text); } private void b7_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b7.Text); } private void b8_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b8.Text); } private void b9_Click( object sender, System.EventArgs e) { if(clr==true) { t.Text=" "; clr=false; } t.AppendText(b9.Text); } private void bp_Click( object sender, System.EventArgs e) { op="+"; if(deci!=true) { op1=Int32.Parse(t.Text); } else { opd1=Convert.ToDecimal(t.Text); Console.WriteLine(opd1); } t.Text=" "; } private void bs_Click( object sender, System.EventArgs e) { op="-"; if(deci!=true) { op1=Int32.Parse(t.Text); } else { opd1=Convert.ToDecimal(t.Text); Console.WriteLine(opd1); } t.Text=" "; } private void bm_Click( object sender, System.EventArgs e) { op="*"; if(deci!=true) { op1=Int32.Parse(t.Text); } else { opd1=Convert.ToDecimal(t.Text); Console.WriteLine(opd1); } t.Text=" "; } private void bd_Click( object sender, System.EventArgs e) { op="/"; if(deci!=true) { op1=Int32.Parse(t.Text); } else { opd1=Convert.ToDecimal(t.Text); Console.WriteLine(opd1); } t.Text=" "; } private void be_Click( object sender, System.EventArgs e) { if(deci!=true) { op2=Int32.Parse(t.Text); cal(op); } else { opd2=Convert.ToDecimal(t.Text); cal(op); } } private void bc_Click( object sender, System.EventArgs e) { clr=true; t.Text="0"; } private void ba_Click( object sender, System.EventArgs e) { clr=true; t.Text="0"; } private void pi_Click( object sender, System.EventArgs e) { t.Text="3.14"; } private void sqr_Click( object sender, System.EventArgs e) { int s; Decimal s1; if(deci!=true) { s=Int32.Parse(t.Text); s=s*s; t.Text=s.ToString(); } else { s1=Convert.ToDecimal(t.Text); s1= s1 * s1; t.Text=s1.ToString(); } } private void dec_Click( object sender, System.EventArgs e) { t.AppendText("."); deci=true; } void cal(string s) { Decimal opd3; switch(s) { case "+":if(deci!=true) { ope=op1+op2; t.Text=ope.ToString(); } else { opd3=opd1 + opd2; t.Text=opd3.ToString(); } op=" "; break; case "-":if(deci!=true) { ope=op1-op2; t.Text=ope.ToString(); } else { opd3=opd1 - opd2; t.Text=opd3.ToString(); } op=" "; break; case "*":if(deci!=true) { ope=op1*op2; t.Text=ope.ToString(); } else { opd3=opd1 * opd2; t.Text=opd3.ToString(); } op=" "; break; case "/": if(deci!=true) { try { ope=op1 / op2; } catch(DivideByZeroException de) { Console.Write("divide by zero erroe"); } finally { Console.WriteLine("finally"); } t.Text=ope.ToString(); } else { opd3=opd1 / opd2; t.Text=opd3.ToString(); } op=" "; break; } } public static void Main() { Application.Run(new calculator()); } }