1. ## Calculator

Hi

Im supposed to make a molecular weight calculator program for school.

The user inputs a formula for example: Ca(OH)2.
Then the program calculates the weight and displays it

Ca(OH)2 -> 74.09

I need some help with parsing the formula.

2. A 2D-array should do it...a lot of typing but it should be sufficient...than just step through it looking for the elements (chem) and adding their weights...post some code

axon

3. The easiest would be if you used a 2d array where the first element is a string holding the symbol and the second a int holding the wieght of the element. Then you run through the input till you find parenteces or a capital letter etc etc.

basically:

on the input Ca(OH)2

you get the C into a variable, then you get the next character it is lower case so you get the next, it is a parenteses so you take the C and the a, combine them and add the weight that corrosponds to it in the 2d array to a sum.

Then you discard the parenteses and you get the nest character, O, you then get the next character which is a H so you take the weight that corresponds with O from the 2d array and add that to the sum.

Then you take the H and get the next character which is a parenteses so you take H and you add the weight that corresponds to it to the sum and you discard the parenteses.

You take the the next character which is a \n so you stop.

the basic layout would be somewhat liek this:

Code:
```start

get character

loop till you find a '\n'

loop till character is a parantese or upper case
get character

add the weight that corresponds to string to sum
clear the string

get character

display the sum

end```
i hope that psuedo code made it clear for you.

in case you know about do while loops it might be evedent from the psuedo code that using them would be better for both of the loops if you don;t know about them, ignore that last sentence.

I hope i made sense and was of service to you

4. Man, that sounds like a pain because you will have to specify all of the 100-some elements and their weights. Once you get that done, take a rest so you don't get carpal tunnel. This is how I would do the parcing: I would treat any symbol that can have a number after it as an "object". For example, Na would be an object, or (KCl) would be an object(made out of smaller objects). After you have found or calculated the weight of each object, multiply it(or whatever calculation) by the number after it(if there is one). If you encounter parenthesis, you will have to call the function recursively until you hit the closing one. I hope that gives you kind of a general idea of how to do it.

5. I had to do a periodic table in java script a year ago...and yes it is a pain inputing all the properties!!

if anyone is interested you could look at it here

its on angelfire, so be prepared for popups,

axon

6. Hey dude, that periodic table is pretty cool. I think all your tedious labor paid off

7. yes it did...it got me enough extra credit in my chem class to bring me to an 'A' !

but I think we might be getting off-topic a bit.. :-)

axon