In: Computer Science
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
// Defines structure
struct MolecularFormula
{
// Data members to store data
string name;
string carbonAtoms;
string hydrogenAtoms;
// Function to return true if current object carbonAtoms is less
than parameter object carbonAtoms
// Otherwise returns false
bool operator < (const MolecularFormula& str) const
{
return (carbonAtoms < str.carbonAtoms);
}// End of function
};// End of structure
// Defines a structure findGreater
struct findGreater
{
// Returns true if first object carbonAtoms is less than second
object carbonAtoms
// Otherwise returns false
bool operator()( const MolecularFormula& first, const
MolecularFormula& second) const
{
return first.carbonAtoms < second.carbonAtoms;
}// End of function
};// End of structure
// Function to display the vector contents
void show(vector <MolecularFormula> mfv)
{
// Loops till end of the vector
for(int c = 0; c < mfv.size(); c++)
cout<<mfv.at(c).carbonAtoms<<mfv.at(c).hydrogenAtoms<<"
"<<mfv.at(c).name<<endl;
}// End of function
// Function to remove duplicate
void removeDuplicate(MolecularFormula mf [], int &len)
{
// Loops till number of elements in MolecularFormula array of
object
for(int c = 0; c < len; c++)
{
// Loops next of c position till number of elements in
MolecularFormula array of object
for(int d = c + 1; d < len; d++)
{
// Checks if c index position carbonAtoms hydrogenAtoms is equals
to
// d index position carbonAtoms hydrogenAtoms
if(mf[c].carbonAtoms == mf[d].carbonAtoms &&
mf[c].hydrogenAtoms == mf[d].hydrogenAtoms)
{
// Concatenates c and d index position name
string name = mf[c].name + " " + mf[d].name;
// Assign the name to d index position name
mf[d].name = name;
// Shift each record to left from c index position to end
for(int e = c; e < len-1; e++)
mf[e] = mf[e + 1];
// Decrease the length by one
len--;
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function
// main function definition
int main()
{
// Declares a vector to store MolecularFormula objects
vector <MolecularFormula> mfv;
// Creates an array of objects of MolecularFormula
MolecularFormula mf [] =
{
{"n-Butane", "C4", "H10"},
{"Propyne", "C3", "H3"},
{"1,3-Butadiyne", "C4", "H2"},
{"Hexane", "C6", "H14"},
{"Butane", "C4", "H10"},
{"iso-Butane", "C4", "H10"},
{"Pentane", "C5", "H12"}
};// End of assignment
// Calls the function to display data before sorting
cout<<"\n Before Removing duplicate\n";
// Loops to add MolecularFormula objects to vector
for(int c = 0; c < 7; c++)
mfv.push_back(mf[c]);
// Calls the function to display data
show(mfv);
// Stores the length of the vector
int len = mfv.size();
cout<<"\n After removing duplicate\n";
// Calls the function to remove duplicate and merge
removeDuplicate(mf, len);
// Clears the vector contents
mfv.clear();
// Loops till current length of the array of object after
removing and merging
for(int c = 0; c < len; c++)
// Assigns the current object to vector
mfv.push_back(mf[c]);
// Calls the function to display data after removing
duplicate
show(mfv);
// Calls the function to sort data
sort( mfv.begin(), mfv.end(), findGreater() );
// Calls the function to display data after sorting
cout<<"\n After Sorting \n";
show(mfv);
return 0;
}// End of main function
Sample Output:
Before Removing duplicate
C4H10 n-Butane
C3H3 Propyne
C4H2 1,3-Butadiyne
C6H14 Hexane
C4H10 Butane
C4H10 iso-Butane
C5H12 Pentane
After removing duplicate
C3H3 Propyne
C4H2 1,3-Butadiyne
C6H14 Hexane
C4H10 n-Butane Butane iso-Butane
C5H12 Pentane
After Sorting
C3H3 Propyne
C4H2 1,3-Butadiyne
C4H10 n-Butane Butane iso-Butane
C5H12 Pentane
C6H14 Hexane