2017-01-31 10 views
-1

My class Time은 하루 중 정적 변수 시간에 따라 시간을 표시하고 시간을 분 단위로 표시 할 수 있습니다. 단순히 정적 변수를 변경하여 업데이트 된 시간을 인쇄하고 싶습니다.정적 변수를 업데이트하여 인스턴스를 업데이트하는 방법은 무엇입니까?

내 main()에서 기본 정적 변수 (하루 24 시간, 한 시간에 60 분)를 기반으로 몇 가지 Time 인스턴스를 선언하고 싶습니다.

Time a; 
Time b(5); 
Time c(61); 

cout << "a = " << a << "\t"; 
cout << "b = " << b << "\t"; 
cout << "c = " << c << "\n"; 

// output is a = 0:00 b = 0:05 c = 1:01 

Time::set_hr_in_day(60); 

Time::set_min_in_hr(24); 
cout << "a = " << a << "\t"; 
cout << "b = " << b << "\t"; 
//output should be a = 0:00 b = 0:05 c = 2:13 

그러나 코드는 여전히 기본 정적 변수의 숫자를 인쇄합니다.

어떻게 해결할 수 있습니까? Btw, 주어진 드라이버에 대해 테스트 할 클래스를 만들려고합니다. 그래서 내 주요 함수를 변경하는 것은 옵션이 아닙니다.

헤더 파일이

#ifndef TIME_H 
#define TIME_H 

#include <iostream> 

using namespace std; 

class Time { 
private: 
    int hour; 
    int minute; 
    static int dayhrs; 
    static int hrsmin; 

public: 

    Time(); 
    Time(int min); 
    Time(int hr, int min); 
    Time(double hrs); 
    int minutes() { return minute; }; 
    int hours() { return hour; }; 

    static void set_hr_in_day(int hr); 
    static void set_min_in_hr(int min); 
    static int dailyhr() { return dayhrs; }; 
    static int hourlymin() { return hrsmin; }; 

friend ostream& operator<<(ostream& os, const Time& t); 


}; 



#endif 

구현

#include "time.h" 
#include <iostream> 

using namespace std; 

int Time::dayhrs = 24;//default 
int Time::hrsmin = 60; 

void Time::set_hr_in_day(int hr) { 
    dayhrs = hr; 
} 

void Time::set_min_in_hr(int min) { 
    hrsmin = min; 
} 


Time::Time() { 
    hour = 0; 
    minute = 0; 
} 

Time::Time(int min) { 
    if (min > (Time::hourlymin() - 1)) { 
     hour = min/Time::hourlymin(); 
     minute = min % Time::hourlymin(); 
    } 
    else { 
     hour = 0; 
     minute = min; 
    } 

} 

Time::Time(int hr, int min) { 
    if (min > (Time::hourlymin() - 1)) { 
     hour = min/Time::hourlymin() + hr; 
     minute = min % Time::hourlymin(); 
    } 
    else { 
     hour = hr; 
     minute = min; 
    } 

    if (hour > (Time::dailyhr() - 1)) 
     hour = hour % (Time::dailyhr()); 
} 

Time::Time(double hrs) { 
    double fraction = 0; 
    fraction = hrs - (int)hrs; 
    minute = fraction * Time::hourlymin(); 
    if (fraction * Time::hourlymin() - (int)(fraction * Time::hourlymin()) >= 0.5) 
     minute += 1; 
    hour = hrs - fraction; 
    if (hour > (Time::dailyhr() - 1)) 
     hour = hour % (Time::dailyhr()); 
} 



ostream& operator<<(ostream& os, const Time& t) 
{ 
    if (t.minute > 9) 
     os << t.hour << ":" << t.minute; 
    else 
     os << t.hour << ":0" << t.minute; 
    return os; 
} 

드라이버

#include <iostream> 
#include "time.h" 

using std::cout; 

int main() { 
    cout << "*****************************************\n"; 
    cout << "Welcome to 'San Angel'!\n"; 
    cout << "[ 1 day = 24 hours, 1 hour = 60 minutes ]\n\n"; 

    Time a; 
    Time b(5); 
    Time c(61); 
    Time d(47, 59); 
    Time X(5.0); 
    Time Y(1.5); 
    Time Z(25.1); 

    cout << "a = " << a << "\t"; 
    cout << "b = " << b << "\t"; 
    cout << "c = " << c << "\n"; 
    cout << "d = " << d << "\t"; 
    cout << "X = " << X << "\t"; 
    cout << "Y = " << Y << "\n"; 
    cout << "\t\tZ = " << Z << "\n"; 


    Time::set_hr_in_day(60); 
    Time::set_min_in_hr(24); 
    cout << "*****************************************\n"; 
    cout << "Welcome to the land of the remembered!\n"; 
    cout << "[ 1 day = 60 hours, 1 hour = 24 minutes ]\n\n"; 

    cout << "a = " << a << "\t"; 
    cout << "b = " << b << "\t"; 
    cout << "c = " << c << "\n"; 
    cout << "d = " << d << "\t"; 
    cout << "X = " << X << "\t"; 
    cout << "Y = " << Y << "\n"; 
    cout << "\t\tZ = " << Z << "\n"; 

return 0; 

} 
/** 

OUTPUT: 

***************************************** 
Welcome to 'San Angel'! 
[ 1 day = 24 hours, 1 hour = 60 minutes ] 

a = 0:00 b = 0:05 c = 1:01 
d = 23:59 X = 5:00 Y = 1:30 
Z = 1:06 
***************************************** 
Welcome to the land of the remembered! 
[ 1 day = 60 hours, 1 hour = 24 minutes ] 

a = 0:00 b = 0:05 c = 2:13 
d = 59:23 X = 12:12 Y = 3:18 
Z = 2:18 */ 
+0

이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –

+0

좋아, 고마워. 언어를 배우는 것만으로 디버거를 사용하는 방법에 익숙하지 않습니다. – coeurs

답변

0

당신 만 항구 건설에 계산을 수행 : 여기

내 코드의 나머지 부분입니다 ctors. 정적 변수를 나중에 수정하면 자동으로 결과를 다시 계산하지 않습니다.

가능한 해결 방법은 생성자에 값을 저장 한 다음 스트림에 쓸 때 실제 계산을 수행하는 것입니다.

+0

나는 그것을 시도 할 것이다. 감사! – coeurs