home *** CD-ROM | disk | FTP | other *** search
- _C PROGRAMMING COLUMN_
- by Al Stevens
-
- [LISTING ONE]
-
- /* --------- auto.h ------------ */
-
- /*
- * simple data element classes
- */
- typedef long odometer;
- typedef double Dollars;
-
- /*
- * a simple date class
- */
- typedef struct {
- int mo, da, yr;
- } date;
-
- /*
- * Automobile class
- */
- typedef struct {
- int modelyear;
- char manufacturer[20];
- char license[9];
- } Automobile;
-
- /*
- * Trip_Record class
- */
- typedef struct {
- date trip_date;
- odometer odometer_in;
- odometer odometer_out;
- int gallons;
- Dollars cost;
- } Trip_Record;
-
-
- [LISTING TWO]
-
- /* --------- auto.c ------------ */
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "auto.h"
-
- static Automobile get_car(void);
- static void reportauto(Automobile);
- char *display_date(date dt);
- static Trip_Record get_trip(void);
- static void reporttrip(Trip_Record);
-
- void main()
- {
- while (1) {
- /* ---- a report for each car in the input ----- */
- Automobile car = get_car();
- Trip_Record trip_totals = {{0,0,0},0,0,0,0};
-
- if (car.modelyear == 0)
- break;
- reportauto(car);
- /* -------- prepare the detail report ---------- */
- printf("\n Date miles gas cost mpg cost/mile");
- printf("\n-------- ----- ----- -------- ----- ---------");
- while (1) {
- /* ----- get the next Trip_Record from the input ---- */
- Trip_Record trip = get_trip();
- if (trip.trip_date.mo == 0)
- break;
- /* -------- report the trip ----------- */
- reporttrip(trip);
- /* -------- collect totals for this car -------- */
- trip_totals.odometer_in =
- max(trip_totals.odometer_in, trip.odometer_in);
- trip_totals.odometer_out = trip_totals.odometer_out ?
- min(trip_totals.odometer_out, trip.odometer_out) :
- trip.odometer_out;
- trip_totals.gallons += trip.gallons;
- trip_totals.cost += trip.cost;
- }
- reporttrip(trip_totals);
- }
- }
-
- /* ---------- get a car's description from the input -------- */
- static Automobile get_car(void)
- {
- static Automobile car;
-
- car.modelyear = 0;
- scanf("%s", car.license);
- if (strcmp(car.license, "end") != 0) {
- scanf("%d", &car.modelyear);
- scanf("%s", car.manufacturer);
- }
- return car;
- }
-
- static void reportauto(Automobile car)
- {
- printf("\n\n%d %s License # %s\n",
- car.modelyear, car.manufacturer, car.license);
- }
-
- /* ------ get a trip record from the input stream ----- */
- static Trip_Record get_trip(void)
- {
- date dt;
- char strdate[9];
- odometer oin = 0, oout = 0;
- int gas = 0;
- Dollars cost = 0;
- static Trip_Record trip;
-
- dt.mo = 0;
- scanf("%s", strdate);
- if (strcmp(strdate, "end")) {
- dt.mo = atoi(strdate);
- dt.da = atoi(strdate + 3);
- dt.yr = atoi(strdate + 6);
- scanf("%ld", &oin);
- scanf("%ld", &oout);
- scanf("%d", &gas);
- scanf("%lf", &cost);
- }
- trip.trip_date = dt;
- trip.odometer_in = oin;
- trip.odometer_out = oout;
- trip.gallons = gas;
- trip.cost = cost;
- return trip;
- }
-
- static void reporttrip(Trip_Record trip)
- {
- int miles = (int) (trip.odometer_in - trip.odometer_out);
- printf("\n%s %5d %5d %#8.2f %5d %#8.2f",
- trip.trip_date.mo ?
- display_date(trip.trip_date) :
- "totals ",
- miles,
- trip.gallons,
- trip.cost,
- trip.gallons ? miles / trip.gallons : 0,
- miles ? trip.cost / miles : 0
- );
- }
-
- char *display_date(date dt)
- {
- static char d[9];
- sprintf(d, "%2d-%02d-%02d", dt.mo, dt.da, dt.yr);
- return d;
- }
-
-
- [LISTING THREE]
-
- // auto.hpp
-
- // -----------------------------
- // simple data element classes
- // -----------------------------
- typedef long odometer;
- typedef double Dollars;
-
- // -----------------------------
- // a simple date class
- // -----------------------------
- struct date {
- int mo, da, yr;
- char *display(void);
- };
-
- // -----------------------------
- // Automobile class
- // -----------------------------
- class Automobile {
- private:
- int modelyear;
- char *manufacturer;
- char *license;
- public:
- Automobile(char *license, int year, char *make);
- Automobile();
- ~Automobile();
- char *display(void);
- int nullcar(void) {return modelyear == 0;}
- };
-
- // -----------------------------
- // Trip_Record class
- // -----------------------------
- class Trip_Record {
- private:
- date trip_date;
- odometer odometer_in;
- odometer odometer_out;
- int gallons;
- Dollars cost;
- public:
- Trip_Record(void);
- Trip_Record(date, odometer, odometer, int, Dollars);
- int mileage(void) {return odometer_in - odometer_out;}
- char *display(void);
- int nulltrip(void) {return trip_date.mo == 0;}
- void operator += (Trip_Record&);
- };
-
-
- [LISTING FOUR]
-
- // auto.cpp
-
- #include <stream.hpp>
- #include <string.h>
- #include <stdlib.h>
- #include "auto.hpp"
-
- char *date::display()
- {
- return form("%2d-%02d-%02d", mo, da, yr);
- }
-
- // --------------- constructors ---------------------------
- Automobile::Automobile()
- {
- modelyear = 0;
- manufacturer = license = NULL;
- }
-
- Automobile::Automobile(char *lic, int year, char *make)
- {
- license = new char[strlen(lic)+1];
- strcpy(license, lic);
- modelyear = year;
- manufacturer = new char[strlen(make)+1];
- strcpy(manufacturer, make);
- }
-
- // -------------- destructor -------------------
- Automobile::~Automobile()
- {
- if (license != NULL)
- delete license;
- if (manufacturer != NULL)
- delete manufacturer;
- }
-
- char *Automobile::display()
- {
- return form("%d %s License # %s",
- modelyear, manufacturer, license);
- }
-
- #define max(a,b) ((a)>(b)?(a):(b))
- #define min(a,b) ((a)<(b)?(a):(b))
-
- void Trip_Record::operator += (Trip_Record& trip)
- {
- this->odometer_in = max(this->odometer_in, trip.odometer_in);
- this->odometer_out = this->odometer_out ?
- min(this->odometer_out, trip.odometer_out) :
- trip.odometer_out;
- this->gallons += trip.gallons;
- this->cost += trip.cost;
- }
-
- // --------------- constructors ---------------------------
- Trip_Record::Trip_Record()
- {
- trip_date.mo = trip_date.da = trip_date.yr = 0;
- odometer_in = odometer_out = 0;
- gallons = 0;
- cost = 0;
- }
-
- Trip_Record::Trip_Record(date trdt, odometer oin, odometer oout,
- int gas, Dollars cst)
- {
- trip_date = trdt;
- odometer_in = oin;
- odometer_out = oout;
- gallons = gas;
- cost = cst;
- }
-
- char *Trip_Record::display()
- {
- int miles = mileage();
- return form("%s %5d %5d %#8.2f %5d %#8.2f",
- trip_date.mo ? trip_date.display() : "totals ",
- miles,
- gallons,
- cost,
- gallons ? miles / gallons : 0,
- miles ? cost / miles : 0
- );
- }
-
-
- [LISTING FIVE]
-
- // autorpt.cpp
-
- #include <stream.hpp>
- #include <string.h>
- #include <stdlib.h>
- #include "auto.hpp"
-
- // =========================================================
- // automobile operating costs system
- // =========================================================
-
- // ------- prototypes
- static Automobile& get_car(void);
- static Trip_Record& get_trip(void);
- static void report(Automobile&);
- static void report(Trip_Record&);
-
- main()
- {
- while (1) {
- // -------- a report for each car in the input
- Automobile car = get_car();
- if (car.nullcar())
- break;
- report(car);
- // -------- prepare the detail report
- cout << "\n Date miles gas cost mpg cost/mile";
- cout << "\n-------- ----- ----- -------- ----- ---------";
- // ---------- a Trip_Record to hold the totals
- Trip_Record trip_totals;
-
- while (1) {
- // -------- get the next Trip_Record from the input
- Trip_Record trip = get_trip();
- if (trip.nulltrip())
- break;
- // -------- report the trip
- report(trip);
- // -------- collect totals for this car
- trip_totals += trip;
- }
- report(trip_totals);
- }
- }
-
- // ---------- get a car's description from the input
- static Automobile& get_car(void)
- {
- char license[7];
- int modelyear = 0;
- char make[25] = "";
-
- cin >> license;
- if (strcmp(license, "end") != 0) {
- cin >> modelyear;
- cin >> make;
- }
- Automobile *car = new Automobile(license, modelyear, make);
- return *car;
- }
-
- static void report(Automobile& car)
- {
- cout << "\n\n" << car.display() << '\n';
- }
-
- // ---------- get a trip record from the input stream
- static Trip_Record& get_trip()
- {
- date dt;
- dt.mo = 0;
- char strdate[9];
- odometer oin = 0, oout = 0;
- int gas = 0;
- Dollars cost = 0;
-
- cin >> strdate;
- if (strcmp(strdate, "end")) {
- dt.mo = atoi(strdate);
- dt.da = atoi(strdate + 3);
- dt.yr = atoi(strdate + 6);
- cin >> oin;
- cin >> oout;
- cin >> gas;
- cin >> cost;
- }
- Trip_Record *trip = new Trip_Record(dt,oin, oout, gas, cost);
- return *trip;
- }
-
- static void report(Trip_Record& trip)
- {
- cout << '\n' << trip.display();
- }