Example #1
0
 public MathEquation(string line, HomeworkType type = HomeworkType.Simple)
 {
     //< Retain the source equation and the HomeWorktype (simple -> no precedence / advanced -> addition first)
     this.Source = line;
     this.Type   = type;
     //< Simplify the equation based on the specified HomeworkType
     this.Simplified = Simplify(Source, Type);
     //< Get the result based on the specific HomeworkType
     this.Result = (Type == HomeworkType.Simple) ? SimpleSolve(Simplified) : PrecedenceSolve(Simplified);
 }
Example #2
0
        public static string Simplify(string line, HomeworkType type)
        {
            var simplified = new StringBuilder();
            var openIdx    = new List <int>();

            foreach (char c in line)
            {
                switch (c)
                {
                case '(':
                    //< Add the leading bracket to 'simplified' string
                    simplified.Append(c);
                    //< Add the index of the bracket to the index list
                    openIdx.Add(simplified.Length);
                    break;

                case ')':
                    //< Get the index of the last open bracket
                    int lastIdx = openIdx.Last();
                    //< Get the current sub-equation to solve (substring starting at last open bracket's index)
                    var eqn = simplified.ToString().Substring(lastIdx);
                    //< Get the result of that equation
                    var result = (type == HomeworkType.Simple) ? SimpleSolve(eqn) : PrecedenceSolve(eqn);
                    //< Replace the current un-solved sub-equation with its result
                    simplified.Remove(lastIdx - 1, simplified.Length - (lastIdx - 1));
                    simplified.Append(result);
                    //< Remove the index to the 'solved' open bracket (the last one)
                    openIdx.RemoveAt(openIdx.Count - 1);
                    break;

                default:
                    simplified.Append(c);               //< Not a parentheses, simply add to the 'simplified' string
                    break;
                }
            }

            return(simplified.ToString());
        }
Example #3
0
 public MathHomework(IEnumerable <string> input, HomeworkType type = HomeworkType.Simple)
 {
     this.Equations = input.Select(x => new MathEquation(x, type)).ToList();
 }