C#窗体计算器

2018-06-23 23:19:30来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

使用窗体写的计算器小程序,不是十分完善,键盘输入只能输入数字键,其他需要换档键的键的输入没有搞懂,先发上来左右,以作留存。

界面截图

主要使用的是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 }

程序冗余还是有点多,目前没有想要要怎么精简

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ADO.Net(五)——实战:对战游戏

下一篇:开通博客,迁移记录