标签:
1.2版本主要添加了分数、取负、开方三个功能,由于这三中运算输入单目运算,所以,新声明了一个新类
class OPeratorV1_2
至此基本完成了一个标准计算器,至于拥有更多功能的科学计算器,日后再做开发,暂定版本2.0
代码如下:
自己写的操作类,负责各种运算,由于是利用了工厂模式,以后新增功能会很方便,特别是今天添加上面的三个功能时,深深体会到了模式的好处。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 计算器 { //简述:用工厂模式取代了简单工厂模式,对比与简单工厂模式,工厂模式,将简单工厂类中的逻辑判断利用接口分离了开来。 interface Result { OperatorV1_1 getOperatorV1_1(); } class plusOperatorV1_1Factory:Result { public OperatorV1_1 getOperatorV1_1() { return new plusOperatorV1_1(); } } class jianOperatorV1_1Factory:Result { public OperatorV1_1 getOperatorV1_1() { return new jianOperatorV1_1(); } } class chenOperatorV1_1Factory:Result { public OperatorV1_1 getOperatorV1_1() { return new chenOperatorV1_1(); } } class chuOperatorV1_1Factory:Result { public OperatorV1_1 getOperatorV1_1() { return new chenOperatorV1_1(); } } class OperatorV1_1 { public virtual string GetResult(double num1, double num2) { return "error"; } } //负责开方、分数、取负 class OPeratorV1_2 { public virtual string GetResult(double num) { return "error"; } } //开方 class kaiFangOperatorV1_2:OPeratorV1_2 { public override string GetResult(double num) { num = Math.Sqrt(num); return num.ToString(); } } //分数 class fenShuOperatorV1_2 : OPeratorV1_2 { public override string GetResult(double num) { num = 1 / num; return num.ToString(); } } //取负 class quFuOperatorV1_2:OPeratorV1_2 { public override string GetResult(double num) { num = -num; return num.ToString(); } } class plusOperatorV1_1 : OperatorV1_1 { public override string GetResult(double num1, double num2) { return (num1 + num2).ToString(); } } class jianOperatorV1_1 : OperatorV1_1 { public override string GetResult(double num1, double num2) { return (num1 - num2).ToString(); } } class chenOperatorV1_1 : OperatorV1_1 { public override string GetResult(double num1, double num2) { return (num1 * num2).ToString(); } } class chuOperatorV1_1 : OperatorV1_1 { public override string GetResult(double num1, double num2) { if (num2 == 0) { return "除数不能为0"; } else { return (num1 / num2).ToString(); } } } }
后台控件代码:
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 class Check //{ // 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; // } // } // public void CheckYunSuan(string myOperator) // { // StringBuilder str = new StringBuilder(); // if (this.textBox1.Text != "") // { // str.Append(this.textBox1.Text); // str.Append(myOperator); // this.textBox1.Text = str.ToString(); // } // else // { // this.textBox1.Text = myOperator; // } // } //} 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 != "" && this.textBox1.Text != "非数字" && 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(); string sText = this.textBox1.Text; if (this.textBox1.Text != "") { string s = this.textBox1.Text; if (textBox1.Text.Contains("+") || s[0] != ‘-‘ && textBox1.Text.Contains("-") || textBox1.Text.Contains("*") || textBox1.Text.Contains("/")) { ShowResult(); } else if (myOperator == "Q" || myOperator == "F" || myOperator == "K") { ShowResult(myOperator); } str.Append(this.textBox1.Text); //后面的两个判断主要是因为如果出现错误的话,就要避免此种情况下在错误信息上添加运算符的情况。 if (myOperator != "Q" && myOperator != "F" && myOperator != "K"&&!this.textBox1.Text.Contains("参数错误")&&!this.textBox1.Text.Contains("非数字")) { str.Append(myOperator); } if(sText==str.ToString())//判定是否执行过相关运算 { this.textBox1.Text = "参数错误"; } else { this.textBox1.Text = str.ToString(); } } else { this.textBox1.Text = myOperator; } } public Form1() { InitializeComponent(); } //简述:判断是否是计算完毕后的再次点击。 2016-5-13 张杨 //修改:废除。 2016-5-13 张杨 //private void textboxIsNull() //{ // if (this.textBox1.Text.Contains("结果为")) // { // this.textBox1.Text = ""; // } //} #region 数字类 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"); } #endregion #region 加减乘除运算 //简述:下面的为加减乘除功能。 //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("/"); } #endregion 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 Operator = "") { string strText = this.textBox1.Text; char myOperator = ‘A‘; if (Operator != "") { myOperator = Operator[0]; } int flag = 0; string result = ""; OperatorV1_1 newResult = new OperatorV1_1(); //取负、分数、开方的运算对象 OPeratorV1_2 newResult2 = new OPeratorV1_2(); if (strText[0] == ‘-‘)//针对为多位负数开方的情况。 { num1.Append(‘-‘); } foreach (char key in strText) { if (strText[0] == key && key == ‘-‘) { continue; } if (isOperator(key)) { flag = 1; myOperator = key; continue; } else { switch (flag) { case 0: num1.Append(key); break; case 1: num2.Append(key); break; } } } switch (myOperator) { case ‘+‘: newResult = new plusOperatorV1_1(); break; case ‘-‘: newResult = new jianOperatorV1_1(); break; case ‘*‘: newResult = new chenOperatorV1_1(); break; case ‘/‘: newResult = new chuOperatorV1_1(); break; case ‘Q‘: newResult2 = new quFuOperatorV1_2(); break; case ‘K‘: newResult2 = new kaiFangOperatorV1_2(); break; case ‘F‘: newResult2 = new fenShuOperatorV1_2(); break; } if (myOperator == ‘Q‘ || myOperator == ‘K‘ || myOperator == ‘F‘) { if (num1.ToString() != null) { result = newResult2.GetResult(double.Parse(num1.ToString())); } else { result = "参数错误"; } } else { string s1 = num1.ToString(); string s2 = num2.ToString(); if (s1.Length>0&& s2.Length>0) { if (!isOperator(s1[0]) && !isOperator(s2[0])) result = newResult.GetResult(double.Parse(num1.ToString()), double.Parse(num2.ToString())); } else { result = "参数错误"; } } //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(); } #region 取负,分数,开方 //开方 private void button16_Click(object sender, EventArgs e) { string s = this.textBox1.Text; if (textBox1.Text.Contains("+") || s[0] != ‘-‘ && textBox1.Text.Contains("-") || textBox1.Text.Contains("*") || textBox1.Text.Contains("/")) { ShowResult(); } CheckYunSuan("K"); } //分数 private void button17_Click(object sender, EventArgs e) { string s = this.textBox1.Text; if (textBox1.Text.Contains("+") || s[0] != ‘-‘ && textBox1.Text.Contains("-") || textBox1.Text.Contains("*") || textBox1.Text.Contains("/")) { ShowResult(); } CheckYunSuan("F"); } //取负 private void button18_Click(object sender, EventArgs e) { string s = this.textBox1.Text; if (textBox1.Text.Contains("+") || s[0] != ‘-‘ && textBox1.Text.Contains("-") || textBox1.Text.Contains("*") || textBox1.Text.Contains("/")) { ShowResult(); } CheckYunSuan("Q"); } #endregion } }
控件设计形式:
标签:
原文地址:http://www.cnblogs.com/JsonZhangAA/p/5559560.html