![]() |
[Ocaml] Interactive (althought currently limited) calculator
I did this in about 3 hours. It's an interactive calculator that can compute addition, subtraction, multiplication, and division. It currently only handles integers and does not recognise brackets or operator precedence.
Also, this is the first time I've written something like this, and I didn't use any resources in order to figure out how to do any of the parsing or calculation. As a result, I bet the code could be greatly improved using common idioms for this type of program. It was a fun exercise and I think it's pretty neat. :) :p :
(*compiling... :
$ ocamlfind opt -package str -linkpkg parse.ml -o parserunning... :
$ ./parse |
Nice man! Just by looking at your output, you did a great job.
|
>> 100 + 2 - 4 + 8 * 50 / 10
530 That should be 138 :o |
Quote:
EDIT: On a semi-related note Andro, I'll be posting the Tic-Tac-Toe code soon (though probably without the GUI stuff). EDIT2: Thanks reggaeton_king! I'll probably add all the features it's missing once I figure out how to implement them (and then there's the whole floating point number thing...oh no). |
I just finished a version with correct operator precedence and the ability to handle fractions (and return results in decimals, but not calculate decimals).
I'll post it once I fix a glitch that causes something like "3 * -2" to cause an error. Also, I used the Num module in Ocaml, so it can handle arbitrary-length numbers and calculations by default. EDIT: On the other hand, 3 * -2 should probably cause an error. If I manage to get brackets working (that'll be tough...) then you'll probably have to just type "3 * (-2)". |
3 * -2 shouldn't cause an error. You just need to distinguish between a unary situation and a binary situation.
|
1 Attachment(s)
Quote:
I'm attaching a source tarball, including a native executable for x86 (or something), because I know most people don't have the facilities to compile this thing. EDIT: The executable makes the attachment too big. I'll have to just post the source. To anyone that's interested: try it out and let me know of any errors or bugs. :) EDIT2: Here's a transcript: :
CamlCalc version 0.1, Copyright (C) 2007 Jesse H-K |
Jesse, I have one unrelated question: when are you going to take that rubber glove off your face? My doctor sticks a lot of stuff up my ass, but his head is not usually one of them. That's my job.
|
Don`t tell me that retired people do retarded things, kind of the other way??????? :confused:
|
Quote:
|
| All times are GMT -5. The time now is 2:37 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC