简单工厂
static void Main(string[] args) { try { double numa = Convert.ToDouble(Console.ReadLine()); string oper = Console.ReadLine(); double numb = Convert.ToDouble(Console.ReadLine()); double result = 0; switch (oper) { case "+": result = numa + numb; break; case "-": result = numa - numb; break; case "*": result = numa * numb; break; case "/": if (numb != 0) { result = numa / numb; } else { throw new Exception("除数不能为0"); } break; default: break; } Console.WriteLine(result); //var a = new System.Data.DataTable().Compute("1*2*3+5",""); //Console.WriteLine(a); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("" + ex.Message); Console.ReadLine(); } /* * 喝酒唱歌,人生真爽 * 对酒当歌,人生真爽 * 对酒当歌,人生几何 * */ /* * 可维护 可复用 可扩展 灵活性好 * 封装 继承 多态 耦合度降低 * */ #region /* 短歌行 * ⑴对酒当歌,人生几何:拿着酒唱歌,这样能有几回呢? ⑵譬如朝露,去日苦多:就像早晨的露水,过去的日子太多。 ⑶慨当以慷,忧思难忘:应当慷慨高歌,心中的忧思难以忘怀。 ⑷何以解忧,唯有杜康:只有杜康酒可以解除我的忧思。 ⑸青青子衿,悠悠我心:衿:衣领;悠悠:长貌,形容思念之深。表示对贤才的思慕。 ⑹但为君故,沉吟至今:但:只;君:指所思慕的人;沉吟:指低吟《子衿》一诗。 ⑺呦呦鹿鸣,食野之苹:永《诗经.鹿鸣》成句,这里借来表示招纳贤才的意思。苹:艾蒿。 ⑻我有嘉宾,鼓瑟吹笙:我用鼓瑟来迎接贤才。 ⑼明明如月,何时可掇:明月是永不能摘掉的,它的运行也是永不能停止的。 ⑽忧从中来,不可断绝:我绵绵不断的忧愁就从这里来。 ⑾越陌度阡,枉用相存:越过田间的小路,问候远道而来的客人。 ⑿契阔谈宴,心念旧恩:久别重逢,宴饮谈心,诉说往日的情意。 ⒀月明星稀,乌鹊南飞:明月升起 ,星星闪烁,一群寻巢乌鹊向南飞去。 ⒁绕树三匝,何枝可依?乌鹊绕树飞了三周却没敛翅,哪里才有它们栖身之所?此二句比喻贤才多多益善。无依比喻贤士身无所托。 ⒂山不厌高,海不厌深:高山不辞土石才见巍峨,大海不弃涓流才见壮阔。比喻用人要“唯才是举”,多多益善。 ⒃周公吐哺,天下归心。只有像周公那样礼待贤才(周公见到贤才,吐出口中正在咀嚼的食物,马上接待。《史记》载周公自谓:“一沐三握发,一饭三吐哺,犹恐失天下之贤。”),才能使天下人心都归向我。篇末引用周公自比,说明求贤建业的心思[1] 。 */ #endregion }
class Operation { public static double GetResult(double numa,double numb,string operate) { double result = 0; switch (operate) { case "+": result = numa + numb; break; case "-": result = numa - numb; break; case "*": result = numa * numb; break; case "/": if (numb==0) { throw new Exception("除数不能为0"); } result = numa / numb; break; default: break; } return result; } } class Program { static void Main(string[] args) { while (true) { try { double numa = Convert.ToDouble(Console.ReadLine()); string operate = Console.ReadLine(); double numb = Convert.ToDouble(Console.ReadLine()); double result = Operation.GetResult(numa, numb, operate); Console.WriteLine(result); } catch (Exception ex) { Console.WriteLine("" + ex.Message); } //Console.ReadLine(); } } }
namespace ConsoleApplication3 { class Operation { public double NumA { get; set; } public double NumB { get; set; } public virtual double GetResult() { double result = 0; return result; } } class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumA + NumB; return result; } } class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumA - NumB; return result; } } class OperationMul : Operation { public override double GetResult() { double result = 0; result = NumA * NumB; return result; } } class OperationDiv : Operation { public override double GetResult() { double result = 0; if (NumB==0) { throw new Exception("除数不能为0"); } else { result = NumA / NumB; } return result; } } class OperationFactory { public static Operation createOpearation(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; default: break; } return oper; } } class Program { static void Main(string[] args) { while (true) { try { double numa = Convert.ToDouble(Console.ReadLine()); string operate = Console.ReadLine(); double numb = Convert.ToDouble(Console.ReadLine()); Operation oper=OperationFactory.createOpearation(operate); oper.NumA = numa; oper.NumB = numb; double result = oper.GetResult(); Console.WriteLine(result); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } }
namespace ClassLibrary1 { public class Operation { public double NumA { get; set; } public double NumB { get; set; } public virtual double GetResult() { double result = 0; return result; } public static string checkNumberInput(string currentNumber,string inputString) { string result = ""; if (inputString ==".") { if (currentNumber.IndexOf(".")<0) { if (currentNumber.Length==0) { result = "0" + inputString; } else { result = currentNumber + inputString; } } } else if (currentNumber=="0") { result = inputString; } else { result = currentNumber + inputString; } return result; } } public class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumA + NumB; return result; } } public class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumA - NumB; return result; } } public class OperationMul : Operation { public override double GetResult() { double result = 0; result = NumA * NumB; return result; } } public class OperationDiv : Operation { public override double GetResult() { double result = 0; if (NumB==0) { throw new Exception("除数不能为0"); } else { result = NumA / NumB; } return result; } } public class OperationSqr : Operation { public override double GetResult() { double result = 0; result = NumB * NumB; return result; } } public class OperationSqrt : Operation { public override double GetResult() { double result = 0; if (NumB<0) { throw new Exception("负数不能开平方"); } result = Math.Sqrt(NumB); return result; } } public class OperationReverse : Operation { public override double GetResult() { double result = 0; result = -NumB; return result; } } public class OperationFactory { public static Operation createOperation(string operate) { Operation operation = null; switch (operate) { case "+": operation = new OperationAdd(); break; case "-": operation = new OperationSub(); break; case "*": operation = new OperationMul(); break; case "/": operation = new OperationDiv(); break; case"sqr": operation = new OperationSqr(); break; case"sqrt": operation = new OperationSqrt(); break; case "+/-": operation = new OperationReverse(); break; default: break; } return operation; } } }
public partial class Form1 : Form { bool bOperate = false; Operation oper; public Form1() { InitializeComponent(); } private void buttonClear_Click(object sender, EventArgs e) { txtShow.Text = ""; } private void button0_Click(object sender, EventArgs e) { if(bOperate) { txtShow.Text = ""; bOperate = false; } string number = ((Button)sender).Text; txtShow.Text = Operation.checkNumberInput(txtShow.Text,number); } private void buttonAdd_Click(object sender, EventArgs e) { if (txtShow.Text!="") { oper = OperationFactory.createOperation(((Button)sender).Text); oper.NumA = Convert.ToDouble(txtShow.Text); bOperate = true; } } private void buttonEqual_Click(object sender, EventArgs e) { if (txtShow.Text!="") { if (((Button)sender).Text!="=") { oper = OperationFactory.createOperation(((Button)sender).Text); } oper.NumB=Convert.ToDouble(txtShow.Text); txtShow.Text=oper.GetResult().ToString(); bOperate=true; } } }
以上均参考大话设计模式