1.2版本主要添加了分數、取負、開方三個功能,由於這三中運算輸入單目運算,所以,新聲明瞭一個新類 class OPeratorV1_2 至此基本完成了一個標準計算器,至於擁有更多功能的科學計算器,日後再做開發,暫定版本2.0 代碼如下: 自己寫的操作類,負責各種運算,由於是利用了工廠模式,以後新增功 ...
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 } }
控制項設計形式: