# 计算器（工厂模式）

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;
}
}
{
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 "+":
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)
{

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()
{
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)
{

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

}
}
}
```

运行效果如下-----

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

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