As part of my Lewis Drawer program, I will obviously, at times, have equations like this: CH3CH2CH2CH3. What I need to do is this...from the input string, parse it for like terms and rewrite the formula with each term listed only one. For instance, in the above sample... C4H10. How would I do this?
Well, it seems to me that all chemical formulae of the type you listed follow this (regex) pattern:

/([A-Z][a-z]?[0-9]*)+/

In layman's terms, repeated sequences of the following:
- One uppercase letter
- Zero or one lowercase letter
- Zero or more numbers

It seems to me that you can create a (number of possible elements)-item list, then sweep through the string from left to right, first finding letter sequences, then (if there's a number) adding that number to the corresponding list element, or if there's none, adding one. Does that make sense? Do you need a more precise TI-BASIC example?
let's say it's in Str1, you could do something like this:

Code:
:""-->Str2
{0}->lEleme
0-->Z
:For(X,1,length(Str1))
::For(Y,1,length(Str2))
:::If inString(Str1,X,1)=inString(Str2,Y,1):Then
::::1-->Z
::::If inString(Str1,X+1,1)="1" or inString(Str1,X+1,2)="2" or inString(Str1,X+1,2)="3" or inString(Str1,X+1,2)="4" or inString(Str1,X+1,2)="5" or inString(Str1,X+1,2)="6" or inString(Str1,X+1,2)="7" or inString(Str1,X+1,2)="8" or inString(Str1,X+1,2)="9":Then
:::::expr(inString(Str1,X+1,1))-->A
:::::lEleme(Y)+expr(inString(Str1,X+1,1))-->lEleme(Y)
:::::X+1-->X
::::Else
:::::lEleme(Y)+1-->lEleme(Y)
::::End
:::End
::End
::If Z=0:Then
:::Str2+inString(Str1,X,1)-->Str2
:::If inString(Str1,X+1,1)="1" or inString(Str1,X+1,2)="2" or inString(Str1,X+1,2)="3" or inString(Str1,X+1,2)="4" or inString(Str1,X+1,2)="5" or inString(Str1,X+1,2)="6" or inString(Str1,X+1,2)="7" or inString(Str1,X+1,2)="8" or inString(Str1,X+1,2)="9":Then
::::lEleme(Y)+expr(inString(Str1,X+1,1))-->lEleme(Y)
::::X+1-->X
:::Else
::::lEleme(Y)+1-->lEleme(Y)
:::End
::End
::0-->Z
:End

Note: I didn't test it at all, so see it as something you can use as inspiration. I did this:
X = Current character checked (Str1).
Y = Current character from character database (Str2) checked.
:Go through all characters.
::Go through all characters in stringdatabase.
:::If the checked letter = letter in stringdatabase
::::Indicate we have found the letter (1-->Z)
::::If the next character is a number
:::::Add A to the number of elements in lEleme.
:::::Add 1 to X, to make sure it's not gonna check the number as an element.
::::If the next character is not a number
:::::Add 1 to the number of elements in lEleme.
::Begin again with loop.
::If Z=0
:::"Apperently the character wasn't found in the stringdatabase, so add it.
:::Add the element to the stringdatabase
:::If the next character is a number
::::Add it to the number of elements already defined.
::::Add 1 to X, to make sure it's not gonna check the number as an element.
:::If the next character is not a number
::::Add 1 to the number of elements already defined.
::Reset Z.
:Begin again with loop.
Do note that this only works for numbers smaller than 10, but I guess it's easy to extend that.Smile
Ouch. Do you use code like that for checking for numbers in your own code? :/ Actually, that wouldn't work. inString(Str1,X+1,1) would need to be sub(Str1,X+1,1). To check if that character is a number, you could do:
Code:
If inString("0123456789",sub(Str1,X+1,1


Edit: Also, a good rule of thumb is: if you are ever copying and pasting code, then you're doing something wrong. I can only assume that you copy-pasted that code and changed the `="0"' part to whatever number you were on.
Lol, much easier indeed player. Fact is I'm only using Asm, in which I would ofcourse use de font equates. I just wasn't paying attention to any optimizations Smile
"de font equates"? What do you mean by that?
oh, sorry, "de" is the dutch article, I mean "the".Razz The font equates are the values assigned to the letters. For example: a $30 is assigned to "0", $31 is assigned to "1", etc. I'd use them to check whether it is between $30 and $39.
Ah, I understand. I'm still surprised, even if you are getting used to ASM, that you would miss such a glaring optimization. Did you not think there was an easier way while you were writing it? (Out of curiosity)
Ofcourse I did, I was just too lazy to think of which one. Srsly. Smile
*bump* How's this going, ACagliano? Do you have any further questions or follow-ups? How did our answers do you for?
Thank for the help, however, this project will not succeed because I am trying to accomplish an impossible task. (See topic Angle Calc Help). Both are related to the same project. I have decided to stop this and resume work on another project.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement