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.
struct course
{
int students;
float average;
};
course mw;
course rgc[10];
course *ms;
mw.students= 23; mw.students = rgc[2].students + 1; (*ms).students=12;
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;
course *c; (*c).average =34.5; note not *c.average c->average = 3.14;
course mw = {32,0.0};
sizeof(course);
course rgc[30];
struct line
{
char data[80];
line *next;
};
line start;
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 continueHeader Module Sfract.h
//
// Fractions Program in Strucures.
//
#ifndef SFRACT
#define SFRACT
struct Fract {
int whole;
int numerator;
int denominator;
};
void displayFract(Fract);
int getFract(Fract&);
Fract addFract(Fract, Fract);
int gcd(int,int);
#endif
Main Module Sfract.cpp
//
// main
//
#include <iostream.h>
#include "Sfract.h"
void main()
{
Fract 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(getFract(item)){
cout << endl << "Adding ";
displayFract(item);
cout << " + ";
displayFract(sum);
sum=addFract(sum,item);
cout << " = ";
displayFract(sum);
cout << endl;
}
}
Code Module SfractM.cpp
//
// Modules
//
#include "Sfract.h"
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
//
// Get a Fraction
//
int getFract(Fract& 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 displayFract(Fract x)
{
if(x.whole) cout << x.whole << " ";
if(x.numerator) cout << x.numerator << "/" << x.denominator;
}
//
// add two fractions
//
Fract addFract(Fract a, Fract b)
{
Fract 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'sAlgorithm to find the greatest common divisor
//
int gcd(int a, int b)
{
if(b) return(gcd(b,a%b));
else return a;
}