C#四则运算之策略模式

Calculator.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace stratage
 {
     public abstract class Calculator    //抽象类 Calculator
     {
         public abstract double Cal(double a, double b); //抽象方法Cal
     }
     public class Add : Calculator      //派生类Add继承抽象类Calculator
     {
         public override double Cal(double a, double b)//并重写了抽象方法Cal
         {
             ;
             result = a + b;
             return result;
         }
     }
     public class Sub : Calculator
     {
         public override double Cal(double a, double b)
         {
             ;
             result = a - b;
             return result;
         }
     }
     public class Mul : Calculator
     {
         public override double Cal(double a, double b)
         {
             ;
             result = a * b;
             return result;
         }
     }
     public class Div : Calculator
     {
         public override double Cal(double a, double b)
         {
             ;
             result = a / b;
             return result;
         }
     }
     public class Context           //上下文
     {
         private Calculator calculate = null;//实例化一个基类的引用对象
         public Context(Calculator _cal)//_cal为派生类的一个对象
         {
             this.calculate = _cal;    //把派生类的对象赋给基类的引用对象
         }
         public double Cal(double a, double b, String symbol)
         {
             return this.calculate.Cal(a, b);//返回计算结果
         }
     }
 }

Form1.cs

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.IO;

 namespace stratage
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         ;
         ;
         public static String AddSymbol = "+";  //加法
         public static String SubSymbol = "-";  //减法
         public static String MulSymbol = "*";  //乘法
         public static String DivSymbol = "/";  //除法
         private void textBox4_KeyDown(object sender, KeyEventArgs e)
         {
             double a = double.Parse(textBox1.Text); //用来存第一个数
             string symbol = textBox2.Text;          //用来存运算符
             double b = double.Parse(textBox3.Text); //用来存第二个数
             Context contex = null;                  //上下文
             if (symbol.Equals(AddSymbol))           //若为加号
             {
                 contex = new Context(new Add());    //加法策略
             }
             else if (symbol.Equals(SubSymbol))      //如果减号
             {
                 contex = new Context(new Sub());    //减法策略
             }
             else if (symbol.Equals(MulSymbol))      //若为乘号
             {
                 contex = new Context(new Mul());    //乘法策略
             }
             else if (symbol.Equals(DivSymbol))      //若为除号
             {
                 contex = new Context(new Div());    //除法策略
             }
             string answer = contex.Cal(a, b, symbol).ToString();  //用answer来存计算出来的答案,此时已经计算出a,b两个数的运算结果。
             if (e.KeyCode == Keys.Enter)              //回车操作
             {
                 string result = textBox1.Text + textBox2.Text + textBox3.Text;//把运算式子存在result里面
                 Count++;                                                      //出题总数加一
                 if (textBox4.Text == answer)                                  //如果输入答案与计算出的answer相等
                 {
                     MessageBox.Show("回答正确!");                            //弹出回答正确
                     listBox1.Items.Add(result + "=" + textBox4.Text.Trim() + "√");//并把运算式子存在listbox里
                     Right++;                                                  //答对题数加一
                 }

                 else                                                          //如果答错
                 {
                     MessageBox.Show("答题错误!");                            //弹出答题错误
                     listBox1.Items.Add(result + "=" + textBox4.Text.Trim() +"×");//同样把运算式子放在listbox
                 }
                 label3.Text = ).PadRight(, , ) + "%";//统计正确率
                 textBox1.Clear();//把文本框清空,进行下一次出题
                 textBox3.Clear();
                 textBox4.Clear();
             }
         }
         private void button1_Click(object sender, EventArgs e)//保存按钮,把已答题目放在txt文件里
         {
             sfd.Filter = "(*.txt)|*.txt";
             if (sfd.ShowDialog() == DialogResult.OK)
             {
                 string sPath = sfd.FileName;
                 FileStream fs = new FileStream(sPath, FileMode.Create);
                 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                 ;
                 ; i <= iCount; i++)
                 {
                     sw.WriteLine(listBox1.Items[i].ToString());
                 }
                 sw.Flush();
                 sw.Close();
                 fs.Close();
             }
         }
     }
 }

 测试截图:

C#四则运算之策略模式

C#四则运算之策略模式

C#四则运算之策略模式

上一篇:android studio 添加get,set方法快捷方式


下一篇:Linux系统的磁盘管理