Calculator (Factory mode)

Source: Internet
Author: User

1, no words, I added a regular expression to split the string read from the TXT file. Borrow my friend's book "Big Talk Design mode", feel the author is really God-man also. The first chapter on the simple and clear introduction of the factory model, just meet the teacher this homework, lucky ha.

2, the code is as follows

Business Logic---

Using system;using system.collections.generic;using system.linq;using system.text;namespace jisuan{public class Opera        tion {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 ResU            lt = 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 ("divisor cannot be 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;}}         }

Interface logic

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< c1/>{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:\\ Compute store. txt";        Operationfactory oo = new operationfactory (); private void Form2_load (object sender, EventArgs e) {StreamReader sr = new StreamReader (PAT            H,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); This regular expression has no solution to 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 ("Your result is" + result); if (e.keycode==keys.enter) {if (TextBox2.Text = = result.                    ToString ()) {MessageBox.Show ("correct");                right++;    }            else {MessageBox.Show ("error");            } Duqu ();            }} public void Duqu () {string[] line = file.readalllines (path); if (I < line.                Length) {TextBox1.Text = Line[i];            TextBox2.Text = "";                } else {MessageBox.Show ("already done"); This.            Close ();            } i++;        count++;            } private void Timer1_Tick (object sender, EventArgs e) {Shijian = Shijian + 1; TextBox3.Text = Shijian.        ToString (); The 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:\\ Compute store. txt";        Operationfactory oo = new operationfactory (); private void Form2_load (object sender, EventArgs e) {StreamReader sr = new StreamReader (PAT            H,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); This regular expression has no solution to 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 ("Your result is" + result); if (e.keycode==keys.enter) {if (TextBox2.Text = = result.                    ToString ()) {MessageBox.Show ("correct");                right++;    }            else {MessageBox.Show ("error");            } Duqu ();            }} public void Duqu () {string[] line = file.readalllines (path); if (I < line.                Length) {TextBox1.Text = Line[i];            TextBox2.Text = "";                } else {MessageBox.Show ("already done"); This.            Close ();            } i++;        count++;            } private void Timer1_Tick (object sender, EventArgs e) {Shijian = Shijian + 1; TextBox3.Text = Shijian.        ToString (); The 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< c1/>{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 () + "%";}}    }

The effect of the operation is as follows-----

All right, it's over, huh?

  

Calculator (Factory mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.