Exemple #1
0
        public MathCell ProcessCellInput(DataGridViewCell inputCell)
        {
            string newFormula = string.Empty;

            if (inputCell.Value != null)
            {
                newFormula = inputCell.Value.ToString();
            }

            MathCell mCell = GetMathCell(inputCell);

            mCell.Formula     = newFormula;
            mCell.RowIndex    = inputCell.RowIndex;
            mCell.ColumnIndex = inputCell.ColumnIndex;

            object prevValue = mCell.Value;

            mCell.EvaluateFormula();
            object currentValue = mCell.Value;

            if (prevValue != null && prevValue.ToString() == "RecursiveReference" ||
                currentValue.ToString() == "RecursiveReference")
            {
                foreach (MathCell usedCell in UsedCells)
                {
                    usedCell.RemoveFromBoundedCells();
                    OnGridMathCellsProvider.UpdateValuesOnGrid(UsedCells, OwnerGrid);
                }
            }

            OnGridMathCellsProvider.UpdateDependentsOnGrid(mCell, OwnerGrid);

            return(mCell);
        }
Exemple #2
0
        public override object Parse(MathCell ownerMathCell)
        {
            string formula = ownerMathCell.DereferencedFormula =
                ownerMathCell.Formula = RemoveRedundantZeroes(ownerMathCell);

            if (AddressesHandler.HasAnyAddress(formula))
            {
                int i = 0;
                while (AddressesHandler.HasAnyAddress(formula)) // While formula contains any reference
                {
                    MathCell refCell    = ownerMathCell.References[i];
                    string   refAddress = refCell.OwnAddress;

                    if (refCell.Value == null)
                    {
                        refCell.EvaluateFormula();
                    }

                    string refValue = refCell.Value.ToString();
                    if (string.IsNullOrEmpty(refValue))
                    {
                        ownerMathCell.DereferencedFormula = formula = formula.Replace(refAddress, "0");
                    }
                    ownerMathCell.DereferencedFormula = formula = formula.Replace(refAddress, refValue);

                    ++i;
                }
            }

            return(base.Parse(ownerMathCell));
        }
Exemple #3
0
        public override object Parse(MathCell ownerMathCell)
        {
            ownerMathCell.ResetReferences();

            string formula = ownerMathCell.Formula;

            if (AddressesHandler.HasAnyAddress(formula))
            {
                List <string> formulaReferences = AddressesHandler.GetAddresses(formula);
                for (int i = 0; i < formulaReferences.Count; ++i)
                {
                    string reference = formulaReferences[i];

                    MathCell refCell =
                        MathCellsProvider.GetInstance.GetMathCell(reference);

                    if (!ownerMathCell.References.Contains(refCell))
                    {
                        ownerMathCell.InsertReference(refCell);
                    }

                    if (!refCell.Dependents.Contains(ownerMathCell))
                    {
                        refCell.InsertDependent(ownerMathCell);
                    }
                }
            }

            return(base.Parse(ownerMathCell));
        }
Exemple #4
0
        private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            MathCell mCell = MathCellsProvider.GetMathCell(dataGridView.CurrentCell);

            AddressBox.Text = mCell.OwnAddress;
            FormulaBox.Text = mCell.Formula;
        }
Exemple #5
0
        public override object Parse(MathCell ownerMathCell)
        {
            string     formula = ownerMathCell.DereferencedFormula;
            Expression expr    = new Expression(formula);

            return(expr.calculate());
        }
