IntegerCalculator.cs 1.2 KB

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