Using STL map for elements(Chem.)

MosaicFuneral 0 Tallied Votes 252 Views Share

I decided to find a use for the STL map, and this is what I decided would be cool to practice with.

Just have three placed in the code. Could use a function to load the requested elements from a file.

#include <iostream>
#include <map>
#include <cstdlib>
using namespace std;

#define SINGLE 1
#define DOUBLE 2
#define TRIPLE 3

class Element
{
      private:
              int   diatomic;
              int   metal;
              int   transitional;
              
      public:
              string name;
              string symbol;
              
              float  amu; //atomic mass unit
             
              int    atmc_num; //protons too
              int    period;
              int    group;
              
              int    isDia(void);
              int    isTrans(void);
              int    isMetal(void);
              
              void   printElementInfo(void);
              
              void   _Element(int dia, int met, int tran,
                             int per, int grp, int atmn);
};

int main()
{
    map<string, Element> elements;
    elements["H"]._Element(true, false, false, 1, 1, 1);
    elements["H"].name   = "Hydrogen";
    elements["H"].symbol = "H";
    elements["H"].amu    = 1.00794;
    
    elements["C"]._Element(false, false, false, 2, 4, 6);
    elements["C"].name   = "Carbon";
    elements["C"].symbol = "C";
    elements["C"].amu    = 12.011;
    
    elements["Uuo"]._Element(false, false, false, 7, 8, 118);
    elements["Uuo"].name   = "Ununoctium";
    elements["Uuo"].symbol = "Uuo";
    elements["Uuo"].amu    = 294;
    
    
    cout << "\"Note: using American grouping label numerals!\"" << endl << endl;
    elements["H"].printElementInfo();
    cout << endl;
    elements["C"].printElementInfo();
    cout << endl;
    elements["Uuo"].printElementInfo();
    
    
    getchar();
    return(0);
}

void Element::_Element(int dia, int met, int tran,
                 int per, int grp, int atmn)
{
    diatomic = dia;
    metal = met;
    tran = transitional;
    period = per;
    group = grp;
    atmc_num = atmn;
}

int    Element::isDia(void)
{
    return(diatomic);
}

int    Element::isTrans(void)
{
    return(transitional);
}

int    Element::isMetal(void)
{
    return(metal);
}

void   Element::printElementInfo(void)
{
       cout << "Name: " << name << endl
            << "Symbol: " << symbol << endl
            << "Atomic number: " << atmc_num << endl
            << "Atomic weight: " << amu << endl
            << "Period: " << period << endl
            << "Group: " << group << endl;
       
       if(diatomic)
       {
           cout << "Diatomic, ";
       }
       if(metal)
       {
           cout << "metal";
           if(transitional)
           {
               cout << ", transitional";
           }
       }
       cout << endl;
}
ddanbe 2,724 Professional Procrastinator Featured Poster

Why use integer for diatomic, metal and transitional? These are clearly booleans and should be used as such.

MosaicFuneral 812 Nearly a Posting Virtuoso

Could blurry vision, runny nose, and half a bottle of cough syrup be an excuse?! lmfo

Well, I've also been told if possible stick to a single data type, preferably that of your target architecture. Mostly as a fall back if you want to do future optimizations.

ddanbe 2,724 Professional Procrastinator Featured Poster

1) a cold might be an excuse
2) you may NEVER stick to a single datatype, you must stick to the datatype which is most appropriate for the case at hand!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.