// tests class Dial
// tdc 23/01/2000
// set editor tabstops to 3
// Change the value of myname. Do _not_ change any other code in this file.
#ifndef dial_h
#include "dial.h"
#endif
#include <iostream>
#include <iomanip>
#include <cstdlib>
const int longring = 24;
const int shortring = 12;
const char myname[] = "Lez Bullwer & Mingguang Lui";
int main( int argc, char* argv[] )
{
void show( const Dial&, const Dial& ); // display the time
void alarm( int ); // ring the alarm
Dial bedtime_hrs( 24, 23 ), bedtime_mins( 60, 0 ),
alarm_hrs = Dial( 24, 7 ), alarm_mins,
snooze_hrs = alarm_hrs,
snooze_mins;
alarm_mins = bedtime_mins;
snooze_mins.set( 60, 30 );
if( argc != 2 )
{
cout << "incorrect number of arguments" << endl;
cout << "usage: testdial <minutes increment>" << endl;
return 1;
}
int increment = atoi( argv[1] ); // single arg is increment in minutes
if( increment < 0 || increment > 1440 )
{
cout << "invalid argument - must be positive and < 1440" << endl;
return 2;
}
cout << myname << endl;
cout << "bedtime = "; show( bedtime_hrs, bedtime_mins );
cout << "alarm set for "; show( alarm_hrs, alarm_mins );
cout << "snooze set for "; show( snooze_hrs, snooze_mins );
cout << "minutes increment: " << increment << endl;
bool timetogetup = ( bedtime_hrs == alarm_hrs ) &&
( bedtime_mins == alarm_mins );
while ( !timetogetup )
{
bedtime_mins += increment;
bedtime_hrs += bedtime_mins.passedtop();
show( bedtime_hrs, bedtime_mins );
timetogetup = ( bedtime_hrs == alarm_hrs ) &&
( bedtime_mins == alarm_mins );
}
alarm( longring );
bool timemustgetup = ( bedtime_hrs == snooze_hrs ) &&
( bedtime_mins == snooze_mins );
while ( !timemustgetup )
{
bedtime_mins += increment;
bedtime_hrs += bedtime_mins.passedtop();
show( bedtime_hrs, bedtime_mins );
timemustgetup = ( bedtime_hrs == snooze_hrs ) &&
( bedtime_mins == snooze_mins );
}
alarm( shortring );
return 0;
}
void alarm ( int rings )
{
cout << 'B';
while( rings-- > 0 )
cout << 'r';
cout << endl;
}
void show( const Dial& a, const Dial& b )
{
cout << setw(2) << setfill('0') << a.value() << ":"
<< setw(2) << b.value() << endl;
}
// tests class clock24
// tdc 23/01/2000
// set editor tabstops to 3
// Do not change this file except the value of myname and increment
#include <iostream>
#include <cstdlib>
#ifndef clock24_h
#include "clock24.h"
#endif
const int shortring = 12;
const int longring = 24;
const char *myname = "Lez Bullwer & Mingguang Liu";
int main( int argc, char *argv[] )
{
void soundalarm( int );
void show( char*, const Clock24& );
void getprogname( char*, char* );
if ( argc != 2 )
{
char progname[30];
getprogname( argv[0], progname );
cout << "incorrect number of arguments" << endl
<< "usage: " << progname << " <minutes increment>" << endl;
return 1;
}
const int increment = atoi( argv[1] );
if( increment < 0 || increment >= 1440 )
{
cout << "invalid argument - must be positive and < 1440" << endl;
return 2;
}
Clock24 bedtime( 23,0 ), alarm, snooze = alarm;
snooze = alarm = bedtime;
alarm.set( 7, 0 );
snooze.set( 7, 30 );
cout << myname << endl;
show( "bedtime = ", bedtime );
show( "alarm set for ", alarm );
show( "snooze set for ", snooze );
cout << "minutes increment: " << increment << endl;
int minutestosleep = alarm - bedtime;
while ( minutestosleep > 0 )
{
bedtime+= increment;
cout << "time = " << bedtime << endl;
minutestosleep-= increment;
}
soundalarm( shortring );
int minutestosnooze = snooze - bedtime;
while ( minutestosnooze > 0 )
{
bedtime+= increment;
cout << "time = " << bedtime << endl;
minutestosnooze-= increment;
}
soundalarm( longring );
cout << endl << endl;
return 0;
}
void soundalarm( int rings )
{
cout << 'B';
while( rings-- > 0 )
cout << 'r';
cout << endl;
}
void show( char comment[], const Clock24& clk )
{
cout << comment << clk << endl;
}
void getprogname( char* pathname, char* progname )
// extracts the file name from a path name
// pre - pathname contains the full path name
// post - progname contains the name of the program excluding the .exe
{
char *a = pathname + strlen(pathname) - 1; // go to end of pathname
char *p = progname, *q = progname;
while ( *a != '.' ) // look back for the dot
a--;
*a = '\0'; // terminate it (don't want the .exe bit)
a--;
while ( a >= pathname && *a != '\\' && *a != '/' && *a != ':' )
a--; // look back for the slash or the start of the path
a++;
q = a;
for ( ; *a; a++, p++ )
*p = ( *a + 'a' - 'A' ); // copy into & change to lower case
*p = '\0'; // terminate it (probably not necessary)
strcpy( progname, q ); // copy into 2nd arg.
}
// header for dial.cpp
// OOPD Assignment 1
// Lez Bullwer & Mingguang Liu
// 24/02/00
#ifndef Dial_h
#define Dial_h
#include <iostream.h>
class Dial
{
public:
Dial (); // default constructor
Dial (int, int); // conversion constructor
Dial (const Dial&); // copy constructor
~Dial(); // destructor
void set (int, int); // member function, set position
int value() const;
int passedtop(); // advance time function
bool operator==(const Dial&) const; // comparsion operator
Dial& operator+=(int);
friend Dial operator+(const Dial&, const Dial&);
friend ostream& operator<<(ostream&, const Dial&);
private:
int dialdivision, position, advance;
};
#endif
// implementation of dial.h
// OOPD Assignment 1
// Lez Bullwer & Mingguang Liu
// 24/02/00
#include"dial.h"
#include<iostream.h>
Dial::Dial() // default constructor
: dialdivision(0), position(0), advance(0) {}
Dial::Dial(int a, int b) // conversion constructor
: dialdivision(a), position(b), advance(0) {}
Dial::Dial (const Dial& b) // copy constructor
{
dialdivision = b.dialdivision;
position = b.position;
advance = b.advance;
}
Dial::~Dial() // destructor
{
}
void Dial::set(int div, int m)
{
dialdivision = div;
position = m;
}
int Dial::value() const // show current position
{
return position;
}
int Dial::passedtop()
{
if (advance == 0)
advance = position/dialdivision; // adjust advance position
position %= dialdivision; // checking gone over the top
return (advance);
}
bool Dial::operator==(const Dial& b) const // comparison operator
{
if (dialdivision != b.dialdivision)
return false; // not the same sort of dial
else
return (position == b.position); // return comparison
}
Dial& Dial::operator+=(int d) // addition & assign operator
{
position += d; // increment position
advance = position/dialdivision; // adjust advance position
position %= dialdivision; // checking gone over the top
return(*this);
}
Dial operator+(const Dial& a, const Dial& b)
{
Dial temp=a;
temp.position += b.position;
return temp;
}
ostream& operator<<(ostream& strm, const Dial& b)
{
strm << b.position;
return strm;
}
// header for clock24.cpp // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #ifndef clock24_h #define clock24_h #include"dial.h" #include<iostream> class Clock24 { public: Clock24 (); // defacult constructor Clock24 (int, int); // conversion constructor Clock24 (const Clock24&); // copy constructor ~Clock24(); // destructor void set (int, int); // set time Clock24& operator+=(int); friend int operator-(const Clock24&, const Clock24&); friend ostream& operator<<(ostream&, const Clock24&); private: Dial hours, minutes; }; #endif
// implementation of clock.h // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #include "Clock24.h" #include<iostream> #include<iomanip> Clock24::Clock24() // default constructor : hours(24, 0), minutes(60, 0) {} Clock24::Clock24 (int a, int b) // conversion constructor : hours(24, a), minutes(60, b) {} Clock24::Clock24 (const Clock24& b) // copy constructor { hours = b.hours; minutes = b.minutes; } Clock24::~Clock24() // destructor { } void Clock24::set (int a, int b) // set Clock time { hours.set(24, a); minutes.set(60, b); } Clock24& Clock24::operator+=(int d) // addition & assign operator { minutes += d; hours += minutes.passedtop(); return (*this); } int operator-(const Clock24& a, const Clock24& b) // subtraction to give total mins { int temp; if ( (a.hours.value() - b.hours.value()) >= 0 ) temp = ( a.hours.value() * 60 + a.minutes.value() ) - ( b.hours.value() * 60 + b.minutes.value() ); else temp = ( a.hours.value() * 60 + a.minutes.value() ) - ( b.hours.value() * 60 + b.minutes.value() ) + 24 * 60; return temp; } ostream& operator << (ostream& strm, const Clock24& b) { strm << setw(2) << setfill('0') << b.hours << ":" << setw(2) << b.minutes; return strm; }
// header for imprl.cpp // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #ifndef imprl_h #define imprl_h #include"dial.h" #include<iostream> class Imperial { public: Imperial (); // default constructor Imperial (int, int); // conversion constructor Imperial (int); // another conversion constructor Imperial (const Imperial&); // copy constructor ~Imperial(); // destructor void set(int, int); // set reading Imperial& operator=(const Imperial&); friend Imperial operator+(const Imperial&, const Imperial&); friend int operator-(const Imperial&, const Imperial&); friend Imperial& operator+=(Imperial&, const Imperial&); friend float operator*(const float, const Imperial&); friend ostream& operator<<(ostream&, const Imperial&); private: Dial yards, feet, inches; }; #endif
// implementation of imprl.h // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #include "imprl.h" #include<iostream> #include<iomanip> const int YRDS = 100, FT = 3, INCH = 12; Imperial::Imperial() // default constructor : yards(YRDS, 0), feet(FT, 0), inches(INCH, 0) {} Imperial::Imperial(int a_feet, int a_inch) // conversion constructor { int x_inch = 0, x_feet = 0, x_yard = 0; x_inch = a_inch; inches.set(12, a_inch); x_feet = inches.passedtop(); x_feet += a_feet; feet.set(3, x_feet); x_yard = feet.passedtop(); yards.set(100, x_yard); } Imperial::Imperial(int a) //another conversion constructor : yards(YRDS, 0), feet(FT, 0), inches(INCH, a) {} Imperial::Imperial(const Imperial& x) // copy constructor { yards = x.yards; feet = x.feet; inches = x.inches; } Imperial::~Imperial() // destructor { } void Imperial::set(int a_feet, int a_inch) { int x_inch = 0, x_feet = 0, x_yard = 0; inches.set(12, a_inch); x_feet += inches.passedtop(); x_feet += a_feet; feet.set(3, x_feet); x_yard += feet.passedtop(); yards.set(100, x_yard); } Imperial& Imperial::operator=(const Imperial& x) // assign operator { if (this == &x) return *this; inches.set(INCH, x.inches.value()); feet.set(FT, x.feet.value()); yards.set(YRDS, x.yards.value()); return (*this); } Imperial operator+(const Imperial& a, const Imperial& b) { Imperial temp; int totalincha, totalinchb, total ; totalincha = a.inches.value() + a.feet.value() * INCH + a.yards.value() * INCH * FT; totalinchb = b.inches.value() + b.feet.value() * INCH + b.yards.value() * INCH * FT; total = totalincha + totalinchb; temp.inches.set(INCH, total % INCH); total /= INCH; temp.feet.set(FT, total % FT); total /= FT; if (total >= 100) cout << "over 100 yards - restarting count" << endl; temp.yards.set(YRDS, (total % YRDS)); return temp; } int operator-(const Imperial& a, const Imperial& b) { int temp, totala, totalb; totala = a.inches.value() + a.feet.value() * INCH + a.yards.value() * INCH * FT; totalb = b.inches.value() + b.feet.value() * INCH + b.yards.value() * INCH * FT; if (totala > totalb) temp = totala - totalb; else temp = totalb - totala; return temp; } Imperial& operator+=(Imperial& a, const Imperial& b) { Imperial temp; int totalincha, totalinchb, total ; totalincha = a.inches.value() + a.feet.value() * INCH + a.yards.value() * INCH * FT; totalinchb = b.inches.value() + b.feet.value() * INCH + b.yards.value() * INCH * FT; total = totalincha + totalinchb; a.inches.set(INCH, total % INCH); total /= INCH; a.feet.set(FT, total % FT); total /= FT; if (total >= 100) cout << "over 100 yards - restarting count" << endl; a.yards.set(YRDS, (total % YRDS)); return a; } float operator*(float flt, const Imperial& x) // multiply operator { float temp; int total; total = x.inches.value() + (x.feet.value() * 12) + (x.yards.value() * 3 * 12); temp = flt * total; return temp; } ostream& operator << (ostream& strm, const Imperial& x) { strm << x.yards << " yds " << x.feet << " ft " << x.inches << " ins"; return strm; }
// tests class imperial - converts from imperial to metric // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #include <iostream> #include <iomanip> #include <cstdlib> #include <ctype.h> #ifndef imprl_h #include "imprl.h" #endif int main( void ) { void show( const float inMetres ); void show( const Imperial& totalout ); bool okayToAdd( const int a, const int b ); int yards, feet, inches; bool anotheradd = true; const float convert = 0.0254; // convert from inches to metres cout << "Imperial addition & conversion to Metric " << endl; cout << "---------------------------------------- " << endl; cout << "Enter length in feet and inches (e.g. 2 6): " << endl; cout << "*** Positive values only ***" << endl; cout << "Length : "; if (!((cin>>feet) && (cin>>inches) && (okayToAdd(feet, inches))) ) { //check input is numeric and within limits cout << "Exiting" << endl; return 0; } Imperial length1(feet, inches); Imperial length2, totallength(length1), subtotal; cout << "Length (non-numeric to finish): "; while ( ((cin>>feet) && (cin>>inches)) && (okayToAdd(feet, inches)) ) { // check input is numeric and within limits length2.set(feet, inches); totallength += length2; show(totallength); cout << "Length (non-numeric to finish): "; } // convert to metric float metres; metres = convert * totallength; show(totallength); show(metres); return 0; } void show( const Imperial& totalout ) { cout << totalout << endl; } void show( const float inMetres ) { cout << " in metres: " << setprecision(6) << setw(4) << inMetres; } bool okayToAdd( const int ft, const int inc ) { return ( (ft >= 0 && inc >= 0) ); }
// tests class imperial - converts from imperial to metric // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #include <iostream> #include <iomanip> #include <cstdlib> #ifndef imprl_h #include "imprl.h" #endif int main( void ) { bool okayToAdd( const int inc ); bool okayForTravel( const int inc ); bool okayForTime( const int inc ); void errormessage(char comment[]); void show( const Imperial& lgth ); int n_yards, n_feet, n_inches, hours; const float convert = 0.0254; // convert from inches to metres char *start, *travel, *time; start = "Enter start position in inches ( 0-11 ): "; travel = "How far does the worm travel per hour? (in inches): "; time = "How many hours does the worm travel?: "; cout << start; while ( (!(cin>>n_inches) || cin.peek() !='\n') || (!okayToAdd(n_inches)) ) // need to check input is number and not greater than 11 { errormessage(start); cin.clear(); cin.ignore(1000, '\n'); } Imperial length1(n_inches); Imperial length2(length1), totallength(); cout << travel; while ( (!(cin>>n_inches) || cin.peek() !='\n') || (!okayForTravel(n_inches)) ) // need to check input is a number and within limits { errormessage(travel); cin.clear(); cin.ignore(1000, '\n'); } cout << time; while ( (!(cin>>hours) || cin.peek() !='\n') || (!okayForTime(hours))) // need to check input is a number and within limits { errormessage(time); cin.clear(); cin.ignore(1000, '\n'); } for (int i = 0; i < hours; i++) { length2 += n_inches; } // convert to metric float metres; metres = convert * length2; cout << "Worm travelled from: "; show(length1); cout << "Worm travelled to: "; show(length2); int total; total = length2 - length1; cout << "Distance in inches: " << total << endl; cout << "In metres this is: " << metres << endl; if (metres > 2.0) cout << " ... and the worm was very tired!!!!!!" << endl; return 0; } void show( const Imperial& lgth ) { cout << lgth << endl; } void show( const float inMetres ) { cout << " in metres: " << setprecision(6) << setw(4) << inMetres; } bool okayToAdd( const int inc ) //check within limits { return ( (inc >= 0 && inc < 12) ); } bool okayForTravel( const int inc ) //check within limits { if (inc > 50) cout << "A worm can't go that fast!!" << endl; return ( (inc >= 0 && inc < 51) ); } bool okayForTime( const int inc ) //check within limits { if (inc > 24) cout << "A worm can't crawl for that long!!" << endl; return ( (inc >= 0 && inc < 25) ); } void errormessage(char comment[]) { cout << "Incorrect input. Try again " << endl; cout << comment ; }
// header for meter.cpp // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #ifndef meter_h #define meter_h #include"dial.h" #include<iostream> class Meter { public: Meter (); // default constructor Meter (int, int, int, int); // conversion constructor Meter (const Meter&); // copy constructor ~Meter(); // destructor void set (int, int, int, int); // set meter reading Meter& operator+=(int); friend int operator-(const Meter&, const Meter&); friend ostream& operator<<(ostream&, const Meter&); private: Dial thou, hund, ten, unit; }; #endif
// implementation of meter.h // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #include "meter.h" #include<iostream> #include<iomanip> const int MAX = 10; Meter::Meter() // default constructor : thou(MAX, 0), hund(MAX, 0), ten(MAX,0), unit(MAX,0) {} Meter::Meter (int a, int b, int c, int d) // conversion constructor : thou(MAX, a), hund(MAX, b), ten(MAX,c), unit(MAX,d) {} Meter::Meter (const Meter& x) // copy constructor { thou = x.thou; hund = x.hund; ten = x.ten; unit = x.unit; } Meter::~Meter() // destructor { } void Meter::set (int a, int b, int c, int d) // set meter reading { thou.set(MAX, a); hund.set(MAX, b); ten.set(MAX, c); unit.set(MAX, d); } Meter& Meter::operator+=(int someunits) // addition & assign operator { unit += someunits; ten += unit.passedtop(); hund += ten.passedtop(); thou += hund.passedtop(); return (*this); } int operator-(const Meter& a, const Meter& b) // subtraction give total units { int temp, value1, value2; value1 = a.thou.value() * 1000 + a.hund.value() * 100 + a.ten.value() * 10 + a.unit.value(); value2 = b.thou.value() * 1000 + b.hund.value() * 100 + b.ten.value() * 10 + b.unit.value(); if ( ( value1 - value2 >= 0 ) ) //a.thou.value() - b.thou.value()) > 0 ) { //which value is larger temp = value1 - value2; } else { temp = (value1 + 10000) - value2; } return temp; } ostream& operator << (ostream& strm, const Meter& x) { strm << setw(1) << setfill('0') << x.thou << x.hund << x.ten << x.unit; return strm; }
// tests class Meter - gives reading and cost // OOPD Assignment 1 // Lez Bullwer & Mingguang Liu // 16/03/2000 #include <iostream> #include <iomanip> #include <cstdlib> #include <ctype.h> #ifndef meter_h #include "meter.h" #endif int main( int argc, char *argv[] ) { void errormessage(char comment[]); bool isPositive( const int inc ); bool argsAreLetters( int argc, char *argv[] ); void getprogname( char*, char* ); int unitsperday, numofdays; int costperunit = 8; //cost in pence if ( (argsAreLetters( argc, argv ) ) || (argc != 5) ) { char progname[30]; getprogname( argv[0], progname ); cout << "incorrect number of arguments" << endl << "current reading required (4 digits) as follows: " << endl << "usage: " << progname << " <thousands hundreds tens units>" << endl << "e.g. if current reading = 1234 then enter: " << endl << "elecmetr 1 2 3 4" << endl; return 1; } int thousands, hundreds, tens, units; char ch_zero = '0'; char ch1 = *argv[1], ch2 = *argv[2], ch3 = *argv[3], ch4 = *argv[4]; thousands = ch1 - ch_zero; hundreds = ch2 - ch_zero; tens = ch3 - ch_zero; units = ch4 - ch_zero; Meter electMeter(thousands, hundreds, tens, units); Meter electPrevious(electMeter); char *unitsused, *daystorun; unitsused = "no. of units used per day? (< 100) : "; cout << unitsused ; while ( (!(cin>>unitsperday) || cin.peek() !='\n') || (!isPositive(unitsperday)) ) // need to check input is a number & within limits { errormessage(unitsused); cin.clear(); cin.ignore(1000, '\n'); } daystorun = "no. of days to run? (< 100) : "; cout << daystorun; while ( (!(cin>>numofdays) || cin.peek() !='\n') || (!isPositive(numofdays)) ) // need to check input is a number & within limits { errormessage(daystorun); cin.clear(); cin.ignore(1000, '\n'); } for (int i = 0; i < numofdays; i++) { electMeter += unitsperday; } int units_used = electMeter - electPrevious; int totalpound, totalpence; totalpound = (units_used * costperunit) / 100; totalpence = (units_used * costperunit) % 100; cout << endl; cout << "Previous reading: " << electPrevious << endl; cout << "Current reading: " << electMeter << endl; cout << "Units used: " << setfill(' ') << setw(4) << units_used << endl; cout << "Cost per unit: £ 0." << setfill('0') << setw(2) << costperunit << endl; cout << " ======= " << endl; cout << "Total cost of units used: £" << setfill(' ') << setw(3) << totalpound << "." << setfill ('0') << setw(2) << totalpence <<endl; return 0; } void errormessage(char comment[]) { cout << "Incorrect input. Try again " << endl; cout << comment ; } bool isPositive( const int inc ) //check value within limits { return ( (inc >= 0 && inc < 100) ); } bool argsAreLetters( int argc, char *argv[] ) // check arguments are numbers { char ch; for ( int i = 1; i < argc; i++) { ch = char(*argv[i]); if ( !(isdigit(ch)) ) return true; } return false; } void getprogname( char* pathname, char* progname ) // extracts the file name from a path name // pre - pathname contains the full path name // post - progname contains the name of the program excluding the .exe { char *a = pathname + strlen(pathname) - 1; // go to end of pathname char *p = progname, *q = progname; while ( *a != '.' ) // look back for the dot a--; *a = '\0'; // terminate it (don't want the .exe bit) a--; while ( a >= pathname && *a != '\\' && *a != '/' && *a != ':' ) a--; // look back for the slash or the start of the path a++; q = a; for ( ; *a; a++, p++ ) *p = ( *a + 'a' - 'A' ); // copy into & change to lower case *p = '\0'; // terminate it (probably not necessary) strcpy( progname, q ); // copy into 2nd arg. }