Exemple #6
0
        public MathCell GetMathCell(string address)
        {
            var(rowIndex, colIndex) = AddressesHandler.GetIndexes(address);
            MathCell mCell = (MathCell)OwnerGrid.dataGridView[colIndex, rowIndex].Tag;

            return(mCell);
        }
        public void DoShift(int delimiterIndex, MathCell mCell, GridForm ownerGrid)
        {
            int colIndex = mCell.ColumnIndex;

            if (colIndex > 0 && colIndex > delimiterIndex && colIndex <= ownerGrid.dataGridView.ColumnCount)
            {
                --mCell.ColumnIndex;
            }
        }
        public void DoShift(int delimiterIndex, MathCell mCell, GridForm ownerGrid)
        {
            int rIndex = mCell.RowIndex;

            if (rIndex >= delimiterIndex && rIndex < ownerGrid.dataGridView.RowCount)
            {
                ++mCell.RowIndex;
            }
        }
        public void DoShift(int delimiterIndex, MathCell mCell, GridForm ownerGrid)
        {
            int cIndex = mCell.ColumnIndex;

            if (cIndex >= delimiterIndex && cIndex < ownerGrid.dataGridView.ColumnCount)
            {
                ++mCell.ColumnIndex;
            }
        }
        public void DoShift(int delimiterIndex, MathCell mCell, GridForm ownerGrid)
        {
            int rowIndex = mCell.RowIndex;

            if (rowIndex > 0 && rowIndex > delimiterIndex && rowIndex <= ownerGrid.dataGridView.RowCount)
            {
                --mCell.RowIndex;
            }
        }
Exemple #11
0
        public override object Parse(MathCell ownerMathCell)
        {
            List <MathCell> refAddressesCheckList = new List <MathCell> {
                ownerMathCell
            };

            RecursionCheck(ownerMathCell, refAddressesCheckList);

            return(base.Parse(ownerMathCell));
        }
Exemple #12
0
 public static void UpdateDependentsOnGrid(MathCell mCell, GridForm ownerGrid)
 {
     for (int i = 0; i < mCell.Dependents.Count; ++i)
     {
         MathCell dependentCell = mCell.Dependents[i];
         dependentCell.EvaluateFormula();
         PutMathCellOnGrid(dependentCell, ownerGrid);
         UpdateDependentsOnGrid(dependentCell, ownerGrid);
     }
 }
Exemple #13
0
        private static string RemoveRedundantZeroes(MathCell ownerMathCell)
        {
            string        formula = ownerMathCell.Formula;
            List <string> oldAdr  = AddressesHandler.GetAddresses(formula);

            const string pattern = @"0+(?=(0|[1-9]))";

            for (int i = 0; i < oldAdr.Count; ++i)
            {
                string updStr = Regex.Replace(oldAdr[i], pattern, "", RegexOptions.Compiled);
                formula = ownerMathCell.Formula.Replace(oldAdr[i], updStr);
            }
            return(formula);
        }
Exemple #14
0
        public static void OpenFile(GridForm grid)
        {
            if (grid.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = grid.openFileDialog.FileName;

                DataSet dataSet = InstanceDataSet(filePath);
                if (dataSet == null)
                {
                    return;
                }

                MathCellsProvider.GetInstance.ResetMathCells();
                grid.ClearGrid();
                grid.ClearOutputBoxes();

                DataTable dataTable = dataSet.Tables[0];
                grid.dataGridView.ColumnCount = dataTable.Columns.Count;
                grid.dataGridView.RowCount    = dataTable.Rows.Count;

                foreach (DataGridViewRow row in grid.dataGridView.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        MathCell newCell = new MathCell(dataTable.Rows[cell.RowIndex][cell.ColumnIndex].ToString())
                        {
                            RowIndex = cell.RowIndex, ColumnIndex = cell.ColumnIndex
                        };

                        if (!string.IsNullOrWhiteSpace(newCell.Formula))
                        {
                            MathCellsProvider.GetInstance.AddCell(newCell);
                        }

                        cell.Tag = newCell;
                    }
                }

                List <MathCell> addedCells = MathCellsProvider.GetInstance.GetUsedCells();
                OnGridMathCellsProvider.UpdateValuesOnGrid(addedCells, grid);

                // first value reevalutes because it can be calculated with non proper references
                addedCells.First().EvaluateFormula();
                OnGridMathCellsProvider.PutMathCellOnGrid(addedCells.First(), grid);

                HeadersUpdater headersUpdater = new HeadersUpdater(grid, new UpdateRows(), new UpdateColumns());
                headersUpdater.Update();
            }
        }
