简易计算器
Posted 露西&哈特菲利亚
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易计算器相关的知识,希望对你有一定的参考价值。
form后台具体代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Calculator { public partial class Form1 : Form { private List<double> value_list = new List<double>();//存用户输入的数字 private List<int> operator_list = new List<int>();//存用户输入的运算符,定义+为0,-为1,×为2,÷为3 //状态记录 private bool add_flag = false;//+按下 private bool minus_flag = false;//-按下 private bool multi_flag = false;//×按下 private bool div_flag = false;//÷按下 private bool result_flag = false;//=按下 private bool can_operate_flag = false;//按下=是否响应 public Form1() { InitializeComponent(); } //数字键按下,含0与.,类似000001223这类情况这里允许,因为C#可以讲000001223自己转化为1223 private void num_down(string num) { if (add_flag || minus_flag || multi_flag || div_flag || result_flag) { if (result_flag)//按下等号,刚刚算完一个运算的状态 { label1.Text = ""; } textBox1.Clear();//如果用户刚刚输入完一个运算符 add_flag = false; minus_flag = false; multi_flag = false; div_flag = false; result_flag = false; } if ((num.Equals(".") && textBox1.Text.IndexOf(".") < 0) || !num.Equals(".")) { //如果用户输入的是小数点.,则要判断当前已输入的数字中是否含有小数点.才允许输入 textBox1.Text += num; label1.Text += num; can_operate_flag = true; } } private void bt0_Click(object sender, EventArgs e) { num_down("0"); } private void bt1_Click(object sender, EventArgs e) { num_down("1"); } private void bt2_Click(object sender, EventArgs e) { num_down("2"); } private void bt3_Click(object sender, EventArgs e) { num_down("3"); } private void bt4_Click(object sender, EventArgs e) { num_down("4"); } private void bt5_Click(object sender, EventArgs e) { num_down("5"); } private void bt6_Click(object sender, EventArgs e) { num_down("6"); } private void bt7_Click(object sender, EventArgs e) { num_down("7"); } private void bt8_Click(object sender, EventArgs e) { num_down("8"); } private void bt9_Click(object sender, EventArgs e) { num_down("9"); } private void bt_point_Click(object sender, EventArgs e) { num_down("."); } //符号键的输入 private void bt_plus_Click(object sender, EventArgs e) { if (!add_flag)//防止用户多次输入一个符号键,符号键只允许输入一次 { result_flag = false; value_list.Add(double.Parse(textBox1.Text));//将当前已输入的数字放入value_list operator_list.Add(0); label1.Text += "+"; add_flag = true; can_operate_flag = false;//刚刚输入完符号,不能构成一条正常的表达式,如111+,设置为不可运行状态 } } private void bt_minus_Click(object sender, EventArgs e) { if (!minus_flag) { result_flag = false; value_list.Add(double.Parse(textBox1.Text)); operator_list.Add(1); label1.Text += "-"; minus_flag = true; can_operate_flag = false; } } private void bt_multi_Click(object sender, EventArgs e) { if (!multi_flag) { result_flag = false; value_list.Add(double.Parse(textBox1.Text)); operator_list.Add(2); label1.Text = "(" + label1.Text + ")" + "×";//给前面的已经输入的东西加个括号。(运算符栈问题是一个很复杂的数据结构问题,这里不做,:P) multi_flag = true; can_operate_flag = false; } } private void bt_div_Click(object sender, EventArgs e) { if (!div_flag) { result_flag = false; value_list.Add(double.Parse(textBox1.Text)); operator_list.Add(3); label1.Text = "(" + label1.Text + ")" + "÷"; div_flag = true; can_operate_flag = false; } } private void bt_result_Click(object sender, EventArgs e) { if (value_list.Count > 0 && operator_list.Count > 0 && can_operate_flag) {//需要防止用户没输入数字,或者只输入了一个数,就按=。 value_list.Add(double.Parse(textBox1.Text)); double total = value_list[0]; for (int i = 0; i < operator_list.Count; i++) { int _operator = operator_list[i];//operator是C#的运算符重载的关键字,前面加个_来区别 switch (_operator) { case 0: total += value_list[i + 1]; break; case 1: total -= value_list[i + 1]; break; case 2: total *= value_list[i + 1]; break; case 3: total /= value_list[i + 1]; break; } } textBox1.Text = total + ""; label1.Text = total + ""; operator_list.Clear();//算完,就清空累积数字与运算数组 value_list.Clear(); result_flag = true;//表示=按下 } } //CE键,将所有东西初始化 private void btCE_Click(object sender, EventArgs e) { operator_list.Clear(); value_list.Clear(); add_flag = false; minus_flag = false; multi_flag = false; div_flag = false; result_flag = false; can_operate_flag = false; textBox1.Clear(); label1.Text = ""; } } }
以上是关于简易计算器的主要内容,如果未能解决你的问题,请参考以下文章