在 C 中增加结构构造的时间
Adding time with structure construction in C
我刚刚开始使用 C,我有一个任务是将输入的日期加上 1 分钟和 30 秒,该日期由年月日时分秒组成。
因此,我使用了结构构造,因为它是要求之一。
但是,在我必须处理许多情况之前,一切看起来都很好。
例如,如果输入的日期是 2014/12/31 23:58:30 或 2014/2/28 23:59:00 ,我必须更改所有内容。最简单的方法是使用 if 语句进行检查,但我认为应该换一种方式,而不是为我想要处理的每种情况编写大量 if
语句。你能告诉我是否有另一种更清晰的方法吗?
struct{
int day;
int month;
int year;
}a;
struct{
int hours;
int minutes;
int seconds;
}b;
认为这种方式可能会使 if 语句多一点。另外一个要求是如果月份输入为 3,则添加 0,输出应为 03.. 无论如何,我似乎无法逃避 "phone number" if 语句。感谢您的回答和花费的时间!!
那么,假设您不关心关于时间的所有真正困难的部分。像这样的东西:leap seconds, leap years, daylight savings time, changes to daylight savings time, and time zone changes.
如果你不这样做,那么让我们像构建自定义加法运算符一样考虑这个问题。
struct time {
unsigned year;
unsigned month;
unsigned day;
unsigned hour;
unsigned minute;
unsigned second;
};
struct time time_add(struct time date, struct time delta) {
date.year += delta.year;
date.month += delta.month;
date.day += delta.day;
date.hour += delta.hour;
date.minute += delta.minute;
date.second += delta.second;
// Implement corrections
while (date.second >= 60) {
date.second -= 60;
date.minute += 1;
}
while (date.minute >= 60) {
date.minute -= 60;
date.hour += 1;
}
while (date.hour >= 24) {
date.hour -= 24;
date.day += 1;
}
// and so on..
return date;
}
使用此方法,您应该能够显着减少必须处理的案例数量。
如果你想用某种方法来缩写它(或者用一些通用的方法在奇怪的、可变的、计数的基础上添加数字,比如旧的便士、先令和英镑系统),试着把这些东西放在一个数组中以及另一个固定数组中每个数字的编号基础,因此您可以执行以下操作:
int basis[] = {
100, /* hundreds of a second in a second */
60, /* secs in a minute */
60, /* mins in an hour */
12, /* hours in a cycle(AM/PM) */
2, /* cycles in a day */
30, /* days, but be careful, not all months have 30 days */
12, /* months */
100, /* years in a century */
0, /* centuries, and sentinel to stop. */
};
int carry = 0;
int i;
for (i = 0; i; i++) { /* sum array a and b into c */
c[i] = a[i] + b[i] + carry; /* sum digit i */
carry = c[i] / basis[i]; /* determine carry, can be abreviated */
c[i] %= basis[i]; /* determine final value, can be abreviated */
} /* for */
c[i] = a[i] + b[i] + carry; /* finally, centuries, not based */
我刚刚开始使用 C,我有一个任务是将输入的日期加上 1 分钟和 30 秒,该日期由年月日时分秒组成。
因此,我使用了结构构造,因为它是要求之一。
但是,在我必须处理许多情况之前,一切看起来都很好。
例如,如果输入的日期是 2014/12/31 23:58:30 或 2014/2/28 23:59:00 ,我必须更改所有内容。最简单的方法是使用 if 语句进行检查,但我认为应该换一种方式,而不是为我想要处理的每种情况编写大量 if
语句。你能告诉我是否有另一种更清晰的方法吗?
struct{
int day;
int month;
int year;
}a;
struct{
int hours;
int minutes;
int seconds;
}b;
认为这种方式可能会使 if 语句多一点。另外一个要求是如果月份输入为 3,则添加 0,输出应为 03.. 无论如何,我似乎无法逃避 "phone number" if 语句。感谢您的回答和花费的时间!!
那么,假设您不关心关于时间的所有真正困难的部分。像这样的东西:leap seconds, leap years, daylight savings time, changes to daylight savings time, and time zone changes.
如果你不这样做,那么让我们像构建自定义加法运算符一样考虑这个问题。
struct time {
unsigned year;
unsigned month;
unsigned day;
unsigned hour;
unsigned minute;
unsigned second;
};
struct time time_add(struct time date, struct time delta) {
date.year += delta.year;
date.month += delta.month;
date.day += delta.day;
date.hour += delta.hour;
date.minute += delta.minute;
date.second += delta.second;
// Implement corrections
while (date.second >= 60) {
date.second -= 60;
date.minute += 1;
}
while (date.minute >= 60) {
date.minute -= 60;
date.hour += 1;
}
while (date.hour >= 24) {
date.hour -= 24;
date.day += 1;
}
// and so on..
return date;
}
使用此方法,您应该能够显着减少必须处理的案例数量。
如果你想用某种方法来缩写它(或者用一些通用的方法在奇怪的、可变的、计数的基础上添加数字,比如旧的便士、先令和英镑系统),试着把这些东西放在一个数组中以及另一个固定数组中每个数字的编号基础,因此您可以执行以下操作:
int basis[] = {
100, /* hundreds of a second in a second */
60, /* secs in a minute */
60, /* mins in an hour */
12, /* hours in a cycle(AM/PM) */
2, /* cycles in a day */
30, /* days, but be careful, not all months have 30 days */
12, /* months */
100, /* years in a century */
0, /* centuries, and sentinel to stop. */
};
int carry = 0;
int i;
for (i = 0; i; i++) { /* sum array a and b into c */
c[i] = a[i] + b[i] + carry; /* sum digit i */
carry = c[i] / basis[i]; /* determine carry, can be abreviated */
c[i] %= basis[i]; /* determine final value, can be abreviated */
} /* for */
c[i] = a[i] + b[i] + carry; /* finally, centuries, not based */