convert an expresion in a result
-
i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?
-
i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?
-
"1+2+3" from the textbox is just a string. How to interpret it is entirely up to you. So, in order to obtain the result, you need to write your own parsing rules and calculating logic.
Best, Jun
ok! But how could i interpret "+" like + operator ? I am thinking to split the string "1+2+3", 1,2 and 3 are numbers, integer, but "+" what type it is?
-
ok! But how could i interpret "+" like + operator ? I am thinking to split the string "1+2+3", 1,2 and 3 are numbers, integer, but "+" what type it is?
That type of string can be split to two categories: operands and operators. Operands are simply numbers. With operators, you cast them one by one. Pseudo code may be like this:
//...
switch(operator1)
{
case "+":
result = operand1+operand2;
break;
case "-":
result = operand1-operand2;
break;
//...
}I am not aware of any shortcuts in this regard.
Best, Jun
-
i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?
First you need to split "+" into an array Then each of the array value needs to be converted or Parsed and added
Regards, Jaiprakash M Bankolli jaiprakash.bankolli@gmail.com http://jaiprakash.blog.com/
-
i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?
You'll need to parse the strings into tokens and go from there. One approach is to use a recursive decent parser[^].
-
i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?
Google "lex yacc".