예제 #1
0
        public void ReadAndCalculateExpression(Cell[,] cells, ReversePolishNotation reversePolishNotation, Transformator transformator)
        {
            string userExpression = Console.ReadLine();
            double result         = transformator.CalculateExpressionWithCoordinates(userExpression, cells, reversePolishNotation);

            Console.WriteLine(result.ToString());
        }
예제 #2
0
 public void TransformCellsWithCoordinates(Cell[,] cells, int maxSize, ReversePolishNotation reversePolishNotation)
 {
     for (int i = 1; i < maxSize; i++)
     {
         for (int j = 1; j < maxSize; j++)
         {
             if (cells[i, j].getValue() == 0 && cells[i, j].GetExpression() != null)
             {
                 string expression = cells[i, j].GetExpression();
                 cells[i, j].SetValue(CalculateExpressionWithCoordinates(expression, cells, reversePolishNotation));
             }
         }
     }
 }
예제 #3
0
 public Spreadsheet(int maxSize)
 {
     _communicator          = new Communicator();
     _transformator         = new Transformator();
     _reversePolishNotation = new ReversePolishNotation();
     _maxSize = maxSize;
     Cells    = new Cell[_maxSize, _maxSize];
     for (int i = 0; i < _maxSize; i++)
     {
         for (int j = 0; j < _maxSize; j++)
         {
             Cells[i, j] = new Cell();
         }
     }
 }
예제 #4
0
        public void StartDialogWithUser(Cell[,] cells, ReversePolishNotation reversePolishNotation, Transformator transformator, int size)
        {
            Console.WriteLine();
            PrintPossibleOptions();
            while (true)
            {
                char command = Console.ReadKey().KeyChar;
                Console.WriteLine();
                switch (command)
                {
                case 'q':
                    Console.WriteLine("Ty for using our Spreadsheet. See you later");
                    return;

                case 'c':
                    Console.WriteLine("Type your expression");
                    ReadAndCalculateExpression(cells, reversePolishNotation, transformator);
                    break;

                case 'u':
                    Console.WriteLine("Updating values is not suported yet");
                    break;

                case 'v':
                    Console.WriteLine("Here you go");
                    PrintSpreadsheetValues(cells, size);
                    break;

                case 'e':
                    Console.WriteLine("Here you go");
                    PrintSpreadsheetExpressions(cells, size);
                    break;

                default:
                    Console.WriteLine("Something goes wrong. try again");
                    PrintPossibleOptions();
                    break;
                }
            }
        }
예제 #5
0
        public void TransformNumbersOnlyExpressions(Cell[,] cells, int maxSize, ReversePolishNotation reversePolishNotation)
        {
            Cell cell = new Cell();

            for (int i = 1; i < maxSize; i++)
            {
                for (int j = 1; j < maxSize; j++)
                {
                    cell = cells[i, j];
                    if (cell.GetExpression() == null)
                    {
                        cell.SetValue(0);
                    }
                    else if (cell.GetExpression()[0] == '-')
                    {
                        cell.SetValue(Convert.ToDouble(cell.GetExpression()));
                    }
                    else if (Regex.IsMatch(cell.GetExpression(), @"^[^A-Z]*$"))
                    {
                        cell.SetValue(reversePolishNotation.Calculate(cell.GetExpression()));
                    }
                }
            }
        }
예제 #6
0
 public void TransformExpressionsToValues(Cell[,] cells, int maxSize, ReversePolishNotation reversePolishNotation)
 {
     TransformNumbersOnlyExpressions(cells, maxSize, reversePolishNotation);
     TransformCellsWithCoordinates(cells, maxSize, reversePolishNotation);
 }
예제 #7
0
        public double CalculateExpressionWithCoordinates(string expression, Cell[,] cells, ReversePolishNotation reversePolishNotation)
        {
            string transformatedExpression = "";

            string[] parts = reversePolishNotation.DivideExpressionOnParts(expression);
            foreach (string part in parts)
            {
                if (Regex.IsMatch(part, @"^[^A-Z]*$"))
                {
                    transformatedExpression += part;
                }
                else
                {
                    transformatedExpression += CoordinatesToCellExpression(cells, part);
                }
            }
            return(reversePolishNotation.Calculate((transformatedExpression)));
        }