Application Development and Automation 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: 
Read only

Convert a arithmetic expression into computed value

jaiprasad_naidu5
Participant
0 Likes
4,343

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.

1 ACCEPTED SOLUTION
Read only

UweFetzer_se38
Active Contributor
3,551

Secret hint: have a look at function module "FORMULA_EVALUATION".

13 REPLIES 13
Read only

vladimir_erakovic
Contributor
0 Likes
3,551

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

Read only

0 Likes
3,551

Hello

i have a doubt, if  i change the string input by...

val = ' 50 * 30 - 20 + 30 / 10 * ( 1 + 2 ) * 5 ' .

then how we will use your concept.

Read only

0 Likes
3,551

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?

Read only

0 Likes
3,551

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.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,551

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 ...

Read only

UweFetzer_se38
Active Contributor
3,552

Secret hint: have a look at function module "FORMULA_EVALUATION".

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,551

I would rather use the "internally released" EVAL_FORMULA

My 2 cents ...

Read only

Former Member
0 Likes
3,551

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.

Read only

0 Likes
3,551

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

Read only

0 Likes
3,551

Hi

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.


Read only

jaiprasad_naidu5
Participant
0 Likes
3,551

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

Read only

Former Member
0 Likes
3,551

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

Read only

Former Member
0 Likes
3,551

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