Thursday, March 10, 2016

C ++ program to implement time operations

#include<iostream.h>
#include<conio.h>
#include<process.h>
class Time
{
 private:
  int hours;
  int minutes;
  int seconds;
 public:
  Time();
  void addhour(int);
  void addminute(int);
  void addsecond(int);
  void displaytime();
  void addtime();
  int gethour();
  int getminute();
  int getsecond();
  void sethour(int);
  void setminute(int);
  void setsecond(int);
};
Time::Time()
 {
  sethour(0);
  setminute(0);
  setsecond(0);
 }
void Time::addhour(int h)
 {
  h=gethour()+h;
  if(h>24)
   {
    h=h%24;
   }
  sethour(h);
 }
int Time::gethour()
 {
  return hours;
 }
void Time::sethour(int h)
 {
  hours=h;
 }
void Time::addminute(int m)
 {
  m=getminute()+m;
  if(m>60)
   {
    m=m%60;
    addhour(m/60);
   }
  setminute(m);
 }
int Time::getminute()
 {
  return minutes;
 }
void Time::setminute(int m)
 {
  minutes=m;
 }
void Time::addsecond(int s)
 {
  s=getsecond()+s;
  if(s>60)
   {
    s=s%60;
    addminute(s/60);
   }
  setsecond(s);
 }
int Time::getsecond()
 {
  return seconds;
 }
void Time::setsecond(int s)
 {
  seconds=s;
 }
void Time::displaytime()
 {
  cout<<"Time:: ";
  cout.width(2);
  cout.fill('0');
  cout<<gethour()<<":";
  cout.width(2);
  cout.fill(2);
  cout<<getminute()<<":";
  cout.width(2);
  cout.fill('0');
  cout<<getsecond();
 }
void Time::addtime()
 {
  int h,m,s;
  cout<<"Enter hours, minutes and seconds to add in time";
  cin>>h>>m>>s;
  addsecond(s);
  addminute(m);
  addhour(h);
 }
void main()
 {
  int i;
  Time t;
  int h;
  int m;
  int s;
  do
   {
    cout<<endl<<"Enter 1.Add hours \n2.Add minutes \n3.Add seconds \n4.Add time \n5.Show time \n6.Exit";
    cout<<endl<<"Enter your choice";
    cin>>i;
    switch(i)
     {
      case 1:
       cout<<endl<<"How many hours to add";
       cin>>h;
       t.addhour(h);
       break;
      case 2:
       cout<<endl<<"How many minutes to add";
       cin>>m;
       t.addminute(m);
       break;
      case 3:
       cout<<endl<<"How many seconds to add";
       cin>>s;
       t.addsecond(s);
       break;
      case 4:
       t.addtime();
       break;
      case 5:
       t.displaytime();
       break;
      case 6:
       exit(0);
      default:
       cout<<endl<<"Please enter a correct choice";
     }
   }while(i!=6);
 }

 output
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 1
 How many hours to add 3
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 5
 Time::04:00:00
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 2
 How many minutes to add 5
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 Time::04:05:00
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 6



No comments:

Post a Comment