public GridData(GridRawData gridRawData) { this.gridCount = gridRawData.gridCount; this.sections = new Section[gridRawData.sectionDatas.Length]; for (int i = 0; i < this.sections.Length; i++) { string sectionData = gridRawData.sectionDatas[i]; char operationSymbol = sectionData[0]; Section.MathOperation operation = Section.MathOperation.None; int result = int.Parse(sectionData.Substring(1, sectionData.Length - 1)); if (operationSymbol == '+') { operation = Section.MathOperation.Add; } else if (operationSymbol == '-') { operation = Section.MathOperation.Substract; } else if (operationSymbol == '×') { operation = Section.MathOperation.Multiply; } else if (operationSymbol == '÷') { operation = Section.MathOperation.Divide; } this.sections[i] = new Section(result, operation, i); } gridCells = new GridCell[gridRawData.gridCount, gridRawData.gridCount]; for (int i = 0; i < gridRawData.gridCount; i++) { for (int j = 0; j < gridRawData.gridCount; j++) { int sectionIndex = gridRawData.sectionIndexes[i, j]; gridCells[i, j] = new GridCell(i, j, this.sections[sectionIndex], this); } } for (int i = 0; i < sections.Length; ++i) { for (int j = 0; j < gridRawData.gridCount; ++j) { for (int k = 0; k < gridRawData.gridCount; ++k) { if (gridRawData.sectionIndexes[j, k] == i) { sections[i].AddGridData(gridCells[j, k]); } } } } }
public static int GetResult(this Section.MathOperation operation, params int[] numbers) { int result = 0; if (operation == Section.MathOperation.Add) { for (int i = 0; i < numbers.Length; i++) { result += numbers[i]; } } else if (operation == Section.MathOperation.Substract) { return(Math.Abs(numbers[0] - numbers[1])); } else if (operation == Section.MathOperation.Multiply) { result = 1; for (int i = 0; i < numbers.Length; i++) { result *= numbers[i]; } } else if (operation == Section.MathOperation.Divide) { if (numbers[0] > numbers[1]) { float floatResult; floatResult = (float)numbers[0] / numbers[1]; result = numbers[0] / numbers[1]; if (result * 10 == floatResult * 10) { return(result); } else { return(-1); } } else { float floatResult; floatResult = (float)numbers[1] / numbers[0]; result = numbers[1] / numbers[0]; if (result * 10 == floatResult * 10) { return(result); } else { return(-1); } } } return(result); }
public static Section.MathOperation Inverse(this Section.MathOperation operation) { if (operation == Section.MathOperation.Add) { return(Section.MathOperation.Substract); } else if (operation == Section.MathOperation.Substract) { return(Section.MathOperation.Add); } else if (operation == Section.MathOperation.Multiply) { return(Section.MathOperation.Divide); } else if (operation == Section.MathOperation.Divide) { return(Section.MathOperation.Multiply); } else { return(Section.MathOperation.None); } }
public static string ToSymbol(this Section.MathOperation operation) { if (operation == Section.MathOperation.Add) { return("+"); } else if (operation == Section.MathOperation.Substract) { return("-"); } else if (operation == Section.MathOperation.Multiply) { return("×"); } else if (operation == Section.MathOperation.Divide) { return("÷"); } else { return("."); } }