OOP Assignment 1
#include<iostream>
//#include<Windows>
using namespace std;
struct my_time
{
private:
int second;
int minute;
int hour;
public:
my_time ();
my_time(int a);
my_time (int a, int b);
my_time (int a, int b, int c);
void time_cal_helper(int a , int b , int c){
second = a;
minute = b;
hour = c;
cout<<second<<" : "<<minute<<" : "<<hour;
};
void ad_sec (int a);
void ad_mint (int b);
void ad_hour (int c);
int total_time (int a, int b, int c)
{
second = a;
minute = b;
hour = c;
int total = second + minute * 60 + hour * 3600;
return total;
};
void show_12 (int a, int b, int c)
{
if (a > 59 || a < 0)
{
cerr << "Seconds value is out of range!";
cout << endl;
exit (-1);
}
second = a;
if (b > 59 || b < 0)
{
cerr << "Minutes value is out of range!";
cout << endl;
exit (-1);
}
minute = b;
if (c > 23 || c < 0)
{
cerr << "Hours value is out of range!";
cout << endl;
exit (-1);
}
hour = c;
if (c > 12)
{
hour = hour - 12;
}
cout << a << " : " << b << " : " << hour;
cout << endl;
};
void show_23 (int a, int b, int c)
{
if (a > 59 || a < 0)
{
cerr << "Seconds value is out of range!";
cout << endl;
exit (-1);
}
second = a;
if (b > 59 || b < 0)
{
cerr << "Minutes value is out of range!";
cout << endl;
exit (-1);
}
minute = b;
if (c > 23 || c < 0)
{
cerr << "Hours value is out of range!";
cout << endl;
exit (-1);
}
hour = c;
cout << a << " : " << b << " : " << hour;
cout << endl;
};
void display ()
{
cout << second << " : " << minute << " : " << hour;
cout << endl;
};
};
my_time::my_time ()
{
second = 0;
minute = 0;
hour = 0;
};
my_time::my_time (int a)
{
if (a > 59 || a < 0)
{
cerr << "Value is out of range!";
exit (-1);
}
second = a;
minute = 0;
hour = 0;
};
my_time::my_time (int a, int b)
{
if (a > 59 || b > 59 || a < 0 || b < 0)
{
cerr << "Value is out of range! ";
cout << endl;
exit (-1);
}
second = a;
minute = b;
hour = 0;
};
my_time::my_time (int a, int b, int c)
{
if (a > 59 || b > 59 || a < 0 || b < 0)
{
cerr << "Value is out of range! ";
cout << endl;
exit (-1);
}
second = a;
minute = b;
hour = c;
};
void
my_time::ad_sec (int a)
{
second += a;
if(second > 59){
minute = minute+second/60;
if(minute>59){
hour = hour+minute/60;
minute = minute%59;
}
second = second%59;
}
};
void
my_time::ad_mint (int b)
{
minute += b;
if(minute>59){
hour = hour+minute/60;
minute = minute%60;
}
};
void
my_time::ad_hour (int c)
{
hour += c;
if(hour>23){
while(hour>23){
hour = hour/23;
}
}
};
int
my_time_difference (int x, int y)
{
return x - y;
}
void seconds_to_time(int a ){
int c = a%3600;
int z = c%60;
int y = (c)/60;
a = a - c;
int x = (a/3600);
my_time t1;
t1.time_cal_helper(z , y , x);
}
int
main ()
{
my_time t1;
t1.display();
my_time t2(12);
t2.display();
my_time t3(59 , 59);
t3.display();
my_time t4(59 , 59 , 23);
t4.display();
t1.show_12(23 ,23 ,23);
t1.show_23(23 ,23 ,23);
seconds_to_time(200000);
int x = t1.total_time(12 ,12 ,12);
int y =t1.total_time(6 ,6 ,6);
cout<<endl;
cout<<endl;
cout<<"Adding seconds , minutes and hours:"<<endl;
t1.ad_mint(32);
t1.ad_hour(12);
t1.ad_sec(30000);
t1.display();
cout<<endl;
cout<<"The total difference between the two times is: "<<my_time_difference(x , y);
cout<<endl;
cout<<endl;
}
Comments
Post a Comment