def execute(self, context, paraList): if (len(paraList) == 0): context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False result = Complex(0) for c in paraList: result = Complex.add(result, c) result = Complex.div(result, Complex(len(paraList))) context.setCurrentResult(result) return self.checkResult(context)
def execute(self, context, paraList): if len(paraList) != 1: context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False para = paraList[0] if para.i != 0: context.setErrorMessage(self.get_name(), R_string.error_invalid_date_type) return False cycles = math.floor(para.r / 2 / math.pi) if cycles < 0: cycles = cycles + 1 angle = para.r - cycles * 2 * math.pi if (math.fabs(angle - math.pi / 2) < self.MIN_NUMBER) \ or (math.fabs(angle - math.pi * 3 / 2) < self.MIN_NUMBER): context.setErrorMessage(self.get_name(), R_string.error_invalid_input) return False result = math.tan(angle) context.setCurrentResult(Complex(result)) return True
def execute(self, context, paraList): if len(paraList) != 1: context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False para = paraList[0] if para.i: context.setErrorMessage(self.get_name(), R_string.error_invalid_date_type) return False result = math.cos(para.r) context.setCurrentResult(Complex(result)) return True
def execute(self, context, paraList): if (len(paraList) != 1): context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False para = paraList[0] if (para.i != 0): context.setErrorMessage(self.get_name(), R_string.error_invalid_date_type) return False context.setCurrentResult(Complex(math.atan(para.r))) return self.checkResult(context)
def execute(self, context, paraList): if len(paraList) != 1: context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False para = paraList[0] if (para.i != 0): context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False context.setCurrentResult( Complex((math.log(1 + para.r) - math.log(1 - para.r)) / 2)) return self.checkResult(context)
def evaluate(self, context): context.pushResult(Complex(0)) paraList = [] for expr in self.exprList: if expr.evaluate(context): paraList.append(context.getCurrentResult()) else: return False context.popResult() fun = context.getFunctionManager().getFunction(self.functionName) if not fun: context.setErrorMessage("Can't found the function:" + self.functionName) return False return fun.execute(context,paraList)
def calculate1(self, tokenList): result = '' unknownToken = '' for token in tokenList: if token.getType() == TokenType.NoType: if len(unknownToken) > 0: unknownToken = unknownToken + ',' unknownToken += token.getContent() if len(unknownToken) > 0: self.currentRecord.success = False return str(R_string.error_unknown_keyword) + unknownToken bContext = BuildContext(self.systemContext, self.constManager, tokenList) expr = AdditiveExpr.buildExpr(bContext) if expr: if len(bContext.tokenList) > 0: tokenString = '' for token in tokenList: if len(tokenString) != 0: tokenString += "," tokenString += token.getContent() self.currentRecord.success = False result = R_string.error_unnecessary_keyword + tokenString else: eContext = EvaluateContext(self.systemContext, self) eContext.pushResult(Complex(0)) if expr.evaluate(eContext): value = eContext.popResult() result = eContext.getFormatter().toString(value) if result: self.currentRecord.success = True self.currentRecord.result = value else: self.currentRecord.success = False result = R_string.error_invalid_input else: self.currentRecord.success = False result = eContext.getErrorMessage() else: self.currentRecord.success = False result = bContext.errorMessage return result
def execute(self, paraList): if (len(paraList) != 2): context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False x = paraList[0] if (x.i != 0): context.setErrorMessage(self.get_name(), R_string.error_invalid_date_type) return False y = paraList[1] if (y.i != 0): context.setErrorMessage(self.get_name(), R_string.error_invalid_date_type) return False context.setCurrentResult(Complex(math.pow(x.r, y.r))) return self.checkResult(context)
def buildExpr(context): #value = context.tokenList.removeFirst().getContent() value = context.tokenList.pop(0).getContent() constValue = context.constManager.find(value) if constValue: return NumberExpr(constValue) else: #index = value.indexOf("∠") index = value.find("∠") if (index != -1): radius = float(value.substring(0, index)) angle = 0 if (value.substring(value.length() - 1).compareTo("°") == 0): degrees = float( value.substring(index + 1, value.length() - 1)) cycles = math.floor(degrees / 360) if (degrees < 0): cycles = cycles + 1 angle = math.radians(degrees - cycles * 360) else: angle = float(value.substring(index + 1)) return NumberExpr( Complex(radius * math.cos(angle), radius * math.sin(angle))) #elif (value.substring(value.length() - 1).compareTo("i") == 0): elif (value[len(value) - 1] == ("i")): if (value.length() == 1): return NumberExpr(Complex(0, 1)) else: return NumberExpr( Complex(0, float(value.substring(0, value.length() - 1)))) #elif (value.substring(value.length() - 1).compareTo("°") == 0): elif (value[len(value) - 1] == ("°")): return NumberExpr( Complex( math.radians( float(value.substring(0, value.length() - 1))))) #elif (value.substring(value.length() - 1).compareTo("%") == 0): elif (value[len(value) - 1] == ("%")): return NumberExpr( Complex( float(value.substring(0, value.length() - 1)) / 100)) else: return NumberExpr(Complex(float(value)))
def execute(self, context,paraList): if len(paraList) != 1: context.setErrorMessage(self.get_name(), R_string.error_invalid_parameter_count) return False para = paraList[0] if (para.i != 0): context.setErrorMessage(self.get_name(), R_string.error_invalid_date_type) return False if (para.r < 1): context.setErrorMessage(self.get_name(), R_string.error_invalid_input) return False value = math.sqrt((para.r + 1) / 2) + math.sqrt((para.r - 1) / 2) if (value == 0): context.setErrorMessage(self.get_name(), R_string.error_invalid_input) return False context.setCurrentResult(Complex(math.log(value) * 2)) return self.checkResult(context)
def evaluate(self, context): if len(self.exprList) - len(self.operatorList) != 1: context.clearResult() context.setErrorMessage('Operator count and Expr count is not match.') return False ex_index = 0 context.pushResult(Complex()) if not self.exprList[ex_index].evaluate(context): return False ex_index = ex_index + 1 op_index = 0 while ex_index < len(self.exprList): expr = self.exprList[ex_index] token = self.operatorList[op_index] ex_index = ex_index + 1 op_index = op_index + 1 if op_index >= len(self.operatorList): op_index = len(self.operatorList) -1 res_prev = context.getCurrentResult() if expr.evaluate(context): res_cur = context.popResult() valueOperator = self.getValueOperator(token.getContent()) if valueOperator.evaluate(res_prev, res_cur): context.pushResult(valueOperator.getResult()) else: context.clearResult() context.setErrorMessage(valueOperator.getOperatorString(), valueOperator.getErrorMessage()) return False else: return False context.setCurrentResult(context.popResult()) return True
def __init__(self, string): self.operatorString = string self.evaluateResult = Complex() self.errorMessage = 0
from Interpreter.CalculateFunction import *