« January 2019 »
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
You are not logged in. Log in
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
Time Clock
Saturday, 26 April 2008
Time Clock

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cctype>
#include <istream>
#include <ostream>
#include <fstream>

using namespace std;

class Time
{
protected:
 int hour;
 int min;
 int sec;
public:
   Time()
      { hour = 0; min = 0; sec = 0; }
 Time(int h, int m, int s)
  { hour = h; min = m; sec = s; }
 virtual int getHour()
  { return hour; }
 int getMin()
  { return min; }
 int getSec()
  { return sec; }
};

class MilTime : public Time
{
protected:
   int milHour;
   int milSec;
public:
   MilTime(int mh = 0, int ms = 0)
   {
      if (mh > 2359 || mh < 0 || ms > 60 || ms < 0)
      {
         milHour = milSec = 0;
         cout << "\n   Invalid data!";
      }
      else
      {
         milHour = mh;
         milSec = ms;
         stdConvert();
      }
   }
   void setTime(int mh = 0, int ms = 0)
   {
      if (mh > 2359 || mh < 0 || ms > 60 || ms < 0)
      {
         milHour = milSec = 0;
         cout << "\n   Invalid data!";
      }
      else
      {
         milHour = mh;
         milSec = ms;
         stdConvert();
      }
   }
   void stdConvert()
   {
      min = milHour % 100;
      hour = milHour / 100;
      if (hour > 12)
         hour -= 12;
      sec = milSec;
   }
   void stdInterval(int mt)
   {
      min = mt % 100;
      if (min > 60)
         min -= 40;
      hour = mt / 100;
   }
   int getHour()
   {
      return milHour;
   }
   void getStdHour()
   {
      if(hour < 10)
         cout << "0";
      cout << hour << " Hours ";
      if(min < 10)
         cout << "0";
      cout << min << " Minutes ";
      if (sec < 10)
         cout << "0";
      cout << sec << " Seconds ";
   }
};

class TimeClock : public MilTime
{
protected:
   MilTime ending;
public:
   TimeClock(int clockIn = 0, int clockOut = 0)
   {
      if (clockIn > 2359 || clockIn < 0 || clockOut > 2359 || clockOut < 0)
      {
         cout << "\n   Invalid data!";
         setTime();
         ending.setTime();
      }
      else
      {
         setTime(clockIn);
         ending.setTime(clockOut);
         getElapsed();
      }
   }
   int getHour()
   {
      return MilTime::getHour();
   }
   int getEnding()
   {
      return ending.MilTime::getHour();
   }
   void getElapsed()
   {
      int temp;
      temp = ending.getHour() - getHour();
      stdInterval(temp);
   }
};

void Clear_Screen(void);
void Flush(void);

int main()
{
   int starting, ending;
   char choice;
   do
   {
   cout << "   Please enter starting time: ";
      cin >> starting;
   cout << "   Please enter ending time:   ";
      cin >> ending;
      TimeClock empHours(starting, ending);
      cout << "\n   Total elapsed time: ";
      empHours.getStdHour();
   cout << "\n\n   Would you like to test again? (Y/N): ";
      cin >> choice;
   Clear_Screen();
   }
   while(choice == 'y' || choice == 'Y');
   cout << endl;
   cout << "\n\n   Thank you for using the program!" << endl;
   Flush();
   cout << "\n\n   Press any key to terminate . . ." << endl;
   getchar();
}


void Clear_Screen(void)
{
   #ifdef _WIN32
      system("cls");
   #else
      system("clear");
   #endif
}

void Flush(void)
{
   int ch;
   do
   {
      ch = getchar();
   }
   while (ch != EOF && ch != '\n');
   clearerr(stdin);
}


Posted by veronicak5678 at 9:00 PM EDT

Newer | Latest | Older