Re: Calculation by string
Re: Calculation by string
- Subject: Re: Calculation by string
- From: Paolo Manna <email@hidden>
- Date: Sat, 17 May 2003 12:08:20 +0200
On Saturday, May 17, 2003, at 06:37 PM, Lorenzo wrote:
I read a string from a file.
This string contains:
@"x * y"
I already have two global int variables called "x" and "y".
Now I would like to get the product of x and y.
How should I do?
3 possibilities, IMO (or better, 2 possibilities and the software
equivalent of using an elephant to open peanuts ;)):
- Specific case, or few simple, well defined cases where input is
highly standardized and a syntax error (or any other error, for that
matter) is unlikely to happen: use NSScanner and write a parsing
routine that does the job.
- Highly generic case (i.e. not limited to "x * y" but handling
something like "((x + 2) * (y / 3)) + z" or worse): take a look at lex
& yacc (or their GNU equivalents flex and bison), that can write a
parser for you given the rules. This means learning 2 new tools,
though...
- A complete overkill in your case, as we're talking about ints, but
I'm reporting it as it's worth considering when "x" and "y" are objects
and "*" is a generic operation between them: try to make "x" and "y"
scriptable (if they're the same type, that's even better), define all
operations ("*") between them you're interested in, and use AppleScript
to send instructions to yourself. This also adds the benefit that other
apps could ask your app to do something for them...
Cheers
Paolo
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.