/************************************************************* * Function: Eval(Cell cell) * Date Created: Feb 8, 2017 * Date Last Modified: Feb 9, 2017 * Description: eval * Return: void *************************************************************/ private void Eval(Cell cell) { createACell m_Cell = cell as createACell; if (string.IsNullOrEmpty(m_Cell.Text)) { m_Cell.SetValue(""); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } else if (m_Cell.Text[0] == '=' && m_Cell.Text.Length >= 3) { string letter = m_Cell.Text.Substring(1); string number = letter.Substring(1); string lettersAZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int col = 0, row = int.Parse(number) - 1; for (int i = 0; i < 26; i++) { if (lettersAZ[i] == letter[0]) { col = i; break; } } m_Cell.SetValue(GetCell(row, col).Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } else { m_Cell.SetValue(m_Cell.Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } }
/************************************************************* * Function: setExp(createACell m_Cell, Cell cell) * Date Created: Feb 8, 2017 * Date Last Modified: Feb 9, 2017 * Description: set value to exp * Return: void *************************************************************/ private void setExp(createACell m_Cell, Cell cell) { ExpTree exptree = new ExpTree(m_Cell.Text.Substring(1)); string[] variables = exptree.GetAllVariables(); foreach (string variableName in variables) { Cell variableCell = GetCell(variableName); double value; if (string.IsNullOrEmpty(variableCell.Value)) { exptree.SetVar(variableCell.Name, 0); } else if (!double.TryParse(variableCell.Value, out value)) { exptree.SetVar(variableName, 0); } else { exptree.SetVar(variableName, value); } } m_Cell.SetValue(exptree.Eval().ToString()); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); #region hw4code //old code from hw4 //string letter = m_Cell.Text.Substring(1); //string number = letter.Substring(1); //string lettersAZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //int col = 0, row = int.Parse(number) - 1; //for (int i = 0; i < 26; i++) //{ // if (lettersAZ[i] == letter[0]) // { // col = i; // break; // } //} //m_Cell.SetValue(GetCell(row, col).Value.ToString()); #endregion }
/************************************************************* * Function: setExp(createACell m_Cell, Cell cell) * Date Created: Feb 8, 2017 * Date Last Modified: Feb 9, 2017 * Description: set value to exp * Return: void *************************************************************/ private void setText(createACell m_Cell, Cell cell) { m_Cell.SetValue(m_Cell.Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); }
/************************************************************* * Function: setEmpty(createACell m_Cell, Cell cell) * Date Created: Feb 8, 2017 * Date Last Modified: Feb 9, 2017 * Description: set value to empty * Return: void *************************************************************/ private void setEmpty(createACell m_Cell, Cell cell) { m_Cell.SetValue(""); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); }
/************************************************************* * Function: setExp(createACell m_Cell, Cell cell) * Date Created: Feb 8, 2017 * Date Last Modified: Feb 9, 2017 * Description: set value to exp * Return: void *************************************************************/ private void setExp(createACell m_Cell, Cell cell, ref bool flag) { ExpTree exptree = new ExpTree(m_Cell.Text.Substring(1)); string[] variables = exptree.GetAllVariables(); foreach (string variableName in variables) { // bad reference if (GetCell(variableName) == null) { m_Cell.SetValue("!(Bad Reference)"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); flag = true; break; } //SetExpressionVariable Cell variableCell = GetCell(variableName); double value; if (string.IsNullOrEmpty(variableCell.Value)) { exptree.SetVar(variableCell.Name, 0); } else if (!double.TryParse(variableCell.Value, out value)) { exptree.SetVar(variableName, 0); } else { exptree.SetVar(variableName, value); } //SetExpressionVariable end; //self reference if (variableName == m_Cell.Name) { m_Cell.SetValue("!(Self Reference)"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); flag = true; break; } } if (flag) { return; } //Circular reference foreach (string varName in variables) { if (varName == m_Cell.Name) // directly same { m_Cell.SetValue("!(Circular Reference)"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); flag = true; break; } if (!depDict.ContainsKey(m_Cell.Name)) //check in dict { continue; } string currCell = m_Cell.Name; for (int i = 0; i < depDict.Count; i++) //find all cell in dict { foreach (string dependentCell in depDict[currCell]) //do same thing as before { if (varName == dependentCell) { m_Cell.SetValue("!(Circular Reference)"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); flag = true; break; } if (!depDict.ContainsKey(dependentCell)) { continue; } currCell = dependentCell; } } } if (flag) { return; } m_Cell.SetValue(exptree.Eval().ToString()); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); #region hw4code //old code from hw4 //string letter = m_Cell.Text.Substring(1); //string number = letter.Substring(1); //string lettersAZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //int col = 0, row = int.Parse(number) - 1; //for (int i = 0; i < 26; i++) //{ // if (lettersAZ[i] == letter[0]) // { // col = i; // break; // } //} //m_Cell.SetValue(GetCell(row, col).Value.ToString()); #endregion }