I present you with...
STRING TO MATHWhat does this do?
Type in "5 + 5"
What do you get?
10.
Type in "4*2"
What do you get?
8.
Type in "2+4*5"
What do you get?
22, not 30
It supports five basic operations (+, -, *, /, plus ^ for exponent). "x" can be used in place of * if you like. It uses order of operations and features parentheses as well. Also, multiplying without a * is fine, as well as positive and negative signs (2*-2, 2*+2). This means 2(5) would be the same as 2*5.
What could have been improved:
- Useless symbols are truncated, and don't return error (not just spaces, but extra letters will be ignored as opposed to showing an error).
- Syntax errors result in segmentation faults. Basically said, if you type in "5+", it will just end the program with error in Windows, and say "Segmentation fault" in bash or similar shells.
I'll probably fix these later.
Anyways, the source is included for the curious and for those who think it might be useful. I ask that if you use it, however, to give credit saying I made it (basically, don't go around saying you made it, since you didn't). It's in the form of a nice header.
The compiled Windows EXE and UNIX executable are included. The UNIX version should work on Mac, but I have no clue how Macs handle applications, so I wouldn't know. If someone has a Mac who can confirm this, please do.
I'd like to ask everyone that they attempt to break the program. If it breaks in any of the ways I listed, don't bother posting. But if some random expression fails to work, let me know.
Also, keep in mind that you have to remember the order of operations. Exponents are first. -4 is -1 * 4, so you'll end up taking 4 to the exponent, not -4. Any answer that makes a non-real number, for example, (-4)^0.5, will display "NaN". Don't bother mentioning that as an error - there's not much I can do about it, although I may eventually add support for imaginary numbers.
EDIT: teehee I forgot to include the zip
This post has been edited by RetroXYZ on Oct 1 2009, 06:27 PM
Attached File ( Number of downloads: 47 )
C__.zip