暑假作业之一——用C#写一个最基本功能的计算器
我参照了Win10自带的计算器进行了设计和计算规则的编写(如图)
写完忽然想到——按完+号再按±会有什么反应?
8+6+negative(14)
改改改
那……直接按等于号呢?
2+= 4
也就是加最开始的2?
2+3+ = 10
不对,是加“目前的数”,也就是显示在下面窗口的数字
改改改
然后我妹妹来玩,对着最开始的零±±±±±±±±±±±±±±±崩了
改改改
然后把MR、MC、M+设置为不可多难过除非按了MS,在这些按键上增添鼠标悬浮时显示提示信息……
以上……但愿没bug了
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 Caculator 12 { 13 public partial class Form1 : Form 14 { 15 int f1, f2, f3, f4, f5,top=-1;//标记 16 double left, right;//记录左右数字 17 String MS, negate;//MS储存内容,negate用于正负号的操作 18 Char symbol;//记录运算符 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 private void Form1_Load(object sender, EventArgs e)//窗体程序运行前执行 25 { 26 this.textBox1.Text = Convert.ToString(0); 27 f1 = 0;//符号数字,是否有符号在数字前,数字在符号前,1为是,下同 28 f2 = 0;//总第一个符号 29 f3 = 0;//第一个等号 30 f4 = 0;//是否按了= 31 f5 = 0;//是否按了“+/-”,按其他符号则还原为0 32 left = 0; 33 right = 0; 34 this.button9.Enabled = false; 35 this.button19.Enabled = false; 36 this.button14.Enabled = false; 37 this.toolTip1.SetToolTip(this.button3, "清除");//提示信息 38 this.toolTip1.SetToolTip(this.button2, "取消"); 39 this.toolTip1.SetToolTip(this.button8, "存入储存"); 40 this.toolTip1.SetToolTip(this.button9, "读取读取"); 41 this.toolTip1.SetToolTip(this.button14, "清除储存"); 42 this.toolTip1.SetToolTip(this.button19, "在存储上加上"); 43 } 44 private void button1_Click(object sender, EventArgs e)//'=' 45 { 46 this.textBox2.Clear(); 47 if (f3 == 0)//第一次按等于号时,连续按等于号即右操作数不变,不断与左操作数进行运算操作 48 { 49 right = Convert.ToDouble(textBox1.Text); 50 f3 = 1; 51 } 52 if (f2 == 1)//按完运算符直接按等于号,2+3+=,计算为与自身相加2+3+(2+3)= 53 { 54 if (symbol == '+') 55 { 56 left += right; 57 } 58 if (symbol == '-') 59 { 60 left -= right; 61 } 62 if (symbol == '*') 63 { 64 left *= right; 65 } 66 if (symbol == '/') 67 { 68 left /= right; 69 } 70 } 71 else 72 { 73 left = right; 74 f2 = 1; 75 } 76 this.textBox1.Text = Convert.ToString(left); 77 f4 = 1; 78 } 79 80 private void button2_Click(object sender, EventArgs e)//CE清空当前值 81 { 82 this.textBox1.Text = "0"; 83 } 84 85 private void button3_Click(object sender, EventArgs e)//C全部清空 86 { 87 this.textBox1.Text= "0"; 88 textBox2.Clear(); 89 left = 0; 90 right = 0; 91 f1 = 0; 92 f2 = 0; 93 f3 = 0; 94 f4 = 0; 95 f5 = 0; 96 } 97 private void button4_Click(object sender, EventArgs e)//除法符号 98 { 99 if (f4 == 1)//按等于号后直接按运算符,将运算结果记为左操作数,对标记进行还原 100 { 101 f2 = 0; 102 f3 = 0; 103 f4 = 0; 104 } 105 if (f1 == 0)//运算符前有数字,连续输入运算符,理解为更改运算符,以最后一个为准 106 { 107 right = Convert.ToDouble(textBox1.Text);//将数字记为右操作数 108 this.textBox2.Text = this.textBox2.Text + this.textBox1.Text + "/";//窗口2显示总算式 109 110 if (f2 == 1)//按这个运算符时,对上一个运算符进行计算 111 { 112 if (symbol == '+') 113 { 114 left += right; 115 } 116 if (symbol == '-') 117 { 118 left -= right; 119 } 120 if (symbol == '*') 121 { 122 left *= right; 123 } 124 if (symbol == '/') 125 { 126 left /= right; 127 } 128 } 129 else//第一个运算符不进行运算操作 130 { 131 left = right; 132 f2 = 1; 133 } 134 this.textBox1.Text = Convert.ToString(left);//将计算结果显示到窗口一 135 } 136 else if (f2 == 1) 137 { 138 this.textBox2.Text = this.textBox2.Text.Remove(this.textBox2.Text.Length - 1); 139 this.textBox2.Text = this.textBox2.Text + "/"; 140 } 141 symbol = '/';//记录当前运算符(未经性计算) 142 f1 = 1; 143 negate = Convert.ToString(left); 144 f5 = 0; 145 } 146 147 private void button5_Click(object sender, EventArgs e) 148 { 149 input(9,e);//使用input函数进行输入操作,下同 150 } 151 152 private void button6_Click(object sender, EventArgs e) 153 { 154 input(8,e); 155 } 156 157 private void button7_Click(object sender, EventArgs e) 158 { 159 input(7,e); 160 } 161 162 private void button8_Click(object sender, EventArgs e)//MS,储存当前数字,并允许使用MR,M+,MC 163 { 164 MS = this.textBox1.Text; 165 this.button9.Enabled = true; 166 this.button19.Enabled = true; 167 this.button14.Enabled = true; 168 } 169 private void button9_Click(object sender, EventArgs e)//MR,将当前数字变为储存的数字 170 { 171 f1 = 0;//相当于输入了数字,f1记为0 172 this.textBox1.Text = MS; 173 } 174 175 private void button10_Click(object sender, EventArgs e) 176 { 177 input(4,e); 178 } 179 180 private void button11_Click(object sender, EventArgs e) 181 { 182 input(5,e); 183 } 184 185 private void button12_Click(object sender, EventArgs e) 186 { 187 input(6,e); 188 } 189 190 private void button13_Click(object sender, EventArgs e)//乘法,详细注释同除法 191 { 192 if (f4 == 1) 193 { 194 f2 = 0; 195 f3 = 0; 196 f4 = 0; 197 } 198 199 if (f1 == 0) 200 { 201 right = Convert.ToDouble(textBox1.Text); 202 this.textBox2.Text = this.textBox2.Text + this.textBox1.Text + "*"; 203 204 if (f2 == 1) 205 { 206 if (symbol == '+') 207 { 208 left += right; 209 } 210 if (symbol == '-') 211 { 212 left -= right; 213 } 214 if (symbol == '*') 215 { 216 left *= right; 217 } 218 if (symbol == '/') 219 { 220 left /= right; 221 } 222 } 223 else 224 { 225 left = right; 226 f2 = 1; 227 } 228 this.textBox1.Text = Convert.ToString(left); 229 } 230 else if (f2 == 1) 231 { 232 this.textBox2.Text = this.textBox2.Text.Remove(this.textBox2.Text.Length - 1); 233 this.textBox2.Text = this.textBox2.Text + "*"; 234 } 235 symbol = '*'; 236 f1 = 1; 237 negate = Convert.ToString(left); 238 f5 = 0; 239 } 240 241 private void button14_Click(object sender, EventArgs e)//MC,清空储存,并禁止MR,M+,MC 242 { 243 MS = ""; 244 this.button9.Enabled = false; 245 this.button19.Enabled = false; 246 this.button14.Enabled = false; 247 } 248 249 private void button15_Click(object sender, EventArgs e) 250 { 251 252 input(1,e); 253 } 254 255 private void button16_Click(object sender, EventArgs e) 256 { 257 258 input(2,e); 259 } 260 261 private void button17_Click(object sender, EventArgs e) 262 { 263 input(3,e); 264 } 265 266 private void button18_Click(object sender, EventArgs e)//减法,详细注释同除法 267 { 268 if (f4 == 1) 269 { 270 f2 = 0; 271 f3 = 0; 272 f4 = 0; 273 } 274 275 if (f1 == 0) 276 { 277 right = Convert.ToDouble(textBox1.Text); 278 this.textBox2.Text = this.textBox2.Text + this.textBox1.Text + "-"; 279 280 if (f2 == 1) 281 { 282 if (symbol == '+') 283 { 284 left += right; 285 } 286 if (symbol == '-') 287 { 288 left -= right; 289 } 290 if (symbol == '*') 291 { 292 left *= right; 293 } 294 if (symbol == '/') 295 { 296 left /= right; 297 } 298 } 299 else 300 { 301 left = right; 302 f2 = 1; 303 } 304 this.textBox1.Text = Convert.ToString(left); 305 } 306 else if(f2==1) 307 { 308 this.textBox2.Text = this.textBox2.Text.Remove(this.textBox2.Text.Length - 1); 309 this.textBox2.Text = this.textBox2.Text + "-"; 310 } 311 f1 = 1; 312 symbol = '-'; 313 negate = Convert.ToString(left); 314 f5 = 0; 315 } 316 317 private void button19_Click(object sender, EventArgs e)//M+将当前数字与储存的数字相加,并储存 318 { 319 MS = Convert.ToString(Convert.ToDouble(MS) + Convert.ToDouble(this.textBox1.Text)); 320 } 321 322 private void textBox1_TextChanged(object sender, EventArgs e) 323 { 324 //窗口一,当前数字 325 } 326 327 328 private void toolTip1_Popup(object sender, PopupEventArgs e) 329 { 330 331 } 332 333 private void button20_Click(object sender, EventArgs e)//负号,将当前数字变为相反数 334 /*如果在运算符之后按,将当前数字的相反数作为右操作数 335 * 例:2+3+(+/-)等于2+3+(-(2+3))即为2+3+(-(5)) 336 * 例:2+3+(+/-)(+/-)即为2+3+(-(-(5))) 337 * 在计算器中记为2+3+negate(negate(5)) 338 * 此时如果输入数字,则将替换这个数字 339 * 例:2+3+(+/-)即为2+3+(-(5)),输入6,变为2+3+6 340 */ 341 { 342 343 if (f1==1) 344 { 345 if (f5 == 0) 346 { 347 textBox2.Text = textBox2.Text + negate; 348 right = left; 349 } 350 right *= -1; 351 textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - negate.Length); 352 negate = "negate(" + negate + ")"; 353 textBox2.Text = textBox2.Text + negate; 354 355 } 356 if (textBox1.Text.IndexOf("-") < 0) 357 { 358 textBox1.Text = "-" + textBox1.Text; 359 } 360 else 361 { 362 textBox1.Text = textBox1.Text.Replace("-", String.Empty); 363 } 364 f5 = 1; 365 } 366 367 private void button21_Click(object sender, EventArgs e) 368 { 369 370 input(0,e); 371 } 372 373 private void button22_Click(object sender, EventArgs e)//小数点,如果前面没有数字,自动补充0,记为“0.” 374 { 375 if (f4 == 1) 376 { 377 button3_Click(this, e); 378 } 379 if (this.textBox1.Text.IndexOf('.') < 0) 380 { 381 if (f1==0) this.textBox1.Text = this.textBox1.Text + "."; 382 else this.textBox1.Text ="0."; 383 } 384 f1 = 0; 385 if (f5 == 1) 386 { 387 textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - negate.Length); 388 } 389 f5 = 0; 390 } 391 392 private void button23_Click(object sender, EventArgs e)//加号,详细注释同除法 393 { 394 395 if (f4 == 1) 396 { 397 f2 = 0; 398 f3 = 0; 399 f4 = 0; 400 } 401 if (f1==0) 402 { 403 right = Convert.ToDouble(textBox1.Text); 404 this.textBox2.Text = this.textBox2.Text+this.textBox1.Text + "+"; 405 if (f2 == 1) 406 { 407 if (symbol == '+') 408 { 409 left += right; 410 } 411 if (symbol == '-') 412 { 413 left -= right; 414 } 415 if (symbol == '*') 416 { 417 left *= right; 418 } 419 if (symbol == '/') 420 { 421 left /= right; 422 } 423 } 424 else 425 { 426 left = right; 427 f2 = 1; 428 } 429 this.textBox1.Text = Convert.ToString(left); 430 431 } 432 else if (f2 == 1) 433 { 434 this.textBox2.Text=this.textBox2.Text.Remove(this.textBox2.Text.Length - 1); 435 this.textBox2.Text = this.textBox2.Text + "+"; 436 } 437 symbol = '+'; 438 f1 = 1; 439 negate = Convert.ToString(left); 440 f5 = 0; 441 } 442 443 444 445 private void textBox2_TextChanged(object sender, EventArgs e) 446 { 447 //窗口二,总算式 448 } 449 private void input(int num, EventArgs e)//输入函数 450 { 451 if (textBox1.Text.Length <= 16)//最大允许16位 452 { 453 if (f4 == 1)//等于号之后输入数字,相当于先按C,清零 454 { 455 button3_Click(this, e); 456 } 457 if (f1 == 0 && this.textBox1.Text != "0") this.textBox1.Text = this.textBox1.Text + Convert.ToString(num); 458 else this.textBox1.Text = Convert.ToString(num); 459 f1 = 0; 460 if (f5 == 1&&textBox2.Text.Length!=0)//替换negate内容,详见负号 461 { 462 textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - negate.Length); 463 } 464 f5 = 0; 465 } 466 } 467 } 468 }