转载

四则运算之策略模式

Calculator.cs

1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5   6 namespace stratage  7 {  8     public abstract class Calculator    //抽象类 Calculator  9     { 10         public abstract double Cal(double a, double b); //抽象方法Cal 11     } 12     public class Add : Calculator      //派生类Add继承抽象类Calculator 13     { 14         public override double Cal(double a, double b)//并重写了抽象方法Cal 15         { 16             double result = 0; 17             result = a + b; 18             return result; 19         } 20     } 21     public class Sub : Calculator 22     { 23         public override double Cal(double a, double b) 24         { 25             double result = 0; 26             result = a - b; 27             return result; 28         } 29     } 30     public class Mul : Calculator 31     { 32         public override double Cal(double a, double b) 33         { 34             double result = 0; 35             result = a * b; 36             return result; 37         } 38     } 39     public class Div : Calculator 40     { 41         public override double Cal(double a, double b) 42         { 43             double result = 0; 44             result = a / b; 45             return result; 46         } 47     } 48     public class Context           //上下文 49     { 50         private Calculator calculate = null;//实例化一个基类的引用对象 51         public Context(Calculator _cal)//_cal为派生类的一个对象 52         { 53             this.calculate = _cal;    //把派生类的对象赋给基类的引用对象 54         } 55         public double Cal(double a, double b, String symbol) 56         { 57             return this.calculate.Cal(a, b);//返回计算结果 58         } 59     } 60 }

Form1.cs

1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Linq;  7 using System.Text;  8 using System.Windows.Forms;  9 using System.IO; 10  11 namespace stratage 12 { 13     public partial class Form1 : Form 14     { 15         public Form1() 16         { 17             InitializeComponent(); 18         } 19         int Count=0; 20         int Right=0; 21         public static String AddSymbol = "+";  //加法 22         public static String SubSymbol = "-";  //减法 23         public static String MulSymbol = "*";  //乘法 24         public static String DivSymbol = "/";  //除法 25         private void textBox4_KeyDown(object sender, KeyEventArgs e) 26         { 27             double a = double.Parse(textBox1.Text); //用来存第一个数 28             string symbol = textBox2.Text;          //用来存运算符 29             double b = double.Parse(textBox3.Text); //用来存第二个数 30             Context contex = null;                  //上下文 31             if (symbol.Equals(AddSymbol))           //若为加号 32             { 33                 contex = new Context(new Add());    //加法策略 34             } 35             else if (symbol.Equals(SubSymbol))      //如果减号 36             { 37                 contex = new Context(new Sub());    //减法策略 38             } 39             else if (symbol.Equals(MulSymbol))      //若为乘号 40             { 41                 contex = new Context(new Mul());    //乘法策略 42             } 43             else if (symbol.Equals(DivSymbol))      //若为除号 44             { 45                 contex = new Context(new Div());    //除法策略 46             } 47             string answer = contex.Cal(a, b, symbol).ToString();  //用answer来存计算出来的答案,此时已经计算出a,b两个数的运算结果。 48             if (e.KeyCode == Keys.Enter)              //回车操作 49             { 50                 string result = textBox1.Text + textBox2.Text + textBox3.Text;//把运算式子存在result里面 51                 Count++;                                                      //出题总数加一 52                 if (textBox4.Text == answer)                                  //如果输入答案与计算出的answer相等 53                 { 54                     MessageBox.Show("回答正确!");                            //弹出回答正确 55                     listBox1.Items.Add(result + "=" + textBox4.Text.Trim() + "√");//并把运算式子存在listbox里 56                     Right++;                                                  //答对题数加一 57                 } 58  59                 else                                                          //如果答错 60                 { 61                     MessageBox.Show("答题错误!");                            //弹出答题错误 62                     listBox1.Items.Add(result + "=" + textBox4.Text.Trim() +"×");//同样把运算式子放在listbox 63                 } 64                 label3.Text = "正确率:" + Convert.ToString(Right * 1.0 / Count * 100).PadRight(5, ' ').Substring(0, 5) + "%";//统计正确率 65                 textBox1.Clear();//把文本框清空,进行下一次出题 66                 textBox3.Clear(); 67                 textBox4.Clear(); 68             } 69         } 70         private void button1_Click(object sender, EventArgs e)//保存按钮,把已答题目放在txt文件里 71         { 72             sfd.Filter = "(*.txt)|*.txt"; 73             if (sfd.ShowDialog() == DialogResult.OK) 74             { 75                 string sPath = sfd.FileName; 76                 FileStream fs = new FileStream(sPath, FileMode.Create); 77                 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); 78                 int iCount = listBox1.Items.Count - 1; 79                 for (int i = 0; i <= iCount; i++) 80                 { 81                     sw.WriteLine(listBox1.Items[i].ToString()); 82                 } 83                 sw.Flush(); 84                 sw.Close(); 85                 fs.Close(); 86             } 87         } 88     } 89 }

测试截图:

四则运算之策略模式

四则运算之策略模式

四则运算之策略模式

正文到此结束
Loading...