C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-11-2004, 06:37 AM   #1
Registered User
 
Join Date: Jan 2004
Posts: 13
Need help with calculator program

Hi, I have been trying to program a simple calculator all day but I just can't get it to work right. It works fine when using the one operator like + or - ect but when mixing them in a calculation like 2+2+2-1 it gives me the answer 1. I just don't get it. I've drawn diagrams of data flow and stuff but i'm stuck.
I know its a big ask but if someone could paste this into vs .net and see where the problem is that would be great. I only wrote code for the numbers 1, 2 and 5. I just need to find a way to make the operators work.

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Calculator
{

	public class Calculator : System.Windows.Forms.Form
	{
		//globals
		decimal result1 = 0;
		decimal result2 = 0;
		decimal result3 = 0;
		string op = "";
		

		private System.Windows.Forms.TextBox screen;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button butplus;
		private System.Windows.Forms.Button butequals;
		private System.Windows.Forms.Button butclear;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.Button button8;
		private System.Windows.Forms.Button button9;
		private System.Windows.Forms.Button button0;
		private System.Windows.Forms.Button butminus;
		private System.Windows.Forms.Button butdivide;
		private System.Windows.Forms.Button butmulti;
		private System.Windows.Forms.Button butsigned;
		private System.Windows.Forms.Button butdecimal;

		private System.ComponentModel.Container components = null;

		public Calculator()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code

		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Calculator));
			this.screen = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.butplus = new System.Windows.Forms.Button();
			this.butequals = new System.Windows.Forms.Button();
			this.butclear = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.button7 = new System.Windows.Forms.Button();
			this.button8 = new System.Windows.Forms.Button();
			this.button9 = new System.Windows.Forms.Button();
			this.button0 = new System.Windows.Forms.Button();
			this.butminus = new System.Windows.Forms.Button();
			this.butdivide = new System.Windows.Forms.Button();
			this.butmulti = new System.Windows.Forms.Button();
			this.butsigned = new System.Windows.Forms.Button();
			this.butdecimal = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// screen
			// 
			this.screen.BackColor = System.Drawing.SystemColors.HighlightText;
			this.screen.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.screen.Location = new System.Drawing.Point(24, 24);
			this.screen.MaxLength = 30;
			this.screen.Name = "screen";
			this.screen.ReadOnly = true;
			this.screen.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.screen.Size = new System.Drawing.Size(312, 29);
			this.screen.TabIndex = 0;
			this.screen.Text = "0";
			// 
			// button1
			// 
			this.button1.BackColor = System.Drawing.Color.Black;
			this.button1.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button1.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button1.Location = new System.Drawing.Point(196, 92);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(32, 32);
			this.button1.TabIndex = 1;
			this.button1.Text = "1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.BackColor = System.Drawing.Color.Black;
			this.button2.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button2.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button2.Location = new System.Drawing.Point(232, 92);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(32, 32);
			this.button2.TabIndex = 2;
			this.button2.Text = "2";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// butplus
			// 
			this.butplus.BackColor = System.Drawing.Color.Black;
			this.butplus.Cursor = System.Windows.Forms.Cursors.Hand;
			this.butplus.Font = new System.Drawing.Font("Comic Sans MS", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butplus.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.butplus.Location = new System.Drawing.Point(304, 92);
			this.butplus.Name = "butplus";
			this.butplus.Size = new System.Drawing.Size(32, 32);
			this.butplus.TabIndex = 3;
			this.butplus.Text = "+";
			this.butplus.Click += new System.EventHandler(this.butplus_Click);
			// 
			// butequals
			// 
			this.butequals.BackColor = System.Drawing.Color.Black;
			this.butequals.Cursor = System.Windows.Forms.Cursors.Hand;
			this.butequals.Font = new System.Drawing.Font("Comic Sans MS", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butequals.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.butequals.Location = new System.Drawing.Point(160, 92);
			this.butequals.Name = "butequals";
			this.butequals.Size = new System.Drawing.Size(32, 32);
			this.butequals.TabIndex = 4;
			this.butequals.Text = "=";
			this.butequals.Click += new System.EventHandler(this.butequals_Click);
			// 
			// butclear
			// 
			this.butclear.BackColor = System.Drawing.Color.Brown;
			this.butclear.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butclear.Location = new System.Drawing.Point(48, 84);
			this.butclear.Name = "butclear";
			this.butclear.Size = new System.Drawing.Size(75, 28);
			this.butclear.TabIndex = 5;
			this.butclear.Text = "Clear";
			this.butclear.Click += new System.EventHandler(this.butclear_Click);
			// 
			// button3
			// 
			this.button3.BackColor = System.Drawing.Color.Black;
			this.button3.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button3.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button3.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button3.Location = new System.Drawing.Point(268, 92);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(32, 32);
			this.button3.TabIndex = 6;
			this.button3.Text = "3";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.BackColor = System.Drawing.Color.Black;
			this.button4.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button4.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button4.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button4.Location = new System.Drawing.Point(196, 128);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(32, 32);
			this.button4.TabIndex = 7;
			this.button4.Text = "4";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button5
			// 
			this.button5.BackColor = System.Drawing.Color.Black;
			this.button5.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button5.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button5.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button5.Location = new System.Drawing.Point(232, 128);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(32, 32);
			this.button5.TabIndex = 8;
			this.button5.Text = "5";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// button6
			// 
			this.button6.BackColor = System.Drawing.Color.Black;
			this.button6.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button6.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button6.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button6.Location = new System.Drawing.Point(268, 128);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(32, 32);
			this.button6.TabIndex = 9;
			this.button6.Text = "6";
			this.button6.Click += new System.EventHandler(this.button6_Click);
			// 
			// button7
			// 
			this.button7.BackColor = System.Drawing.Color.Black;
			this.button7.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button7.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button7.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button7.Location = new System.Drawing.Point(196, 164);
			this.button7.Name = "button7";
			this.button7.Size = new System.Drawing.Size(32, 32);
			this.button7.TabIndex = 10;
			this.button7.Text = "7";
			this.button7.Click += new System.EventHandler(this.button7_Click);
			// 
			// button8
			// 
			this.button8.BackColor = System.Drawing.Color.Black;
			this.button8.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button8.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button8.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button8.Location = new System.Drawing.Point(232, 164);
			this.button8.Name = "button8";
			this.button8.Size = new System.Drawing.Size(32, 32);
			this.button8.TabIndex = 11;
			this.button8.Text = "8";
			this.button8.Click += new System.EventHandler(this.button8_Click);
			// 
			// button9
			// 
			this.button9.BackColor = System.Drawing.Color.Black;
			this.button9.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button9.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button9.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button9.Location = new System.Drawing.Point(268, 164);
			this.button9.Name = "button9";
			this.button9.Size = new System.Drawing.Size(32, 32);
			this.button9.TabIndex = 12;
			this.button9.Text = "9";
			this.button9.Click += new System.EventHandler(this.button9_Click);
			// 
			// button0
			// 
			this.button0.BackColor = System.Drawing.Color.Black;
			this.button0.Cursor = System.Windows.Forms.Cursors.Hand;
			this.button0.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button0.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.button0.Location = new System.Drawing.Point(196, 200);
			this.button0.Name = "button0";
			this.button0.Size = new System.Drawing.Size(32, 32);
			this.button0.TabIndex = 13;
			this.button0.Text = "0";
			this.button0.Click += new System.EventHandler(this.button0_Click);
			// 
			// butminus
			// 
			this.butminus.BackColor = System.Drawing.Color.Black;
			this.butminus.Cursor = System.Windows.Forms.Cursors.Hand;
			this.butminus.Font = new System.Drawing.Font("Comic Sans MS", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butminus.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.butminus.Location = new System.Drawing.Point(304, 128);
			this.butminus.Name = "butminus";
			this.butminus.Size = new System.Drawing.Size(32, 32);
			this.butminus.TabIndex = 14;
			this.butminus.Text = "-";
			this.butminus.Click += new System.EventHandler(this.butminus_Click);
			// 
			// butdivide
			// 
			this.butdivide.BackColor = System.Drawing.Color.Black;
			this.butdivide.Cursor = System.Windows.Forms.Cursors.Hand;
			this.butdivide.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butdivide.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.butdivide.Location = new System.Drawing.Point(304, 164);
			this.butdivide.Name = "butdivide";
			this.butdivide.Size = new System.Drawing.Size(32, 32);
			this.butdivide.TabIndex = 15;
			this.butdivide.Text = "/";
			this.butdivide.Click += new System.EventHandler(this.butdivide_Click);
			// 
			// butmulti
			// 
			this.butmulti.BackColor = System.Drawing.Color.Black;
			this.butmulti.Cursor = System.Windows.Forms.Cursors.Hand;
			this.butmulti.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butmulti.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.butmulti.Location = new System.Drawing.Point(304, 200);
			this.butmulti.Name = "butmulti";
			this.butmulti.Size = new System.Drawing.Size(32, 32);
			this.butmulti.TabIndex = 16;
			this.butmulti.Text = "x";
			this.butmulti.Click += new System.EventHandler(this.butmulti_Click);
			// 
			// butsigned
			// 
			this.butsigned.BackColor = System.Drawing.Color.Black;
			this.butsigned.Cursor = System.Windows.Forms.Cursors.Hand;
			this.butsigned.Font = new System.Drawing.Font("Comic Sans MS", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butsigned.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.butsigned.Location = new System.Drawing.Point(268, 200);
			this.butsigned.Name = "butsigned";
			this.butsigned.Size = new System.Drawing.Size(32, 32);
			this.butsigned.TabIndex = 17;
			this.butsigned.Text = "+/-";
			this.butsigned.Click += new System.EventHandler(this.butsigned_Click);
			// 
			// butdecimal
			// 
			this.butdecimal.BackColor = System.Drawing.Color.Black;
			this.butdecimal.Cursor = System.Windows.Forms.Cursors.Hand;
			this.butdecimal.Font = new System.Drawing.Font("Comic Sans MS", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butdecimal.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.butdecimal.Location = new System.Drawing.Point(232, 200);
			this.butdecimal.Name = "butdecimal";
			this.butdecimal.Size = new System.Drawing.Size(32, 32);
			this.butdecimal.TabIndex = 18;
			this.butdecimal.Text = ".";
			this.butdecimal.Click += new System.EventHandler(this.butdecimal_Click);
			// 
			// Calculator
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.Silver;
			this.ClientSize = new System.Drawing.Size(360, 406);
			this.Controls.Add(this.butdecimal);
			this.Controls.Add(this.butsigned);
			this.Controls.Add(this.butmulti);
			this.Controls.Add(this.butdivide);
			this.Controls.Add(this.butminus);
			this.Controls.Add(this.button0);
			this.Controls.Add(this.button9);
			this.Controls.Add(this.button8);
			this.Controls.Add(this.button7);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.butclear);
			this.Controls.Add(this.butequals);
			this.Controls.Add(this.butplus);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.screen);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Calculator";
			this.Text = "The Bruce Calculator";
			this.ResumeLayout(false);

		}
		#endregion


		[STAThread]
		static void Main() 
		{
			Application.Run(new Calculator());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			if(screen.Text == "0")
			{
				this.screen.Text = "1";
			}
			else
			{
				screen.AppendText("1");
			}


			}

		private void button2_Click(object sender, System.EventArgs e)
		{
			if(screen.Text == "0")
			{
				this.screen.Text = "2";
			}
			else
			{
				screen.AppendText("2");
			}
		}



		private void button3_Click(object sender, System.EventArgs e)
		{
		
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
		
		}

		private void button5_Click(object sender, System.EventArgs e)
		{
			if(screen.Text == "0")
			{
				this.screen.Text = "5";
			}
			else
			{
				screen.AppendText("5");
			}
		}

		private void button6_Click(object sender, System.EventArgs e)
		{
		
		}

		private void button7_Click(object sender, System.EventArgs e)
		{
		
		}

		private void button8_Click(object sender, System.EventArgs e)
		{
		
		}

		private void button9_Click(object sender, System.EventArgs e)
		{
		
		}

		private void button0_Click(object sender, System.EventArgs e)
		{
		
		}

		private void butclear_Click(object sender, System.EventArgs e)
		{
			screen.Text = "0";
			result1 = 0;
			result2 = 0;
			result3 = 0;
		}

		private void butplus_Click(object sender, System.EventArgs e)
		{	
			op = "+";
			if(result1 == 0)
			{
				result1 = decimal.Parse(screen.Text);
				screen.Text = "";
			}
			else
			{
				result1 = result1 + decimal.Parse(screen.Text);
				screen.Text = "";
			}
			

		}

		private void butequals_Click(object sender, System.EventArgs e)
		{
			result2 = decimal.Parse(screen.Text);
			switch(op)
			{
				case "+":
					arithmetic temp = new arithmetic();
					result3 = temp.add(result1, result2);
					result1 = result3;
					screen.Text = result1.ToString();
					result3 = 0;
					
					break;

				case "-":
					arithmetic temp1 = new arithmetic();
					result3 = temp1.minus(result1, result2);
					result1 = result3;
					screen.Text = result1.ToString();
					result3 = 0;
					break;

				case "/":
					arithmetic temp2 = new arithmetic();
					result3 = temp2.divide(result1, result2);
					result1 = result3;
					screen.Text = result1.ToString();
					result3 = 0;
					break;

				case "*":
					arithmetic temp3 = new arithmetic();
					result3 = temp3.multi(result1, result2);
					result1 = result3;
					screen.Text = result1.ToString();
					result3 = 0;
					break;
			}
			
		}

		private void butminus_Click(object sender, System.EventArgs e)
		{
			op = "-";
			if(result1 == 0)
			{
				result1 = decimal.Parse(screen.Text);
				screen.Text = "";
			}
			else
			{
				result1 = result1 - decimal.Parse(screen.Text);
				screen.Text = "";
			}
		}

		private void butdivide_Click(object sender, System.EventArgs e)
		{
			op = "/";
			if(result1 == 0)
			{
				result1 = decimal.Parse(screen.Text);
				screen.Text = "";
			}
			else
			{
				result1 = result1 / decimal.Parse(screen.Text);
				screen.Text = "";
			}
		}

		private void butmulti_Click(object sender, System.EventArgs e)
		{
			op = "*";
			if(result1 == 0)
			{
				result1 = decimal.Parse(screen.Text);
				screen.Text = "";
			}
			else
			{
				result1 = result1 * decimal.Parse(screen.Text);
				screen.Text = "";
			}
		}

		private void butdecimal_Click(object sender, System.EventArgs e)
		{
		
		}

		private void butsigned_Click(object sender, System.EventArgs e)
		{
		
		}


	}
}


///////////////class in another file

using System;

namespace Calculator
{
	
	public class arithmetic
	{
		public arithmetic()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public decimal add(decimal a, decimal b)
		{
			return a + b;
		}

		public decimal minus(decimal a, decimal b)
		{
			return a - b;
		}

		public decimal divide(decimal a, decimal b)
		{
			return a / b;
		}

		public decimal multi(decimal a, decimal b)
		{
			return a * b;
		}


	}
}
Thanks heaps for any help with this,
Kate
Kate is offline   Reply With Quote
Old 01-16-2004, 10:48 AM   #2
Registered User
 
Join Date: Nov 2002
Posts: 10
I think your problem lies in your butequals_Click(), when you switch on 'op' it will only switch on the last value assigned to 'op'.
Calthun is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 05:32 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22