Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Topics
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Topics

  • C++ (The Book)
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Thanks..
    37 weeks 2 days ago
  • Hmmm,Interesting one,thx for
    38 weeks 3 days ago
  • Buyer beware
    39 weeks 2 days ago
  • REPLY:Actual Processes Involved
    39 weeks 3 days ago
  • Back to Ruby
    48 weeks 4 days ago
  • Links provided
    1 year 12 weeks ago
  • Module for wordpress to Drupal 6.x
    1 year 12 weeks ago
  • The wordpress plugin looks
    1 year 13 weeks ago
  • Good point..... You're
    1 year 13 weeks ago
  • Many thanks. If I'm going to
    1 year 13 weeks ago

New forum topics

  • Imported posts only visible to user1
  • What should the port number be
  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
more

Sponsored links

Steve's Stuff
All about my running and from time to time other stuff

Improve Memory
All about memory techniques, excellent and relevant articles.

pace

Calculate Pace (Running Program) - # 2

Submitted by Steve on Sat, 23 Feb, 2008 - 10:29
  • C++
  • class
  • distance
  • objects
  • pace
  • time

I'm now into week # 14 of study, in which Classes and Objects have been introduced. (Studying from the book, C++ Primer Plus Fifth Edition, by Stephen Prata - about to commence chapter 12). In order to cement knowledge learned to date I decided to modify a program I wrote back in week 5. That program written calculates the pace of a run which is outputted as minutes per kilometre. It is only a one source code file program containing a few functions.

I've modified it, maybe modify is the wrong word, completely re-written it. This time basing the functionality upon Classes and Objects and purposely including multiple source code files (headers and definitions). Also in the process including a user decision as to the base unit (kilometres or miles).

The concept of Classes and Objects, though I understand them, opened my eyes more so towards the end of this little project. Originally, I only planned to report the pace in the units as given by user input, ie either 'mins per mile' or 'mins per km'. As I was finishing up I thought I might as well display the opposite or converted value. To my surprise, very few lines of code were required, the main bit being just another constructor passing in a reference of the user inputted details.

Without further ado, here's the code. I'll start with the main() program, hopefully you'll note that it's clean and easy to read, that's because all the work is done via the other files.

I'm open to any constructive criticism or suggestions and without further ado (again), here's the code.

/*
  Name: main.cpp
  Copyright:
  Author: Steven Taylor
  Date: 21/02/08 21:25
  Description: Main program calling classes to work out pace of a run
*/

#include <iostream>
#include "run01.h"
#include "functions.h"

using std::cout;
using std::cin;

int main()
{
    char active = 'y';
    run session;
    cout << "Enter details (q to quit):\n";
    while (active == 'y' || active == 'Y')
    {
        cin >> session;
        run convert(session);
        cout << "\n" << session << " or " << convert << "\n\n";
        cout << "Another session (y or n) ";
        cin >> active;
        // anything but Y or y assumes NO
    }
     // exit routine
    cout << "\n\n...Press ENTER to Exit System...";
    clear_buffer();
    cin.get();
    return 0;
}

File # 2

/*
  Name: run01.h
  Copyright:
  Author: Steven Taylor
  Date: 21/02/08 14:10
  Description: Header file for a run
*/

#ifndef RUN01_H_
#define RUN01_H_

class run
{
    private:
        static const int SECS_IN_MIN = 60;
        static const int MINS_IN_HOUR = 60;
        static const int SECS_IN_HOUR = 3600;
        static const double KM_PER_MILE = 1.60934;
        static const double MILES_PER_KM = 0.621371;
       
        // inputted values
        double dd;               // distance (km or miles)
        int hh;                  // hour component of time duration
        int mm;                  // minutes component of time duration
        int ss;                 // seconds component of time duration
        char unit;              // k = kilometre (default), m = miles
        // calculated values
        int mins;
        int secs;
    public:
        run();                  // default constructor
        run(double d, int h, int m, int s, char u = 'k');
        run(double d, int m, int s, char u = 'k');
        ~run();
        run(const run &r);      // converts to other unit of distance
               
        void set_run(double d, int h, int m, int s, char u = 'm');
        void set_run(double d, int m, int s, char u = 'k');
       
        void calculate_pace();
       
        void show_values();
             
       
        friend std::ostream & operator<<(std::ostream &os, const run &r);
        friend std::istream & operator>>(std::istream & is, run &r);
};
#endif

File # 3

/*
  Name: run01.cpp - definition file
  Copyright:
  Author: Steven Taylor
  Date: 21/02/08 14:56
  Description:
*/

#include <iostream>
#include "run01.h"
#include <fstream>
#include "functions.h"

