コード例 #1
0
        private static Cell Parse(string s)
        {
            StringBuilder enter = new StringBuilder(s);
            Cell          cell  = NumericCellFactory.GetCell(vertical, horizontal, enter);

            if (cell is ErrorCell)
            {
                return(GetValueByCell(s));
            }
            else
            {
                return(cell);
            }
        }
コード例 #2
0
ファイル: CellFactory.cs プロジェクト: biglier/SpreadSheet
        public static Cell GetCell(int vertical, int horizontal, StringBuilder enter, Dictionary <Coordinates, Cell> dictionary,
                                   List <String> columnList)
        {
            char firstSymbol = enter[0];

            switch (firstSymbol)
            {
            case '\'':
            {
                return(new StringCell(vertical, horizontal, enter));
            }

            case '=':
            {
                return(ExpressionCellFactory.GetCell(vertical, horizontal, enter, "#In Proccess", dictionary, columnList));
            }

            default:
                return(NumericCellFactory.GetCell(vertical, horizontal, enter));
            }
        }
コード例 #3
0
 public static Cell GetCell(int _vertical, int _horizontal, StringBuilder enter, string conclusion,
                            Dictionary <Coordinates, Cell> _dictionary, List <String> _columnList)
 {
     dictionary  = _dictionary;
     columnsList = _columnList;
     vertical    = _vertical;
     horizontal  = _horizontal;
     firstEnter  = enter;
     if (enter.Length == 1)
     {
         string c = "#Input more characters";
         return(new ErrorCell(vertical, horizontal, enter, c));
     }
     else
     {
         List <OperationPositions> operationPositions = new List <OperationPositions>();
         for (int i = 1; i < enter.Length; i++)
         {
             if (enter[i] == '-' || enter[i] == '+' || enter[i] == '*' || enter[i] == '/')
             {
                 operationPositions.Add(new OperationPositions(enter[i], i));
             }
         }
         if (operationPositions.Count == 0)
         {
             Cell cell = Parse(enter.ToString().Substring(1));
             return(cell);
         }
         if (operationPositions[0].position == 1 && operationPositions[operationPositions.Count - 1].position == enter.Length - 1)
         {
             return(new ErrorCell(vertical, horizontal, enter, "#Check operations position"));
         }
         Cell fVarCell = Parse(enter.ToString().Substring(1, operationPositions[0].position - 1));
         if (fVarCell is ErrorCell)
         {
             return(fVarCell);
         }
         float result = ReturnFloat(fVarCell);
         for (int j = 0; j < operationPositions.Count; j++)
         {
             if (operationPositions.Count > 1 && j < operationPositions.Count - 1 &&
                 operationPositions[j].position == operationPositions[j + 1].position + 1)
             {
                 return(new ErrorCell(vertical, horizontal, enter, "#Check operations"));
             }
             Cell varCell;
             int  lenght = 0;
             if (j == operationPositions.Count - 1)
             {
                 lenght = enter.Length - (1 + operationPositions[j].position);
             }
             else
             {
                 lenght = operationPositions[j + 1].position - (operationPositions[j].position + 1);
             }
             varCell = Parse(enter.ToString().Substring(operationPositions[j].position + 1, lenght));
             if (varCell is ErrorCell)
             {
                 return(varCell);
             }
             float variable = ReturnFloat(varCell);
             if (variable == 0 && operationPositions[j].operation == '/')
             {
                 return(new ErrorCell(vertical, horizontal, enter, "#zeroDivision"));
             }
             try
             {
                 result = Calculation.Calculate(operationPositions[j].operation, result, variable);
             }
             catch (Exception aEx)
             {
                 return(new ErrorCell(vertical, horizontal, enter, "#There is invalid in Calculation"));
             }
         }
         StringBuilder resultString = new StringBuilder(result.ToString());
         return(NumericCellFactory.GetCell(vertical, horizontal, resultString));
     }
 }