だから、これは私の問題です:
12月12日など、日付をサポートする列挙型を記述します。次に、翌日を生成する関数を追加します。つまり
nextday(date)
、12月12日は12月13printdate(date)
日です。日付を読みやすく印刷する関数も記述します。この関数は、2月が28日であり、毎月何日であるかを最もよく知っていると想定できます。2つのメンバーで構造体を使用します。1つは月で、2つ目は月の日です—int
(またはshort
)。次に、1月1日の日付を印刷し、翌日1月2日を印刷します。これは、2月28日、3月14日、10月31日、および12月31日の日付に対して行います。
そしてそれが私の解決策です
#include <stdio.h>
#include <ctype.h>
typedef enum month { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec } month;
typedef struct date { enum month m; int d; } date;
void print_month(struct date date) { //simple function for displaying month and day
switch (date.m) {
case jan:
printf("January day %d\n", date.d);
break;
case feb:
printf("February day %d\n", date.d);
break;
case mar:
printf("March day %d\n", date.d);
break;
case apr:
printf("April day %d\n", date.d);
break;
case may:
printf("May day %d\n", date.d);
break;
case jun:
printf("June day %d\n", date.d);
break;
case jul:
printf("July day %d\n", date.d);
break;
case aug:
printf("August day %d\n", date.d);
break;
case sep:
printf("September day %d\n", date.d);
break;
case oct:
printf("October day %d\n", date.d);
break;
case nov:
printf("November day %d\n", date.d);
break;
case dec:
printf("December day %d\n", date.d);
break;
default:
printf("Out of range!");
break;
}
}
enum month next_day(struct date next) { //next month|day function which is the problem.
if (next.m == jan || mar || may || jul || aug || oct || dec) {
next.d + 1 % 31;
}
else if (next.m == apr || jun || sep || nov) {
next.d + 1 % 30;
}
else if (next.m == feb) {
next.d + 1 % 28;
if (next.d > 28)
next.m = mar;
}
return (month)next.m;
}
int main(void) {
struct date date_1 = { feb, 28 };
struct date date_2 = { mar, 14 };
struct date date_3 = { oct, 31 };
struct date date_4 = { dec, 31 };
print_month(date_1);
print_month(date_2);
print_month(date_3);
print_month(date_4);
printf("\n");
print_month(next_day(date_1)); //err C2440
print_month(next_day(date_2)); //err C2440
print_month(next_day(date_3)); //err C2440
print_month(next_day(date_4)); //err C2440
printf("\n\n");
return 0;
}
私はいくつかの提案があります:
print_date()
文字列の配列を使用して簡略化できます。next_day()
は日付を返す必要があります。変更されたバージョンは次のとおりです。
#include <stdio.h>
typedef enum month { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec } month;
typedef struct date { enum month m; int d; int y; } date;
static const char * const month_name[] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
};
void print_month(struct date date) { //simple function for displaying month and day
switch (date.m) {
case jan:
case feb:
case mar:
case apr:
case may:
case jun:
case jul:
case aug:
case sep:
case oct:
case nov:
case dec:
printf("%s %d, %d\n", month_name[date.m], date.d, date.y);
break;
default:
printf("Out of range!\n");
break;
}
}
date next_day(struct date next) { //next month|day function which is the problem.
int last_day;
switch (next.m) {
case jan:
case mar:
case may:
case jul:
case aug:
case oct:
case dec:
last_day = 31;
break;
case apr:
case jun:
case sep:
case nov:
last_day = 30;
break;
case feb:
// check for Gregorian leap years
// see https://stackoverflow.com/questions/3220163/how-to-find-leap-year-programmatically-in-c
if (next.y % 4 == 0 && (next.y % 100 != 0 || next.y % 400 == 0))
last_day = 29;
else
last_day = 28;
break;
default:
return next;
}
if (next.d++ == last_day) {
next.d = 1;
if (next.m++ == dec) {
next.m = jan;
next.y++;
}
}
return next;
}
int main() {
struct date date_1 = { feb, 28, 2020 };
struct date date_2 = { mar, 14, 2020 };
struct date date_3 = { oct, 31, 2020 };
struct date date_4 = { dec, 31, 2020 };
struct date date_5 = { feb, 28, 2021 };
print_month(date_1);
print_month(date_2);
print_month(date_3);
print_month(date_4);
print_month(date_5);
printf("\n");
print_month(next_day(date_1));
print_month(next_day(date_2));
print_month(next_day(date_3));
print_month(next_day(date_4));
print_month(next_day(date_5));
printf("\n\n");
return 0;
}
出力:
2020年2月28日2020年 3月14日2020年 10月31日2020年 12月31日 2021年2月28日2020年2 月29日2020年 3月15日2020年 11 月1日2021年1月1日2021年 3月1日
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加