c
1 2#pragma warning(disable: 4996) 3#include<stdio.h> 4 5#define ROW_SIZE 6 //カレンダーの行のサイズ 6 7enum M_LIST { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOW, DEC, N_MONTH }; 8enum W_LIST { SUN, MON, TUE, WED, THU, FRI, SAT, N_WEEK }; 9int monthdays[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //月の日数 10 11//プロトタイプの宣言 12int januaryoneday(int year); 13int leapyear(int year); 14int calendarmake(int calendar[N_MONTH][ROW_SIZE][N_WEEK], int week , int monthdays[]); 15 16 17 18 19int main(void) { 20 21 char* weeks[] = { "日","月","火","水","木","金","土" }; 22 23 int calendar[N_MONTH][ROW_SIZE][N_WEEK]; // カレンダーの多次元配列 24 25 int year; //入力された西暦 26 int month; //入力された月 27 int day; //入力された日 28 int week; // //入力された西暦の1月1日の曜日 29 int judgment; //入力された値がうるう年だったかどうか 30 int sumday; // 入力された西暦までの日数の合計 31 int birthw; //入力された誕生日の曜日 32 int monthdaysum = 0; // 入力された誕生日の月までの合計の日数 33 34 printf("西暦を入力してください(入力された西暦の1月1日の曜日が表示されます)または、誕生日の西暦月日を入力してください(例:2019 01 01)生年月日の間はスペースを入れてください)"); 35 scanf("%d %d %d", &year, &month, &day); 36 37 38 39 week = januaryoneday(year); //入力された西暦の1月1日の曜日を取得 40 41 printf("%s\n", weeks[week]); //入力された西暦の1月1日の曜日を表示 42 43 judgment = leapyear(year); //うるう年の判定結果を取得 44 45 46 monthdays[FEB] = (judgment == 0) ? 29 : 28; // うるう年であれば2月を29日に変更 47 48 49 50 sumday = ((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400); //入力された西暦までの日数の合計 51 52 for (int i = JAN; i < month - 1; i++) { //入力された誕生日の月までの合計の日数 53 54 monthdaysum += monthdays[i]; 55 56 57 } 58 59 60 birthw = (sumday + monthdaysum + day) % 7; // 入力された誕生日までの合計の日数 の曜日を取得 61 62 printf("%s", weeks[birthw]); //入力された誕生日の曜日を表示 63 64 calendarmake(calendar[N_MONTH][ROW_SIZE][N_WEEK], week, monthdays); 65 66 67 68 int c = calendarmake(calendar, week); // カレンダーで表示された最後の曜日 69 70 printf("%s\n", weeks[c]); 71 72} 73 74 75 76//入力された西暦の1月1日の曜日を返す 77int januaryoneday(int year) { 78 79 int sumday; // 入力された西暦までの累計日数 80 int dayw; // 曜日計算 81 82 sumday = ((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400); //入力された1月1日までの日数 83 84 dayw = (sumday + 1) % 7; 85 86 return dayw; 87} 88 89 90//入力された西暦がうるう年かの判定 91int leapyear(int year) { 92 93 int leap; 94 95 leap = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) ? 0 : -1; //うるう年なら0を 違うなら-1 を 96 97 return leap; 98 99} 100 101//カレンダーを作る 102int calendarmake(int calendar[N_MONTH][ROW_SIZE][N_WEEK], int week ) { 103 104 int month; 105 int col; 106 int row; 107 int day = 0; 108 109 110 for (month = JAN; month < DEC; month++) { 111 112 for (row = 0; row < ROW_SIZE; row++) { 113 114 for (col = 0; col < N_WEEK; col++) { 115 116 if (row == 0 && col < week) { 117 118 continue; 119 } 120 121 day++; 122 123 calendar[month][row][col] = day; 124 125 if (monthdays[month] == day) { 126 127 week = col; 128 129 day = 0; 130 break; 131 } 132 } 133 } 134 } 135 136 return week; 137} 138 139 140 141 142
//カレンダーを作る
int calendarmake(int calendar[N_MONTH][ROW_SIZE][N_WEEK], int week )
で三次元配列に日にちを格納したいのですが、なかなか進まなかったのでアドバイス頂けると幸いです。
---------追記-------
int calendarmake(int calendar[N_MONTH][ROW_SIZE][N_WEEK], int week )
関数の中を書いてみました。
自分なりに書いてみたのですが、曜日が合わないです。
アドバイス頂けると助かります。
for (month = JAN; month < DEC; month++) {
は
for (month = JAN; month <= DEC; month++) {
あるいは
for (month = JAN; month < N_MONTH; month++) {
ではないですか?
指摘ありがとうございます。
`int calendarmake(int calendar[N_MONTH][ROW_SIZE][N_WEEK], int week , int monthdays[]);`
を呼び出すのに
`calendarmake(calendar[N_MONTH][ROW_SIZE][N_WEEK], week, monthdays);`
なんてのは...曜日が合わないとかそういう次元じゃないように思います。
配列の扱いを再確認して下さい。
コンパイラは少なくとも警告を出しているんじゃないかと思いますが、違いますか?
修正中の行はコメント化するなどして, コンパイル・実行すれば再現出来る状態でご提示ください.
「曜日が合わない」とは具体的に何年何月何日としたら何曜日になったのでしょうか.
また, 単に曜日を出すだけであれば calendar[N_MONTH][ROW_SIZE][N_WEEK] は必要無いと思います.
calendar[N_MONTH][ROW_SIZE][N_WEEK] はどのような内容でどのようにお使いのつもりでしょうか.
回答1件
あなたの回答
tips
プレビュー