代码改变世界

利用C#编写简易计算器

2012-04-08 11:47  精诚所至 金石为开  阅读(2992)  评论(5编辑  收藏  举报

本例创建一个简易计算器,能进行加减乘除的计算,程序界面如下图所示。

20120409114358

程序代码如下。

using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace eg43_addCtrlAtRuntimeApp
{

	public partial class Calculator1 : Form
	{
		private int opMain=0;
		private double mainNum1=0;
		private double mainNum2=0;
		private bool isSecond=false;
		private bool isDone=false;
		private bool isDecimal=false;
		public Calculator1()
		{
			InitializeComponent();
		}
		
		void Calculator1KeyDown(object sender, KeyEventArgs e)
		{
			filterKeys(e.KeyValue);
		}
		
		void Btn1Click(object sender, EventArgs e)
		{
			setText("1");
		}
		
		
		void Btn2Click(object sender, EventArgs e)
		{
			setText("2");
		}
		
		void Btn3Click(object sender, EventArgs e)
		{
			setText("3");
		}
		
		void Btn4Click(object sender, EventArgs e)
		{
			setText("4");
		}
		
		void Btn5Click(object sender, EventArgs e)
		{
			setText("5");
		}
		
		void Btn6Click(object sender, EventArgs e)
		{
			setText("6");
		}
		
		void Btn7Click(object sender, EventArgs e)
		{
			setText("7");
		}
		
		void Btn8Click(object sender, EventArgs e)
		{
			setText("8");
		}
		
		void Btn9Click(object sender, EventArgs e)
		{
			setText("9");
		}
		
		void Btn0Click(object sender, EventArgs e)
		{
			setText("0");
		}
		
		void BtnAddClick(object sender, EventArgs e)
		{
			setOperator(1);
		}
		
		void BtnSubtractClick(object sender, EventArgs e)
		{
			setOperator(2);
		}
		
		void BtnMultiplyClick(object sender, EventArgs e)
		{
			setOperator(3);
		}
		
		void BtnDivideClick(object sender, EventArgs e)
		{
			setOperator(4);
		}
		
		void BtnClearClick(object sender, EventArgs e)
		{
			isSecond=false;
			setText("clear");
		}
		
		void BtnNegativeClick(object sender, EventArgs e)
		{
			changeSign();
		}
		
		void BtnDecimalClick(object sender, EventArgs e)
		{
			setDecimal();
		}
		
		void BtnEqualsClick(object sender, EventArgs e)
		{
			doEquals();
		}
		public void setText(string textset)
		{
			if(textset.Equals("clear"))
			{
				txtResult.Text="";
				isDone=false;
				isSecond=false;
				isDecimal=false;
			}
			else
			{
				if(isSecond)
				{
					txtResult.Text=textset;
					isSecond=false;
					isDecimal=false;
				}
				else
				{
					if(isDone)
					{
						txtResult.Text=textset;
						isDone=false;
					}
					else
					{
						txtResult.Text+=textset;
					}
				}
			}
			btnEquals.Select();
		}
		public void Calc(double num1,double num2,int op)
		{
			double answer=0;
			switch(op)
			{
				case 1:
					answer=num1+num2;
					break;
				case 2:
					answer=num1-num2;
					break;
				case 3:
					answer=num1*num2;
					break;
				case 4:
					answer=num1/num2;
					break;
			}
			setText(answer.ToString());
		}
		private void doEquals()
		{
			mainNum2=double.Parse(txtResult.Text);
			setText("clear");
			Calc(mainNum1,mainNum2,opMain);
			isDone=true;
		}
		private void changeSign()
		{
			double storNum;
			if(txtResult.Text.Length>0)
			{
				storNum=double.Parse(txtResult.Text);
				storNum*=-1;
				txtResult.Text=storNum.ToString();
			}
			btnEquals.Select();
		}
		private void setOperator(int operation)
		{
			if(txtResult.Text.Length>0)
			{
				opMain=operation;
				mainNum1=double.Parse(txtResult.Text);
				isSecond=true;
				isDone=false;
				btnEquals.Select();
			}
		}
		private void setDecimal()
		{
			if(!isDecimal)
			{
				setText(".");
				isDecimal=true;
			}
			btnEquals.Select();
		}
		public void filterKeys(int keyCode)
		{
			switch(keyCode)
			{
				case 96:
					setText("0");
					break;
				case 97:
					setText("1");
					break;
				case 98:
					setText("2");
					break;
				case 99:
					setText("3");
					break;
				case 100:
					setText("4");
					break;
				case 101:
					setText("5");
					break;
				case 102:
					setText("6");
					break;
				case 103:
					setText("7");
					break;
				case 104:
					setText("8");
					break;
				case 105:
					setText("9");
					break;
				case 67:
					setText("clear");
					break;
				case 107:
					setOperator(1);
					break;
				case 109:
					setOperator(2);
					break;
				case 106:
					setOperator(3);
					break;
				case 111:
					setOperator(4);
					break;
				case 110:
					setDecimal();
					break;
			}
		}
		
	}
}