Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [AGK2] String Calculator - can calculate a string and evaluate equations [Source]

Author
Message
Jack
20
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 9th Jun 2015 03:32 Edited at: 9th Jun 2015 03:34
Hello folks

I had to build some kind of string calculator for some of my projects, so here you go:







What it can:

Calculate a string like (23489+(23482/234))*123478-123
sin(), cos(), tan()
Brackets
Evaluate (=<> = 1 or 0 true of false

What it can`t:
2+2*2 will be 8 not 6, if you use brackets 2+(2*2) you get the right answer



Enjoy and improve


[/url]

Attachments

Login to view attachments
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 9th Jun 2015 09:47
I was going to add the functionality to correct the missing feature you mentioned but your code style is impossible to follow. Your indentation and whitespace is all over the place!

How do you even know what is going on?

But for the record, I did try 2+2*2 to see the results and it returned 0 not 6 or 8

AGK V2 user - Tier 1 (mostly)
Jack
20
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 18th Jun 2015 22:21 Edited at: 18th Jun 2015 22:26
Hello Scraggle, the evalulation will return 0, because its not an equation. I'll add an update with better.
The main calculation sequence code was adapted from an old DBC code. The lack of comments is made for faster process, but if you want to learn the codes behaviour, you can use the print loops inside the functions to get some debugging. A math parser is actually very hard to program. You read the brackets from inside to the outside and calculate from left to right.
There are some cases, where the string has to be parsed from right to left. I'll try to comment this out.

The following version is much better. I could remove a lot of functions and added the inside-out interpretation for correct bracket calculations.


Limitations:
10--22 will be interpreted as 10-22 and should be 10+22
2+2*2 will be 8, not 6
evalulate wont work (remove the end$=calculate(... line in calculate_term(, to get this to work again, this will be better )

I will change this problems later. The inside-out interpretation is far more important for my current project.


Here we go:




[/url]

Login to post a reply

Server time is: 2024-11-25 21:30:45
Your offset time is: 2024-11-25 21:30:45