01-04-2008 4:30 AM
How can I calculate dynamic operators.
I would like to calculate formular by changing text '+' to plus and text '-' to minus which user can determine formular by themself .
Example :
Data: total type p,
num1 type n value '3',
num2 type n value '5'.
parameters operator type c default '+'.
total = num1 operator num2.
I want "total" result like this --> total = 3 + 5 --> total = 8. ***
Thank you for your help.
Sarun
01-04-2008 6:08 AM
Friend,
Use this...
PARAMETERS: operator type c DEFAULT '+'.
data:
num1 type n value '2',
num2 type n value '5'.
data: tot type p.
data: st type string.
concatenate num1 operator num2 into st SEPARATED BY space.
CALL FUNCTION 'FORMULA_EVALUATION'
EXPORTING
FORMULA = st
IMPORTING
VALUE = tot.
write:/ tot.
01-04-2008 4:32 AM
Friend,
you can use case...endcase...
case operator.
when '+'.
total = num1 + num2.
when '-'.
total = num1 - num2.
endcase.
like above use for *, /
01-04-2008 4:41 AM
Thank you so much but formula is not fixed but it will be determined by user . It mean it's a dynamic formula.
01-04-2008 4:34 AM
hi ,
U want "total" result like this --> total = 3 + 5 --> total = 8. ***
for this try this
'total' = num1 '' num2 '--> total = ' num1num2.
Try this ..
THX
01-04-2008 4:40 AM
HI try this ,
Data: total type p,
num1 type n value '3',
num2 type n value '5',
num3 type n.
num3 = num1 + num2.
write : 'total = ' .
write : num1.
write : '+'.
write : num2.
write : ' --> total = '.
write : num3.
THX
01-04-2008 4:57 AM
Data: total type p,
num1 type i ,
num2 type i .
parameters operator type c default '+'.
start-of-selection.
num1 = 10.
num2 = 5.
while num1 >= num2.
case operator.
when '+'.
total = num1 + num2.
operator = '-'.
when '-'.
total = num1 - num2.
operator = '+'.
endcase.
num1 = num1 - 1.
write:/ total.
endwhile.
01-04-2008 6:08 AM
Friend,
Use this...
PARAMETERS: operator type c DEFAULT '+'.
data:
num1 type n value '2',
num2 type n value '5'.
data: tot type p.
data: st type string.
concatenate num1 operator num2 into st SEPARATED BY space.
CALL FUNCTION 'FORMULA_EVALUATION'
EXPORTING
FORMULA = st
IMPORTING
VALUE = tot.
write:/ tot.
01-04-2008 6:25 AM