本文整理汇总了C#中SpreadsheetUtilities.Formula.GetVariables方法的典型用法代码示例。如果您正苦于以下问题:C# Formula.GetVariables方法的具体用法?C# Formula.GetVariables怎么用?C# Formula.GetVariables使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpreadsheetUtilities.Formula
的用法示例。
在下文中一共展示了Formula.GetVariables方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
String lpPattern = @"\(";
String rpPattern = @"\)";
String opPattern = @"[\+\-*/]";
String varPattern = @"[a-zA-Z_](?: [a-zA-Z_]|\d)*";
//String doublePattern = @"(?: \d+\.\d* | \d*\.\d+ | \d+ ) (?: [eE][\+-]?\d+)?";
String doublePattern = @"^[0-9\.]*$";
String spacePattern = @"\s+";
// Overall pattern
String pattern = String.Format("({0}) | ({1}) | ({2}) | ({3}) | ({4}) | ({5})",
lpPattern, rpPattern, opPattern, varPattern, doublePattern, spacePattern);
Formula f = new Formula("2.0 + x7 + b6+9- c");
String s = f.GetVariables().ToList().ToString();
Debug.WriteLine(s);
}
示例2: Test53
public void Test53()
{
Formula f = new Formula("2+X1", s => "" + s + "_", s => true);
HashSet<string> variables = new HashSet<string> { "X1_" };
Assert.IsTrue(variables.SetEquals(f.GetVariables()));
}
示例3: NormalizeFormula
/// <summary>
/// Returns the formula with all the variables replaced with their normalized counterpart.
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
protected Formula NormalizeFormula(Formula f)
{
IEnumerable<string> e = f.GetVariables();
foreach (string s in e)
{
f = new Formula(Regex.Replace(f.ToString(), s, Normalize(s)));
}
return f;
}
示例4: SetCellContents
/// <summary>
/// Takes in a cell name and a formula and returns the dependents
/// Calculates the value of the formula then recalculates the value of any cell that depends directly or inderectly on it.
/// </summary>
/// <param name="name"></param>
/// <param name="formula"></param>
/// <returns></returns>
protected override ISet<string> SetCellContents(string name, Formula formula)
{
name = Normalize(name);
HashSet<string> mySet = new HashSet<string>();
HashSet<string> replaceSet = new HashSet<string>();
if (Object.ReferenceEquals(formula, null))
{
throw new ArgumentNullException();
}
if (Object.ReferenceEquals(name, null))
{
throw new InvalidNameException();
}
if (!Regex.IsMatch(name, "^([a-z]|[A-Z])+\\d+$") | !IsValid(name))
{
throw new InvalidNameException();
}
try
{
foreach (string s in formula.GetVariables())
{
replaceSet.Add(s);
}
// dependentCells.ReplaceDependees(name, mySet);
dependentCells.ReplaceDependees(name, replaceSet);
GetCellsToRecalculate(name);
}
catch (CircularException)
{
throw new CircularException();
}
Formula myEvl = new Formula(formula.ToString(), IsValid, Normalize);
myCell = new Cell(formula, myEvl.Evaluate(myLookup));
if (mySpreadsheet.ContainsKey(name))
{
mySpreadsheet[name] = myCell;
}
else
{
mySpreadsheet.Add(name, myCell);
}
mySet.Add(name);
foreach (string s in GetCellsToRecalculate(name))
{
mySet.Add(s);
}
foreach (string s in mySet)
{
myCell = new Cell(GetCellContents(s), myEvaluate(s));
mySpreadsheet[s] = myCell;
}
Changed = true;
return mySet;
}
示例5: Test49
public void Test49()
{
Formula f = new Formula("2*X2+X2");
List<string> actual = new List<string>(f.GetVariables());
HashSet<string> expected = new HashSet<string>() { "X2" };
Assert.AreEqual(actual.Count, 1);
Assert.IsTrue(expected.SetEquals(actual));
}
示例6: GetVariables1
public void GetVariables1()
{
Formula testFormula = new Formula("(3+x)*y");
foreach (string s in testFormula.GetVariables())
Assert.IsTrue(Regex.IsMatch(s, @"[xy]"));
}
示例7: GetVariables3
public void GetVariables3()
{
Formula testFormula = new Formula("(3+x)*y", s => s.ToUpper(), s => (Regex.IsMatch(s, @"[A-Z]")));
foreach (string s in testFormula.GetVariables())
Assert.IsTrue(Regex.IsMatch(s, @"[XY]"));
}
示例8: SetCellContents
/// <summary>
/// If name is null or invalid, throws an InvalidNameException.
///
/// Otherwise, the contents of the named cell becomes number. The method returns a
/// set consisting of name plus the names of all other cells whose value depends,
/// directly or indirectly, on the named cell.
///
/// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the
/// set {A1, B1, C1} is returned.
/// </summary>
public override ISet<string> SetCellContents(string name, double number)
{
if (!Cell.validName(name))
{
throw new InvalidNameException();
}
HashSet<string> dependents = new HashSet<string>();
List<string> dependentCells = GetCellsToRecalculate(name).ToList();
// If cell doesn't exist, add it
if (!allCells.ContainsKey(name))
{
allCells.Add(name, new Cell(name, number));
foreach (string s in dependentCells)
{
dependents.Add(s);
}
dependents.Add(name);
return dependents;
}
// If cell exists, overwrite it
Cell temp;
if (allCells.TryGetValue(name, out temp))
{
string content = temp.getContents().ToString();
temp.setContents(number);
// remove dependencies, if they exist
Formula f = new Formula(content);
foreach (string s in f.GetVariables())
{
if (s.Equals(name))
{
graph.RemoveDependency(name, s);
}
}
}
// Get all dependents, indirect and direct, and then add them to a List which is then
// added to the returned HashSet
dependents.Add(name);
foreach (string s in dependentCells)
{
dependents.Add(s);
}
return dependents;
}
示例9: Change
/// <summary>
/// Verifies that no circular exception will be created and then sends CHANGE request to server.
/// </summary>
/// <param name="name">The cell to be changed</param>
/// <param name="content">The candidate contents of the cell</param>
public void Change(string name, string content)
{
// If we're waiting for a change to be confirmed by the server, don't take a new change.
if (_currentChangeCell != null)
{
return;
}
var normalizedName = Normalize(name);
// Check if content is null.
if (content == null)
{
throw new ArgumentNullException();
}
// Check if name is null or invalid.
if (normalizedName == null || !_validCellNameRegex.IsMatch(normalizedName) || !IsValid(normalizedName))
{
throw new InvalidNameException();
}
if (content.StartsWith("="))
{
Formula formula = new Formula(content.Substring(1), IsValid, Normalize);
// The HashSet dependents contains name and all of name's direct and indirect dependents.
var dependents = new HashSet<string>(GetCellsToRecalculate(normalizedName));
// Variables contains name's new dependees.
var variables = formula.GetVariables();
// Checking if any of name's new dependees are already its dependents
// or if name is its own new dependee.
if (dependents.Overlaps(variables))
{
throw new CircularException();
}
}
_currentChangeCell = normalizedName;
_currentChangeContent = content;
_socket.BeginSend("CHANGE\n" + _name + "\n" + _version + "\nCell:" + normalizedName + "\nLength:" + content.Length.ToString() + "\n" + content + "\n", (e, o) => { }, null);
}
示例10: SetContentsOfCell
/// <summary>
/// Sets the contents of the cell. Determines whether the
/// contents are a string, double, or formula and sets each
/// appropriately
/// </summary>
/// <param name="name">Name of cell whose contents are to be set</param>
/// <param name="content"></param>
/// <returns></returns>
public override ISet<string> SetContentsOfCell(string name, string content)
{
if (content == null)
throw new ArgumentNullException();
if (name == null || !IsValid(name))
throw new InvalidNameException();
name = removeNewLines(name);
content = removeNewLines(content);
name = Normalize(name);
if(IsValid(name))
{
double doubleValue = 0.0;
bool isDouble = double.TryParse(content, out doubleValue);
HashSet<string> cellsToRecalc;
if (isDouble)
cellsToRecalc = (HashSet<string>)SetCellContents(name, doubleValue);
else if (Regex.IsMatch(content, @"[\+\-\*\/=]"))
{
char[] splitFormula = content.ToCharArray();
string newFormula = "";
for (int i = 0; i < splitFormula.Length; i++)
{
if (splitFormula[i] != '=')
newFormula += splitFormula[i];
}
newFormula = Normalize(newFormula);
Formula f = new Formula(newFormula);
foreach (string s in f.GetVariables())
{
if (!IsValid(s))
throw new FormulaFormatException("One or more variables is not valid");
}
cellsToRecalc = (HashSet<string>)SetCellContents(name, f);
}
else
cellsToRecalc = (HashSet<string>)SetCellContents(name, content);
return cellsToRecalc;
}
else
throw new InvalidNameException();
}
示例11: SetCellContents
/// <summary>
/// sets the cell, name, to contain a specified formula
/// </summary>
/// <remarks>
/// If the formula parameter is null, throws an ArgumentNullException.
///
/// Otherwise, if name is null or invalid, throws an InvalidNameException.
///
/// Otherwise, if changing the contents of the named cell to be the formula would cause a
/// circular dependency, throws a CircularException. (No change is made to the spreadsheet.)
///
/// Otherwise, the contents of the named cell becomes formula. The method returns a
/// Set consisting of name plus the names of all other cells whose value depends,
/// directly or indirectly, on the named cell.
///
/// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the
/// set {A1, B1, C1} is returned.
/// circ dependency example:
/// when setting b1 to a1
/// a1 = b1 DG has pair (b1,a1)
/// b1 = a1 DG can't be allowed to add(a1,b1)
/// </remarks>
/// <exception cref="ArgumentNullException"> If the formula parameter is null, throws an ArgumentNullException.</exception>
/// <exception cref="InvalidNameException"> Otherwise, if name is null or invalid, throws an InvalidNameException.</exception>
/// <exception cref="CircularException">
/// Otherwise, if changing the contents of the named cell to be the formula would cause a
/// circular dependency, throws a CircularException. (No change is made to the spreadsheet.)
/// </exception>
/// <returns>
/// The method returns a
/// Set consisting of name plus the names of all other cells whose value depends,
/// directly or indirectly, on the named cell.
/// </returns>
protected override ISet<string> SetCellContents(string name, Formula formula)
{
//If the formula parameter is null, throws an ArgumentNullExceptio
if (formula == null)
{
throw new ArgumentNullException();
}
//If name is null or invalid, throws an InvalidNameException
if (name == null || !Regex.IsMatch(name, @"[a-zA-Z]+\d+"))
{
throw new InvalidNameException();
}
IEnumerable<string> storedDents = DG.GetDependents(name);
DG.ReplaceDependents(name, new HashSet<string>());
foreach (string var in formula.GetVariables())
{
try
{
DG.AddDependency(name, var);
}
catch (InvalidOperationException)
{
DG.ReplaceDependents(name, storedDents);
throw new CircularException();
}
}
if (Sheet.ContainsKey(name))
Sheet[name].Contents = formula;
else
Sheet.Add(name, new Cell(name, formula,lookerupper));
foreach (string nombre in GetCellsToRecalculate(name))
{
//contents setter recalculates value.
Sheet[nombre].Contents = Sheet[nombre].Contents;
}
Changed = true;
HashSet<string> toreturn = new HashSet<string>(GetCellsToRecalculate(name));
toreturn.Add(name);
return toreturn;
}
示例12: SetCellContents
/// <summary>
/// If the formula parameter is null, throws an ArgumentNullException.
///
/// Otherwise, if name is null or invalid, throws an InvalidNameException.
///
/// Otherwise, if changing the contents of the named cell to be the formula would cause a
/// circular dependency, throws a CircularException. (No change is made to the spreadsheet.)
///
/// Otherwise, the contents of the named cell becomes formula. The method returns a
/// Set consisting of name plus the names of all other cells whose value depends,
/// directly or indirectly, on the named cell.
///
/// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the
/// set {A1, B1, C1} is returned.
/// </summary>
protected override ISet<string> SetCellContents(string name, Formula formula)
{
//check if name is valid
IsValidInput(name, formula);
//Save state of old dependency graph
SSCell oldCell = null;
IEnumerable<string> oldDependees = new HashSet<string>();
if (nameContentDict.TryGetValue(name, out oldCell))
{
oldDependees = depenGraph.GetDependees(oldCell.Name);
}
depenGraph.ReplaceDependees(name, formula.GetVariables());
//check for circular dependiencies
try
{
//check for circular dependeinces
ISet<string> returnSet = GetAllDepdendents(name);
// if we get here no circular dependency was found
AddToGraphAndDict(name, formula);
//reevaluate all dependents
foreach (string cellName in returnSet)
{
nameContentDict[cellName].ReEvaluate(Lookup);
}
return returnSet;
}
catch (CircularException)
{
//restore dependecy graph
depenGraph.ReplaceDependees(name, oldDependees);
throw new CircularException();
}
}
示例13: SetContentsOfCell
// ADDED FOR PS5
/// <summary>
/// If content is null, throws an ArgumentNullException.
///
/// Otherwise, if name is null or invalid, throws an InvalidNameException.
///
/// Otherwise, if content parses as a double, the contents of the named
/// cell becomes that double.
///
/// Otherwise, if content begins with the character '=', an attempt is made
/// to parse the remainder of content into a Formula f using the Formula
/// constructor. There are then three possibilities:
///
/// (1) If the remainder of content cannot be parsed into a Formula, a
/// SpreadsheetUtilities.FormulaFormatException is thrown.
///
/// (2) Otherwise, if changing the contents of the named cell to be f
/// would cause a circular dependency, a CircularException is thrown.
///
/// (3) Otherwise, the contents of the named cell becomes f.
///
/// Otherwise, the contents of the named cell becomes content.
///
/// If an exception is not thrown, the method returns a set consisting of
/// name plus the names of all other cells whose value depends, directly
/// or indirectly, on the named cell.
///
/// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the
/// set {A1, B1, C1} is returned.
/// </summary>
public override ISet<String> SetContentsOfCell(String name, String content)
{
//spreadsheet has been changed
Changed = true;
//check input and Normalize name
string normalizedName = IsValidInput(name, content);
if (content.Equals(""))
{
//if an empty string is recieved the graph must be updated
if (nameContentDict.ContainsKey(name))
{
//any cell that dependends on this is know a formula error
foreach(string cell in GetAllDepdendents(name))
{
nameContentDict[cell].Value = new FormulaError();
}
depenGraph.RemoveDependency(name, content);
nameContentDict.Remove(name);
}
return new HashSet<string>();
}
//see if content is a double, if so get value return
double d;
if (double.TryParse(content, out d))
{
//add to dependen graph and name, cell to the Dcitonary, return dependents
return SetCellContents(normalizedName, d);
}
//if is it a formula
else if (content[0] == '=')
{
//get the rest of the string after the "="//double check off by one error here
string formulaString = content.Substring(1, content.Length - 1);
Formula formula;
try
{
formula = new Formula(formulaString, Normalize, IsValid);
//PS3 has different requirments for variables so check new standards here
foreach( string str in formula.GetVariables())
IsValidName(str);
//add to dependen graph and name, cell to the Dcitonary, return dependents
return SetCellContents(normalizedName, formula);
}
//catch the invalid name exception
catch (InvalidNameException) { throw new FormulaFormatException("bad formula"); }
}
else
//it is a string, add to dependen graph and name, cell to the Dcitonary, return dependents
return SetCellContents(normalizedName, content);
}
示例14: PublicTestGetVariables2
public void PublicTestGetVariables2()
{
List<string> expected = new List<string>();
expected.Add("X");
expected.Add("Y");
Formula f1 = new Formula("X + x + Y", s => s.ToUpper(), s => true);
List<string> actual = new List<string>(f1.GetVariables());
CollectionAssert.AreEqual(expected, actual);
}
示例15: GetDirectDependents
/// <summary>
/// If name is null, throws an ArgumentNullException.
///
/// Otherwise, if name isn't a valid cell name, throws an InvalidNameException.
///
/// Otherwise, returns an enumeration, without duplicates, of the names of all cells whose
/// values depend directly on the value of the named cell. In other words, returns
/// an enumeration, without duplicates, of the names of all cells that contain
/// formulas containing name.
///
/// For example, suppose that
/// A1 contains 3
/// B1 contains the formula A1 * A1
/// C1 contains the formula B1 + A1
/// D1 contains the formula B1 - C1
/// The direct dependents of A1 are B1 and C1
/// </summary>
protected override IEnumerable<string> GetDirectDependents(string name)
{
if (name == null)
{
throw new ArgumentNullException();
}
if (!Cell.validName(name))
{
throw new InvalidNameException();
}
// Loop over every cell in spreadsheet, checking for dependents of name by looking for
// cells that contain name
HashSet<string> dependents = new HashSet<string>();
foreach (KeyValuePair<string, Cell> kvp in allCells)
{
// Turn name into a string, convert to a formula and call get variables on the formula
// if there are variables in the formula (the contents of the cell) and one of those
// variables is name, add the name of the cell that contains name to the dependents list
if (kvp.Value.getContents() is Formula)
{
string content = kvp.Value.getContents().ToString();
Formula f = new Formula(content, Normalize, IsValid);
foreach (string str in f.GetVariables())
{
if (str.Equals(name))
{
dependents.Add(kvp.Key);
}
}
}
}
return dependents;
}