计算器(工厂模式)

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() + "%";

        }
    }
}

  运行效果如下-----

好了,结束哈

  

时间: 11-23

计算器(工厂模式)的相关文章

通过一个计算器的例子,了解设计模式中的工厂模式

计算器实例方法声明 @interface ViewController : UIViewController { BOOL bBegin; BOOL backOpen; BOOL cptOpen; BOOL secOpen; double fstOperand; double sumOperand; NSString *operator; } @property (strong, nonatomic) IBOutlet UITextField *display; @property (stron

简单工厂模式 - 怎样实现高逼格的计算器(C#)

1. 面向过程的计算器 static void Main(string[] args) { //面向过程的计算器 try { Console.WriteLine("请输入数字A: "); string strNumA = Console.ReadLine(); Console.WriteLine("请选择运算符号(+.-.*./): "); string strOperate = Console.ReadLine(); Console.WriteLine("

简单工厂模式

目前正在看<大话设计模式>,写此系列的文章记录下学习的经过. 简单工厂模式 先看如下代码,使用面向对象的原理实现计算器功能 Operation运算类 1 public class Operation 2 { 3 private double numberA = 0; 4 private double numberB = 0; 5 6 public double NumberA 7 { 8 get { return numberA; } 9 set { numberA = value; } 10

大话设计模式:简单工厂模式

由于面向过程编程造成的代码膨胀问题越来越严重,使其维护的代价高,灵活性很低.为了使代码易维护.易扩展.易复用和灵活性好,所以我们在采用面向对象编程的时候,防止采用面向对象的语言实际上却做着面向过程的事儿,更需要采用某种设计模式,核心就是使程序变得高内聚,低耦合,这样的程序才能达到上面的四个优点.而简单工厂模式的出现也正是为了达到这样一种效果,将工厂和产品分块,具体解决了实例化那个对象(具体产品)的需求.从而实现了高内聚,低耦合.使程序易维护.易扩展.易复用和灵活性好.同时也用到了面向对象编程的三

Python 设计模式--简单工厂模式

简单工厂模式(Factory Pattern)是一种创建型的设计模式,像工厂一样根据要求生产对象实例. 特点:根据不同的条件,工厂实例化出合适的对象. <大话设计模式>中实例:四则运算计算器 代码: 1 #!/usr/bin/env python 2 #-*- coding: utf-8 -*- 3 4 class Operation: 5 def getResult(self): 6 pass 7 8 class OperationAdd(Operation): 9 def getResul

【设计模式】简单工厂模式

以面向对象的思想和简单工厂模式,写一个C++计算器程序,代码如下: #include <iostream> using namespace std; class Operation { public: Operation(double left, double right) { lhs = left; rhs = right; } const double GetLeft() const { return lhs; } const double GetRight() const { retur

《大话设计模式》学习笔记系列--1. 简单工厂模式

简单工厂模式实现了一种"工厂"概念的面向对象设计模式,它可以在不指定对象具体类型的情况下创建对象.其实质是定义一个创建对象的接口,但让实现这个接口的类来决定实例化具体类.工厂方法让类的实例化推迟到子类中进行. 以书本上的计算器程序为例,其UML描述如下: 图中,AddOperator, SubtactOpertor继承算式基类Operator,而CreateOperator则是负责创建一个操作类,而不指明具体的子类类型. 下面,我们来看代码: 首先操作积累: /// <summa

工厂模式——工厂方法模式

工厂方法模式,咋一看在这个计算器程序里好像反而使程序变得更加“复制”,确实代码量好像是增加了,到底有什么好处呢?我想多领会领会,必然有它存在的理由. 我们先来看看“简单工厂模式”和“工厂方法模式”的类继承关系.上篇提到的“简单工厂模式”的类继承关系如下图所示: “工厂方法模式”的类继承关系如下图所示: 发现四个运算类分别有了各自的工厂类,这是为什么呢?我们先来实现代码. 运算符抽象类以及四个它的实现类不做任何变动,参照上篇. 由于多了四个工厂类,所以把工厂类抽象成一个工厂类接口. package

简单工厂模式的C++实现

用简单工厂模式实现一个计算器类: 1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Operation 7 { 8 public: 9 Operation(double numA = 0, int numB = 0) : numberA(numA), numberB(numB) 10 { 11 } 12 virtual ~Operation() 13 { 14 cout <