接下來是作業6-8,同樣亦寫了三個檔
第一,tick.h
#ifndef TICK_H
#define TICK_H
class Time {
public:
Time( int = 0, int = 0, int = 0 ); // constructor
// set functions
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
// get functions
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second
void printStandard(); // output standard time
void tick(Time,const int);
private:
int hour; // 0 - 23
int minute; // 0 - 59
int second; // 0 - 59
};
#endif
第二,tick.cpp
#include <iostream>
using namespace std;
#include "tick.h"
// Constructor function to initialize private data.
// Calls member function setTime to set variables.
// Default values are 0 (see class definition).
Time::Time( int hr, int min, int sec )
{ setTime( hr, min, sec ); }
// Set the values of hour, minute, and second.
void Time::setTime( int h, int m, int s )
{
setHour( h );
setMinute( m );
setSecond( s );
}
// Set the hour value
void Time::setHour( int h )
{ hour = ( h >= 0 && h < 24 ) ? h : 0; }
// Set the minute value
void Time::setMinute( int m )
{ minute = ( m >= 0 && m < 60 ) ? m : 0; }
// Set the second value
void Time::setSecond( int s )
{ second = ( s >= 0 && s < 60 ) ? s : 0; }
// Get the hour value
int Time::getHour() { return hour; }
// Get the minute value
int Time::getMinute() { return minute; }
// Get the second value
int Time::getSecond() { return second; }
// Print time in standard format
void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << ( minute < 10 ? "0" : "" ) << minute
<< ":" << ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM" : " PM" );
}
void Time::tick(Time tt,const int count)
{
cout << "Incrementing second " << count
<< " times:\nStart time: ";
tt.printStandard();
for ( int i = 0; i < count; i++ ) {
tt.setSecond( ( tt.getSecond() + 1 ) % 60);
if (tt.getSecond() == 0)
{
tt.setMinute( (tt.getMinute() + 1) % 60);
if ( tt.getMinute() == 0 )
tt.setHour( ( tt.getHour() + 1 ) % 24);
}
cout << "\nsecond + 1: ";
tt.printStandard();
}
cout << endl;
}
第三,6-6.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "tick.h"
int main()
{
Time t;
t.setTime( 10, 58, 57 );
t.tick( t, 3 );
t.setTime( 10, 59, 57 );
t.tick( t, 3 );
t.setTime( 11, 59, 57 );
t.tick( t, 3 );
return 0;
}
--
Bach partita & sonata for violin solo
My favorite:
Henryk Szerying, Arthur Grumiaux, Nathan Milstein
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 163.30.187.205