• 策略模式(完善)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 简易运算
    {
        public interface ODD
        {
            int members(int a, int b);
    
        }
        class Class2 : ODD
        {
    
            public int members(int a, int b)
            {
                return a + b;
            }
    
        }
        class sub : ODD
        {
            public int members(int a, int b)
            {
                return a - b;
            }
    
        }
        class Mul : ODD
        {
            public int members(int a, int b)
            {
                return a * b;
    
            }
    
        }
        class Div : ODD
        {
            public int members(int a, int b)
            {
                return a / b;
    
            }
        }
    
       class suanfa
        {
           private ODD jisuan;
            public void math(string TR)
            {
                switch (TR)
                {
                    case "+":
                        jisuan = new Class2();
                        break;
                    case "-":
                        jisuan = new sub();
                        break;
                    case "*":
                        jisuan = new Mul();
                        break;
                    case "/":
                        jisuan = new Div();
                        break;
                }
    
            }
            public int clation(int a, int b)
            {
                return jisuan.members(a, b);
            }
        }
    }
    

      Form1代码:

    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 简易运算
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string path = "E:/";
            public static int count = 0;
            
            public static int right = 0;
            public static int sum;
    
            public int m = 0;
            public int n = 0;
            private void Form1_Load(object sender, EventArgs e)
            {
                Class1 s = new Class1();
                s.text1();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                StreamWriter l1 = File.AppendText("1.txt");
                l1.WriteLine(text1.Text);
                l1.Close();
                StreamWriter l2 = File.AppendText("2.txt");
                l2.WriteLine(textBox2.Text);
                l2.Close();
                StreamWriter l3 = File.AppendText("3.txt");
                l3.WriteLine(text2.Text);
                l3.Close();
                richTextBox1.Text += text1.Text + textBox2.Text +text2.Text+ "
    ";
                n++;
                text1.Text = "";
                text2.Text = "";
                textBox2.Text = "";
                savet.Enabled = true;
                opent.Enabled = true;
                text1.Focus();
            }
    
            private void savet_Click(object sender, EventArgs e)
            {
                SaveFileDialog TxtSaveDialog = new SaveFileDialog();
                TxtSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (File.Exists(path))
                {
                    this.richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);
                    MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Clear();
                    savet.Enabled = false;
                }
                else
                {
                    if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
                    {
    
                        this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.RichText);
                        MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.richTextBox1.Clear();
                        savet.Enabled = false;
                    }
                }
            }
    
            private void opent_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);
                    savet.Enabled = false;
                    opent.Enabled = false;
                    MessageBox.Show("打开成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                int a = Convert.ToInt32(jishi.Text);
    
                jishi.Text = a.ToString();
                timer1.Enabled = true;
                timer1.Interval = 1000;
    
               
                string[] l1 = new string[100];
                l1 = File.ReadAllLines("1.txt");
                string[] l2 = new string[100];
                l2 = File.ReadAllLines("2.txt");
                string[] l3 = new string[100];
                l3 = File.ReadAllLines("3.txt");
                Box1.Text = l1[0];
                Box4.Text = l2[0];
                Box2.Text = l3[0];
              
            } 
    
            private void Box3_KeyDown(object sender, KeyEventArgs e)
            {
                
                //string s = Box4.Text;
                //Pack  operast = new Pack();
                //operast.OP = int.Parse(Box1.Text);
                //operast.Pt = int.Parse(Box2.Text);
                //operast.Oper = Box4.Text;
                //suanfa fan = new suanfa();
                //string str = textBox2.Text;
                //fan.fa = int.Parse(Box1.Text);
                //fan.fb = int.Parse(Box2.Text);
                //string st = Box4.Text;
                //fan.math(st);
                //int sum = fan.result;
                //switch (s)
                //{
                //    case "+":
                //        sum = int.Parse(Box1.Text) + int.Parse(Box2.Text);
    
                //        break;
                //    case "-":
                //        sum = int.Parse(Box1.Text) - int.Parse(Box2.Text);
    
                //        break;
                //    case "*":
                //        sum = int.Parse(Box1.Text) * int.Parse(Box2.Text);
    
                //        break;
                //    case "/":
                //        sum = int.Parse(Box1.Text) / int.Parse(Box2.Text);
    
                //        break;
                //    default:
                //        break;
                //}
                suanfa suanfa1=new suanfa();
                int a = int.Parse(Box1.Text);
                int b = int.Parse(Box2.Text);
                string c = Box4.Text;
                suanfa1.math(c);
                 sum = suanfa1.clation(a, b);
    
                if (e.KeyCode == Keys.Enter)
                {
                    if (sum.ToString() == Box3.Text)
                    {
                        MessageBox.Show("回答正确哦!");
                        count++;
                        right++;
                    }
                    else
                    {
                        MessageBox.Show("回答错误哦!");
                        count++;
                    }
                    m++;
                    if (m < n)
                    {
                        Box3.Clear();
                        string[] ll = new string[100];
                        ll = File.ReadAllLines("1.txt");
                        Box1.Text = ll[m];
                        string[] l2 = new string[100];
                        l2 = File.ReadAllLines("2.txt");
                        Box4.Text = l2[m];
                        string[] l3 = new string[100];
                        l3 = File.ReadAllLines("3.txt");
                        Box2.Text = l3[m];
                    }
                    else
                    {
                        jishi.Enabled = false;
                        Form2 frm = new Form2();
                        frm.ShowDialog();
                    }
                }
            }
    
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                //t = t + 1;
                //jishi.Text = t.ToString();
                if(Convert.ToInt32(jishi.Text)<=0)
                {
                    timer1.Enabled = false;
                    MessageBox.Show("时间到");
                }
                int a = Convert.ToInt32(jishi.Text);
                a = a - 1;
                jishi.Text = a.ToString();
    
            }
            private void Box1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void jishi_Click(object sender, EventArgs e)
            {
    
            }
    
            private void timer1_Tick_1(object sender, EventArgs e)
            {
    
            }
          
        }
    }
    

      Form2代码:

    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;
    
    namespace 简易运算
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                textBox2.Text = Form1.count.ToString();
                textBox1.Text = Form1.right.ToString();
                textBox3.Text = ((Form1.right / (double)(Form1.count)) * 100).ToString() + "%";
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    

      测试截图:

  • 相关阅读:
    HDU 6370 dfs+并查集
    牛客网暑期ACM多校训练营(第六场)G
    HDU 6351暴力枚举 6354计算几何
    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 A,D
    2018 百度之星 初赛 第六题 HDU6349
    HDU 6336 子矩阵求和
    HDU 6333 莫队+组合数
    BZOJ 2308 莫队入门经典
    Linux系统管理第一章
    2019年7月17日
  • 原文地址:https://www.cnblogs.com/harlem/p/5083606.html
Copyright © 2020-2023  润新知