It is currently Wed Aug 23, 2017 6:48 am

All times are UTC - 5 hours




 Page 1 of 1 [ 5 posts ] 
Author Message
 Post subject:
PostPosted: Sat Aug 11, 2007 6:11 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
I show you how to make your code more readable by replacing if-else statements with switches working from enumerated variables.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 3 - Switch and Enum Keywords
PostPosted: Tue Dec 13, 2011 2:45 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I have a function that is returning an enumeration.

sample code...
// Some Header File

enum Count {
   First      = 1,
   Second  = 1,
   Third     = 2,
   Fourth   = 2,
   Fith       = 3,
   Sixth     = 3,
   Seventh = 3,
   Eigth      = 4,
};

// Some .cpp File

Count SomeClass::SetType() {
    if ( someValue1 == someCondition1 ) {
       return First;
    }
    if ( someValue2 == SomeCondition2 ) {
        return Second;
    }
}


In the above example when I debug my code it goes into the correct if statement
but when I check my return variable It isn't returned as Second it is returned as First.
I know that in the Enum the values for First and Second are listed as the same
which they are supposed to be for my code. Is this just something that visual studio or c++
does automatically with enumerations... where if two entries in the same enum have the same value
and you are returning them from a series of if statements, it will return the first one of that value?

this is the actual enum that I am using in my code with the function to return the enumeration.
enum AtomicGroup {

      ATOMIC_GROUP_UNKNOWN    = -1,
      ATOMIC_GROUP_ERROR      = 0,   
   
      HYDROGEN_GROUP         = 1,   // Not Listed As Alkali Metal Since It Is A Gas
      ALKALI_METALS         = 1,   // 1
      HELIUM_GROUP         = 2,    // Technically This Is Group 18, But For This To Work It Has To Be 2
      ALKALINE_EARTH_METALS   = 2,   // 2
      SCANDIUM_FAMILY         = 3,   // See Note: * 3-12
      LANTHANIDES            = 3,
      ACTINIDES            = 3,
      TITANIUM_FAMILY         = 4,
      VANADIUM_FAMILY         = 5,
      CHROMINUM_FAMILY      = 6,
      MANGANESE_FAMILY      = 7,
      IRON_FAMILY            = 8,
      COBALT_FAMILY         = 9,
      NICKEL_FAMILY         = 10,
      COPPER_FAMILY         = 11,
      ZINC_FAMILY            = 12,
      BORON_GROUP            = 13,   // 3
      CARBON_GROUP         = 14,   // 4
      NITROGEN_GROUP         = 15,   // 5
      CHALCOGENS            = 16,   // 6
      HALOGENS            = 17,   // 7      
      NOBLE_GASES            = 18,   // 8 Except Helium Which Only Has 2
      
      // LAST: Only Used For Indexing
      ATOMIC_GROUP_LAST,

      // * The General Method For Counting Valence Electrons Is Generally Not Useful
      //   For Tranistion Metals. Instead The Modified "d electron count" Method Is Used
   }; // AtomicGroup


