C++ experiment --- alarm clock

**Topic 2: **Alarm Clock

Description

Define the Time class, including
three int type attributes of hour, minute, and second , which are the hour, minute, and second of the time.
Constructor, destructor, and output corresponding information in it. See the example for the format.
Other necessary functions to make the program run correctly.
Define the Alarm class, including:
an object of the Time class and a string attribute, which are the time of the alarm clock and the name of the alarm clock.
Constructor, destructor, and output corresponding information in it. See the example for the format.
The int remainSeconds(Time& now) method is used to calculate how many seconds are left before the alarm event at the current time now. Assume that now must be less than the alarm set time.
The getThing() method is used to return the name of the alarm clock.
The
first line of Input is a string without whitespace, which is the name of the alarm clock.
Lines 2 and 3 are two times, which are the time of the alarm clock and the current time. See the example for
Output
.
Sample Input

GetUp
10 10 10
9 9 9

Sample Output

Time 10:10:10 is created.
Alarm GetUp is created.
Time 9:9:9 is created.
Alarm GetUp will start after 3661 seconds.
Time 9:9:9 is erased.
Alarm GetUp is erased.
Time 10:10:10 is erased.

Given the main function of the title:

int main()
{
    
    
    int h, m, s;
    string th;
    cin>>th;
    cin>>h>>m>>s;
    Alarm alarm(h,m,s,th);
    cin>>h>>m>>s;
    Time now(h,m,s);
    cout<<"Alarm "<<alarm.getThing()<<" will start after "<<alarm.remainSeconds(now)<<" seconds."<<endl;
    return 0;
}

code:

#include<iostream>
#include<string>

using namespace std;

class Time{
    
    
	int hour,minute,second;
public:
	Time(int h,int m,int s){
    
    
		hour=h;
		minute=m;
		second=s;
		cout<<"Time "<<hour<<":"<<minute<<":"<<second<<" is created."<<endl;
	}
	~Time(){
    
    
		cout<<"Time "<<hour<<":"<<minute<<":"<<second<<" is erased."<<endl;
	}
	int getH(){
    
    
		return hour;
	}
	int getM(){
    
    
		return minute;
	}
	int getS(){
    
    
		return second;
	}
};


class Alarm{
    
    
	string name;
	Time time;
public:
	Alarm(int h,int m,int s,string Name):time(h,m,s),name(Name){
    
    
		cout<<"Alarm GetUp is created."<<endl;	
	}
	~Alarm(){
    
    
		cout<<"Alarm GetUp is erased."<<endl;
	}
	int remainSeconds(Time& now){
    
    
		int len_now=now.getH()*3600+now.getM()*60+now.getS();
		int len_alarm=time.getH()*3600+time.getM()*60+time.getS();
		return (len_alarm-len_now);	
	}
	string getThing(){
    
    
		return name;
	}
};


int main()
{
    
    
    int h, m, s;
    string th;
    cin>>th;
    cin>>h>>m>>s;
    Alarm alarm(h,m,s,th);
    cin>>h>>m>>s;
    Time now(h,m,s);
    cout<<"Alarm "<<alarm.getThing()<<" will start after "<<alarm.remainSeconds(now)<<" seconds."<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115033953