Guest User

Untitled

a guest
Feb 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 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. /// <summary>
  9. /// Абстрактный класс Операции.
  10. /// </summary>
  11.  
  12.  
  13.  
  14. abstract class Operation
  15. {
  16. public abstract Double Calculate();
  17.  
  18. }
  19.  
  20. /// <summary>
  21. /// Абстрактный класс Унарной Операции. Наследуется от класса всех Операций
  22. /// </summary>
  23. abstract class UnaryOperation : Operation
  24. {
  25. protected Double Number;
  26. public UnaryOperation(Double number)
  27. {
  28. Number = number;
  29. }
  30. }
  31. /// <summary>
  32. /// Абстрактный класс Бинарной Опрерации. Наследуется от класса всех Операций.
  33. /// </summary>
  34. abstract class BinaryOperation : Operation
  35. {
  36. protected Double Number1;
  37. protected Double Number2;
  38. public BinaryOperation(Double number1, Double number2)
  39. {
  40. Number1 = number1;
  41. Number2 = number2;
  42. }
  43. }
  44. class SumOperation : BinaryOperation
  45. {
  46. public SumOperation(Double number1, Double number2)
  47. : base(number1, number2)
  48. { }
  49. public override Double Calculate()
  50. {
  51. return Number1 + Number2;
  52. }
  53. }
  54. class MinOperation : BinaryOperation
  55. {
  56. public MinOperation(Double number1, Double number2)
  57. : base(number1, number2)
  58. { }
  59. public override Double Calculate()
  60. {
  61. return Number1 - Number2;
  62. }
  63. }
  64.  
  65. class Factorial : UnaryOperation
  66. {
  67. public Factorial(Double number)
  68. : base(number)
  69. { }
  70. public override Double Calculate()
  71. {
  72. Double Factorial = 1;
  73. for (int i = 1; i <= Number; i++)
  74. {
  75. Factorial = Factorial * i;
  76. }
  77.  
  78. return Factorial;
  79.  
  80. }
  81. }
  82.  
  83. class Calculator
  84. {
  85. public void StartWork()
  86. {
  87. List<Operation> operations = new List<Operation>();
  88. while (true)
  89. {
  90. Console.Write("Input expression = ");
  91. String expression = Console.ReadLine();
  92. if (expression == "run")
  93. break;
  94. Operation operation = Parse(expression);
  95. operations.Add(operation);
  96. }
  97. foreach (Operation operation in operations)
  98. {
  99. Double result = operation.Calculate();
  100. Console.WriteLine(result);
  101. }
  102. }
  103. private Operation Parse(String expression)
  104. {
  105. Double Number1;
  106. Double Number2;
  107. Double Number;
  108. if (expression.Contains('+'))
  109. {
  110. String[] numbers = expression.Split('+');
  111. Number1 = Convert.ToDouble(numbers[0]);
  112. Number2 = Convert.ToDouble(numbers[1]);
  113. return new SumOperation(Number1, Number2);
  114. }
  115. else if (expression.Contains('-'))
  116. {
  117. String[] numbers = expression.Split('-');
  118. Number1 = Convert.ToDouble(numbers[0]);
  119. Number2 = Convert.ToDouble(numbers[1]);
  120. return new MinOperation(Number1, Number2);
  121. }
  122. else if (expression.Contains('!'))
  123. {
  124. String[] numbers = expression.Split('!');
  125. Number = Convert.ToDouble(numbers[0]);
  126. return new Factorial(Number);
  127. }
  128.  
  129. else
  130. {
  131. Console.WriteLine("AAAAAAAAAAAAAAA");
  132. throw new NotImplementedException(); ///Обработка всех непредусмотренных операций.
  133.  
  134. }
  135. }
  136. }
  137.  
  138. class Program
  139. {
  140. static void Main(string[] args)
  141. {
  142. Calculator calculator = new Calculator();
  143. calculator.StartWork();
  144. Console.ReadKey();
  145. }
  146. }
  147. }
Add Comment
Please, Sign In to add comment