/// <summary> /// This method sets all brackets needed in this seq. /// </summary> /// <param name="sParam">Unparsed seq.</param> /// <returns>Seq. with brackets.</returns> private string CountExpression(string sParam) { ///Here will be your code int i1, i2; bool flag = true; List<string> sParams = new List<string>(); // Use List , not string[], because we need to delete some elements and insert some new for (i1 = 0 , i2 = 0; i2 < sParam.Length; i2++) { if (sParam[i2] == '*' || sParam[i2] == '/' || sParam[i2] == '+' || sParam[i2] == '-') { sParams.Add(sParam.Substring(i1,i2-i1)); i1 = i2 + 1; sParams.Add(sParam[i2].ToString()); } } sParams.Add(sParam.Substring(i1)); Base a = new Base(); Base b = new Base(); Base c; while (flag) { flag = false; for (int i = 0; i < sParams.Count; i++) { if (sParams[i] == "*" || sParams[i] == "/") // In First we find all operations * ans / and count them { a.SetUntyped(sParams[i - 1]); b.SetUntyped(sParams[i + 1]); switch (sParams[i]) { case "*": c = a * b; break; case "/": c = a / b; break; default: c = new Base(); break; } sParams.RemoveAt(i - 1); sParams.RemoveAt(i); sParams[i - 1] = c.Get(); flag = true; break; } } } while (sParams.Count != 1) { for (int i = 0; i < sParams.Count; i++) // After we work with + and - { if (sParams[i] == "+" || sParams[i] == "-") { a.SetUntyped(sParams[i - 1]); b.SetUntyped(sParams[i + 1]); switch (sParams[i]) { case "+": c = a + b; break; case "-": c = a - b; break; default: c = new Base(); break; } sParams.RemoveAt(i - 1); sParams.RemoveAt(i); sParams[i - 1] = c.Get(); break; } } } return sParams[0]; }
/// <summary> /// Method, processing simple sequence - A <+,-,*,/> B /// </summary> /// <param name="sParam">Sequence</param> /// <param name="fFunc">Func class instance</param> /// <returns>Result of the sequence.</returns> private string processSimpleSeq(string sParam, Func fFunc) { if (sParam.IndexOf('+') > -1) { string[] sSeqParams = sParam.Split('+'); Base a = new Base(); a.SetUntyped(sSeqParams[0].Trim()); Base b = new Base(); b.SetUntyped(sSeqParams[1].Trim()); Base c = a + b; return c.Get(); } else if (sParam.IndexOf('-') > -1) { string[] sSeqParams = sParam.Split('-'); Base a = new Base(); a.SetUntyped(sSeqParams[0].Trim()); Base b = new Base(); b.SetUntyped(sSeqParams[1].Trim()); Base c = a - b; return c.Get(); } else if (sParam.IndexOf('*') > -1) { string[] sSeqParams = sParam.Split('*'); Base a = new Base(); a.SetUntyped(sSeqParams[0].Trim()); Base b = new Base(); b.SetUntyped(sSeqParams[1].Trim()); Base c = a * b; return c.Get(); } else if (sParam.IndexOf('/') > -1) { string[] sSeqParams = sParam.Split('/'); Base a = new Base(); a.SetUntyped(sSeqParams[0].Trim()); Base b = new Base(); b.SetUntyped(sSeqParams[1].Trim()); Base c = a / b; return c.Get(); } else return sParam; }
/// <summary> /// This method process some logical sequence and returns result - is it true or false. /// </summary> /// <param name="sParam">Some sequence</param> /// <param name="fFunc">Func class instance</param> /// <returns>Is this seq. true or false.</returns> public bool processLogicalSeq(string sParam, Func fFunc) { bool answer = false; string[] logicalMarks = new string[6] { "<=", ">=", "==", "!=", "<", ">"}; string[] sParams = sParam.Split(logicalMarks, StringSplitOptions.None); if (sParams.Length != 1) { string usedLogic = sParam.Substring(sParams[0].Length, sParam.Length - sParams[0].Length - sParams[1].Length).Trim(); sParams[0] = processComplicatedSeq(processVars(sParams[0].Trim(), fFunc)); sParams[1] = processComplicatedSeq(processVars(sParams[1].Trim(), fFunc)); if (!bStartBreaking) { Base a = new Base(); a.SetUntyped(sParams[0]); Base b = new Base(); b.SetUntyped(sParams[1]); switch (usedLogic) { case "<": answer = (a < b); break; case ">": answer = (a > b); break; case "<=": answer = (a <= b); break; case ">=": answer = (a >= b); break; case "!=": answer = (a != b); break; case "==": answer = (a == b); break; } } else switch (sParams[0][0]) { case '@': try { answer = Convert.ToBoolean(fFunc.getVar(sParams[0].Trim())); } catch { answer = false; } break; case '&': try { answer = Convert.ToBoolean(fFunc.getGlVar(sParams[0].Trim())); } catch { answer = false; } break; default: break; } } return answer; }