sidupac Posted September 4, 2011 Share Posted September 4, 2011 I have a string: '4*1' and i need to be able to convert it to an integer while calculating the value. I can only read the calculation as a string and then i need to work out the value, no other way around it. Does anyone know a solution? Examples of strings i would get are: '4+4-1' need to convert so should read = 7 '4/2' need to convert so should read = 2 '4+2^2' need to convert so should read = 8 Quote Link to comment Share on other sites More sharing options...
Zyt3x Posted September 4, 2011 Share Posted September 4, 2011 It's all depending on how advanced you want the calculator to be.. If you have parantheses and stuff like that then it all gets complicated. If you have an internet connection then an easy way to do it is to pass it on to an already existing calculator on the web and then just read from that. Quote Link to comment Share on other sites More sharing options...
FHannes Posted September 4, 2011 Share Posted September 4, 2011 SCAR can't calculate expressions unless you write a script to analyze the expression yourself, I may add an eval function to SCAR in the future though. Quote Link to comment Share on other sites More sharing options...
KingKong Posted September 4, 2011 Share Posted September 4, 2011 SCAR can't calculate expressions unless you write a script to analyze the expression yourself, I may add an eval function to SCAR in the future though. Doesn't scar(or more likely pascalscript) support iteration of strings? Quote Link to comment Share on other sites More sharing options...
FHannes Posted September 4, 2011 Share Posted September 4, 2011 SCAR can't calculate expressions unless you write a script to analyze the expression yourself, I may add an eval function to SCAR in the future though. Doesn't scar(or more likely pascalscript) support iteration of strings? I'm not sure what you mean, iterating over the characters in a string? Quote Link to comment Share on other sites More sharing options...
KingKong Posted September 5, 2011 Share Posted September 5, 2011 I'm not sure what you mean, iterating over the characters in a string? Yes, thats the one. Because if it supports string iteration, then its possible to loop and check for the existence of numbers and symbols by comparing it to say an array of numbers and symbols, which then allows us to evaluate the expression input by the user. Quote Link to comment Share on other sites More sharing options...
FHannes Posted September 5, 2011 Share Posted September 5, 2011 Well yeah, that's what I meant by him having to analyze the expression himself. Quote Link to comment Share on other sites More sharing options...
Zyt3x Posted September 5, 2011 Share Posted September 5, 2011 I still don't get what KingKong is talking about Quote Link to comment Share on other sites More sharing options...
KingKong Posted September 5, 2011 Share Posted September 5, 2011 Well yeah, that's what I meant by him having to analyze the expression himself. Just tried to iterate over a string in scar3.25(portable) and it didn't work. I still don't get what KingKong is talking about This is what i mean: program new; var a: Integer; str: String; begin str := 'abcde'; for a:= 0 to high(str) do writeln(str[a]); end. If strings are iterable, then you should get a, b, c, d and e written out to the debug console, but they're aren't so it doesn't work. Quote Link to comment Share on other sites More sharing options...
Zyt3x Posted September 5, 2011 Share Posted September 5, 2011 Ah! Yeah I agree. That should definitely be possible. Keep in mind strings (at least in Simba) start at 1 to Length(Str) as opposed to arrays that start at 0 to High(Arr); Quote Link to comment Share on other sites More sharing options...
FHannes Posted September 5, 2011 Share Posted September 5, 2011 Use AnsiString, string is kind of fucked up because of the bad unicode support in the engine: [scar]var a: Integer; str: AnsiString; begin str := 'abcde'; for a := 1 to Length(str) do WriteLn(str[a]); end.[/scar] I'll see if I can get that to work with string later on. Quote Link to comment Share on other sites More sharing options...
Bixby Sayz Posted September 7, 2011 Share Posted September 7, 2011 Is there an equivalent of LastDelimiter in SCAR? Would make this task very easy then. Quote Link to comment Share on other sites More sharing options...
FHannes Posted September 7, 2011 Share Posted September 7, 2011 There is LastPos if that's what you mean... Quote Link to comment Share on other sites More sharing options...
Bixby Sayz Posted September 7, 2011 Share Posted September 7, 2011 There is LastPos if that's what you mean... LastPos will work, but only searches for the last position of a single character. LastDelimiter searches for the last position of any of multiple characters. Implemented in FreePascal, so I thought there might be a chance. Was just thinking randomly of things that might help with this problem. Guess one could write their own if needed that badly. Quote Link to comment Share on other sites More sharing options...
FHannes Posted September 7, 2011 Share Posted September 7, 2011 Hmm, I've checked and LastPos is actually supposed to find the last position of a string, there appears to be a bug in there somewhere... But anyway, I can add LastDelimiter. Quote Link to comment Share on other sites More sharing options...
Bixby Sayz Posted September 7, 2011 Share Posted September 7, 2011 Regardless this "simple" initial question is actually not so simple. They are looking for a full fledged calculator routine. Not so simple after all if they want operator precedence and anything beyond the 4 basic operators. Quote Link to comment Share on other sites More sharing options...