输入公式和对应值,先算括号、再算乘除,再算加减
结果
参考代码
调用函数EVAL_FORMULA计算
DATA: dataf TYPE f, datap TYPE lbkum. PARAMETERS: formula(40) DEFAULT 'A*B*C*D', a_val TYPE lbkum DEFAULT 3, b_val TYPE lbkum DEFAULT 1, c_val TYPE lbkum DEFAULT 2, d_val TYPE lbkum DEFAULT 4. DATA:lv_retcode TYPE sy-subrc, lv_funcname(30) TYPE c, lv_message(70) TYPE c, lv_pos TYPE i. START-OF-SELECTION. CALL FUNCTION 'CHECK_FORMULA' EXPORTING formula = formula program = sy-repid routine = 'SUB_CHECK_FORMULA' * UNIT_OF_MEASURE = ' ' * IMPORTING * FUNCNAME = * MESSAGE = * POS = * SUBRC = EXCEPTIONS error_in_formula = 1 missing_parameter = 2 OTHERS = 3. * CALL FUNCTION 'CHECK_FORMULA' * EXPORTING * formula = formula * program = sy-repid * IMPORTING * subrc = lv_retcode * funcname = lv_funcname * message = lv_message * pos = lv_pos * EXCEPTIONS * error_in_formula = 1 * missing_parameter = 2 * OTHERS = 3. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. CALL FUNCTION 'EVAL_FORMULA' EXPORTING formula = formula program = sy-repid routine = 'SUB_GET_VALUE' IMPORTING value = dataf EXCEPTIONS division_by_zero = 1 exp_error = 2 formula_table_not_valid = 3 invalid_expression = 4 invalid_value = 5 log_error = 6 parameter_error = 7 sqrt_error = 8 units_not_valid = 9 missing_parameter = 10 OTHERS = 11. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ELSE. MOVE dataf TO datap. WRITE:/ formula, datap. ENDIF. FORM sub_check_formula USING VALUE(name) CHANGING VALUE(subrc). subrc = 0. ENDFORM. FORM sub_get_value USING VALUE(name) CHANGING VALUE(value) VALUE(subrc). subrc = 0. value = 0. CASE name. WHEN 'A'. value = a_val. WHEN 'B'. value = b_val. WHEN 'C'. value = c_val. WHEN 'D'. value = d_val. ENDCASE. ENDFORM.
TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back 此页面的语言为英语 翻译为中文(简体) 标签:sy,val,公式,value,lv,ABAP,计算,formula,TYPE From: https://www.cnblogs.com/BinGeneral/p/18487378