Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Design a class TIME which stores hr,min and sec. The class should have the methods to support the following : i) User may give the time value in 24-hour format. ii) User may give the time in AM/PM format. iii) Display the time in 24-hour format. iv) Display the time in AM/PM format. v) User may like to add minute with a time value.

Source Code :

#include<iostream.h>
class tim {
      int hr,min,sec,fl;
      char a;
      public:
      tim() {
            hr=0;
            min=0;
            sec=0;
            fl=0;
      }
      void get_value() {
            int c;
            cout<<endl<<"ENTER TIME";
            cout<<endl<<"1. Enter Time in 24 hr Format";
            cout<<endl<<"2. Enter Time in 12 hr Format";
            cout<<endl<<"Enter Choice : ";
            cin>>c;
            switch(c) {
                  case 1: {
                        cout<<endl<<"Hour : "; 
                        cin>>hr;
                        cout<<"Minute : ";
                        cin>>min;
                        cout<<"Second : ";
                        cin>>sec;
                        if(hr>23 || min>59 || sec>59) {
                              cout<<"Invalid Entry of Time";
                        }
                        else {
                              fl=1;
                              cout<<"Time Entered Successfully";
                        }
                  }break;
                  case 2: {
                        cout<<endl<<"Hour : ";
                        cin>>hr;
                        cout<<"Minute : ";
                        cin>>min;
                        cout<<"Second : ";
                        cin>>sec;
                        cout<<"Enter a for am/p for pm : ";
                        cin>>a;
                        if(hr>12 || min>59 || sec>59) {
                              if(a=='p' || a=='a')
                                    cout<<"Invalid Entry of Time";
                        }
                        else {
                              fl=2;
                              cout<<"Time Entered Successfully";
                        }
                  }break;
                  default: {
                        cout<<"INVALID CHOICE";
                  }
            }
      }
      void show_value() {
            if(fl==1) {
                  cout<<endl<<"In 24-Hour Format : "<<hr<<":"<<min<<":"<<sec;
                  cout<<endl<<"In 12-Hour Format : ";
                  if(hr>12)
                        cout<<(hr-12)<<":"<<min<<":"<<sec<<" pm";
                  else
                        cout<<hr<<":"<<min<<":"<<sec<<" am";
            }
            else if(fl==2) {
                  cout<<endl<<"In 24-Hour Format : ";
                  if(a=='p')
                        cout<<(hr+12)<<":"<<min<<":"<<sec;
                  else if(a=='a')
                        cout<<hr<<":"<<min<<":"<<sec;
                  cout<<endl<<"In 12-Hour Format : ";
                  if(a=='p')
                        cout<<hr<<":"<<min<<":"<<sec<<" pm";
                  else
                        cout<<hr<<":"<<min<<":"<<sec<<" am";
            }
            else {
                  cout<<"Enter Time First";
            }
      }
      void add() {
            int mm;
            if(fl==1 || fl==2) {
                  cout<<"Enter Minutes to be Added with Time Entered : ";
                  cin>>mm;
                  if((mm+min)>59) {
                        hr+=(min+mm)/60;
                        min=(mm+min)%60;
                  }
                  else {
                        min+=mm;
                  }
            }
            else
                  cout<<"Enter Time First";
      }                
};
int main() {
      tim ob1;
      int ch;
      while(true) {
            cout<<endl<<"   MAIN MENU ";
            cout<<endl<<"1. Enter Time";
            cout<<endl<<"2. Display Time";
            cout<<endl<<"3. Add Minute With The Time";
            cout<<endl<<"4. Exit";
            cout<<endl<<"Enter Choice : ";
            cin>>ch;
            switch(ch) {
                  case 1: {
                        ob1.get_value();
                  }break;
                  case 2: {
                        ob1.show_value();
                  }break;
                  case 3: {
                        ob1.add();
                        ob1.show_value();
                  }break;
                  case 4: {
                        exit(0);
                  }break;
                  default: {
                        cout<<"INVALID CHOICE";
                  }
            }
      }
}    



Output :

MAIN MENU
1.  Enter Time
2.  Display Time
3.  Add Minute With The Time
4.  Exit
Enter Choice : 1

ENTER TIME
1.  Enter Time in 24 hr Format
2.  Enter Time in 12 hr Format
Enter Choice : 1

Hour : 15
Minute : 30
Second : 45
Time Entered Successfully

MAIN MENU
1.  Enter Time
2.  Display Time
3.  Add Minute With The Time
4.  Exit
Enter Choice : 2

In 24-Hour Format : 15:30:45
In 12-Hour Format : 3:30:45 pm

MAIN MENU
1.  Enter Time
2.  Display Time
3.  Add Minute With The Time
4.  Exit
Enter Choice : 3
Enter Minutes to be Added with Time Entered : 40

In 24-Hour Format : 16:10:45
In 12-Hour Format : 4:10:45 pm

MAIN MENU
1.  Enter Time
2.  Display Time
3.  Add Minute With The Time
4.  Exit
Enter Choice : 4

No comments:

Post a Comment