Generating straight-chain aliphatic compound structures.

MosaicFuneral 0 Tallied Votes 138 Views Share

After flipping through some organic chemistry books I had, I found a interesting little table of basic hydrocarbons. I decided it would be nice to play around with it, by putting together something that could to some accuracy generate them.

I am by no means a chemist. You don't even want to know my grade...

Anyway, I had fun with it, while I had a cold.

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

#define ALKYL 3
#define ALK_  4

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

struct Structure
{
    /*int is bond type*/
    map<Structure, int>    isomers;
    
    /*symbol, then subscript*/
    map<string, int> symbols;
    int              bond_t;
    
    void clear(void);
};

class Aliphatic
{
      private:
             Structure        temp;
      
      public:
             vector<Structure> format;
             
             void              create(int c, int h, int bondtype, int extbonds);
             void              print(void);
};

static void printAliphatic(map<string, Aliphatic> &aliphatic,
                           map<string, Aliphatic>::iterator &it)
{
    for(it = aliphatic.begin(); it != aliphatic.end(); it++)
    {
        cout << (*it).first << " is: "; 
        (*it).second.print();
        cout << endl;
    }
}

int main()
{
    map<int, string>                 greek_prefix; //excluding the first four
    map<string, Aliphatic>::iterator it;
    map<string, Aliphatic>           alkane;
    map<string, Aliphatic>           alkyl; //an odd group
    map<string, Aliphatic>           alkene;
    map<string, Aliphatic>           alkyne;
    map<string, Aliphatic>           alkadiene;
    map<string, Aliphatic>           alkatriene;
    string                           temp;
    
    
    greek_prefix[1] = "Meth";
    greek_prefix[2] = "Eth";
    greek_prefix[3] = "Prop";
    greek_prefix[4] = "But";
    greek_prefix[5] = "Pent";
    greek_prefix[6] = "Hex";
    greek_prefix[7] = "Hept";
    greek_prefix[8] = "Oct";
    greek_prefix[9] = "Non";
    greek_prefix[10] = "Dec";
    
    
    for(int i = 1; i < 61; i++)
    {
        if(i < 11)
        {
            temp = greek_prefix[i] + "ane";
            alkane[temp].create(i, ALK_, SINGLE, 0);
        }
        else if(i > 10 && i < 21)
        {
            temp = greek_prefix[i - 10] + "yl";
            alkyl[temp].create(i - 10, ALKYL, SINGLE, 0);
        }
        else if(i > 21 && i < 31)
        {
            temp = greek_prefix[i - 20] + "ene";
            alkene[temp].create(i - 20, ALK_, DOUBLE, 0);
        }
        else if(i > 31 && i < 41)
        {
            temp = greek_prefix[i - 30] + "yne";
            alkyne[temp].create(i - 30, ALK_, TRIPLE, 0);
        }
        else if(i > 42 && i < 51)
        {
            temp = greek_prefix[i - 40] + "diene";
            alkadiene[temp].create(i - 40, ALK_, DOUBLE, 1);
        }
        else if(i > 53)
        {
            temp = greek_prefix[i - 50] + "triene";
            alkatriene[temp].create(i - 50, ALK_, DOUBLE, 2);
        }
    }
    
    
    cout << "Printing Alkane group:" << endl << endl;
    printAliphatic(alkane, it);
    
    cout << endl << "Printing Alkyl group: " << endl << endl;
    printAliphatic(alkyl, it);
    
    cout << endl << "Printing Alkene group: " << endl << endl;
    printAliphatic(alkene, it);
    
    cout << endl << "Printing Alkyne group: " << endl << endl;
    printAliphatic(alkyne, it);
    
    cout << endl << "Printing Alkadiene group: " << endl << endl;
    printAliphatic(alkadiene, it);
    
    cout << endl << "Printing Alkatriene group: " << endl << endl;
    printAliphatic(alkatriene, it);
    
    getchar();
    return(0);
}

void     Structure::clear(void)
{
     isomers.clear();
     symbols.clear();
     bond_t = 0;
}

void     Aliphatic::create(int c, int h, int bondtype, int extbonds)
{
    int bondtemp = bondtype;
         
    if(c < 2 && bondtype < 2)
    {
         temp.symbols["C"] = 1;
         temp.symbols["H"] = h;
         temp.bond_t       = bondtype;
         
         format.push_back(temp);
    }
    else
    {
        for(int i = 0; i < c; i++)
        {
            if(i == 0 || i == (c - 1))
            {
                temp.symbols["C"] = 1;
                
                if(h > 3)
                {
                    /*Alkane*/
                    temp.symbols["H"] = h - 1;
                    
                    /*Alkene*/
                    if(bondtype > 1 && i == 0)
                    {
                        temp.symbols["H"] -= (bondtype - 1);
                    }
                    
                    if(i > 0 && extbonds > 0)
                    {
                        temp.symbols["H"] = h - 2;
                        if(bondtype > 2)
                        {
                            temp.symbols["H"] -= 1;
                        }
                    }
                }
                else if(h < 4)
                {
                    /*Alkyl*/
                    temp.symbols["H"] = h;
                    if(i > 0)
                    {
                        temp.symbols["H"] -= 1;
                    }
                }
                
            }
            else
            {
                temp.symbols["C"] = 1;
                temp.symbols["H"] = 2;
                if(i == 1)
                {
                    /*Alkene*/
                    if(bondtype > 1)
                    {
                        temp.symbols["H"] -= 1;
                        
                        /*Alkyne*/
                        if(bondtype > 2)
                        {
                            temp.symbols.erase("H");
                        }
                    }
                }
                
                if((c - 1 - extbonds) == i)
                {
                    bondtemp = bondtype;
                }
            }
        
            temp.bond_t = bondtemp;
            if(i == 0) { bondtemp = 1; }
            
            format.push_back(temp);
            temp.clear();
        }
    }
}


void    Aliphatic::print(void)
{
    map<string,int>::iterator it;


    for(int i = 0; i < format.size(); i++)
    {
        for (it = format[i].symbols.begin(); it != format[i].symbols.end(); it++)
        {
              cout << (*it).first;
              if((*it).second > 1)
              {
                  cout << (*it).second << " ";
              }
        }
        
        if(i != format.size() - 1)
        {
            if(format[i].bond_t == 1) { cout << "- "; }
            else if(format[i].bond_t == 2) { cout << "= "; }
            else if(format[i].bond_t == 3) { cout << (char)240 << " "; }
        }
    }
}