Demo entry 6624506

实验1

   

Submitted by 王明键 on Jun 16, 2017 at 10:34
Language: C. Code size: 1.7 kB.

// TimeType.h
#ifndef TIMETYPE_H_INCLUDED
#define TIMETYPE_H_INCLUDED

class TimeType{
	public:
		void Set(int hours, int minutes, int seconds);
		void Increment();
		void Write() const;
		bool Equal(TimeType otherTime) const;
		bool LessThan(TimeType otherTime) const;
	private:
		int hrs;
		int mins;
		int secs;
};

#endif // TIMETYPE_H_INCLUDED

//TimeType.cpp
#include "TimeType.h"
#include <iostream>
void TimeType::Set(int hours, int minutes, int seconds)
{
	hrs = hours;
	mins = minutes;
	secs = seconds;
}

void TimeType::Increment()
{
	secs++;
	if (secs>59) {
		secs=0;
		mins++;
		if (mins>59) {
			mins=0;
			hrs++;
			if (hrs>23) hrs=0;
		}
	}
}

void TimeType::Write() const
{
    using namespace std;
	if (hrs<10) cout << '0';
	cout << hrs << ':';
	if (mins<10) cout <<'0';
	cout << mins << ':';
	if (secs<10) cout <<'0';
	cout << secs;
}

bool TimeType::Equal(TimeType otherTime) const
{
	return (hrs==otherTime.hrs && mins==otherTime.mins && secs==otherTime.secs);
}
bool TimeType::LessThan(TimeType otherTime) const
{
	return (hrs<otherTime.hrs||hrs==otherTime.hrs && mins<otherTime.mins||hrs==otherTime.hrs && mins==otherTime.mins && secs<otherTime.secs);
}

//Diary.cpp
#include <iostream>
#include "TimeType.h"
int main()
{
    using namespace std;
	TimeType time;
	int hours;
	int minutes;
	int seconds;
	cout<<"Enter a time (use hours < 0 to quit): ";
	cin>>hours>>minutes>>seconds;
	while(hours>=0)
	{
		time.Set(hours, minutes, seconds);
		time.Increment();
		cout<<"Incremented time is ";
		time.Write();
		cout<<endl;
		cout<<"Enter a time (use hours < 0 to quit): ";
		cin>>hours>>minutes>>seconds;
	}
	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).