Exemple #15
0
        private void DataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell inputCell     = dataGridView.CurrentCell;
            MathCell         inputMathCell = MathCellsProvider.GetInstance.ProcessCellInput(inputCell);

            dataGridView.CurrentCell.Value = inputMathCell.Value;
            FormulaBox.Text = inputMathCell.Formula;

            if (MathCellsProvider.GetInstance.GetUsedCells().Contains(inputMathCell))
            {
                MathCellsProvider.GetInstance.RemoveCell(inputMathCell);
            }
            MathCellsProvider.GetInstance.AddCell(inputMathCell);
            IsGridChanged = true;
        }
Exemple #16
0
        public override object Parse(MathCell ownerMathCell)
        {
            string formula = ownerMathCell.Formula;

            if (HasAnyRefSign(formula))
            {
                if (HasInvalidRefFormat(formula))
                {
                    throw new InvalidReferenceFormat();
                }

                if (HasInvalidIndexing(formula))
                {
                    throw new InvalidReferenceIndexing();
                }
            }

            return(base.Parse(ownerMathCell));
        }
        public void Shift()
        {
            foreach (DataGridViewRow row in _ownerGrid.dataGridView.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    MathCell mCell = MathCellsProvider.GetMathCell(cell);
                    _shiftWay.DoShift(_delimiterIndex, mCell, _ownerGrid);
                }
            }

            List <MathCell> usedMathCells = MathCellsProvider.GetInstance.GetUsedCells();

            foreach (MathCell usedCell in usedMathCells)
            {
                usedCell.ShiftReferences();
            }

            OnGridMathCellsProvider.UpdateValuesOnGrid(usedMathCells, _ownerGrid);
        }
Exemple #18
0
        private static void RecursionCheck(MathCell refCell, List <MathCell> refCheckList)
        {
            List <MathCell> inspectRef = refCell.References;

            List <MathCell> recursiveCells = refCheckList.AsQueryable().Intersect(inspectRef).ToList();

            if (recursiveCells.Count != 0)
            {
                foreach (MathCell recursiveCell in recursiveCells)
                {
                    recursiveCell.RemoveFromReferences();
                }
                throw new RecursiveReference();
            }

            foreach (MathCell depCell in inspectRef)
            {
                refCheckList.Add(depCell);
                RecursionCheck(depCell, refCheckList);
            }
        }
Exemple #19
0
        public override object Parse(MathCell ownerMathCell)
        {
            // Removes spaces for proper parsing
            string formula = ownerMathCell.Formula = RemoveSpaces(ownerMathCell.Formula);

            List <string> refAddresses = AddressesHandler.GetAddresses(formula);

            if (refAddresses.Count != 0 && formula.First() != '=')
            {
                ownerMathCell.ResetReferences();
                throw new InvalidReferenceFormat();
            }

            if (string.IsNullOrEmpty(formula) || formula.First() != '=')
            {
                ownerMathCell.ResetReferences();
                return(formula);
            }

            return(base.Parse(ownerMathCell)); // Proceed
        }
