/// <summary> /// Return result of exact fraction from queue of postfix. /// </summary> /// <param name="postFix"></param> /// <returns></returns> public static ProperFraction evaluatePostFix(Queue <Object> postFix) { if (postFix == null) { return(null); } Queue <Object> Fractionlist = new Queue <Object>(); //Cast all int64 into properFraction. foreach (Object element in postFix) { if (element is Int64) { Fractionlist.Enqueue(new ProperFraction((Int64)element)); } else { Fractionlist.Enqueue(element); } } Stack <ProperFraction> nstack = new Stack <ProperFraction>(); // while it's more than one element in the queue. while (Fractionlist.Count != 0) { Object obj = Fractionlist.Dequeue(); //Retrieve oldest element. if (obj is ProperFraction) //If it's a properfraction { nstack.Push((ProperFraction)obj); //add to the stack. } else//else it must be operator { if (!(obj is char) || nstack.Count == 0) { throw new Exception("Please Debug when this error happend."); } //pop two numbers from the statck and evaluate. ProperFraction secondnumber = nstack.Pop(); ProperFraction firstnumber = null; if (nstack.Count != 0) { firstnumber = nstack.Pop(); } //if cannot get the second one and there is only one element, then it must be unary operator. // add the result to the stack. nstack.Push(compute(firstnumber, secondnumber, (char)obj)); } } return(nstack.Pop()); }
public static ProperFraction operator -(ProperFraction a) { if (a == null) { throw new Exception(); } ProperFraction pf = new ProperFraction(!a.sign); pf.decimalpart = new ImproperFraction(a.decimalpart.getNumerator(), a.decimalpart.getDenominator()); pf.integerpart = a.integerpart; return(pf); }
/// <summary> /// method is tested. /// <para> /// This method takes in two number and an operator and return the result. /// </para> /// If there is only one number is not null, this method will assume the operator is '-' or '+'; /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns> /// Null is the computation cannot be done or error has occured. /// </returns> public static ProperFraction compute(ProperFraction a, ProperFraction b, char opt) { // the operator is valide: if (getOptRank(opt) == -1) { return(null); } // If there is one number at least: if (a == null && b == null) { return(null); } //At this point, the operator will be valide and there will be at least one numebr. if (a != null && b != null)//Both bumber is not null { switch (opt) { case '+': return(a + b); case '-': return(a - b); case '*': return(a * b); case '/': return(a / b); } throw new Exception("Something wrong, please check consistency on logic."); } //One of the number is null.s if (opt == '-') { return(a == null ? -b:-a); } if (opt == '+') { return(a == null ? b : a); } return(null); }
public static ProperFraction evalueteStacks(Stack <ProperFraction> numberstack, Stack <char> optstack) { //make sure the input is both not null: if (numberstack == null || optstack == null) { return(null); } //If there is no more operator in the stack, that means there is only one number in the number stack, and it's if (optstack.Count == 0)// it's the base case: { //check if there is really one number in the stack: if (numberstack.Count != 1) { throw new Exception("Something shouldn't happen hapepened."); } return(numberstack.Pop()); } //Comfirm that there is a Operator in the stack. char opt = optstack.Pop(); ProperFraction secondnumber = numberstack.Pop(); ProperFraction firstnumber = null; if (numberstack.Count != 0) { firstnumber = numberstack.Pop(); } ProperFraction computedresult = compute(firstnumber, secondnumber, opt); if (computedresult == null) { return(null); //This is hit if the number and the operator cannot be computed. } numberstack.Push(computedresult); return(evalueteStacks(numberstack, optstack)); }
public static ProperFraction evaluateSimple(IList <Object> expression) { Stack <ProperFraction> numstack = new Stack <ProperFraction>(); Stack <char> opstack = new Stack <char>(); //Queue q = new Queue(); //iterate through a queue of number, operator,or a sub expression. foreach (Object ele in expression) { //if is number if (ele is ProperFraction) { //put into numberstack. numstack.Push(ele as ProperFraction); } // elseif operator else if (ele is char) { if (opstack.Count == 0)// if operator stack is empty, put it in. { opstack.Push((char)ele); } else // else that stack is not empty. { char opt = (char)ele; if (compare(opt, opstack.Peek()) == 1)//if it's bigger to the most recent opt in stack { // put it in op stack. opstack.Push(opt); } else //else: it's leq to the top opt. { while (compare(opt, opstack.Peek()) < 1 || opstack.Count != 0)//while current op leq than top one on stack || operator stack is not empty. { // pop top char topoperator = opstack.Pop(); // pop two numbers if there is no more number on top, use zero ProperFraction secondnumber = numstack.Pop(); ProperFraction firstnumber = null; if (numstack.Count != 0) { firstnumber = numstack.Pop(); } //compute the top two number using the operator, the order matters. ProperFraction res = compute(firstnumber, secondnumber, opt); //put the number back to the number stack. numstack.Push(res); } //put that operator in the opt stack? opstack.Push(opt); } } } else//else it must be sub expression { if (!(ele is IList <Object>))// This shit if not a list { throw new Exception("Bad element: " + ele); } //Turn the sub expression into a number. ProperFraction subexpressionnumber = evaluateSimple((IList <Object>)ele); numstack.Push(subexpressionnumber); } } // Evaluate the remaining expression empty the stack; // return result. ProperFraction subexpressionresult = evalueteStacks(numstack, opstack); if (subexpressionresult == null) { throw new Exception("Expression or sub expression cannot be evaluated. "); } return(subexpressionresult); }