你的位置:首页 > ASP.net教程

[ASP.net教程]自己动手写计算器v1.0


       今天突发奇想,想着看了还几个设计模式了,倒不如写点东西来实践它们。发现计算器这种就比较合适,打算随着设计模式的学习,会对计算器不断的做改进。

包括功能的增加和算法的改进。初学者难免犯错,希望大家不吝指教。

    计算器V1.0:主要实现了计算器最常见的加减乘除功能,同时还有一个特殊功能,例如:我们执行完1+2后,如果点击等号,会执行加法运算输出结果。但我们如果点击的是运算符(如-),那么不仅会执行加法运算,还会将-号放置到执行结果后,表示这次执行的将会是减法运算。

    代码:Operator类负责使用简单工厂模式来生成加减乘除运算。

Form窗体后台代码:

using 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  {    //简述:变量集中声明处    StringBuilder num1 = new StringBuilder();    StringBuilder num2 = new StringBuilder();    public void CheckTextbox(string num)    {      StringBuilder str = new StringBuilder();      if (this.textBox1.Text != "")      {        str.Append(this.textBox1.Text);        str.Append(num);        this.textBox1.Text = str.ToString();      }      else      {        this.textBox1.Text = num;      }    }    //简述:输入运算符。 2016-5-13 张杨    public void CheckYunSuan(string myOperator)    {      StringBuilder str = new StringBuilder();      if (this.textBox1.Text != "")      {        if (textBox1.Text.Contains("+") || textBox1.Text.Contains("-") || textBox1.Text.Contains("*") || textBox1.Text.Contains("/"))        {          ShowResult();        }        str.Append(this.textBox1.Text);        str.Append(myOperator);        this.textBox1.Text = str.ToString();      }      else      {        this.textBox1.Text = myOperator;      }    }    public Form1()    {      InitializeComponent();    }    private void button1_Click(object sender, EventArgs e)    {      CheckTextbox("1");    }    private void button2_Click(object sender, EventArgs e)    {      CheckTextbox("2");    }    private void button3_Click(object sender, EventArgs e)    {      CheckTextbox("3");    }    private void button4_Click(object sender, EventArgs e)    {      CheckTextbox("4");    }    private void button5_Click(object sender, EventArgs e)    {      CheckTextbox("5");    }    private void button6_Click(object sender, EventArgs e)    {      CheckTextbox("6");    }    private void button7_Click(object sender, EventArgs e)    {      CheckTextbox("7");    }    private void button8_Click(object sender, EventArgs e)    {      CheckTextbox("8");    }    private void button9_Click(object sender, EventArgs e)    {      CheckTextbox("9");    }    //简述:下面的为加减乘除功能。    //2016-5-13 张杨    private void button10_Click(object sender, EventArgs e)    {      CheckYunSuan("+");    }    private void button11_Click(object sender, EventArgs e)    {      CheckYunSuan("-");    }    private void button12_Click(object sender, EventArgs e)    {      CheckYunSuan("*");    }    private void button13_Click(object sender, EventArgs e)    {      CheckYunSuan("/");    }    private void button14_Click(object sender, EventArgs e)    {      this.textBox1.Text = String.Empty;    }    //简述:判断字符是否为运算符。 2016-5-13 张杨    public bool isOperator(char key)    {      if (key == '+' || key == '-' || key == '*' || key == '/')      {        return true;      }      else      {        return false;      }    }    //简述:计算结果。 2016-5-13  张杨    private void ShowResult()    {      string strText = this.textBox1.Text;      char myOperator = 'A';      int flag = 0;      string result = "";      foreach (char key in strText)      {        if (isOperator(key))        {          flag = 1;          myOperator = key;          continue;        }        else        {          switch (flag)          {            case 0: num1.Append(key); break;            case 1: num2.Append(key); break;          }        }      }      result = OperatorFactory.GetResult(myOperator, double.Parse(num1.ToString()), double.Parse(num2.ToString()));      num1 = num1.Remove(0, num1.Length);      num2 = num2.Remove(0, num2.Length);      this.textBox1.Text = result;    }    private void button15_Click(object sender, EventArgs e)    {      ShowResult();    }  }}

 

重点来了,Operator类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 计算器{  //简述:加减乘除处理类,采用了简单工厂模式。 2016-5-13 张杨  class Operator  {    public virtual string GetResult(double num1, double num2)    {      return "error";    }  }  public class OperatorFactory  {    static Operator myOperator=new Operator();    public static string GetResult(char key,double num1,double num2)    {      switch (key)      {        case '+': myOperator = new plusOperator(); break;        case '-': myOperator = new jianOperator(); break;        case '*': myOperator = new chenOperator(); break;        case '/': myOperator = new chuOperator(); break;      }      return myOperator.GetResult(num1,num2);    }  }  class plusOperator : Operator  {    public override string GetResult(double num1, double num2)    {      return (num1 + num2).ToString();    }  }  class jianOperator : Operator  {    public override string GetResult(double num1, double num2)    {      return (num1 - num2).ToString();    }  }  class chenOperator : Operator  {    public override string GetResult(double num1, double num2)    {      return (num1 * num2).ToString();    }  }  class chuOperator : Operator  {    public override string GetResult(double num1, double num2)    {      if (num2 == 0)      {        return "除数不能为0";      }      else      {        return (num1 / num2).ToString();      }    }  }}

运行结果: