05-12 11:13
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[C/C++] Example / clockType 본문

Programming/C&C++

[C/C++] Example / clockType

cinema4dr12 2014. 6. 12. 11:43

//
//  main.cpp
//
//  Created by gchoi on 2014. 5. 16..
//  Copyright (c) 2014년 gchoi. All rights reserved.
//

#include <iostream>
#include <assert.h>
#include <math.h>

using namespace std;

class clockType {
private:
    int hr;
    int min;
    int sec;
   
public:
    clockType();
    clockType(int hr, int min, int sec);
    void setTime(int hr, int min, int sec);
    void getTime(int& hr, int& min, int& sec);
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType& otherClock) const;
};

clockType::clockType() {
    hr = 0;
    min = 0;
    sec = 0;
}

clockType::clockType(int hour, int minute, int second) {
    int mod, rem;
   
    mod = second / 60;
    rem = second % 60;
   
    min = minute + mod;
    sec = rem;
   
    mod = min / 60;
    rem = min % 60;
   
    min = rem;
    hr = hour + mod;
   
    hr = hr % 24;
}

void clockType::setTime(int hour, int minute, int second) {
    int mod, rem;
   
    mod = second / 60;
    rem = second % 60;
   
    min = minute + mod;
    sec = rem;
   
    mod = min / 60;
    rem = min % 60;
   
    min = rem;
    hr = hour + mod;
   
    hr = hr % 24;
}

void clockType::getTime(int &hour, int &minute, int &second) {
    hr = hour;
    min = minute;
    sec = second;
}

void clockType::printTime() const {
    cout << hr << ":" << min << ":" << sec << endl;
}

void clockType::incrementSeconds() {
    sec++;
   
    int mod, rem;
   
    mod = sec / 60;
    rem = sec % 60;
   
    min += mod;
    sec = rem;
   
    mod = min / 60;
    rem = min % 60;
   
    min = rem;
    hr += mod;
   
    hr = hr % 24;
}

void clockType::incrementMinutes() {
    min++;
   
    int mod, rem;
   
    mod = min / 60;
    rem = min % 60;
   
    min = rem;
    hr += mod;
   
    hr = hr % 24;
   
    cout << "oops" << endl;
}

void clockType::incrementHours() {
    hr++;
   
    hr = hr % 25;
}

bool clockType::equalTime(const clockType& otherClock) const {
    int cnt = 0;
   
    if(hr != otherClock.hr) cnt++;
    if(min != otherClock.min) cnt++;
    if(sec != otherClock.sec) cnt++;
   
    if(cnt > 0) return false;
    else return true;
}

///////////////////////////////////////////////////////////////
// MAIN
///////////////////////////////////////////////////////////////
int main(int argc, const char * argv[])
{
    clockType* ct = new clockType(72, 59, 59);
    clockType* ct2 = new clockType(72, 42, 59);
   
    cout << ct2->equalTime(*ct) << endl;
   
    return 0;
}
Comments