星空网 > 软件开发 > 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();      }    }  }}

运行结果:

自己动手写计算器v1.0




原标题:自己动手写计算器v1.0

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

物流保税:https://www.goluckyvip.com/tag/93271.html
跨境电商常用五大国际快递:https://www.goluckyvip.com/tag/93272.html
进口货物物流:https://www.goluckyvip.com/tag/93273.html
外国物流:https://www.goluckyvip.com/tag/93274.html
国际物流物流:https://www.goluckyvip.com/tag/93275.html
跨境快递查询:https://www.goluckyvip.com/tag/93276.html
怎样做出一个有利可图的SaaS产品?:https://www.kjdsnews.com/a/1836639.html
【再放信号】美国Etsy即将放开中国卖家入驻,官方邮件你收到了吗?:https://www.kjdsnews.com/a/1836640.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流