Explore BrainMass

C++ : Sorting hydrocarbon chemical formulae

We have been given a file of chemical formulae, specifically hydrocarbons that consist of only hydrogen and carbon atoms.

Each line of the file contains the name of the chemical followed by its molecular formula in the form C#H# where # is the number of carbon (C) and hydrogen (H) atoms. No name in the file contains blanks.

Because hydrocarbons with the same molecular formula can appear in many structural forms and each of the forms has a different name, the same molecular formula might appear more than once in the file with different names. (e.g.: Butane and 2-methylpropane both have the molecular formula C4H10.)

We would like to keep one entry for each unique molecular formula along with all the names for that formula and then display all these entries in order by ascending number of carbon atoms.

Please refer to the attachment for further details.


Solution Preview

Please find attached 373024.cpp that has been tested for warning free compilation using "g++ -Wall 373024.cpp", and for execution with attached input files.

Program takes the ...

Solution Summary

Source code has been provided with functionality to output the processed information in both the formats; it is a matter of commenting one line and uncommenting another as explained in the source code.