2008 Nov 12 1:54 PM
Hi,
does anybody know how to process a formula that comes in an string?
Por ejemplo:
a = 1.
b = 2.
c = 3.
string = '( a + b ) * c'.
....
string = '( 1 + 2 ) * 3'.
how can I get the result of that formula.
Thanks in advance.
2008 Nov 12 2:51 PM
You can try to generate a Dynamic Subroutine-pool and call it from the your program.
Check this sample code:
DATA: l_string TYPE string,
a TYPE i,
b TYPE i,
c TYPE i,
res TYPE i.
DATA: l_source TYPE STANDARD TABLE OF char72 WITH HEADER LINE.
a = 1.
b = 2.
c = 3.
FIELD-SYMBOLS: <fs> TYPE ANY.
l_string = '( A + B ) * C'.
l_source = 'PROGRAM ZTEST_TMP_123.'.
APPEND l_source.
l_source = 'form test using a b c changing res.'.
APPEND l_source.
l_source = 'RES = '.
APPEND l_source.
l_source = l_string.
APPEND l_source.
l_source = '.'.
APPEND l_source.
l_source = 'endform.'.
APPEND l_source.
DATA: prog TYPE string,
tab TYPE STANDARD TABLE OF string,
mess TYPE string,
sid TYPE string.
GENERATE SUBROUTINE POOL l_source NAME prog
MESSAGE mess
SHORTDUMP-ID sid.
IF sy-subrc = 0.
PERFORM ('TEST') IN PROGRAM (prog) IF FOUND
USING a b c CHANGING res .
ELSEIF sy-subrc = 4.
MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
MESSAGE sid TYPE 'I'.
ENDIF.
WRITE: l_string, '=', res.
Regards,
Naimesh Patel
2008 Nov 12 2:51 PM
You can try to generate a Dynamic Subroutine-pool and call it from the your program.
Check this sample code:
DATA: l_string TYPE string,
a TYPE i,
b TYPE i,
c TYPE i,
res TYPE i.
DATA: l_source TYPE STANDARD TABLE OF char72 WITH HEADER LINE.
a = 1.
b = 2.
c = 3.
FIELD-SYMBOLS: <fs> TYPE ANY.
l_string = '( A + B ) * C'.
l_source = 'PROGRAM ZTEST_TMP_123.'.
APPEND l_source.
l_source = 'form test using a b c changing res.'.
APPEND l_source.
l_source = 'RES = '.
APPEND l_source.
l_source = l_string.
APPEND l_source.
l_source = '.'.
APPEND l_source.
l_source = 'endform.'.
APPEND l_source.
DATA: prog TYPE string,
tab TYPE STANDARD TABLE OF string,
mess TYPE string,
sid TYPE string.
GENERATE SUBROUTINE POOL l_source NAME prog
MESSAGE mess
SHORTDUMP-ID sid.
IF sy-subrc = 0.
PERFORM ('TEST') IN PROGRAM (prog) IF FOUND
USING a b c CHANGING res .
ELSEIF sy-subrc = 4.
MESSAGE mess TYPE 'I'.
ELSEIF sy-subrc = 8.
MESSAGE sid TYPE 'I'.
ENDIF.
WRITE: l_string, '=', res.
Regards,
Naimesh Patel
2008 Nov 12 3:15 PM