static void Main(string[] args) { Operation oper = null; oper = OperationFactory.createOperate("+"); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); Console.WriteLine(result); Console.ReadLine(); }
static void Main(string[] args) { #region 过程式 //Console.WriteLine("请输入数字A: "); //string A = Console.ReadLine(); //Console.WriteLine("请选择运算符号(+、-、*、/): "); //string B = Console.ReadLine(); //Console.WriteLine("请输入数字B: "); //string C = Console.ReadLine(); //string D = ""; //if (B == "+") //{ // D = Convert.ToString(Convert.ToDouble(A) + Convert.ToDouble(C)); //} //if (B == "-") //{ // D = Convert.ToString(Convert.ToDouble(A) - Convert.ToDouble(C)); //} //if (B == "*") //{ // D = Convert.ToString(Convert.ToDouble(A) * Convert.ToDouble(C)); //} //if (B == "/") //{ // D = Convert.ToString(Convert.ToDouble(A) / Convert.ToDouble(C)); //} //Console.WriteLine("结果是: " + D); //Console.ReadKey(); #endregion #region 修改后的过程式 //try //{ // Console.WriteLine("请输入数字A: "); // string strNumberA = Console.ReadLine(); // Console.WriteLine("请选择运算符号(+、-、*、/): "); // string strOperate = Console.ReadLine(); // Console.WriteLine("请输入数字B: "); // string strNumberB = Console.ReadLine(); // string strResult = ""; // switch (strOperate) // { // case "+": // strResult = Convert.ToString(Convert.ToDouble(strNumberA) + Convert.ToDouble(strNumberB)); // break; // case "-": // strResult = Convert.ToString(Convert.ToDouble(strNumberA) - Convert.ToDouble(strNumberB)); // break; // case "*": // strResult = Convert.ToString(Convert.ToDouble(strNumberA) * Convert.ToDouble(strNumberB)); // break; // case "/": // strResult = Convert.ToString(Convert.ToDouble(strNumberA) / Convert.ToDouble(strNumberB)); // break; // default: // break; // } // Console.WriteLine("结果是: " + strResult); // Console.ReadKey(); //} //catch (Exception ex) //{ // Console.WriteLine("您的输入有错: " + ex.Message); // throw; //} #endregion #region 使用面向对象类 //try //{ // Console.WriteLine("请输入数字A: "); // string strNumberA = Console.ReadLine(); // Console.WriteLine("请选择运算符号(+、-、*、/): "); // string strOperate = Console.ReadLine(); // Console.WriteLine("请输入数字B: "); // string strNumberB = Console.ReadLine(); // string strResult = ""; // strResult = Convert.ToString(Operate.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB), strOperate)); // Console.WriteLine("结果是: " + strResult); // Console.ReadKey(); //} //catch (Exception ex) //{ // Console.WriteLine("您的输入有错: " + ex.Message); // throw; //} #endregion #region 使用松耦合类 + 简单工厂 try { Console.WriteLine("请输入数字A: "); string strNumberA = Console.ReadLine(); Console.WriteLine("请选择运算符号(+、-、*、/): "); string strOperate = Console.ReadLine(); Console.WriteLine("请输入数字B: "); string strNumberB = Console.ReadLine(); Operation oper; oper = OperationFactory.createOperate(strOperate); oper.NumberA = Convert.ToDouble(strNumberA); oper.NumberB = Convert.ToDouble(strNumberB); string result = Convert.ToString(oper.GetResult()); Console.WriteLine("结果是: " + result); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine("您的输入有错: " + ex.Message); Console.ReadKey(); } #endregion }