Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- abstract class Operation
- {
- public abstract Double Calculate();
- }
- abstract class UnaryOperation : Operation
- {
- protected Double Number;
- public UnaryOperation(Double number)
- {
- Number = number;
- }
- }
- abstract class BinaryOperation : Operation
- {
- protected Double Number1;
- protected Double Number2;
- public BinaryOperation(Double number1, Double number2)
- {
- Number1 = number1;
- Number2 = number2;
- }
- }
- class SumOperation : BinaryOperation
- {
- public SumOperation(Double number1, Double number2)
- : base(number1, number2)
- { }
- public override Double Calculate()
- {
- return Number1 + Number2;
- }
- }
- class MinOperation : BinaryOperation
- {
- public MinOperation(Double number1, Double number2)
- : base(number1, number2)
- { }
- public override Double Calculate()
- {
- return Number1 - Number2;
- }
- }
- class Factorial : UnaryOperation
- {
- public Factorial(Double number)
- : base(number)
- { }
- public override Double Calculate()
- {
- return Number!;
- }
- }
- class Calculator
- {
- public void StartWork()
- {
- List<Operation> operations = new List<Operation>();
- while (true)
- {
- Console.Write("Input expression = ");
- String expression = Console.ReadLine();
- if (expression == "run")
- break;
- Operation operation = Parse(expression);
- operations.Add(operation);
- }
- foreach (Operation operation in operations)
- {
- Double result = operation.Calculate();
- Console.WriteLine(result);
- }
- }
- private Operation Parse(String expression)
- {
- Double Number1;
- Double Number2;
- Double Number;
- if (expression.Contains('+'))
- {
- String[] numbers = expression.Split('+');
- Number1 = Convert.ToDouble(numbers[0]);
- Number2 = Convert.ToDouble(numbers[1]);
- return new SumOperation(Number1, Number2);
- }
- if (expression.Contains('-'))
- {
- String[] numbers = expression.Split('-');
- Number1 = Convert.ToDouble(numbers[0]);
- Number2 = Convert.ToDouble(numbers[1]);
- return new MinOperation(Number1, Number2);
- }
- if (expression.Contains('!'))
- {
- String[] numbers = expression.Split('!');
- Number = Convert.ToDouble(numbers[0]);
- return new Factorial(Number);
- }
- else
- {
- Console.WriteLine("AAAAAAAAAAAAAAA");
- throw new NotImplementedException();
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Calculator calculator = new Calculator();
- calculator.StartWork();
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment