找传奇、传世资源到传世资源站!

入门级计算器示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 计算器{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } double num1; double num2; double yunsuan = 0; double result; bool DecimalFlag = false; bool OperatorFlag = false; string Operate; public void NumClick(double num) //数字处理方法 { if (!OperatorFlag) { if (DecimalFlag) { textBox1.Text = textBox1.Text num; } else { if (textBox1.Text == "0") { textBox1.Text = num ""; } else { textBox1.Text = textBox1.Text num; } } OperatorFlag = false; } else textBox1.Text = num.ToString(); OperatorFlag = false; } private void Form1_Load(object sender, EventArgs e) { textBox1.ReadOnly = true; } private void textBox1_TextChanged(object sender, EventArgs e) { textBox1.BackColor = Color.White; } private void button18_Click(object sender, EventArgs e) //清空键C { num1 = 0; num2 = 0; yunsuan = 0; textBox1.Text = "0"; DecimalFlag = false; OperatorFlag = false; } private void button1_Click(object sender, EventArgs e) //数字键0 { NumClick(0); } private void button3_Click(object sender, EventArgs e) //数字键1 { NumClick(1); } private void button4_Click(object sender, EventArgs e) //数字键2 { NumClick(2); } private void button5_Click(object sender, EventArgs e) //数字键3 { NumClick(3); } private void button6_Click(object sender, EventArgs e) //数字键4 { NumClick(4); } private void button7_Click(object sender, EventArgs e) //数字键5 { NumClick(5); } private void button8_Click(object sender, EventArgs e) //数字键6 { NumClick(6); } private void button9_Click(object sender, EventArgs e) //数字键7 { NumClick(7); } private void button10_Click(object sender, EventArgs e) //数字键8 { NumClick(8); } private void button11_Click(object sender, EventArgs e) //数字键9 { NumClick(9); } private void button2_Click(object sender, EventArgs e) //小数点 { if (!DecimalFlag) textBox1.Text = textBox1.Text "."; else textBox1.Text = textBox1.Text ""; DecimalFlag = true; } private void button12_Click(object sender, EventArgs e) //加号 { OperatorFlag = true; Operate = "add"; if (yunsuan == 0) { num1 = double.Parse(textBox1.Text); textBox1.Text = num1.ToString(); } else { num2 = double.Parse(textBox1.Text); { if (yunsuan == 1) num1 = num1 num2; else if (yunsuan == 2) num1 = num1 - num2; else if (yunsuan == 3) num1 = num1 * num2; else num1 = num1 / num2; } textBox1.Text = num1.ToString(); } yunsuan = 1; DecimalFlag = false; } private void button13_Click(object sender, EventArgs e) //减号 { OperatorFlag = true; Operate = "minus"; if (yunsuan == 0) { num1 = double.Parse(textBox1.Text); textBox1.Text = num1.ToString(); } else { num2 = double.Parse(textBox1.Text); { if (yunsuan == 1) num1 = num1 num2; else if (yunsuan == 2) num1 = num1 - num2; else if (yunsuan == 3) num1 = num1 * num2; else num1 = num1 / num2; } textBox1.Text = num1.ToString(); } yunsuan = 2; DecimalFlag = false; } private void button14_Click(object sender, EventArgs e) //乘号 { OperatorFlag = true; Operate = "multiply"; if (yunsuan == 0) { num1 = double.Parse(textBox1.Text); textBox1.Text = num1.ToString(); } else { num2 = double.Parse(textBox1.Text); { if (yunsuan == 1) num1 = num1 num2; else if (yunsuan == 2) num1 = num1 - num2; else if (yunsuan == 3) num1 = num1 * num2; else num1 = num1 / num2; } textBox1.Text = num1.ToString(); } yunsuan = 3; DecimalFlag = false; } private void button15_Click(object sender, EventArgs e) //除号 { OperatorFlag = true; Operate = "divide"; if (yunsuan == 0) { num1 = double.Parse(textBox1.Text); textBox1.Text = num1.ToString(); } else { num2 = double.Parse(textBox1.Text); { if (yunsuan == 1) num1 = num1 num2; else if (yunsuan == 2) num1 = num1 - num2; else if (yunsuan == 3) num1 = num1 * num2; else num1 = num1 / num2; } textBox1.Text = num1.ToString(); } yunsuan = 4; DecimalFlag = false; } private void button16_Click(object sender, EventArgs e) //等号 { num2 = double.Parse(textBox1.Text); switch (Operate) { case "add": result = num1 num2; textBox1.Text = result.ToString(); break; case "minus": result = num1 - num2; textBox1.Text = result.ToString(); break; case "multiply": result = num1 * num2; textBox1.Text = result.ToString(); break; case "divide": if (num2 == 0) { textBox1.Text = "除数不能为0"; } else { result = num1 / num2; textBox1.Text = result.ToString(); } break; } OperatorFlag = true; yunsuan = 0; } private void button17_Click(object sender, EventArgs e) //退格键 { if (textBox1.Text == ""|| textBox1.Text=="0" || textBox1.Text.Length==1) textBox1.Text = "0"; else textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复