run::run()
{
    dd = 0.0;
    hh = mm = ss = mins = secs = 0;
    unit = 'k';
}
run::run(double d, int h, int m, int s, char u)
{
    dd = d;
    hh = h;
    mm = m;
    ss = s;
    unit = u;
    calculate_pace();
}
run::run(double d, int m, int s, char u)
{
    dd = d;
    hh = 0;
    mm = m;
    ss = s;
    unit = u;
    calculate_pace();
}
run::run(const run &r)
{
    if (r.unit == 'k')
    {
        unit = 'm';
        dd = r.dd / KM_PER_MILE;
    }
    else
    {
        unit = 'k';
        dd = r.dd / MILES_PER_KM;
    }
    hh = r.hh;
    mm = r.mm;
    ss = r.ss;
    calculate_pace();
}
run::~run()
{}
void run::set_run(double d, int h, int m, int s, char u)
{
    dd = d;
    hh = h;
    mm = m;
    ss = s;
    unit = u;
    calculate_pace();
}
void run::set_run(double d, int m, int s, char u)
{
    dd = d;
    hh = 0;
    mm = m;
    ss = s;
    unit = u;
    calculate_pace();
}

void run::calculate_pace()
{
    int total_secs = (hh * SECS_IN_HOUR) + (mm * SECS_IN_MIN) + ss;
    int secs_per_unit = ((double(total_secs) / dd) + 0.5);
     
    if (secs_per_unit > 0)
    {
        mins = secs_per_unit / MINS_IN_HOUR;
        secs = secs_per_unit % MINS_IN_HOUR;
    }
}
void run::show_values()
{
    std::ofstream fout;
    fout.open("debug.txt");
    fout << "hh = " << hh << " mm = " << mm << " ss = " << ss << " unit = " << unit << "\n";
    fout << "mins = " << mins << " secs = " << secs << "\n";
}
   
std::ostream & operator<<(std::ostream &os, const run &r)
{
    os << "Pace : ";
    os << r.mins << ":";
    if (r.secs < 10)
        os << "0";
    os << r.secs;
    if (r.unit == 'k')
        os << " min/km";
    else
        os << " min/mile";
    return os;
}
   
std::istream & operator>>(std::istream & is, run &r)
{
    std::cout << "Unit of distance <k>ilometres or <m>iles: ";
    if (is >> r.unit)
    {
        switch (r.unit)
        {
            case 'M':
            case 'm':
                r.unit = 'm';
                break;
            default :
                r.unit = 'k';
         }
    }
   
    std::cout << "Distance ";
    if (r.unit == 'k')
        std::cout << "(km): ";
    else
        std::cout << "(miles): ";
   
    while (!(is >> r.dd))
    {
        std::cout << "-Distance : ";
        clear_buffer();
    }
         
    std::cout << "Hours:   ";
    while (!(is >> r.hh))
    {
        std::cout << "\n-Hours: ";
        clear_buffer();
    }
   
    std::cout << "Minutes: (0 to 59): ";
    while (!(is >> r.mm) || r.mm >= r.MINS_IN_HOUR)
    {
        std::cout << "\n-Minutes: (0 to 59): ";
        clear_buffer();
    }
     
    std::cout << "Seconds: (0 to 59): ";
    while (!(is >> r.ss) || r.ss >= r.SECS_IN_MIN)
    {
        std::cout << "\n-Seconds: (0 to 59): ";
        clear_buffer();
    }
   
    r.calculate_pace();

    return is;
}

File # 4

/*
  Name: functions.h
  Copyright:
  Author: Steven Taylor
  Date: 22/02/08 16:21
  Description: General functions file
*/

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

void clear_buffer();
#endif

File # 5

/*
  Name: functions.cpp
  Copyright:
  Author: Steven Taylor
  Date: 22/02/08 16:24
  Description: General function(s) definitions
*/

#include <iostream>
#include "functions.h"
using std::cin;

void clear_buffer()
{
    cin.clear();
    while (cin.get() != '\n')
        continue;
}

So that's it, what do you think?

  • Add new comment
  • Read more
  • 846 reads
  • 1 attachment

Calculate Pace (Running Program)

Submitted by Steve on Sun, 23 Dec, 2007 - 20:39
  • C++
  • distance
  • pace
  • structure
  • time

During week 5 of study, learning functions and in particular references passed to and from functions. The following program, in a way, cemented some of the stuff studied. It wasn't a part of the book but since I'm interested in running, I thought I'd write a little program that calculates the average pace of a run given the distance (km) and duration (time) of the run.

  • 1 comment
  • Read more
  • 982 reads
  • 1 attachment

 Subscribe in a reader

free hit counter


RoopleTheme