2013 Nov 13 2:57 PM
Dear All,
I have a requirement of converting a arithmetic expression into a computed value.
Example : Variable of type string will have expression as : '123 + 456 + 2'.
I want the result as 581. I tried various methods but it doesn't work.
Can any help in achieving this.
Thank you,
jp.
2013 Nov 13 3:42 PM
Secret hint: have a look at function module "FORMULA_EVALUATION".
2013 Nov 13 3:11 PM
Hi Jaiprasad,
Try like this:
DATA: val TYPE string.
DATA: v1(4) TYPE n,
v2(4) TYPE n,
v3(4) TYPE n,
v4(4) TYPE n.
val = '50 + 35 + 24'.
SPLIT val AT ' + ' INTO v1 v2 v3.
v4 = v1 + v2 + v3.
Adjust to your needs but that's one way.
Regards,
Vladimir
2013 Nov 13 5:39 PM
2013 Nov 13 6:01 PM
Hello Chandra,
Then we will change the concept
I don't know. This was for example in question. For that kind of equations it should be some algorithm..
Do you have some idea?
2013 Nov 13 6:04 PM
No dear, I wasn't testing you, i am really looking for some logic but i think in that case we need to use some postfix/ prefix algorithm.
2013 Nov 13 8:08 PM
i am really looking for some logic but i think in that case we need to use some postfix/ prefix algorithm.
Unless you want to re-invent the wheel ...
2013 Nov 13 3:42 PM
Secret hint: have a look at function module "FORMULA_EVALUATION".
2013 Nov 13 4:50 PM
I would rather use the "internally released" EVAL_FORMULA
My 2 cents ...
2013 Nov 13 4:05 PM
Hi,
you can see this example
DATA va_string TYPE string VALUE '123 + 456 + 2'.
TYPES: BEGIN OF ty_calc,
v1(5) TYPE n,
END OF ty_calc.
DATA t_string TYPE TABLE OF ty_calc WITH HEADER LINE.
DATA va_sum(5) TYPE n.
START-OF-SELECTION.
REFRESH t_string.
CLEAR: t_string, va_sum.
SPLIT va_string AT '+' INTO TABLE t_string.
LOOP AT t_string.
IF sy-tabix = 1..
va_sum = t_string-v1.
ELSE.
va_sum = va_sum + t_string-v1.
ENDIF.
ENDLOOP.
WRITE va_sum.
2013 Nov 14 4:52 AM
Hi Ivan,
Thanks for your reply. You logic perfectly works in case it is only addition. If the expression changes dynamically with different operators then how do we address it.
Example :
1. (123 + 456) / 654
2. (123 * 12) + 123 - 567
regards,
JP
2013 Nov 14 7:03 AM
Simple
Try like this
Data : var_final type p,
v_len type i.
Parameter : var type string.
Replace all OCCURRENCES OF '(' in var with ' (?'.
Replace all OCCURRENCES OF ')' in var with ' )?'.
Replace all OCCURRENCES OF '/' in var with ' /?'.
Replace all OCCURRENCES OF '+' in var with ' +?'.
Replace all OCCURRENCES OF '*' in var with ' *?'.
Replace all OCCURRENCES OF '-' in var with ' -?'.
v_len = strlen( var ).
Do v_len times.
replace '?' WITH SPACE INTO var.
enddo.
shift var LEFT DELETING LEADING SPACE.
CALL FUNCTION 'EVAL_FORMULA'
EXPORTING
* DEGREES = ' '
formula = var
* PROGRAM = ' '
* ROUTINE = ' '
* UNIT_OF_MEASURE = ' '
* NO_EXISTENCE_CHECK = ' '
IMPORTING
VALUE = var_final
* 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.
ENDIF.
Write : / var_final.
2013 Nov 14 4:41 AM
Hi,
As Mr.Chandra said the expression would change dynamically depending upon the requirement. Few examples I would give :
1. (123 + 456) / 654
2. (123 * 12) + 123 - 567
regards,
JP
2013 Nov 14 6:09 AM
Hi,
You can use this FM to remove the special characters
ES_REMOVE_SPECIAL_CHARACTER
Then you can use split command.
Code :
DATA : exp TYPE CHAR100 VALUE '(123 * 12) + 123 - 567 / 234',
str TYPE char100.
TYPES : BEGIN OF ty_num,
num TYPE char10,
END OF ty_num.
DATA : i_tab TYPE TABLE OF ty_num,
wa_tab TYPE ty_num.
START-OF-SELECTION.
CALL FUNCTION 'ES_REMOVE_SPECIAL_CHARACTER'
EXPORTING
text1 = exp
IMPORTING
CORR_STRING = str.
SPLIT str At space INTO TABLE i_tab.
LOOP AT i_tab INTO wa_tab.
If wa_tab-num eq space.
DELETE i_tab INDEX sy-tabix.
ENDIF.
ENDLOOP.
Or,
START-OF-SELECTION.
REPLACE all OCCURRENCES OF regex '[^\d]' in exp WITH '/'.
SPLIT exp At '/' INTO TABLE i_tab.
Loop again.
Hope this helps.
Regards
2013 Nov 14 6:15 AM
Hi,
There is a function module available for the purpose as mentioned by Suhas Saha.
> First check for the validity of the syntax of equation using FM
CHECK_FORMULA
> If validated then compute the equation using FM
EVAL_FORMULA
Regards,
Ashish Rawat