• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

利用C#编写简易计算器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

程序代码如下。

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;
			}
		}
		
	}
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap