Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

function to calculate string

Former Member
0 Kudos

Hi,

I am looking for a function that you can insert: '5+(8*3)-2' and it will give you the value.

something like a calculator function but for a text string.

Did any of you guys have seen something like this before?

thanks,

Itay

3 REPLIES 3

former_member583013
Active Contributor
0 Kudos

Not really...I think that you should need to code it yourself -:)

Greetings,

Blag.

Former Member
0 Kudos

Hi,

You have to do it dynamically, the input parameters are values (for example in your case

( 5, 8, 3, 2) operation (* + / -) are formulas...

R001 = 5, R002 = 8, R003 = 3, R004 = 2

1. Create parameter 100 char length.

2. Get the formula from the user (for example 5+ ( 8 * 3 ) - 2 into the parameter field {like (R001 + (R002 * R003 ) - R004}

3. Use Key word "SPLIT" amd move the values into a internal table (each value will be in a row...

4. Now get the value of R001,R002,R003,R004 & operators....

5. Create a progam dynamically (take the help of dynamic program)

6. Create variables in the program (these variables should be created based on the values in SPLIT commanded used internal table.... with one addition variable for the RESULT, all the varibales declared should be of data type "F".

7. use the SYNTAX-CHECK FOR <program> to check the program syntax is correct.

8. if subrc eq 0 then Submit the dynamic program

9. WRITE the result....

You will get the result...take the below code help

DATA: BEGIN OF fm_split_formula OCCURS 0,

text(10) TYPE c,

END OF fm_split_formula,

BEGIN OF fm_repo OCCURS 0,

line(72),

END OF fm_repo.

DATA: fm_f_check(240),

fm_h_check(72) ,

fm_g_check TYPE i,

fm_offset1 TYPE i,

fm_offset2 TYPE i,

fm_subrc LIKE sy-subrc,

fm_mtext1.

DATA: BEGIN OF fm_func,

f1(7) VALUE 'STRLEN(',

f2(7) VALUE 'SQRT( ',

f3(7) VALUE 'MOD ',

f4(7) VALUE 'DIV ',

f5(7) VALUE 'COS( ',

f6(7) VALUE 'SIN( ',

f7(7) VALUE 'LOG( ',

f8(7) VALUE 'EXP( ',

END OF fm_func.

DATA: fm_loc_index LIKE sy-index,

fm_repo_cnt TYPE i,

fm_cnt_lp TYPE i,

fm_name(10) VALUE 'FORMEL'.

CONSTANTS: fm_operand(8) VALUE ' =()+-*/',

fm_numbers(12) VALUE ' 1234567890.',

fm_result(4) VALUE 'R1 =',

*- Start of insert for # DR1K900907 by Prabhu Rajesh

fm_result1(3) VALUE 'R =',

*- End of insert for # DR1K900907 by Prabhu Rajesh

fm_res(1) VALUE 'R'.

CLEAR : fm_split_formula, fm_repo,fm_repo_cnt.

REFRESH: fm_split_formula, fm_repo.

SPLIT fm_formula AT ' ' INTO TABLE fm_split_formula.

LOOP AT fm_split_formula.

IF NOT ( fm_split_formula-text = fm_func-f1 OR

fm_split_formula-text = fm_func-f2 OR

fm_split_formula-text = fm_func-f3 OR

fm_split_formula-text = fm_func-f4 OR

fm_split_formula-text = fm_func-f5 OR

fm_split_formula-text = fm_func-f6 OR

fm_split_formula-text = fm_func-f7 OR

fm_split_formula-text = fm_func-f8 OR

fm_split_formula-text CO fm_numbers OR

fm_split_formula-text CO fm_operand ).

fm_repo = fm_split_formula-text.

APPEND fm_repo.

ENDIF.

ENDLOOP.

If the hint is useful… Say thanks by reward….

Regards,

Prabhu Rajesh