static void Main(string[] args) { try { //IExpression exp = new DivExp(new ConstExp(0), MonomialExp.createInstance(3, 0)); IExpression exp = new DivExp(new MulExp(new ConstExp(10), MonomialExp.createInstance(3, 0)), new AddExp(new MulExp(MonomialExp.createInstance(5, 2), new ConstExp(4)), MonomialExp.createInstance(3, 2))); //IExpression exp = new DivExp(MonomialExp.createInstance(3, 2), new ConstExp(-2)); double eValue = exp.evaluate(3); Console.WriteLine("Value:"); Console.WriteLine("The value of expression when x = 3 : {0}", eValue); // --------------- Console.WriteLine("\nNotations:"); INotation infixNtn = new InfixNotation(); INotation prefixNtn = new PrefixNotation(); INotation posfixNtn = new PosfixNotation(); Console.WriteLine("Infix Notation: {0}", exp.toString(infixNtn)); Console.WriteLine("Prefix Notation: {0}", exp.toString(prefixNtn)); Console.WriteLine("Posfix Notation: {0}", exp.toString(posfixNtn)); // --------------- Console.WriteLine("\nDerivative with notations:"); Console.WriteLine("Infix Notation: {0}", exp.derive().toString(infixNtn)); Console.WriteLine("Prefix Notation: {0}", exp.derive().toString(prefixNtn)); Console.WriteLine("Posfix Notation: {0}", exp.derive().toString(posfixNtn)); } catch (Exception e) { Console.WriteLine("Error : {0}", e.Message); } }
/* * List<Expression> GetListExpression(string exp) * { * //string[] listOpe = new string[] { "+", "-", "*", "/", "(", ")" }; * string listOpe = "+-/*()"; * List<Expression> listExp = new List<Expression>(); * List<Expression> subListExp = new List<Expression>(); * bool priortize = false; * * for(int i=0; i<exp.Length; i++) * { * if(exp[i] == '(') * { * priortize = true; * } * else if(exp[i] == ')') * { * priortize = false; * } * * if (i == exp.Length - 1) * { * if(exp[i] == 'x') * { * Expression monoExp = MonomialExp.CreateInstance(1, 1); * } * else * { * if (exp[i] != 'x' && !listOpe.Contains(exp[i])) * { * Expression constExp = new ConstExp(double.Parse(exp[i].ToString())); * } * } * } * else * { * if (listOpe.Contains(exp[i]) && !listOpe.Contains(exp[i + 1])) * { * string temp = ""; * for (int j = i + 1; j < exp.Length; j++) * { * if (listOpe.Contains(exp[j])) * { * temp = exp.Substring(i + 1, j - (i + 1)); * i = j - 1; * break; * } * } * if (temp.Contains('x')) * { * double a = double.Parse(temp.Substring(0, temp.IndexOf('x'))); * double n = 1; * if (temp.IndexOf('x') != temp.Length - 1) * { * n = double.Parse(temp.Substring(temp.IndexOf('x') + 1, temp.Length - (temp.IndexOf('x') + 1))); * } * Expression monoExp = MonomialExp.CreateInstance(a, n); * * if(priortize == true) * { * listExp.Add(monoExp); * } * else * { * subListExp.Add(monoExp); * } * } * else * { * Expression constExp = new ConstExp(double.Parse(temp)); * listExp.Add(constExp); * * if (priortize == true) * { * listExp.Add(constExp); * } * else * { * subListExp.Add(constExp); * } * } * } * } * } * * for(int m=0; m<subListExp.Count; m++) * { * listExp.Add(subListExp[m]); * } * * return listExp; * } * * List<string> GetListOperator(string exp) * { * string listOpe2 = "+-/*"; * List<string> ope = new List<string>(); * List<string> subOpe = new List<string>(); * bool priortize = false; * * for (int i = 0; i < exp.Length; i++) * { * if (exp[i] == '(') * { * priortize = true; * } * else if (exp[i] == ')') * { * priortize = false; * } * * if (priortize == true && listOpe2.Contains(exp[i])) * { * ope.Add(exp[i].ToString()); * } * else if (priortize == false) * { * if (exp[i] == '*' || exp[i] == '/') * { * subOpe.Add(exp[i].ToString()); * } * else if (exp[i] == '+' || exp[i] == '-') * { * subOpe.Add(exp[i].ToString()); * } * } * } * * for (int k = 0; k < subOpe.Count; k++) * { * ope.Add(subOpe[k]); * } * * return ope; * } * * List<Expression> ShortenExpression(List<Expression> exps, string exp) * { * List<string> listOperator = GetListOperator(exp); * List<Expression> listExp = exps; * * //for() * while(listExp.Count > 2) * { * * } * * return listExp; * } * * * private void Button1_MouseClick(object sender, MouseEventArgs e) * { * List<Expression> listExp = GetListExpression(ConstExpressionTextBox.Text); * List<string> listOpe = GetListOperator(ConstExpressionTextBox.Text); * * for(int i=0; i<listExp.Count; i++) * { * Notation ntn = null; * ListExpresstionTextBox.AppendText(listExp[i].toString(ntn) + "\n"); * } * * for(int j=0; j<listOpe.Count; j++) * { * ListExpresstionTextBox.AppendText(listOpe[j] + "\n"); * } * } */ Expression GetMonoExpression(string exp) { Expression ex; if (exp.Contains('x')) { double a = double.Parse(exp.Substring(0, exp.IndexOf('x'))); double n = 1; if (exp.IndexOf('x') != exp.Length - 1) { n = double.Parse(exp.Substring(exp.IndexOf('x') + 1, exp.Length - (exp.IndexOf('x') + 1))); } ex = MonomialExp.CreateInstance(a, n); } else { ex = new ConstExp(double.Parse(exp)); } return(ex); }
public IExpression derive() { return(MonomialExp.createInstance(this.a * this.n, this.n - 1)); }