Guest User

Untitled

a guest
Feb 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace ConsoleApplication1
  7. {
  8. abstract class Operation
  9. {
  10. public abstract Double Calculate();
  11. }
  12. abstract class UnaryOperation : Operation
  13. {
  14. protected Double Number;
  15. public UnaryOperation(Double number)
  16. {
  17. Number = number;
  18. }
  19. }
  20. abstract class BinaryOperation : Operation
  21. {
  22. protected Double Number1;
  23. protected Double Number2;
  24. public BinaryOperation(Double number1, Double number2)
  25. {
  26. Number1 = number1;
  27. Number2 = number2;
  28. }
  29. }
  30. class SumOperation : BinaryOperation
  31. {
  32. public SumOperation(Double number1, Double number2)
  33. : base(number1, number2)
  34. { }
  35. public override Double Calculate()
  36. {
  37. return Number1 + Number2;
  38. }
  39. }
  40. class MinOperation : BinaryOperation
  41. {
  42. public MinOperation(Double number1, Double number2)
  43. : base(number1, number2)
  44. { }
  45. public override Double Calculate()
  46. {
  47. return Number1 - Number2;
  48. }
  49. }
  50.  
  51. class Factorial : UnaryOperation
  52. {
  53. public Factorial(Double number)
  54. : base(number)
  55. { }
  56. public override Double Calculate()
  57. {
  58. return Number!;
  59. }
  60. }
  61.  
  62. class Calculator
  63. {
  64. public void StartWork()
  65. {
  66. List<Operation> operations = new List<Operation>();
  67. while (true)
  68. {
  69. Console.Write("Input expression = ");
  70. String expression = Console.ReadLine();
  71. if (expression == "run")
  72. break;
  73. Operation operation = Parse(expression);
  74. operations.Add(operation);
  75. }
  76. foreach (Operation operation in operations)
  77. {
  78. Double result = operation.Calculate();
  79. Console.WriteLine(result);
  80. }
  81. }
  82. private Operation Parse(String expression)
  83. {
  84. Double Number1;
  85. Double Number2;
  86. Double Number;
  87. if (expression.Contains('+'))
  88. {
  89. String[] numbers = expression.Split('+');
  90. Number1 = Convert.ToDouble(numbers[0]);
  91. Number2 = Convert.ToDouble(numbers[1]);
  92. return new SumOperation(Number1, Number2);
  93. }
  94. if (expression.Contains('-'))
  95. {
  96. String[] numbers = expression.Split('-');
  97. Number1 = Convert.ToDouble(numbers[0]);
  98. Number2 = Convert.ToDouble(numbers[1]);
  99. return new MinOperation(Number1, Number2);
  100. }
  101. if (expression.Contains('!'))
  102. {
  103. String[] numbers = expression.Split('!');
  104. Number = Convert.ToDouble(numbers[0]);
  105. return new Factorial(Number);
  106. }
  107.  
  108. else
  109. {
  110. Console.WriteLine("AAAAAAAAAAAAAAA");
  111. throw new NotImplementedException();
  112. }
  113. }
  114. }
  115. class Program
  116. {
  117. static void Main(string[] args)
  118. {
  119. Calculator calculator = new Calculator();
  120. calculator.StartWork();
  121. Console.ReadKey();
  122. }
  123. }
  124. }
Add Comment
Please, Sign In to add comment