⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11_clockprog.cpp

📁 computer clock codein c++,computer clock codein c++,computer clock codein c++,computer clock codein
💻 CPP
字号:
//The complete program listing of the program that defines 
//and uses the class clockType

#include <iostream>
using namespace std;

class clockType
{
public:
    void setTime(int, int, int);
    void getTime(int&, int&, int&) const;
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType&) const;

private:
    int hr;
    int min;
    int sec;
};


int main()
{
    clockType myClock;
    clockType yourClock;  

    int hours;
    int minutes;
    int seconds;

       //set the time of myClock
    myClock.setTime(5, 4, 30);                          //Line 1

    cout << "Line 2: myClock: ";                        //Line 2
    myClock.printTime();    //print the time of myClock //Line 3
    cout << endl;                                       //Line 4

    cout << "Line 5: yourClock: ";                      //Line 5
    yourClock.printTime(); //print the time of yourClock  Line 6
    cout << endl;                                       //Line 7

       //set the time of yourClock
    yourClock.setTime(5, 45, 16);                       //Line 8

    cout << "Line 9: After setting, yourClock: ";       //Line 9
    yourClock.printTime(); //print the time of yourClock  Line 10
    cout << endl;                                       //Line 11

       //compare myClock and yourClock
    if (myClock.equalTime(yourClock))                   //Line 12
        cout << "Line 13: Both times are equal."
             << endl;                                   //Line 13
    else                                                //Line 14
        cout << "Line 15: The two times are not equal."
             << endl;                                   //Line 15

    cout << "Line 16: Enter the hours, minutes, and "
         << "seconds: ";                                //Line 16
    cin >> hours >> minutes >> seconds;                 //Line 17
    cout << endl;                                       //Line 18

       //set the time of myClock using the value of the
       //variables hours, minutes, and seconds
    myClock.setTime(hours, minutes, seconds);           //Line 19

    cout << "Line 20: New myClock: ";                   //Line 20
    myClock.printTime();    //print the time of myClock //Line 21
    cout << endl;                                       //Line 22

       //increment the time of myClock by one second
    myClock.incrementSeconds();                         //Line 23

    cout << "Line 24: After incrementing myClock by " 
         << "one second, myClock: ";                    //Line 24
    myClock.printTime();    //print the time of myClock //Line 25
    cout << endl;                                       //Line 26

      //retrieve the hours, minutes, and seconds of the 
      //object myClock
    myClock.getTime(hours, minutes, seconds);           //Line 27

      //output the value of hours, minutes, and seconds
    cout << "Line 28: hours = " << hours 
         << ", minutes = " << minutes 
         << ", seconds = " << seconds << endl;          //Line 28

    return 0;
}//end main


void clockType::setTime(int hours, int minutes, int seconds)
{
	if (0 <= hours && hours < 24)
	    hr = hours;
	else 
	    hr = 0;

	if (0 <= minutes && minutes < 60)
	    min = minutes;
	else 
	    min = 0;

	if (0 <= seconds && seconds < 60)
	    sec = seconds;
	else 
	    sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

void clockType::incrementHours()
{
	hr++;
	if(hr > 23)
 	   hr = 0;
}

void clockType::incrementMinutes()
{
	min++;
	if (min > 59)
	{
	    min = 0;
	    incrementHours();
	}
}

void clockType::incrementSeconds()
{
	sec++;

	if (sec > 59)
	{
	    sec = 0;
	    incrementMinutes();
	}
}

void clockType::printTime() const
{
	if (hr < 10)
	    cout << "0";
	cout << hr << ":";

	if (min < 10)
	    cout << "0";
	cout << min << ":";

	if (sec < 10)
	   cout << "0";
	cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
	return (hr == otherClock.hr 
		    && min == otherClock.min 
		    && sec == otherClock.sec);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -