예제 #1
0
 /// <param name="l"> Left side. </param>
 /// <param name="r"> Right side. </param>
 public Equation(Formula[] l, Formula[] r)
 {
     left = new Formula[l.Length];
     leftCoeff = new int[l.Length];
     right = new Formula[r.Length];
     rightCoeff = new int[r.Length];
     //copies the formulas instead of referencing
     //(reduces side-effects of using Equation class)
     for(int i = 0; i < l.Length; i++) {
         if(l[i] == null || l[i].Size == 0) {
             throw new ArgumentException("Invalid formula in equation.");
         }
         left[i] = new Formula(l[i]);
         leftCoeff[i] = 1;
     }
     for(int i = 0; i < r.Length; i++) {
         if(r[i] == null || r[i].Size == 0) {
             throw new ArgumentException("Invalid formula in equation.");
         }
         right[i] = new Formula(r[i]);
         rightCoeff[i] = 1;
     }
 }
예제 #2
0
 //gets a Formula starting from next
 //hydrates arent handled
 private Formula GetFormula()
 {
     Formula formula = new Formula();
     while(BoundCheck) {
         //end inner formula, return this
         if(chars[next] == ')') {
             Next();
             return formula;
         }
         //start inner formula
         else if(chars[next] == '(') {
             Next();
             //gets formula
             Formula f1 = GetFormula();
             //gets the number after the closing parens
             //i.e. get 7 from (CO2)7
             if(f1 == null ) return null;
             int n = GetNumber();
             //adds it to the main formula
             formula.MergeFormula(f1.Factor(n));
         }
         //start symbol
         else if (char.IsUpper(chars[next])){
             Element e = GetSymbol();
             if(e == null) {
                 continue;
             }
             int n = GetNumber();
             formula.Add(new NElements(n, e));
             //error or end of data
         } else {
             return null;
         }
     }
     return formula;
 }
예제 #3
0
 /// <summary> Copy constructor. </summary>
 /// <param name="f"> Formula to copy. </param>
 public Formula(Formula f)
 {
     MergeFormula(f);
 }
예제 #4
0
 /// <summary> Merges this formula with other.
 /// Result is this object; other isn't affected. </summary>
 /// <example> H2O merged with H2 will result in H4O </example>
 public void MergeFormula(Formula other)
 {
     foreach(NElements ne in other.elementMap.Values) {
         Add(new NElements(ne));
     }
 }
예제 #5
0
 //returns an equivalent formula that takes into account all
 //formulas and coefficients
 //i.e. [O2,2H2] results in H4O2
 private static Formula CountMolecules(Formula[] formulas, int[] coeff)
 {
     Formula f = new Formula();
     for(int i = 0; i < formulas.Length; i++) {
         //creates new equivalent formula without coefficient
         Formula f2 = new Formula(formulas[i]);
         //merges the formula into f
         f.MergeFormula(f2.Factor(coeff[i]));
     }
     return f;
 }