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

C#窗体计算器

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

主要使用的是TextBox和button控件

 

  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.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace WindowsFormsApplication1
 12 {
 13     public partial class jisuanqi : Form
 14     {
 15         //存储上次点击了什么按钮,0代表什么都没点击,1代表点击了数字按钮,2代表点击了运算符
 16         private int prev = 0;
 17         //存储中间结果
 18         private decimal zjjg = 0;
 19         //记录上次按的什么运算符
 20         private string  preysf = "+";
 21         //记录上次按的是等号
 22         private int denghao = 0;
 23 
 24 
 25         public jisuanqi()
 26         {
 27             InitializeComponent();
 28         }
 29 
 30         private void button1_Click(object sender, EventArgs e)
 31         {
 32             Button btn = sender as Button;
 33 
 34             //替换(如果下面文本框内容为0或者上次点击了运算符)
 35             if (prev == 2 || TXTbottom.Text == "0")
 36             {
 37                 TXTbottom.Text = btn.Text;
 38             }
 39             //追加(如果下面文本框内容不为0并且上次没有点击运算符)
 40             else
 41             {
 42                 TXTbottom.Text += btn.Text;
 43             }
 44 
 45             prev = 1;
 46             
 47         }
 48 
 49         private void button4_Click(object sender, EventArgs e)
 50         {
 51             Button btn = sender as Button;
 52 
 53            
 54 
 55             //上次按了数字
 56             if (prev == 1)
 57             {
 58                 TXTTOP.Text += TXTbottom.Text + btn.Text;
 59                 switch (preysf)
 60                 {
 61                     case "+":
 62                         zjjg = zjjg + Convert.ToDecimal(TXTbottom.Text);
 63                         break;
 64                     case "-":
 65                         zjjg = zjjg - Convert.ToDecimal(TXTbottom.Text);
 66                         break;
 67                     case "*":
 68                         zjjg = zjjg * Convert.ToDecimal(TXTbottom.Text);
 69                         break;
 70                     case "/":
 71                         zjjg = zjjg / Convert.ToDecimal(TXTbottom.Text);
 72                         break;
 73                 }
 74                 TXTbottom.Text = zjjg.ToString();
 75             }
 76             //上次按了运算符///
 77             else
 78             {
 79                 string s = TXTTOP.Text;
 80                 if (s.Length >= 1)
 81                 {
 82                     s = s.Substring(0, s.Length - 1);
 83                     s = s + btn.Text;
 84                 }
 85                 else
 86                 {
 87                     s = "0";
 88                     s = s + btn.Text;
 89                 }
 90                 TXTTOP.Text = s;
 91             }
 92             //加号
 93             prev = 2;
 94             preysf = btn.Text;
 95         }
 96 
 97         private void button15_Click(object sender, EventArgs e)
 98         {
 99             TXTTOP.Text = "";
100             TXTbottom.Text = "0";
101             prev = 0;
102             zjjg = 0;
103             preysf = "+";
104         }
105 
106         private void jisuanqi_KeyDown(object sender, KeyEventArgs e)
107         {
108 
109 
110             if (
111                 e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0 ||
112                 e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1 || 
113                 e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2 ||
114                 e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3 ||
115                 e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4 || 
116                 e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5 ||
117                 e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6 ||
118                 e.KeyCode == Keys.D7 || e.KeyCode == Keys.NumPad7 || 
119                 e.KeyCode == Keys.D8 || e.KeyCode == Keys.NumPad8 ||
120                 e.KeyCode == Keys.D9 || e.KeyCode == Keys.NumPad9 
121                 )
122             {
123                 if (prev == 2 || TXTbottom.Text == "0")
124                 {
125                     string a = e.KeyData.ToString();
126                     TXTbottom.Text = a.Substring((a.Length-1),1); 
127                 }
128                 else
129                 {
130                     string a = e.KeyData.ToString();
131                     TXTbottom.Text += a.Substring((a.Length - 1), 1); 
132                     
133                 }
134                 prev = 1;
135             }
136             //else if (
137             //    //e.KeyCode == Keys.Add || e.KeyCode == Keys.Subtract || e.KeyCode == Keys.Multiply || e.KeyCode == Keys.Divide||
138             //    e.KeyCode==Keys.Shift&&e.KeyCode==Keys.D8
139             //    )
140             //{
141             //    MessageBox.Show(e.KeyData.ToString());
142             //}
143             else if(e.KeyCode==Keys.Back)
144             {
145                 string s = TXTbottom.Text;
146                 if (s.Length >= 1)
147                 {
148                     s = s.Substring(0, s.Length - 1);
149                 }
150                 else
151                 {
152                     s = "";
153                 }
154                 TXTbottom.Text = s;
155             }
156         }
157 
158         private void button17_Click(object sender, EventArgs e)
159         {
160             string s = TXTbottom.Text;
161             if (s.Length >= 1)
162             {
163                 s = s.Substring(0, s.Length - 1);
164             }
165             else
166             {
167                 s = "";
168             }
169             TXTbottom.Text = s;
170         }
171 
172         private void button14_Click(object sender, EventArgs e)//等号
173         {
174             Button btn = sender as Button;
175 
176             //上次按了数字
177             if (prev == 1)
178             {
179                 TXTTOP.Text += TXTbottom.Text + btn.Text;
180                 switch (preysf)
181                 {
182                     case "+":
183                         zjjg = zjjg + Convert.ToDecimal(TXTbottom.Text);
184                         break;
185                     case "-":
186                         zjjg = zjjg - Convert.ToDecimal(TXTbottom.Text);
187                         break;
188                     case "*":
189                         zjjg = zjjg * Convert.ToDecimal(TXTbottom.Text);
190                         break;
191                     case "/":
192                         zjjg = zjjg / Convert.ToDecimal(TXTbottom.Text);
193                         break;
194                 }
195                 TXTbottom.Text = zjjg.ToString();
196             }
197             //上次按了运算符///
198             else
199             {
200                 string s = TXTTOP.Text;
201                 if (s.Length >= 1)
202                 {
203                     s = s.Substring(0, s.Length - 1);
204                     s = s + btn.Text;
205                 }
206                 else
207                 {
208                     s = "0";
209                     s = s + btn.Text;
210                 }
211                 TXTbottom.Text = s;
212             }
213             //加号
214             prev = 2;
215             preysf = btn.Text;
216             TXTTOP.Text = "";
217             zjjg = 0;
218             preysf = "+";
219             denghao = 1;
220         }
221 
222         private void jisuanqi_Load(object sender, EventArgs e)
223         {
224 
225         }
226     }
227 }

鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
centos7安装mysql和mysql-connector-c++发布时间:2022-07-13
下一篇:
c++switch语句发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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