Exemple #20
0
        public static void DeleteColumn(GridForm grid)
        {
            if (grid.dataGridView.ColumnCount <= 1)
            {
                return;
            }

            int colIndex = grid.dataGridView.CurrentCell.ColumnIndex;

            bool isDeletionAllowed = true;

            if (IsColumnHasValue(grid, colIndex))
            {
                isDeletionAllowed = IsDeletionAllowed("column");
            }

            if (isDeletionAllowed)
            {
                foreach (DataGridViewRow row in grid.dataGridView.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.ColumnIndex == colIndex)
                        {
                            MathCell delCell = MathCellsProvider.GetMathCell(cell);
                            delCell.UpdateDependentsBeforeDelete();
                            MathCellsProvider.GetInstance.RemoveCell(delCell);
                        }
                    }
                }

                grid.dataGridView.Columns.RemoveAt(colIndex);

                MathCellsShifter mathCellsShifter = new MathCellsShifter(new ShiftAfterColumnDeletion(), colIndex, grid);
                mathCellsShifter.Shift();

                HeadersUpdater headersUpdater = new HeadersUpdater(grid, new UpdateColumns());
                headersUpdater.Update();
            }
        }
Exemple #21
0
        public override object Parse(MathCell ownerMathCell)
        {
            string formula = ownerMathCell.DereferencedFormula;

            if (HasDivByZero(formula))
            {
                throw new DivByZero();
            }

            formula = ownerMathCell.DereferencedFormula = SurroundNegativeWithBrackets(formula);
            formula = ownerMathCell.DereferencedFormula = formula.Substring(formula.IndexOf('=') + 1); // Removes =

            Expression expr             = new Expression(formula);
            bool       isValidOperation = expr.checkSyntax(); // mXParser built-in method

            if (!isValidOperation)
            {
                throw new InvalidOperation();
            }

            return(base.Parse(ownerMathCell));
        }
Exemple #22
0
        public static void DeleteRow(GridForm grid)
        {
            if (grid.dataGridView.RowCount <= 1)
            {
                return;
            }

            int rowIndex = grid.dataGridView.CurrentCell.RowIndex;

            bool isDeletionAllowed = true;

            if (IsRowHasValue(grid, rowIndex))
            {
                isDeletionAllowed = IsDeletionAllowed("row");
            }

            if (isDeletionAllowed)
            {
                DataGridViewRow deletionRow = grid.dataGridView.Rows[rowIndex];

                foreach (DataGridViewCell cell in deletionRow.Cells)
                {
                    MathCell delCell = MathCellsProvider.GetMathCell(cell);
                    delCell.UpdateDependentsBeforeDelete();
                    MathCellsProvider.GetInstance.RemoveCell(delCell);
                }

                grid.dataGridView.Rows.RemoveAt(rowIndex);

                MathCellsShifter mathCellsShifter = new MathCellsShifter(new ShiftAfterRowDeletion(), rowIndex, grid);
                mathCellsShifter.Shift();

                HeadersUpdater headersUpdater = new HeadersUpdater(grid, new UpdateRows());
                headersUpdater.Update();
            }
        }
Exemple #23
0
 public void InsertReference(MathCell dependCell)
 {
     References.Insert(0, dependCell);
 }
Exemple #24
0
 public static void PutMathCellOnGrid(MathCell mCell, GridForm ownerGrid)
 {
     ownerGrid.dataGridView[mCell.ColumnIndex, mCell.RowIndex].Value = mCell.Value;
 }
Exemple #25
0
        private void DataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            MathCell mCell = MathCellsProvider.GetMathCell(dataGridView.CurrentCell);

            dataGridView.CurrentCell.Value = mCell.Formula;
        }
Exemple #26
0
 public virtual object Parse(MathCell ownerMathCell)
 {
     return(_nextParseAlgo?.Parse(ownerMathCell));
 }
Exemple #27
0
 public void InsertDependent(MathCell dependCell)
 {
     Dependents.Insert(0, dependCell);
 }
Exemple #28
0
        public static MathCell GetMathCell(DataGridViewCell currCell)
        {
            MathCell mCell = (MathCell)currCell.Tag;

            return(mCell);
        }
Exemple #29
0
 public void RemoveCell(MathCell mCell)
 {
     UsedCells.Remove(mCell);
 }
Exemple #30
0
 public void AddCell(MathCell mCell)
 {
     UsedCells.Add(mCell);
 }