Hi I m working on calculater application in C#.it is performing single operations well like12+12=24;10-8=2,but not multiple like10-8+5=it is giving wrong answer.please help me to solve this problem.
CODE
namespace Calculator
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
double store=0;
double result=0;
string input="";
double memory=0;
bool newNumber=true;
string operation="";
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem Edit;
private System.Windows.Forms.MenuItem EditCopy;
private System.Windows.Forms.MenuItem EditPaste;
private System.Windows.Forms.Button btnBkSpc;
private System.Windows.Forms.Label lblMemory;
private System.Windows.Forms.Button btnC;
private System.Windows.Forms.Button btnCE;
private System.Windows.Forms.Button btnMC;
private System.Windows.Forms.Button btnMR;
private System.Windows.Forms.Button btnMS;
private System.Windows.Forms.Button btnM;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btnAddSubt;
private System.Windows.Forms.Button btnDot;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnSqrt;
private System.Windows.Forms.Button btnSubtract;
private System.Windows.Forms.Button btnSq;
private System.Windows.Forms.Button btnMultiply;
private System.Windows.Forms.Button btnExpo;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnEqual;
private System.Windows.Forms.TextBox txtDisplay;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.Edit = new System.Windows.Forms.MenuItem();
this.EditCopy = new System.Windows.Forms.MenuItem();
this.EditPaste = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.btnBkSpc = new System.Windows.Forms.Button();
this.lblMemory = new System.Windows.Forms.Label();
this.btnC = new System.Windows.Forms.Button();
this.btnCE = new System.Windows.Forms.Button();
this.btnMC = new System.Windows.Forms.Button();
this.btnMR = new System.Windows.Forms.Button();
this.btnMS = new System.Windows.Forms.Button();
this.btnM = new System.Windows.Forms.Button();
this.btn1 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.btn4 = new System.Windows.Forms.Button();
this.btn5 = new System.Windows.Forms.Button();
this.btn6 = new System.Windows.Forms.Button();
this.btn7 = new System.Windows.Forms.Button();
this.btn8 = new System.Windows.Forms.Button();
this.btn9 = new System.Windows.Forms.Button();
this.btn0 = new System.Windows.Forms.Button();
this.btnAddSubt = new System.Windows.Forms.Button();
this.btnDot = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnSqrt = new System.Windows.Forms.Button();
this.btnSubtract = new System.Windows.Forms.Button();
this.btnSq = new System.Windows.Forms.Button();
this.btnMultiply = new System.Windows.Forms.Button();
this.btnExpo = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.txtDisplay = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.Edit,
this.menuItem4});
//
// Edit
//
this.Edit.Index = 0;
this.Edit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.EditCopy,
this.EditPaste});
this.Edit.Text = "&Edit";
//
// EditCopy
//
this.EditCopy.Checked = true;
this.EditCopy.Index = 0;
this.EditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
this.EditCopy.Text = "&Copy";
//
// EditPaste
//
this.EditPaste.Index = 1;
this.EditPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
this.EditPaste.Text = "&Paste";
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem6});
this.menuItem4.Text = "&Help";
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = "&Help Topics";
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "About Calculator";
//
// btnBkSpc
//
this.btnBkSpc.ForeColor = System.Drawing.Color.Black;
this.btnBkSpc.Location = new System.Drawing.Point(56, 40);
this.btnBkSpc.Name = "btnBkSpc";
this.btnBkSpc.Size = new System.Drawing.Size(56, 23);
this.btnBkSpc.TabIndex = 1;
this.btnBkSpc.Text = "&Bk Spc";
this.btnBkSpc.Click += new System.EventHandler(this.btnBkSpc_Click);
this.btnBkSpc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.btnBkSpc_KeyDown);
//
// lblMemory
//
this.lblMemory.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblMemory.Location = new System.Drawing.Point(16, 40);
this.lblMemory.Name = "lblMemory";
this.lblMemory.Size = new System.Drawing.Size(32, 23);
this.lblMemory.TabIndex = 2;
//
// btnC
//
this.btnC.Location = new System.Drawing.Point(120, 40);
this.btnC.Name = "btnC";
this.btnC.Size = new System.Drawing.Size(48, 23);
this.btnC.TabIndex = 3;
this.btnC.Text = "&C";
this.btnC.Click += new System.EventHandler(this.btnC_Click);
//
// btnCE
//
this.btnCE.Location = new System.Drawing.Point(176, 40);
this.btnCE.Name = "btnCE";
this.btnCE.Size = new System.Drawing.Size(56, 23);
this.btnCE.TabIndex = 4;
this.btnCE.Text = "&CE";
this.btnCE.Click += new System.EventHandler(this.btnCE_Click);
//
// btnMC
//
this.btnMC.Location = new System.Drawing.Point(16, 72);
this.btnMC.Name = "btnMC";
this.btnMC.Size = new System.Drawing.Size(32, 23);
this.btnMC.TabIndex = 5;
this.btnMC.Text = "MC";
this.btnMC.Click += new System.EventHandler(this.btnMC_Click);
//
// btnMR
//
this.btnMR.Location = new System.Drawing.Point(16, 104);
this.btnMR.Name = "btnMR";
this.btnMR.Size = new System.Drawing.Size(32, 23);
this.btnMR.TabIndex = 6;
this.btnMR.Text = "MR";
this.btnMR.Click += new System.EventHandler(this.btnMR_Click);
//
// btnMS
//
this.btnMS.Location = new System.Drawing.Point(16, 136);
this.btnMS.Name = "btnMS";
this.btnMS.Size = new System.Drawing.Size(32, 23);
this.btnMS.TabIndex = 7;
this.btnMS.Text = "MS";
this.btnMS.Click += new System.EventHandler(this.btnMS_Click);
//
// btnM
//
this.btnM.Location = new System.Drawing.Point(16, 168);
this.btnM.Name = "btnM";
this.btnM.Size = new System.Drawing.Size(32, 23);
this.btnM.TabIndex = 8;
this.btnM.Text = "M+";
this.btnM.Click += new System.EventHandler(this.btnM_Click);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(56, 72);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(32, 23);
this.btn1.TabIndex = 9;
this.btn1.Text = "&1";
this.btn1.Click += new System.EventHandler(this.btn1_Click);
//
// btn2
//
this.btn2.Location = new System.Drawing.Point(88, 72);
this.btn2.Name = "btn2";
this.btn2.Size = new System.Drawing.Size(32, 23);
this.btn2.TabIndex = 10;
this.btn2.Text = "&2";
this.btn2.Click += new System.EventHandler(this.btn2_Click);
//
// btn3
//
this.btn3.Location = new System.Drawing.Point(120, 72);
this.btn3.Name = "btn3";
this.btn3.Size = new System.Drawing.Size(32, 23);
this.btn3.TabIndex = 11;
this.btn3.Text = "&3";
this.btn3.Click += new System.EventHandler(this.btn3_Click);
//
// btn4
//
this.btn4.Location = new System.Drawing.Point(56, 104);
this.btn4.Name = "btn4";
this.btn4.Size = new System.Drawing.Size(32, 23);
this.btn4.TabIndex = 12;
this.btn4.Text = "&4";
this.btn4.Click += new System.EventHandler(this.btn4_Click);
//
// btn5
//
this.btn5.Location = new System.Drawing.Point(88, 104);
this.btn5.Name = "btn5";
this.btn5.Size = new System.Drawing.Size(32, 23);
this.btn5.TabIndex = 13;
this.btn5.Text = "&5";
this.btn5.Click += new System.EventHandler(this.btn5_Click);
//
// btn6
//
this.btn6.Location = new System.Drawing.Point(120, 104);
this.btn6.Name = "btn6";
this.btn6.Size = new System.Drawing.Size(32, 23);
this.btn6.TabIndex = 14;
this.btn6.Text = "&6";
this.btn6.Click += new System.EventHandler(this.btn6_Click);
//
// btn7
//
this.btn7.Location = new System.Drawing.Point(56, 136);
this.btn7.Name = "btn7";
this.btn7.Size = new System.Drawing.Size(32, 23);
this.btn7.TabIndex = 15;
this.btn7.Text = "&7";
this.btn7.Click += new System.EventHandler(this.btn7_Click);
//
// btn8
//
this.btn8.Location = new System.Drawing.Point(88, 136);
this.btn8.Name = "btn8";
this.btn8.Size = new System.Drawing.Size(32, 23);
this.btn8.TabIndex = 16;
this.btn8.Text = "&8";
this.btn8.Click += new System.EventHandler(this.btn8_Click);
//
// btn9
//
this.btn9.Location = new System.Drawing.Point(120, 136);
this.btn9.Name = "btn9";
this.btn9.Size = new System.Drawing.Size(32, 23);
this.btn9.TabIndex = 17;
this.btn9.Text = "&9";
this.btn9.Click += new System.EventHandler(this.btn9_Click);
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(56, 168);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(32, 23);
this.btn0.TabIndex = 18;
this.btn0.Text = "&0";
this.btn0.Click += new System.EventHandler(this.btn0_Click);
//
// btnAddSubt
//
this.btnAddSubt.Location = new System.Drawing.Point(88, 168);
this.btnAddSubt.Name = "btnAddSubt";
this.btnAddSubt.Size = new System.Drawing.Size(32, 23);
this.btnAddSubt.TabIndex = 19;
this.btnAddSubt.Text = "+/-";
//
// btnDot
//
this.btnDot.Location = new System.Drawing.Point(120, 168);
this.btnDot.Name = "btnDot";
this.btnDot.Size = new System.Drawing.Size(32, 23);
this.btnDot.TabIndex = 20;
this.btnDot.Text = "&.";
this.btnDot.Click += new System.EventHandler(this.btnDot_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(168, 72);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(32, 23);
this.btnAdd.TabIndex = 21;
this.btnAdd.Text = "&+";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnSqrt
//
this.btnSqrt.Location = new System.Drawing.Point(200, 72);
this.btnSqrt.Name = "btnSqrt";
this.btnSqrt.Size = new System.Drawing.Size(32, 23);
this.btnSqrt.TabIndex = 22;
this.btnSqrt.Text = "sqrt";
this.btnSqrt.Click += new System.EventHandler(this.btnSqrt_Click);
//
// btnSubtract
//
this.btnSubtract.Location = new System.Drawing.Point(168, 104);
this.btnSubtract.Name = "btnSubtract";
this.btnSubtract.Size = new System.Drawing.Size(32, 23);
this.btnSubtract.TabIndex = 23;
this.btnSubtract.Text = "&-";
this.btnSubtract.Click += new System.EventHandler(this.btnSubtract_Click);
//
// btnSq
//
this.btnSq.Location = new System.Drawing.Point(200, 104);
this.btnSq.Name = "btnSq";
this.btnSq.Size = new System.Drawing.Size(32, 23);
this.btnSq.TabIndex = 24;
this.btnSq.Text = "sq";
this.btnSq.Click += new System.EventHandler(this.btnSq_Click);
//
// btnMultiply
//
this.btnMultiply.Location = new System.Drawing.Point(168, 136);
this.btnMultiply.Name = "btnMultiply";
this.btnMultiply.Size = new System.Drawing.Size(32, 23);
this.btnMultiply.TabIndex = 25;
this.btnMultiply.Text = "&*";
this.btnMultiply.Click += new System.EventHandler(this.btnMultiply_Click);
//
// btnExpo
//
this.btnExpo.Location = new System.Drawing.Point(200, 136);
this.btnExpo.Name = "btnExpo";
this.btnExpo.Size = new System.Drawing.Size(32, 23);
this.btnExpo.TabIndex = 26;
this.btnExpo.Text = "1/x";
this.btnExpo.Click += new System.EventHandler(this.btnExpo_Click);
//
// btnDivide
//
this.btnDivide.Location = new System.Drawing.Point(168, 168);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(32, 23);
this.btnDivide.TabIndex = 27;
this.btnDivide.Text = "&/";
this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click);
//
// btnEqual
//
this.btnEqual.Location = new System.Drawing.Point(200, 168);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(32, 23);
this.btnEqual.TabIndex = 28;
this.btnEqual.Text = "&=";
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
//
// txtDisplay
//
this.txtDisplay.Location = new System.Drawing.Point(16, 8);
this.txtDisplay.Name = "txtDisplay";
this.txtDisplay.Size = new System.Drawing.Size(216, 20);
this.txtDisplay.TabIndex = 29;
this.txtDisplay.Text = "";
this.txtDisplay.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 209);
this.Controls.Add(this.txtDisplay);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnExpo);
this.Controls.Add(this.btnMultiply);
this.Controls.Add(this.btnSq);
this.Controls.Add(this.btnSubtract);
this.Controls.Add(this.btnSqrt);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnDot);
this.Controls.Add(this.btnAddSubt);
this.Controls.Add(this.btn0);
this.Controls.Add(this.btn9);
this.Controls.Add(this.btn8);
this.Controls.Add(this.btn7);
this.Controls.Add(this.btn6);
this.Controls.Add(this.btn5);
this.Controls.Add(this.btn4);
this.Controls.Add(this.btn3);
this.Controls.Add(this.btn2);
this.Controls.Add(this.btn1);
this.Controls.Add(this.btnM);
this.Controls.Add(this.btnMS);
this.Controls.Add(this.btnMR);
this.Controls.Add(this.btnMC);
this.Controls.Add(this.btnCE);
this.Controls.Add(this.btnC);
this.Controls.Add(this.lblMemory);
this.Controls.Add(this.btnBkSpc);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Calculater";
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void btn1_Click(object sender, System.EventArgs e)
{
InputNumber("1");
}
private void btn2_Click(object sender, System.EventArgs e)
{
InputNumber("2");
}
private void btn3_Click(object sender, System.EventArgs e)
{
InputNumber("3");
}
private void btn4_Click(object sender, System.EventArgs e)
{
InputNumber("4");
}
private void btn5_Click(object sender, System.EventArgs e)
{
InputNumber("5");
}
private void btn6_Click(object sender, System.EventArgs e)
{
InputNumber("6");
}
private void btn7_Click(object sender, System.EventArgs e)
{
InputNumber("7");
}
private void btn8_Click(object sender, System.EventArgs e)
{
InputNumber("8");
}
private void btn9_Click(object sender, System.EventArgs e)
{
InputNumber("9");
}
private void btn0_Click(object sender, System.EventArgs e)
{
InputNumber("0");
}
private void btnDot_Click(object sender, System.EventArgs e)
{
InputNumber(".");
}
private void InputNumber(string input)
{
if(newNumber==true)
{
txtDisplay.Text=input;
}
else
{
txtDisplay.Text+=input;
}
newNumber=false;
}
private void btnC_Click(object sender, System.EventArgs e)
{
txtDisplay.Text="0";
result=0;
operation="";
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
OperationClicked("+");
}
private void btnSubtract_Click(object sender, System.EventArgs e)
{
OperationClicked("-");
}
private void btnMultiply_Click(object sender, System.EventArgs e)
{
OperationClicked("*");
}
private void btnDivide_Click(object sender, System.EventArgs e)
{
OperationClicked("/");
}
private void btnSqrt_Click(object sender, System.EventArgs e)
{
OperationClicked("sqrt");
}
private void btnSq_Click(object sender, System.EventArgs e)
{
OperationClicked("sq");
}
private void btnEqual_Click(object sender, System.EventArgs e)
{
performOperation();
}
private void OperationClicked(string op)
{ newNumber=true;
store = double.Parse( txtDisplay.Text);
performOperation();
operation = op;
}
private void performOperation()
{
double term2 = double.Parse(txtDisplay.Text);
if(operation != "")
{
switch(operation)
{
case"+":
result=store+term2;
break;
case"-":
result=store-term2;
break;
case"*":
result=store*term2;
break;
case"/":
result=store/term2;
break;
case"sqrt":
result=Math.Sqrt(store);
break;
case"sq":
result=store*store;
break;
case"exp":
result=Math.Exp(store);
break;
}
txtDisplay.Text = result.ToString();
}
}
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char c = e.KeyChar;
if(char.IsNumber©)
{
if(txtDisplay.Text== "0")
{
txtDisplay.Text = c.ToString();
}
else
{
txtDisplay.Text = txtDisplay.Text+ c;
}
}
}
private void btnExpo_Click(object sender, System.EventArgs e)
{
}
private void btnMC_Click(object sender, System.EventArgs e)
{
memory=0;
txtDisplay.Text="";
lblMemory.Text="";
}
private void btnMR_Click(object sender, System.EventArgs e)
{
txtDisplay.Text=memory.ToString();
}
private void btnMS_Click(object sender, System.EventArgs e)
{
memory=Double.Parse(txtDisplay.Text);
txtDisplay.Text="";
lblMemory.Text="M";
}
private void btnM_Click(object sender, System.EventArgs e)
{
memory+=double.Parse(txtDisplay.Text);
}
private void btnCE_Click(object sender, System.EventArgs e)
{
txtDisplay.Text="0";
}
private void btnBkSpc_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyData)
{
case Keys.Back:
if(txtDisplay.Text.Length-1>=0)
{
txtDisplay.Text=txtDisplay.Text.Remove(txtDisplay.Text.Length-1,1);
}
break;
case Keys.Enter:
txtDisplay.Text=result.ToString();
break;
}
}
private void btnBkSpc_Click(object sender, System.EventArgs e)
{
}
}
}
This post has been edited by PsychoCoder: 7 Nov, 2007 - 06:00 PM