码迷,mamicode.com
首页 > 其他好文 > 详细

计算器(工厂模式)

时间:2015-11-24 22:51:12      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

1、已经无语,我添加了正则表达式分割从txt文件 读取到的字符串。借我朋友的书《大话设计模式 》,感觉作者真乃神人也。第一章就简单明了的介绍了工厂模式,刚好遇到老师这个作业,幸运哈。

2、代码如下

业务逻辑---

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

namespace jisuan
{
    public class Operation
    {
        private double numberA = 0;
        private double numberB = 0;
        public double NumberA
        { 
            get{return numberA;}
            set { numberA = value; }
        }
        public double NumberB
        {
            get { return numberB; }
            set { numberB = value; }
        }
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }   
    }
    class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }
    class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }
    class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if(NumberB==0)
            {
                throw new Exception("除数不能为0");
            }
            result = NumberA / NumberB;
            return result;
        }
    }
}

  

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


namespace jisuan
{
    public class OperationFactory
    {
        public static Operation createOperate(string operate)
        {
            Operation oper = null;
            switch (operate)
            {
                case "+":
                    oper = new OperationAdd();
                    break;
                case "-":
                    oper = new OperationSub();
                    break;
                case "*":
                    oper = new OperationMul();
                    break;
                case "/":
                    oper = new OperationDiv();
                    break;
            }
            return oper;
        }
     
    }
}

界面逻辑

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;

namespace jisuan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            Form3 ff = new Form3();
            ff.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            Form2 ff = new Form2();
            ff.Show();

        }
    }
}

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;
using System.IO;
using System.Text.RegularExpressions;

namespace jisuan
{
    public partial class Form2 : Form
    {
        public int i;
        public static int shijian = 0;
        public static int count;
        public static int right;

        public Form2()
        {
            InitializeComponent();
        }
        
      
        
        string path = @"D:\\计算存储.txt";
        OperationFactory oo = new OperationFactory();
        private void Form2_Load(object sender, EventArgs e)
        {
            
            StreamReader sr = new StreamReader(path,Encoding.Default);

            richTextBox1.Text = sr.ReadToEnd();
            sr.Close();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {

            duqu();
            textBox3.Text = shijian.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
   
           
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            string xx = textBox1.Text;
            string ss = @"(\d+)(\S+)+(\d+)";
            Match mast = Regex.Match(xx, ss);
            ///这正则表达式无解了
            int strNumberA = int.Parse(mast.Groups[1].Value);
            string strNumberC = mast.Groups[2].Value;
            int strNumberB = int.Parse(mast.Groups[3].Value);

            Operation oper;
            oper = OperationFactory.createOperate(strNumberC);
            oper.NumberA = Convert.ToDouble(strNumberA);
            oper.NumberB = Convert.ToDouble(strNumberB);
            double result = oper.GetResult();
            Console.Write("你的结果为" + result);
            if(e.KeyCode==Keys.Enter)
            {
                if (textBox2.Text == result.ToString())
                {
                    MessageBox.Show("正确");
                    right++;

                }
                else
                {
                    MessageBox.Show("错误");
                }
                duqu();
            }
        }

        public void duqu()
        {
            string[] line = File.ReadAllLines(path);
            if (i < line.Length)
            {
                textBox1.Text = line[i];
                textBox2.Text = "";

            }
            else
            {
                MessageBox.Show("已经做完");
                this.Close();

            }
            i++;
            count++;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            shijian = shijian + 1;
            textBox3.Text = shijian.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled=false;
            textBox2.Enabled = false;

            this.Close();
            Form4 ff = new Form4();
            ff.Show();
           
        }
    }
}

form3

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;
using System.Text.RegularExpressions;

namespace jisuan
{
    public partial class Form2 : Form
    {
        public int i;
        public static int shijian = 0;
        public static int count;
        public static int right;

        public Form2()
        {
            InitializeComponent();
        }
        
      
        
        string path = @"D:\\计算存储.txt";
        OperationFactory oo = new OperationFactory();
        private void Form2_Load(object sender, EventArgs e)
        {
            
            StreamReader sr = new StreamReader(path,Encoding.Default);

            richTextBox1.Text = sr.ReadToEnd();
            sr.Close();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {

            duqu();
            textBox3.Text = shijian.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
   
           
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            string xx = textBox1.Text;
            string ss = @"(\d+)(\S+)+(\d+)";
            Match mast = Regex.Match(xx, ss);
            ///这正则表达式无解了
            int strNumberA = int.Parse(mast.Groups[1].Value);
            string strNumberC = mast.Groups[2].Value;
            int strNumberB = int.Parse(mast.Groups[3].Value);

            Operation oper;
            oper = OperationFactory.createOperate(strNumberC);
            oper.NumberA = Convert.ToDouble(strNumberA);
            oper.NumberB = Convert.ToDouble(strNumberB);
            double result = oper.GetResult();
            Console.Write("你的结果为" + result);
            if(e.KeyCode==Keys.Enter)
            {
                if (textBox2.Text == result.ToString())
                {
                    MessageBox.Show("正确");
                    right++;

                }
                else
                {
                    MessageBox.Show("错误");
                }
                duqu();
            }
        }

        public void duqu()
        {
            string[] line = File.ReadAllLines(path);
            if (i < line.Length)
            {
                textBox1.Text = line[i];
                textBox2.Text = "";

            }
            else
            {
                MessageBox.Show("已经做完");
                this.Close();

            }
            i++;
            count++;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            shijian = shijian + 1;
            textBox3.Text = shijian.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled=false;
            textBox2.Enabled = false;

            this.Close();
            Form4 ff = new Form4();
            ff.Show();
           
        }
    }
}

form4---

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 jisuan
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form2.shijian.ToString();
            textBox2.Text = Form2.right.ToString();
            textBox3.Text = Form2.count.ToString();
            textBox4.Text =((Form2.right/(double)(Form2.count))*100).ToString()+"%";
            textBox5.Text = ((Form2.right / (double)(Form2.shijian)) * 100).ToString() + "%";


        }
    }
}

  运行效果如下-----

技术分享技术分享技术分享技术分享技术分享技术分享

好了,结束哈

 

  

 

计算器(工厂模式)

标签:

原文地址:http://www.cnblogs.com/mctianyou/p/4993158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!