public ArithmeticSubstitute(ArithmeticOperand operand, char name) { //operand cannot be another substitute if (operand.IsSubstitution) { throw new Exception("Invalid operand"); } p_Operand = operand; p_Name = name; }
private ArithmeticOperand processSubstitute(ArithmeticOperand operand, LinkedList<ArithmeticSubstitute> sub) { char name = (char)operand.Value; ArithmeticSubstitute ret; bool found = substituteExist(name, sub, out ret); if (found) { return ret.Operand; } throw new Exception("Substitute '" + name + "' was not found"); }
private void processOperand(ref ArithmeticOperand operand, bool canResize, ref ArithmeticNumeric buffer, LinkedList<ArithmeticSubstitute> subs) { if (operand.IsSubstitution) { ArithmeticOperand newOpAnd = processSubstitute(operand, subs); //do we have to resize the buffer? to store the substitution? if (canResize) { ArithmeticNumeric val = (ArithmeticNumeric)newOpAnd.Value; //decimal? if (!buffer.IsDecimal && newOpAnd.IsDecimal) { ArithmeticNumeric bufferCopy = buffer; buffer = ArithmeticNumeric.CreateOfSize(val.Size, true); buffer += bufferCopy; } //need more memory? else if (buffer.Size < val.Size) { ArithmeticNumeric bufferCopy = buffer; buffer = ArithmeticNumeric.CreateOfSize(val.Size, newOpAnd.IsDecimal); buffer += bufferCopy; } } if (operand.IsNegative) { newOpAnd.IsNegative = true; } operand = newOpAnd; return; } if (operand.IsQueue) { ArithmeticNumeric ret = ((ArithmeticQueue)operand.Value).Calculate(subs); operand = new ArithmeticOperand(ret, operand.IsNegative); } if (operand.IsFunction) { ArithmeticNumeric ret = ((ArithmeticFunction)operand.Value).Call(subs); operand = new ArithmeticOperand(ret, operand.IsNegative); } }
private void performCalc(ref ArithmeticNumeric current, ArithmeticOperator op, ArithmeticOperand opAnd) { //get the operand as decimal ArithmeticNumeric opAndDecimal = getOperandValue(opAnd); //perform the calculation switch (op) { case ArithmeticOperator.Addition: current += opAndDecimal; break; case ArithmeticOperator.Subtract: current -= opAndDecimal; break; case ArithmeticOperator.Multiply: current *= opAndDecimal; break; case ArithmeticOperator.Divide: current /= opAndDecimal; break; case ArithmeticOperator.Modulus: current %= opAndDecimal; break; case ArithmeticOperator.Power: current = Math.Pow( Convert.ToDouble(current.RAWObject), Convert.ToDouble(opAndDecimal.RAWObject)); break; } }
private ArithmeticNumeric getOperandValue(ArithmeticOperand operand) { ArithmeticNumeric val = (ArithmeticNumeric)operand.Value; if (operand.IsNegative) { val = -val; } return val; }
public ArithmeticQueue AddOperation(ArithmeticOperator op, ArithmeticOperand opAnd1, ArithmeticOperand opAnd2) { //there must be no operators/operands present since it would otherwise //create an invalid calculation queue if (p_Operands.Count != 0) { throw new Exception("This call must be used only when there are no operands to chain off. Consider AddOperation(op,operand)"); } //operand larger than the result size? if so, set the result size to that of the operand sbyte opAnd1Size = 0; sbyte opAnd2Size = 0; if (opAnd1.IsNumeric) { if (opAnd1.IsDecimal) { p_HasDecimal = true; } opAnd1Size = ArithmeticNumeric.SizeOfNumericObj(opAnd1.Value); } if (opAnd2.IsNumeric) { if (opAnd2.IsDecimal) { p_HasDecimal = true; } opAnd2Size = ArithmeticNumeric.SizeOfNumericObj(opAnd2.Value); } if (opAnd1Size > p_ResultSize) { p_ResultSize = opAnd1Size; } if (opAnd2Size > p_ResultSize) { p_ResultSize = opAnd2Size; } //add it p_Operators.AddLast(op); p_Operands.AddLast(opAnd1); p_Operands.AddLast(opAnd2); p_BIDMASSorted = false; //division? if (op == ArithmeticOperator.Divide) { HasDecimal = true; } return this; }
public ArithmeticQueue AddOperation(ArithmeticOperator op, ArithmeticOperand operand) { //valid call? (there must be operands already) otherwise this call would //make the arithmatic queue invalid. if (p_Operands.Count == 0) { throw new Exception("Cannot add on to chain when there is no operands to chain off. Consider using AddOperation(op,opAnd1,opAnd2)."); } //operand larger than the result size? if so, set the result size to that of the operand sbyte operandSize = 0; if (operand.IsNumeric) { if (operand.IsDecimal) { p_HasDecimal = true; } operandSize = ArithmeticNumeric.SizeOfNumericObj(operand.Value); } if (operandSize > p_ResultSize) { p_ResultSize = operandSize; } //add it p_Operators.AddLast(op); p_Operands.AddLast(operand); p_BIDMASSorted = false; //division if (op == ArithmeticOperator.Divide) { HasDecimal = true; } return this; }