C#实现简易计算器

任务

做一个简单的计算器,能够实现加减法。

  

代码

1.	using System;  
2.	using System.Collections.Generic;  
3.	using System.ComponentModel;  
4.	using System.Data;  
5.	using System.Drawing;  
6.	using System.Linq;  
7.	using System.Text;  
8.	using System.Windows.Forms;  
9.	  
10.	namespace WindowsFormsApplication1  
11.	{  
12.	    public partial class 简易计算器 : Form  
13.	    {  
14.	        double total = 0;  
15.	        double t = 0;  
16.	        string p="";  
17.	  
18.	        public 简易计算器()  
19.	        {  
20.	            InitializeComponent();  
21.	        }  
22.	  
23.	        private void buttonNum_Click(object sender, EventArgs e) //数字键  
24.	        {  
25.	            if (sender == button1) DispBox.Text += "1";  
26.	            else if (sender == button2) DispBox.Text += "2";  
27.	            else if (sender == button3) DispBox.Text += "3";  
28.	            else if (sender == button4) DispBox.Text += "4";  
29.	            else if (sender == button5) DispBox.Text += "5";  
30.	            else if (sender == button6) DispBox.Text += "6";  
31.	            else if (sender == button7) DispBox.Text += "7";  
32.	            else if (sender == button8) DispBox.Text += "8";  
33.	            else if (sender == button9) DispBox.Text += "9";  
34.	        }  
35.	  
36.	  
37.	        private void button_Op_Click(object sender, EventArgs e)  
38.	        {  
39.	            total = double.Parse(DispBox.Text); //保存之前的值  
40.	          
41.	              if (sender == ButtonAdd)  
42.	            {  
43.	                DispBox.Text = String.Empty;   //输入符号前先清屏  
44.	                p = "+";  
45.	            }  
46.	              else if (sender == ButtonSub)  
47.	            {  
48.	                DispBox.Text = String.Empty;  
49.	                p = "-";  
50.	            }  
51.	          
52.	        }  
53.	  
54.	  
55.	        private void ButtonResult_Click(object sender, EventArgs e) //结果显示  
56.	        {  
57.	            t = double.Parse(DispBox.Text);   
58.	  
59.	            DispBox.Text = String.Empty;  
60.	            DispBox.Text += total;  
61.	            if (p == "+")  
62.	            {  
63.	                total += t;  
64.	                DispBox.Text += "+";  
65.	            }  
66.	            else if (p == "-")  
67.	            {  
68.	                total -= t;  
69.	                DispBox.Text += "-";  
70.	            }  
71.	            DispBox.Text += t;  
72.	            DispBox.Text += "=";  
73.	            DispBox.Text += total;  
74.	            total = 0;  
75.	            p = "";  
76.	        }  
77.	  
78.	        private void button13_Click(object sender, EventArgs e) //清屏  
79.	        {  
80.	            DispBox.Text=String.Empty;  
81.	        }  
82.	   
83.	    }  
84.	} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值