using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 计算器 { class Calc { private double number1 = 0; //第1个数 private double number2 = 0; //第2个数 private double number3 = 0; //结果 public enum Sign { And, Substract, Multiply, Divide, Sign } //加减乘除符号枚举 public string Process { get; set; } //计算记录 public Calc() { } public double Number1 { get { return number1; } set { number1 = value; } } public double Number2 { get { return number2; } set { number2 = value; } } public double Number3 { get { return number3; } set { number3 = value; } } public void Calc_method(Sign sign) { switch (sign) { case Sign.And: //加法 number3 = number1 + number2; break; case Sign.Divide: //除法 number3 = number1 / number2; break; case Sign.Multiply: //乘法 number3 = number1 * number2; break; case Sign.Substract: //减法 number3 = number1 - number2; break; } } } }
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 计算器 { public partial class MyCalc : Form { public MyCalc() { InitializeComponent(); } Calc calc1; Calc.Sign sign1; public static string tmp = "0"; public static bool booltag = false; private void MyCalc_Load(object sender, EventArgs e) { calc1 = new Calc(); sign1 = new Calc.Sign(); } private void btnSign_Click(object sender, EventArgs e) { string s = ((Button)sender).Tag.ToString(); switch (s) { case "+": processMethod(s); sign1 = Calc.Sign.And; break; case "-": processMethod(s); sign1 = Calc.Sign.Substract; break; case "*": processMethod(s); sign1 = Calc.Sign.Multiply; break; case "/": processMethod(s); sign1 = Calc.Sign.Divide; break; } } private void processMethod(string s) { if (booltag) { calc1.Process = calc1.Process.Substring(0, calc1.Process.Length - 1) + s; //改变运算符 } else { if (sign1 == Calc.Sign.Divide && calc1.Number2 == 0) //除数不能为零 { calc1.Process = calc1.Process.Substring(0, calc1.Process.Length - 1) + s; //改变运算符 if (calc1.Number1 == 0) txtDisplay.Text = "结果未定义"; else txtDisplay.Text = "除数不能为零"; booltag = true; return; } calc1.Process += calc1.Number2.ToString() + s; calc1.Calc_method(sign1); //计算 calc1.Number1 = calc1.Number3; //保存结果到1 } txtProcess.Text = calc1.Process.ToString(); //显示记录 txtDisplay.Text = calc1.Number3.ToString(); //显示结果 booltag = true; } private void btn_Click(object sender, EventArgs e) { string s = ((Button)sender).Tag.ToString(); if (s == "C") { calc1 = new Calc(); sign1 = new Calc.Sign(); tmp = "0"; booltag = false; txtProcess.Text = ""; //显示记录 txtDisplay.Text = "0"; //显示结果 return; } if (s == "."&& tmp.IndexOf(".")!=-1) { return; } if (sign1 == Calc.Sign.Sign) { return; } if (s == "←") { int n = tmp.Length; if (n > 1) { tmp = tmp.Substring(0, n - 1); calc1.Number2 = Convert.ToDouble(tmp); txtDisplay.Text = calc1.Number2.ToString(); //显示输入 } return; } if (s == "CE") { tmp = "0"; calc1.Number2 = Convert.ToDouble(tmp); txtDisplay.Text = calc1.Number2.ToString(); //显示输入 return; } if (booltag) //按过运算符 { tmp = "0"; booltag = false; } tmp = tmp + s; calc1.Number2 = Convert.ToDouble(tmp); txtDisplay.Text = calc1.Number2.ToString(); //显示输入 } private void btnSign_Click_1(object sender, EventArgs e) { if (booltag == false) { if (sign1 == Calc.Sign.Divide && calc1.Number2 == 0) //除数不能为零 { if (calc1.Number1 == 0) txtDisplay.Text = "结果未定义"; else txtDisplay.Text = "除数不能为零"; } else { if (sign1 != Calc.Sign.Sign) { calc1.Process += calc1.Number2.ToString() + "="; calc1.Calc_method(sign1); //计算 calc1.Number1 = calc1.Number3; //保存结果到1 txtProcess.Text = calc1.Process.ToString(); //显示记录 txtDisplay.Text = calc1.Number3.ToString(); //显示结果 sign1 = Calc.Sign.Sign; } } booltag = true; } } } }