• 计算器软件实现系列(四) 策略模式


    一 封装时策略模式的书写

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace shuxuefudao
    {
        class qita
        {
    
            public void qingkong()
            {
                File.WriteAllText("writer.txt", string.Empty);
                File.WriteAllText("writer1.txt", string.Empty);
                File.WriteAllText("writer2.txt", string.Empty);
            }
        }
        public interface Calculator //声明一个计算的接口
        {
            double Cal(double a,double  b);
        }
       public class Add : Calculator   //接口实现加法运算
       {
         public double Cal(double a, double b)
         {
             double result = 0;
             result = a + b;
             return result;
         }
       }
       public class Sub : Calculator //接口实现减法运算
     {
         public double Cal(double a, double b)
         {
             double result = 0;
             result = a - b;
             return result;
         }
     }
       public class Mul : Calculator  //接口实现乘法运算
     {
         public double Cal(double a, double b)
         {
             double result = 0;
             result = a * b;
            return result;
        }
     }
       public class Div : Calculator  //接口实现除法运算
     {
        public  double Cal(double a, double b)
        {
            double result = 0;
            result = a / b;
            return result;
         }
     }
     public class  Environment        //定义那个需要动态改变算法的对象   
     {
         private Calculator calculate;
         public Environment(Calculator calculate)
         {
            this.calculate = calculate;    
         }
         public double Cal(double a, double b, String m) //返回运算结果
         {
             return this.calculate.Cal(a, b);
        }
     }
     }
     

    二 策略模式的引用

    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 shuxuefudao
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string path = "E:
    tf";
            public static int Count = 0; // 题目出的数量
            public static int zuode = 0; //做的题目数量
            public static int zhengque = 0;
            public static int lefttime;
            public static int time;
            public static int sum;
            int i =0;
            private void Form1_Load(object sender, EventArgs e)
            {
              
            }
            private void open_Click(object sender, EventArgs e) //打开文件的方法
            {
                OpenFileDialog TxTOpenDialog = new OpenFileDialog();
                TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
                {
                    path = TxTOpenDialog.FileName;
                    this.richTextBox1.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                    save.Enabled = false;
                    open.Enabled = false;
                    MessageBox.Show("读取成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
    
            private void save_Click(object sender, EventArgs e) //保存文件的方法
            {
                SaveFileDialog TxtSaveDialog = new SaveFileDialog();
                TxtSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (File.Exists(path))
                {
    
                    this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                    MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Clear();
                    save.Enabled = false;
                }
                else
                {
                    if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
                    {
    
                        this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.RichText);
                        MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.richTextBox1.Clear();
                        save.Enabled = false;
                    }
                }
            }
    
            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                save.Enabled = true;
                if (this.richTextBox1.Text == "" || this.richTextBox1.Text == null)
                {
                    open.Enabled = true;
                }
            }
            private void open2_Click(object sender, EventArgs e) //打开试题的方法
            {
                OpenFileDialog TxTOpenDialog = new OpenFileDialog();
                TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
                {
                    path = TxTOpenDialog.FileName;
                    this.richTextBox2.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                    save.Enabled = false;
                    open.Enabled = false;
                    MessageBox.Show("打开成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
    
            private void daoru_Click(object sender, EventArgs e) //导入试题的方法
            {
                richTextBox2.Text = richTextBox1.Text;
            }
    
            private void daan1_Click(object sender, EventArgs e)
            {
                if (daan1.Text == "显示答案")
                {
                    daan.PasswordChar = Convert.ToChar(0);
                    daan1.Text = "隐藏答案";
                }
                else if (daan1.Text == "隐藏答案")
                {
                    daan.PasswordChar = '.';
                    daan1.Text = "显示答案";
    
                }
            }
    
            private void kaishi_Click(object sender, EventArgs e)
            {
                string[] ll = new string[100];
                ll = File.ReadAllLines("writer.txt");
                textBox1.Text = ll[0];
                string[] lli = new string[100];
                lli = File.ReadAllLines("writer1.txt");
                textBox2.Text = lli[0];
                string[] llp = new string[100];
                llp = File.ReadAllLines("writer2.txt");
                textBox3.Text = llp[0];
                int minute;
                try
                {
                    minute = int.Parse(this.shijian.Text);
                }
                catch (System.Exception ex)
                {
                    this.shijian1.Text = "输入错误";
                    return;
                }
                lefttime = minute;
                this.timer1.Interval = 1000;
                this.timer1.Enabled = true;
                this.timer1.Start();
    
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                time = Convert.ToInt32(shijian.Text);
                if (lefttime <= 0)
                {
                    timer1.Enabled = false;
                    MessageBox.Show("答题时间到!");
                    textBox4.Enabled = false;
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();
                }
                this.shijian1.Text = "剩余时间" + lefttime.ToString() + "";
                lefttime--;  
            }
    
            private void jieshu_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
    
            private void button1_Click(object sender, EventArgs e) //请编辑下道题的事件
            {
                Count++;
                ti.Text = Count.ToString();
                StreamWriter writer = File.AppendText("writer.txt");
                writer.WriteLine(left.Text);
                writer.Close();
                StreamWriter writer1 = File.AppendText("writer1.txt");
                writer1.WriteLine(fuhao.Text);
                writer1.Close();
                StreamWriter writer2 = File.AppendText("writer2.txt");
                writer2.WriteLine(right.Text);
                writer2.Close();
                richTextBox1.Text += left.Text + fuhao.Text + right.Text + label2.Text + "" + "
    ";
                left.Clear();
                fuhao.Clear();
                right.Clear();
            }
            private void textBox4_KeyDown(object sender, KeyEventArgs e)
            {
                
                try //异常处理机制,预防数组发生越界
                {
                Environment environment=null;
                double a = Convert.ToDouble(textBox1.Text); //为相关的变量赋值
                double b = Convert.ToDouble(textBox3.Text);
                string m = textBox2.Text;
                int result;
                switch (m)
                {
                    case "+":
                        environment = new Environment(new Add()); //策略模式的引用
                        break;
                    case "-":
                        environment = new Environment(new Sub());
    
                        break;
                    case "*":
                        environment = new Environment(new Mul());
    
                        break;
                    case "/":
                        environment = new Environment(new Div());
    
                        break;
                    default:
                        break;
                }
                
                if (e.KeyCode == Keys.Enter)
                {
                   
                    if (int.TryParse(textBox4.Text, out result) == false)
                    {
                        MessageBox.Show("请输入数字");
                    }
                    string answer = environment.Cal(a, b, m).ToString();
                    daan.Text += answer + "
    ";
                    if (textBox4.Text == answer.ToString())
                    {
                        MessageBox.Show("回答正确");
                        zuode++;
                        zhengque++;   
                    }
                    else
                    {
                        MessageBox.Show("回答错误");
                        zuode++;
                    }
                    i++;
                    textBox4.Clear();
                    string[] ll = new string[100];
                    ll = File.ReadAllLines("writer.txt");
                    textBox1.Text = ll[i];
                    string[] lli = new string[100];
                    lli = File.ReadAllLines("writer1.txt");
                    textBox2.Text = lli[i];
                    string[] llp = new string[100];
                    llp = File.ReadAllLines("writer2.txt");
                    textBox3.Text = llp[i];          
                 }  
                }
                catch(Exception ex)
                {
                    Form2 frm = new Form2();
                    frm.ShowDialog();
                }
    
            }
            private void button2_Click(object sender, EventArgs e) //清空上次编辑试题的方法
            {
                qita aaa = new qita();
                aaa.qingkong();
            }
            private void button3_Click(object sender, EventArgs e) //退出程序的方法
            {
                Application.Exit();
            }
        }
    }

    三 界面的运行

  • 相关阅读:
    [转]Hamcrest使用方法实例
    Maven配置浅析
    Guava API
    awk排序作业
    [转]awk使用手册
    Vue源码探究-全局API
    vue 组件间传值
    在2018年如何优雅的开发一个typescript语言的npm包?
    PHP四种序列化方案
    从 0 到 1 再到 100, 搭建、编写、构建一个前端项目
  • 原文地址:https://www.cnblogs.com/wyh19941210/p/5003448.html
Copyright © 2020-2023  润新知