IntegerCalculator.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. namespace InABox.WPF
  5. {
  6. public class IntegerCalculatorFunction : AbstractCalculatorFunction<int, NumericCalculatorType>
  7. {
  8. protected override int Calculate(int current, int next, NumericCalculatorType type)
  9. {
  10. var result = type switch
  11. {
  12. NumericCalculatorType.Sum => current + next,
  13. NumericCalculatorType.Product => current * next,
  14. NumericCalculatorType.Subtraction => current - next,
  15. NumericCalculatorType.Maximum => current > next ? current : next,
  16. NumericCalculatorType.Minimum => current < next ? current : next,
  17. _ => current
  18. };
  19. return result;
  20. }
  21. }
  22. public class IntegerArrayConverter : AbstractNumericArrayConverter<int>
  23. {
  24. protected override int[] Convert(IEnumerable<string> values)
  25. {
  26. var result = values.Select(int.Parse).ToArray();
  27. return result;
  28. }
  29. }
  30. public class IntegerCalculator : AbstractNumericCalculator<int, IntegerCalculatorFunction>
  31. {
  32. [TypeConverter(typeof(IntegerArrayConverter))]
  33. public override int[] Constants { get; set; }
  34. }
  35. }