Structures
 

    A structure is a group of several components or members. Unlike the components of an array the components of a structure may be of different types. Once declared structs behave like scalar data types.

  1. Declarations. You declare a structure by listing its members along with their data types between curly braces.
  2. struct course
     { 
       int   students;
       float average;
     };
    
     course mw;
     course rgc[10];
     course *ms;
    
  3. Accessing fields. Fields are accessed using the dot (.) operator.
  4. mw.students= 23;
    mw.students = rgc[2].students + 1;
    (*ms).students=12;
    
  5. Complex Structures Members need not be scalar. They may be arrays, other structures.
  6. struct course
    {
     int   students;
     float average;
     int   grades[35];
    };
    
    course c;
    
    c.grades[3]=4;
    
    struct schedule
    {
       int room;
       int time;
       int day;
    };
    
    struct course
    {
      int      students;
      float    average;
      int      grades[35];
      schedule when;
    };
    
    course c;
    
     c.when.room =127;
    
  7. Pointers to structures
  8. course *c;
    
    (*c).average =34.5; note not *c.average
    
     c->average = 3.14;
    
  9. Initializing Structures.
  10. course mw = {32,0.0};

  11. sizeof operator on structures.
  12. sizeof(course);

  13. Arrays of structures.
  14. course rgc[30];

  15. Self referencing structures.
  16. struct line
    { 
        char data[80];
        line *next;
    };
    
    line start;
    

  17. Example.

    Adding a list of fractions

    Enter a list of fractions to add
     Enter e to end
    1 1/2
    
    Adding 1 1/2 +  = 1 1/2
    3 7/8
    
    Adding 3 7/8 + 1 1/2 = 5 3/8
    4
    
    Adding 4  + 5 3/8 = 9 3/8
    3/5
    
    Adding 3/5 + 9 3/8 = 9 39/40
    e
    Press any key to continue
    
    Header Module Sfraction.h
    //
    // Fractions Program in Strucures.
    //
    #ifndef SFRACT
    #define SFRACT
    
    
    struct Fraction {
       int whole;
       int numerator;
       int denominator;
    };
    
    void     displayFraction(Fraction);
    int      getFraction(Fraction&);
    Fraction addFraction(Fraction, Fraction);
    int      gcd(int,int);
    
    #endif
    
    Main Module Sfraction.cpp
    //
    //  main
    //
    
    #include <iostream>
    #include "Sfraction.h"
    
    using namespace std;
    
    int main()
    {
       Fraction sum, item;
    
       sum.whole=sum.numerator=0;
       sum.denominator=1;
    
       cout << "Enter a list of fractions to add" << endl
            << " Enter e to end" << endl;
    
       while(getFraction(item)){
          cout << endl << "Adding ";
          displayFraction(item);
          cout << " + ";
          displayFraction(sum);
          sum=addFraction(sum,item);
          cout << " = ";
          displayFraction(sum);
          cout << endl;
       }
    
       system("pause");
       return 0;
    }
    
    Code Module SfractionM.cpp
    //
    // Modules
    //
    #include "Sfraction.h"
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    
    using namesapce std;
    //
    // Get a Fraction
    //
    int getFraction(Fraction& k)
    {
       char *p,line[81];
       int a[3]={0,0,0};
       int count=0;
       cin.getline(line,80);
       if(line[0] == 'e') return 0;
       if(cin.eof())      return 0;
       p=strtok(line," /");
       do {
          a[count]=atoi(p);
          count++;
          p=strtok(NULL," /");
       } while(p);
       count--;
       k.whole=k.numerator=0;
       k.denominator=1;
       switch (count) {
          case 0: k.whole=a[0]; break;
          case 1: k.numerator=a[0],k.denominator=a[1]; break;
          case 2: k.whole=a[0],k.numerator=a[1],k.denominator=a[2];
       }
       return 1;
    }
    
    // 
    // Display a Fraction
    //
    void displayFraction(Fraction x)
    {
       if(x.whole)     cout << x.whole << " ";
       if(x.numerator) cout << x.numerator << "/" 
                            << x.denominator;
    }
    
    //
    //   add two fractions
    //
    Fraction addFraction(Fraction a, Fraction b)
    {
       Fraction answer;
       int  common,worka,workb,numer,x;
    
       // Compute the common denominator
       common=a.denominator * b.denominator;
    
       // convert from mixed fractions.
       worka = a.whole * a.denominator + a.numerator;
       workb = b.whole * b.denominator + b.numerator;
    
       // compute the numerator
       numer = worka * b.denominator + workb * a.denominator;
    
       // save whole answer
       answer.whole= numer / common;
    
       // reduce to lowest term
       numer %= common;
       x=gcd(common,numer);
       answer.numerator=numer/x;
       answer.denominator=common/x;
    
       return answer;
    }
    
    //
    // Euler's Algorithm to find the greatest common divisor
    //
    int gcd(int a, int b)
    {
       if(b) return(gcd(b,a%b));
       else return a;
    }