我的课程时间可以显示时间,具体取决于一天中的静态变量小时数和一小时内的分钟数。我希望能够通过简单地更改我的静态变量来打印更新的时间。
在我的main()中,我想基于默认的静态变量声明一些Time实例(一天24小时,一小时60分钟)。
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
但是,代码仍会打印出默认静态变量的数字。
关于如何解决此问题的任何想法?顺便说一句,我正在尝试创建一个类来测试给定的驱动程序,因此更改我的主要功能不是一个选项。
以下是我的其余代码:
头文件
#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 :(得分:0)
您只在构造函数中执行计算。稍后修改静态变量时,不会自动重新计算结果。
一种可能的解决方案是将值存储在构造函数中,然后在写入流时进行实际计算。