using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace GossipDesignPattern.SimpleFactory { class Program { static void Main(string[] args) { Console.Write("input first number:"); string A = Console.ReadLine();
Console.Write("(+,-,*,/): "); string B = Console.ReadLine();
Console.WriteLine("input second number:");
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("result: " + D); } }
class ProgramV2 { static void Main(string[] args) { try { Console.Write("input first number:"); string strNumberA = Console.ReadLine();
Console.Write("(+,-,*,/): "); string strOperate = Console.ReadLine();
Console.WriteLine("input second number:"); 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 "/": if (strNumberB != "0") strResult = Convert.ToString(Convert.ToDouble(strNumberA) / Convert.ToDouble(strNumberB)); else strResult = "Divisor can't be 0"; break; } Console.WriteLine("result: " + strResult);
Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("Error input:" + ex.ToString()); } } }
public 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 "/": result = numberA / numberB; break; }
return result; }
static void Main(string[] args) { try { Console.Write("input first number:"); string strNumberA = Console.ReadLine();
Console.Write("(+,-,*,/): "); string strOperate = Console.ReadLine();
Console.WriteLine("input second number:"); string strNumberB = Console.ReadLine();
string strResult = ""; strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB), strOperate));
Console.WriteLine("result: " + strResult);
Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("Error input:" + ex.ToString()); } } }
public class OperationBase { 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 : OperationBase { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } }
class OperationSub : OperationBase { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; } }
class OperationMul : OperationBase { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } }
class OperationDiv : OperationBase { public override double GetResult() { double result = 0; if (NumberB == 0) throw new Exception("Divisor can't be 0"); result = NumberA / NumberB; return result; } }
public class OperationFactory { public static OperationBase CreateOperate(string operate) { OperationBase 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; }
static void main(string[] args) { OperationBase oper; oper = OperationFactory.CreateOperate("+"); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); } }
public class FactoryMethod { interface IFactory { OperationBase CreateOperation(); }
class AddFactory : IFactory { public OperationBase CreateOperation() { return new OperationAdd(); } }
class SubFactory : IFactory { public OperationBase CreateOperation() { return new OperationSub(); } }
class MulFactory : IFactory { public OperationBase CreateOperation() { return new OperationMul(); } }
class DivFactory : IFactory { public OperationBase CreateOperation() { return new OperationDiv(); } }
static void main(string[] args) { IFactory operFactory = new AddFactory(); OperationBase oper = operFactory.CreateOperation(); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); } } }
|