Jump to content
sidupac

Calculate integer from string

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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 :P

 

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.

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...