Can Nobody Help Me?

# Thread: Can Nobody Help Me?

1. Member
Join Date
Dec 1969
Posts
41

## Can Nobody Help Me?

Let&#039s say that there is a textbox on a page and somebody can type in a calculation. <BR><BR>So, Joe, comes to my page and puts in the following calculation (400-200)/2. (Any calculation, which also has to keep the order of precedence.) <BR><BR>After Joe hits submit, my asp code sais store this calculation as strCalc = "(400-200)/2".<BR><BR>When it comes time to show him his result he gets back the following answer: (400-200)/2 because the calculation itself was stored as a string. If it were an actual calculation he would have received the answer: 100<BR><BR>How can I get it to perform the calculation? <BR><BR>I can not get past this stumper. Any help would be greatly appreciated.<BR><BR>Thanks.

2. LeeAnnie Guest

## RE: Can Nobody Help Me?

I saw this post a few days ago, and I have been thinking about this....<BR><BR>Let&#039s see if I can steer you in a good direction with my thoughts on this. It&#039s complicated, but it may work if properly written.<BR><BR>You can use the function instr() to search for known characters such as parentheses. It would return the position of the character you were looking for.<BR><BR>For example:<BR>strCalc = "(400-200)/2"<BR>instr(strCalc, "(") would return a 1 and<BR>instr(strCalc, "-") would return a 5<BR><BR>What you could do is do a while loop where the loop would continue to do one calculation at a time until the string had no more characters other than the final number. You would need to transverse the string to pull the numbers out from between the parentheses, placing them in variables, say var1 and var2. You would also have to find the operations within the parentheses so you know what calculation to do. If char = "*" then do this, if char="/" then do that. Looks as though a case statement would be in order there....<BR><BR>Inside the loop, you would first check for parentheses and do the calculations. Then make the string equal to the partially solved answer. For example, "(400-200)/2" would become "200/2"<BR><BR>Reiterate through the loop, so "if no more parentheses" check for multiplication or division. "200/2" would become "100"<BR>If strCalc does not contain *, /, +, -, (, or ), then you know you would have arrived to your answer.<BR><BR>Hope this was at least somewhat helpful.... and easy to understand! :o)

3. Member
Join Date
Dec 1969
Posts
41

## RE: Can Nobody Help Me?

Yes, I appreciate your help. <BR><BR>Actually I was kind of looking for a simple answer like a function that would recognize it as a calculation to be performed. :-)<BR><BR>I am doing something very similiar to what you said, but when you are pulling thousands or even hundreds of thousands of records it is quite a hungry beast and uses a lot of resources.<BR><BR>I just thought somebody would have a quick fix.<BR><BR>Thank your for your help. Please let me know if you can think of anything else to try later.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•