1)首先使用C#的面向过程的编写代码实现了简单计算器的计算,代码如下:
try { Console.WriteLine("请输入数字A:"); string numberA = Console.ReadLine(); Console.WriteLine("请输入计算字符“+,-,*,/”"); string operater = Console.ReadLine(); Console.WriteLine("请输入数字B:"); string numberB = Console.ReadLine(); double strRusult=0; if (operater == "+") { strRusult = Convert.ToDouble(numberA) + Convert.ToDouble(numberA); } else if (operater == "-") { strRusult = Convert.ToDouble(numberA) - Convert.ToDouble(numberA); } else if (operater == "*") { strRusult = Convert.ToDouble(numberA) * Convert.ToDouble(numberA); } else if (operater == "/") { if (Convert.ToDouble( numberB) != 0) { strRusult = Convert.ToDouble(numberA) / Convert.ToDouble(numberA); } else { throw new Exception("除数不能为零!"); } } Console.WriteLine("计算机果是" + strRusult.ToString()); Console.ReadKey(); } catch (Exception ex) { throw new Exception(ex.Message, ex); }
在此过程中的缺点很明显就是在每次的计算过程中,都要对每个运算符进行if语句的验证,而且代码是不可以重用的,很是繁琐,不利于后期的功能添加和维护。
2)使用面像对象的思想对验证和的方法进行封转,使,展示层和逻辑运算代码层进行分割,然后在主运算层进行调用,代码,如下:
首先,定义一个Operation的类,其中的代码如下:
namespace 面向对象的计算器 { class Operation { public static double GetResult(double numberA, double numberB, string operate) { double result = 0d; switch (operate) { case"+": { result = numberA + numberB; } break; case "-": { result = numberA - numberB; } break; case"*": { result = numberA * numberB; } break; case"/": { if(numberB!=0) { result=numberA/numberB; } else { Console.WriteLine("除数不能为零!"); } } break; } return result; } } }定义玩相应的类以后,在主界面进行调用,代码如下:
try { Console.WriteLine("请输入数字A:"); string numberA = Console.ReadLine(); Console.WriteLine("请输入计算字符“+,-,*,/”"); string operater = Console.ReadLine(); Console.WriteLine("请输入数字B:"); string numberB = Console.ReadLine(); string strRusult = ""; strRusult = Convert.ToString(Operation.GetResult(Convert.ToDouble(numberA), Convert.ToDouble(numberB), operater)); Console.WriteLine("计算机果是" + strRusult); Console.ReadKey(); } catch (Exception ex) { throw new Exception(ex.Message, ex); }
这样完成对运算符验证的封装,而且使用Switch语句,对运算符进行验证,不用每次对每个运算符都进行验证,节省代码,可以代码重用。
另外,体现完全面像对象思想的简单计算器的计算,就是定义一个计算类,具有相应计算的属性,课计算的虚方法。分别定义相应的计算的类,然后,在进行计算时候调用,相应的类,并且对类中包含方法进行重写,实现,封装,继承,多态的思想,
代码结构:
代码如下:
定义的 Operation1.cs父类,其他的计算方法从该类进行继承,
namespace 面向对象的计算器 { class Operation1 { 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; } } }
OperationAdd.cs类实现加法运算:
namespace 面向对象的计算器 { class OperationAdd:Operation1 { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } }
OperationSub.cs类实现减法运算:
namespace 面向对象的计算器 { class OperationSub:Operation1 { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; //return base.GetResult(); } } }
namespace 面向对象的计算器 { class OperationMul:Operation1 { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; //return base.GetResult(); } } }
OpetionDiv.cs类实现除法的运算:
namespace 面向对象的计算器 { class OpetionDiv:Operation1 { public override double GetResult() { double result = 0; if (NumberB != 0) { result = NumberA / NumberB; } else { //throw new Exception("除数不能为零!"); Console.WriteLine("除数不能为零!"); } return result; //return base.GetResult(); } } }
OperationFactory.cs类,来判断主函数应该调用哪个类进行运算:
namespace 面向对象的计算器 { class OperationFactory { public static Operation1 creatOperation(string operate) { Operation1 oper = null; switch (operate) { case "+": { oper = new OperationAdd(); break; } case "-": { oper = new OperationSub(); break; } case "*": { oper = new OperationMul(); break; } case "/": { oper = new OpetionDiv(); break; } } return oper; } } }
主函数中进行调用:
Operation1 oper = new Operation1(); oper = OperationFactory.creatOperation("+"); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); Console.WriteLine("计算结果为:" + result); Console.ReadKey();
最后完全使用面向对象的思想,实现封装,继承,多态的思想。每一块的代码,都是可以重用,每个部分单独成块,互不影响。
版权声明:本文为博主原创文章,未经博主允许不得转载。