Computer Science and Programming

Rowan University
Prof. Gregory Safko

Project 1

Assigned: February 26, 2003

Due: March 12, 2003

 


Chemical Analysis (chem.cpp)

Write a program that will read in a chemical formula, and will break it down into it's elements and the number of times (the moles) that these elements appear in the compound.

As an example, the formula for water is H2O. This means that there are 2 moles of H (Hydrogen), and 1 mole of O (Oxygen). If only one mole appears, the '1' is not written (it is assumed). 

Elements are found on the Periodic Table of Elements (site 1- site 2) as either a single (capital) letter (such as H,O,C,N, etc.), or as a two letter representation, with the first letter capitalized, and the second letter as lower case (such as Fe, Ag, Ni, etc.)

It may be helpful (and perhaps necessary) to use the functions that you have written in previous labs to solve this problem.

Your program will work as follows: 

(User input is underlined here for illustration only.)

 

Please enter a chemical compound: H2O
Compound: H2O

Element    Moles

H          2

O          1
Press any key to continue


Please enter a chemical compound: H2SO4
Compound: H2SO4

Element    Moles

H          2

S          1

O          4

Press any key to continue


Please enter a chemical compound: AgNO3
Compound: AgNO3

Element    Moles

Ag         1

N          1

O          3

Press any key to continue


Please enter a chemical compound: NaCl
Compound: NaCl

Element    Moles

Na         1

Cl         1

Press any key to continue


 

 

Some things to note:

1. Always assume the compound will be entered with the proper case sensitivity. In the next to last example, silver nitrate (AgNO3) was entered with a lower case 'g', which means it is not an element, but part of the previous letter (in this case, the 'A').  Notice that table salt (NaCl, above) appears as 1 mole of Na and 1 mole of Cl, and not as 1 mole of N, 1 mole of a, 1 mole of C, and 1 mole of l

 

2. No compound starts with a number. Compounds always start with a letter (an element)

 

3. Note that the program always echoes back the compound to the screen. This is a good practice to get into, so you don't "destroy" the input data with your parsing of the string.

 

4. You may assume that the "synthetic elements" (man-made elements greater than element 109, with such names as Uun, Uuu, Uub, etc.) will not be used as input data.

 

 


 

Back to the main course page