Calculator

This is a discussion on Calculator within the C++ Programming forums, part of the General Programming Boards category; Hi Im supposed to make a molecular weight calculator program for school. The user inputs a formula for example: Ca(OH)2. ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Question 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. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    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
                 add character to string
                 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. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    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. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Hey dude, that periodic table is pretty cool. I think all your tedious labor paid off

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 01:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21