Lirbo

Cell

Apr 26th, 2023
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.88 KB | None | 0 0
  1. public abstract class Cell
  2.         {
  3.             protected string cell;
  4.  
  5.             public virtual int Calculate()
  6.             {
  7.                 return 0;
  8.             }
  9.  
  10.             public virtual int GetValue()
  11.             {
  12.                 return 0;
  13.             }
  14.             public override string ToString()
  15.             {
  16.                 return cell;
  17.             }
  18.  
  19.             public string GetCell() { return cell; }
  20.         }
  21.  
  22.         public class ValueCell : Cell
  23.         {
  24.             private int value;
  25.             public ValueCell(string cell, int value) : base()
  26.             {
  27.                 this.cell = cell;
  28.                 this.value = value;
  29.             }
  30.  
  31.             public override int GetValue() { return value; }
  32.             public void SetValue(int value) { this.value = value; }
  33.  
  34.             public override string ToString()
  35.             {
  36.                 return $"{cell}:{value}";
  37.             }
  38.         }
  39.  
  40.         public abstract class FunctionCell : Cell
  41.         {
  42.             public virtual string FunctionName()
  43.             {
  44.                 return "N/A";
  45.             }
  46.         }
  47.  
  48.         public class SumCell : FunctionCell
  49.         {
  50.             private ValueCell[] valueCells;
  51.             public SumCell(string cell, Cell[] valueCells) : base()
  52.             {
  53.                 this.cell = cell;
  54.                 this.valueCells = new ValueCell[valueCells.Length];
  55.                 for(int i = 0; i < valueCells.Length; i++)
  56.                 {
  57.                     this.valueCells[i] = (ValueCell)valueCells[i];
  58.                 }
  59.             }
  60.             public override string ToString()
  61.             {
  62.                 string cellsString = "";
  63.                 for (int i = 0; i < valueCells.Length; i++)
  64.                     cellsString = cellsString + $" {valueCells[i].GetCell()}";
  65.                 return $"{cell}:SUM({cellsString})";
  66.             }
  67.  
  68.             public override int Calculate()
  69.             {
  70.                 int sum = 0;
  71.                 for (int i = 0; i < valueCells.Length; i++)
  72.                     sum += valueCells[i].GetValue();
  73.                 return sum;
  74.             }
  75.  
  76.             public override string FunctionName()
  77.             {
  78.                 return "Sum";
  79.             }
  80.         }
  81.  
  82.         public class MinCell : FunctionCell
  83.         {
  84.             private ValueCell[] valueCells;
  85.             public MinCell(string cell, ValueCell[] valueCells) : base()
  86.             {
  87.                 this.cell = cell;
  88.                 this.valueCells = valueCells;
  89.             }
  90.             public override string ToString()
  91.             {
  92.                 string cellsString = "";
  93.                 for (int i = 0; i < valueCells.Length; i++)
  94.                     cellsString = cellsString + $" {valueCells[i].GetCell()}";
  95.                 return $"{cell}:MIN({cellsString})";
  96.             }
  97.  
  98.             public override int Calculate()
  99.             {
  100.                 int min = Math.Min(valueCells[0].GetValue(), valueCells[1].GetValue());
  101.                 for(int i = 2; i < valueCells.Length; i++)
  102.                     min = Math.Min(min, valueCells[i].GetValue());
  103.                 return min;
  104.             }
  105.  
  106.             public override string FunctionName()
  107.             {
  108.                 return "Min";
  109.             }
  110.         }
  111.  
  112.         public class MaxCell : FunctionCell
  113.         {
  114.             private Cell[] valueCells;
  115.             public MaxCell(string cell, Cell[] valueCells) : base()
  116.             {
  117.                 this.cell = cell;
  118.                 this.valueCells = new ValueCell[valueCells.Length];
  119.                 for(int i = 0; i < valueCells.Length; i++)
  120.                 {
  121.                     if (valueCells[i] is ValueCell)
  122.                         this.valueCells[i] = (ValueCell)valueCells[i];
  123.                     else
  124.                     {
  125.                         ValueCell temp = new ValueCell(valueCells[i].GetCell(), valueCells[i].Calculate());
  126.                         this.valueCells[i] = temp;
  127.                     }
  128.                        
  129.                 }
  130.                    
  131.             }
  132.             public override string ToString()
  133.             {
  134.                 string cellsString = "";
  135.                 for (int i = 0; i < valueCells.Length; i++)
  136.                     cellsString = cellsString + $" {valueCells[i].GetCell()}";
  137.                 return $"{cell}:MAX({cellsString})";
  138.             }
  139.  
  140.             public override int Calculate()
  141.             {
  142.                 int max = Math.Max(valueCells[0].GetValue(), valueCells[1].GetValue());
  143.                 for (int i = 2; i < valueCells.Length; i++)
  144.                     max = Math.Max(max, valueCells[i].GetValue());
  145.                 return max;
  146.             }
  147.  
  148.             public override string FunctionName()
  149.             {
  150.                 return "Max";
  151.             }
  152.         }
  153.  
  154.         public class AvgCell : FunctionCell
  155.         {
  156.             private ValueCell[] valueCells;
  157.             public AvgCell(string cell, ValueCell[] valueCells) : base()
  158.             {
  159.                 this.cell = cell;
  160.                 this.valueCells = valueCells;
  161.             }
  162.             public override string ToString()
  163.             {
  164.                 string cellsString = "";
  165.                 for (int i = 0; i < valueCells.Length; i++)
  166.                     cellsString = cellsString + $" {valueCells[i].GetCell()}";
  167.                 return $"{cell}:AVG({cellsString})";
  168.             }
  169.             public override int Calculate()
  170.             {
  171.                 int sum = 0;
  172.                 int count = 0;
  173.                 for(int i = 0; i < valueCells.Length; i++)
  174.                 {
  175.                     sum += valueCells[i].GetValue();
  176.                     count++;
  177.                 }
  178.                 return sum / count;
  179.             }
  180.  
  181.             public override string FunctionName()
  182.             {
  183.                 return "Avg";
  184.             }
  185.         }
Advertisement
Add Comment
Please, Sign In to add comment