计算器-面向对象-工厂
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Windows_ji_suan_qi { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) //输入数字1 { textBox1.Text = textBox1.Text + 1; } private void button4_Click(object sender, EventArgs e) //输入数字2 { textBox1.Text = textBox1.Text + 2; } private void button5_Click(object sender, EventArgs e) //输入数字3 { textBox1.Text = textBox1.Text + 3; } private void button6_Click(object sender, EventArgs e) //输入数字4 { textBox1.Text = textBox1.Text + 4; } private void button7_Click(object sender, EventArgs e) //输入数字5 { textBox1.Text = textBox1.Text + 5; } private void button8_Click(object sender, EventArgs e) //输入数字6 { textBox1.Text = textBox1.Text + 6; } private void button11_Click(object sender, EventArgs e) //输入数字7 { textBox1.Text = textBox1.Text + 7; } private void button10_Click(object sender, EventArgs e) //输入数字8 { textBox1.Text = textBox1.Text + 8; } private void button9_Click(object sender, EventArgs e) //输入数字9 { textBox1.Text = textBox1.Text + 9; } private void button1_Click(object sender, EventArgs e) //输入数字0 { textBox1.Text = textBox1.Text + 0; } private void button2_Click(object sender, EventArgs e) //输入 点 { textBox1.Text = textBox1.Text + "."; } private void textBox1_TextChanged_1(object sender, EventArgs e) //设置长度 为16 { textBox1.MaxLength = 16; } private void button14_Click(object sender, EventArgs e) //清零按钮 { oper.NumberA = 0; oper.NumberB = 0; textBox1.Text = ""; textBox1.Focus(); } private void button12_Click(object sender, EventArgs e) //撤销按键 { string temp = null; temp = textBox1.Text; if (temp.Length < 1) return; textBox1.Text = temp.Substring(0, temp.Length - 1); } private void button13_Click(object sender, EventArgs e) //按钮+ { oper.NumberA = Convert.ToDouble(textBox1.Text); //转换变量 并赋值 textBox1.Text = ""; //置空 textBox1.Focus(); //聚焦 oper = OperationFactory.createOperate("+"); //传值 } private void button15_Click(object sender, EventArgs e)//按钮- { oper.NumberA = Convert.ToDouble(textBox1.Text); textBox1.Text = ""; textBox1.Focus(); oper = OperationFactory.createOperate("-"); } private void button16_Click(object sender, EventArgs e) //按钮* { oper.NumberA = Convert.ToDouble(textBox1.Text); textBox1.Text = ""; textBox1.Focus(); oper = OperationFactory.createOperate("*"); } private void button17_Click(object sender, EventArgs e) //按钮 / { oper.NumberA = Convert.ToDouble(textBox1.Text); textBox1.Text = ""; textBox1.Focus(); oper = OperationFactory.createOperate("/"); } private void button18_Click(object sender, EventArgs e) //按钮= { oper.NumberB = Convert.ToDouble(textBox1.Text); double result = oper.GetResult(); textBox1.Text = result.ToString(); } /**/ ///<summary> ///运算类工厂 ///</summary> Operation oper = new Operation(); public class OperationFactory { public static Operation createOperate(string operate) { /* 只有用了static后才能OperationFactory.createOperate("-"); 不然要先OperationFactory a=new OpertationFactory(); a.cerateOperate("-"); */ Operation oper = null; switch (operate) { case "+": oper = new OperationAdd(); break; case "-": oper = new OperationSub(); break; case "*": oper = new OperationMul(); break; case "/": oper = new OperationDiv(); break; } return oper; } } /**/ ///<summary> ///运算类 ///</summary> public class Operation { private static double _numberA = 0; private static double _numberB = 0; /**/ ///<summary> ///数字A ///</summary> public double NumberA { get { return _numberA; } set { _numberA = value; } } ///<summary> ///数字B ///</summary> public double NumberB { get { return _numberB; } set { _numberB = value; } } ///<summary> ///得到运算结果 ///</summary> ///<returns></returns> public virtual double GetResult() { double result = 0; return result; } } ///<summary> ///加法类 ///</summary> class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } ///<summary> ///减法类 ///</summary> class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; } } ///<summary> ///乘法类 ///</summary> class OperationMul : Operation { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } ///<summary> ///除法类 ///</summary> class OperationDiv : Operation { public override double GetResult() { double result = 0; if (NumberB == 0) //判断除数是否为0 { MessageBox.Show("输入的除数不能为0,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } result = NumberA / NumberB; return result; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) //退出 按钮 { DialogResult aaa = MessageBox.Show("真的要退出吗?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (aaa == DialogResult.Cancel) e.Cancel = true; } } }