// -----------------------------------------------------------------------
// InitializeAtomicGroup()
// Initialize The Current Element's Atomic Group
AtomicGroup EnergyLevel::InitializeAtomicGroups() {
   
   if ( _element == H ) {
      return HYDROGEN_GROUP;
   }
   if ( (_element == Li) || (_element == Na) || (_element == K)  ||
       (_element == Rb) || (_element == Cs) || (_element == Fr) ) {
      return ALKALI_METALS;
   }
   
   if ( _element == He ) {
      return HELIUM_GROUP;
   }
   if ( (_element == Be) || (_element == Mg) || (_element == Ca) ||
       (_element == Sr) || (_element == Ba) || (_element == Ra) ) {
      return ALKALINE_EARTH_METALS;
   }
   
   if ( (_element == Sc) || (_element == Y) ||
       (_element == Lu) || (_element == Lr) ) {
      return SCANDIUM_FAMILY;
   }
   // Although Lu And Lr Are Part Of The Lanthanide And
   // Actinide Series, Because Of Their Shell Ordering
   // They Are D-Block, And Not F Block For This Reason
   // They Are Found In the SCANDIUM_FAMILY
   if ( (_element == La) || (_element == Ce) || (_element == Pr) || (_element == Nd) ||
       (_element == Pm) || (_element == Sm) || (_element == Eu) || (_element == Gd) ||
       (_element == Tb) || (_element == Dy) || (_element == Ho) || (_element == Er) ||
       (_element == Tm) || (_element == Yb) ) {
      return LANTHANIDES;
   }
   if ( (_element == Ac) || (_element == Th) || (_element == Pa) || (_element == U)  ||
       (_element == Np) || (_element == Pu) || (_element == Am) || (_element == Cm) ||
       (_element == Bk) || (_element == Cf) || (_element == Es) || (_element == Fm) ||
       (_element == Md) || (_element == No) ) {
      return ACTINIDES;
   }

   if ( (_element == Ti) || (_element == Zr) || (_element == Hf) || (_element == Rf) ) {
      return TITANIUM_FAMILY;
   }

   if ( (_element == V) || (_element == Nb) || (_element == Ta) || (_element == Db) ) {
      return VANADIUM_FAMILY;
   }

   if ( (_element == Cr) || (_element == Mo) || (_element == W) || (_element ==Sg) ) {
      return CHROMINUM_FAMILY;
   }

   if ( (_element == Mn) || (_element == Tc) || (_element == Re) || (_element == Bh) ) {
      return MANGANESE_FAMILY;
   }

   if ( (_element == Fe) || (_element == Ru) || (_element == Os) || (_element == Hs) ) {
      return IRON_FAMILY;
   }

   if ( (_element == Co) || (_element == Rh) || (_element == Ir) || (_element == Mt) ) {
      return COBALT_FAMILY;
   }

   if ( (_element == Ni) || (_element == Pd) || (_element == Pt) || (_element == Ds) ) {
      return NICKEL_FAMILY;
   }

   if ( (_element == Cu) || (_element == Ag) || (_element == Au) || (_element == Rg) ) {
      return COPPER_FAMILY;
   }

   if ( (_element == Zn) || (_element == Cd) || (_element == Hg) || (_element == Cn) ) {
      return ZINC_FAMILY;
   }

   if ( (_element == B)  || (_element == Al) || (_element == Ga) ||
       (_element == In) || (_element == Tl) || (_element == Uut) ) {
      return BORON_GROUP;
   }

   if ( (_element == C)  || (_element == Si) || (_element == Ge) ||
       (_element == Sn) || (_element == Pb) || (_element == Uuq) ) {
      return CARBON_GROUP;
   }

   if ( (_element == N)  || (_element == P)  || (_element == As) ||
       (_element == Sb) || (_element == Bi) || (_element == Uup) ) {
      return NITROGEN_GROUP;
   }

   if ( (_element == O)  || (_element == S)  || (_element == Se) ||
       (_element == Te) || (_element == Po) || (_element == Uuh) ) {
      return CHALCOGENS;
   }

   if ( (_element == F)  || (_element == Cl) || (_element == Br) ||
       (_element == I)  || (_element == At) || (_element == Uus) ) {
      return HALOGENS;
   }

   // Even Though Helium Is Noble Gas For This To Work Properly
   // Helium Is Set In Its Own Group Seperate From These,
   // The Reason For This Is Helium Has 2 Electrons Where The Rest
   // Have 18 Electrons In Their Outer Shell
   if ( (_element == Ne) || (_element == Ar) || (_element == Kr) ||
       (_element == Xe) || (_element == Rn) || (_element == Uuo) ) {
      return NOBLE_GASES;
   }

   return ATOMIC_GROUP_ERROR;

} // InitializeAtomicGroups


In the above section if I was to pass in say Li as the element of choice and we know that Lithium is the third element,
I want the above code to return ALKALI_METALS, not HYDROGEN_GROUP even though they both have a value of 1. This will become important later on. Even though hydrogen has 1 valence electron and so does Lithium. The difference for the return of the enumeration will not become important until we start to classify these elements by their state of matter, such as solid, liquid and gas. Here Hydrogen is a gas with 1 valence, where Lithium and all other elements below it are Alkali Metals with 1 valence, giving them different properties at room temperature. The Value of 1 can suffice for this purpose, but when debugging through the code it would be nice to see the appopriate group, and later on I will need the correct enum return when I start to use string values.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 3 - Switch and Enum Keywords
PostPosted: Tue Dec 13, 2011 7:14 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
Enumerations are just named integers, so you should not be putting duplicate values into an enumeration.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 3 - Switch and Enum Keywords
PostPosted: Tue Dec 13, 2011 7:55 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
ok, that does make sense, however how would i go about doing what i stated from my earlier post? would i use defines, or const int/unsigned int ?


Offline
 Profile  
 
 Post subject: Re: C++ VMK 3 - Switch and Enum Keywords
PostPosted: Tue Dec 13, 2011 8:11 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
It feels to me like each element should have properties assigned to it. Each property would be a separate enumeration. So Lithium would have Group 2 as one property, Period 2 as a separate property. You could also create an std::map the returns a string that tells you the name of each group based on the group number.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron