• 締切済み

C言語で3次元配列を使い一年分のカレンダーを作成

asuncionの回答

  • asuncion
  • ベストアンサー率33% (2126/6286)
回答No.1

>char *weekday[] = { "日,月,火,水,木,金,土" }; //曜日 自分だったら、 char *weekday[] = { "日", "月", "火", "水", "木", "金", "土" }; とします。 >void main(void) { main()の型がvoidっていうのはたぶんまずいと思います。 自分だったら int main(void) { と書きます。 >printf("西暦を出力"); >scanf("%d", &year); //年度の出力 この2行の「出力」は「入力」が正しいですか? >/*うるう年の日数*/ >if ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0)mday[FEB] = 29; { year % 400 の直前の&&は、||ではありませんか?だって、閏年の定義って、確か 西暦が4で割り切れれば閏年 ただし、100で割り切れれば平年 ただし、400で割り切れれば閏年 ですよね? >char cal[N_MONTH][WEEK][N_WEEK]; //3次元配列の宣言 これが3次元配列であることは見ればわかります。そういうコメントより、 N_MONTH WEEK N_WEEK の意味を書く方がよっぽど意味があるでしょう。 >printf("%d\n", (month + 1)); //月を入力 このコメントの「入力」は「出力」のことですか?

uzumechan
質問者

補足

コメントありがとうございます。自分なりにやってみてここまで出来たのですが、日数をうまく表示する方法を教えていただけないでしょうか? #pragma warning(disable:4996) #include <stdio.h> #define WEEK 6 enum M_LIST { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, N_MONTH }; enum W_LIST { SUN, MON, TUE, WED, THU, FRI, SAT, N_WEEK }; int mday[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //各月の日数 /*プロトタイプ宣言*/ int monthday(int year); void karenda(int total); /*メイン*/ void main(void) { int year; printf("西暦を入力"); scanf("%d", &year); //年度の入力 karenda(monthday(year)); } /*求める月の前月までの総日数*/ int monthday(int year) { int total = 0; /*求める年の前年までの総日数を求める*/ total = (((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400) + 1); /*うるう年の判定*/ if ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) { mday[FEB] = 29; } else { mday[FEB] = 28; } return total; } /*カレンダー*/ void karenda(int total) { int month, week, day; int box = total % 7; //1月1日の曜日 char cal[N_MONTH][WEEK][N_WEEK] = { 0 }; //3次元配列を宣言し0を入れる int cnt = 1; for (month = 0; month < N_MONTH; month++) { printf("%d\n", (month + 1)); //月を出力 printf(" 日 月 火 水 木 金 土\n"); for (week = 0; week < WEEK; week++) { for (day = 0; day < N_WEEK; day++) { if (week == 0 && box > 0) { printf(" "); box--; } else if(){ printf("%3d",); } } printf("\n"); } cnt; } } うるう年の&&の部分は!=にしているので大丈夫です。 よろしくお願いします。

関連するQ&A

  • C言語で3次元配列を使い一年分のカレンダーを作成

    カレンダーは完成したのですが曜日がズレてしまい綺麗に表示されません。どのように改善すれば良いのでしょうか?よろしくお願いします。 現状です #pragma warning(disable:4996) #include <stdio.h> #include <Windows.h> enum M_LIST { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, N_MONTH }; enum W_LIST { SUN, MON, TUE, WED, THU, FRI, SAT, N_WEEK }; int mday[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //各月の日数 char *weekday[] = { "日","月","火","水","木","金","土" }; //各曜日 /*プロトタイプ宣言*/ int monthday(int year); void Array(int total, char box[N_MONTH][N_WEEK][N_WEEK]); void karenda(char box[N_MONTH][N_WEEK][N_WEEK]); /*メイン*/ void main(void) { int year, total; char box[N_MONTH][N_WEEK][N_WEEK] = { 0 }; //3次元配列を宣言し0を入れる /*画面制御の初期化*/ COORD coord; HANDLE hStdout; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); printf("西暦を入力"); scanf("%d", &year); //年度の入力 total = monthday(year); Array(total, box); karenda(box); } /*求める月の前月までの総日数*/ int monthday(int year) { int total = 0; /*求める年の前年までの総日数を求める*/ total = (((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400) + 1); /*うるう年の判定*/ if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0) { mday[FEB] = 29; } else { mday[FEB] = 28; } return total % 7; } /*カレンダーの配列*/ void Array(int total, char box[N_MONTH][N_WEEK][N_WEEK]) { int month, row, col, day; col = total; for (month = JAN; month < N_MONTH; month++) { row = 1; day = 1; while (day <= mday[month]) { box[month][row][col] = day; if (col > SAT) { //土曜までいったら次の週 row++; col = SUN; } day++; col++; } } } /*カレンダーの出力*/ void karenda(char box[N_MONTH][N_WEEK][N_WEEK]) { int month, week, day; for (month = JAN; month < N_MONTH; month++) { //月の出力 printf("%3d\n", month + 1); for (week = SUN; week < N_WEEK; week++) { for (day = SUN; day < N_WEEK; day++) { if (week == 0) { //曜日の出力 printf("%s", weekday[day]); } if (box[month][week][day] == 0) { //0なら空白 printf(" "); } else { printf("%3d", box[month][week][day]); } } printf("\n"); } printf("\n"); } } 今はこのような形で表示されます(空白は_で表しています) 日____月____火____水____木____金____土 ________1___2___3___4___5 6___7___8___9__10__11__12 13_14__15__16__17__18__19 20_21__22__23__24__25__26 27_28__29__30__31

  • C言語「3次元配列で1年間分のカレンダー作成」

    C言語課題で3次元配列で与えられた年の1年間分のカレンダーを作成するという課題で日数の表示の方法が分かりません。回答などいただけると参考になりますので教えていただけるとありがたいです。 現在の状態です(この状態ではエラーは起きていません) #pragma warning(disable:4996) #include <stdio.h> #include <Windows.h> enum M_LIST { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, N_MONTH }; enum W_LIST { SUN, MON, TUE, WED, THU, FRI, SAT, N_WEEK }; char *weekday[] = { "SUN","MON","TUE","WED","THU","FRI","SAT"}; //曜日 int mday[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; //各月の日数 /*プロトタイプ宣言*/ int monthday(int year); void karenda(int total); /*メイン*/ void main(void) { int year; /*画面制御の初期化*/ COORD coord; HANDLE hStdout; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); char cal[N_MONTH][N_WEEK][N_WEEK] = { 0 }; //3次元配列を宣言し0を入れる printf("西暦を入力"); scanf("%d", &year); //年度の入力 karenda(monthday(year)); } /*求める月の前月までの総日数*/ int monthday(int year) { int total = 0; /*求める年の前年までの総日数を求める*/ total = (((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400) + 1)%7; /*うるう年の判定*/ if ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) { mday[FEB] = 29; } else { mday[FEB] = 28; } return total; } /*カレンダー*/ void karenda(int total) { int month, row, day; for (month = JAN; month < N_MONTH; month++) { row = 0; day = 1; while (day <= mday[month]) { for (row = SUN; row < N_WEEK; row++) { if (day <= SAT) { day++; } } day++; } } } よろしくお願いします。 注)karenda(int total)にはprintfを使わず完成させたいです。

  • カレンダー作成

    C言語初心者です。 西暦と月を入力してその月のカレンダーを作成するプログラムの問題なのですが #include <stdio.h> #define MMAX 12 #define COMP (year - 1) int main(void) {  int i, j, year, month, day, youbi, ycnt, mcnt = 0;  int mday[MMAX] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  printf("西暦と月を入力して下さい-->");  scanf("%4d%2d, &year, &month");  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)   mday[1] = 29;  for (i = 0; i < [A]; i++) {   mcnt += mday[i];  }  mcnt++;  [B] = ((COMP + COMP / 4 - COMP / 100 + COMP / 400) + mcnt) % 7;  printf("\n%4d 年%2d 月\n", year, month);  printf("----------------------------\n");  printf(" 日 月 火 水 木 金 土\n");  for (j = 0, ycnt = 0; j < youbi; j++, ycnt++) {   printf(" ");  }  for (day =1; day <= mday[month - 1]; day++) {   if ([C])    printf("[%2d]", day);   else    printf(" %2d ", day);   ycnt++;   if ([D]) {    printf("\n");    ycnt = 0;   }  }  return (0); } [A]、[B]、[C]、[D]に答えを入れなきゃいけないのですが私が考えた答えだと カレンダーの表示すらされません。どこがいけないのでしょうか? #include <stdio.h> #define MMAX 12 #define COMP (year - 1) int main(void) {  int i, j, year, month, day, youbi, ycnt, mcnt = 0;  int mday[MMAX] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  printf("西暦と月を入力して下さい-->");  scanf("%4d%2d, &year, &month");  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)   mday[1] = 29;  for (i = 0; i < month; i++) {   mcnt += mday[i];  }  mcnt++;  youbi = ((COMP + COMP / 4 - COMP / 100 + COMP / 400) + mcnt) % 7;  printf("\n%4d 年%2d 月\n", year, month);  printf("----------------------------\n");  printf(" 日 月 火 水 木 金 土\n");  for (j = 0, ycnt = 0; j < youbi; j++, ycnt++) {   printf(" ");  }  for (day =1; day <= mday[month - 1]; day++) {   if (youbi = 0)    printf("[%2d]", day);   else    printf(" %2d ", day);   ycnt++;   if (youbi > 7) {    printf("\n");    ycnt = 0;   }  }  return (0); } よろしくお願いします。

  • C言語で分からないところがあるのですが……

    C言語で分からないところがあるのですが…… すみません。C言語を学習していてつまづいたので、皆さんの意見を聞きたいと思います。 現在、カレンダーを表示するプログラムをつくっています。 Yearとmonthをユーザが入力すると、その年その月のカレンダーが出るという算段です。(画像貼っておきます。ソースコードは下) これはできました。 これをいじって、Yearをユーザから受け取ると、その年の1月から12月までのカレンダーがば~っと表示されるようにしろ、と言われました。 Yearとmonthを受け取って書くやつは友人の助けを得ながらなんとかかけましたが、もう無理です。多分、for文を使うんだと思うんですが……助けてください! #include <stdio.h> int dayofweek(int year, int month); int daysinmonth(int year, int month); int daysinyear(int year); void showcal(int dow, int days); int main(void) { int year, month; int dow; int dim; printf ("Year?: "); scanf ("%d", &year); printf ("Month?: "); scanf ("%d", &month); dow = dayofweek(year, month); dim = daysinmonth(year, month); showcal(dow, dim); return 0; } void showcal (int dow, int days) { int i, j, d; printf ("Su Mo Tu We Th Fr Sa\n"); d = 1; for (i = 0; i < dow; i++) { printf (" "); } for (; i < 7; i++) { printf (" %d ", d); d++; } printf("\n"); for (j = 0; d <= days; j++) { for (i = 0; i < 7 && d <= days; i++) { if (d < 10) printf (" %d ", d); else printf ("%d ", d); d++; } printf("\n"); } } int daysinmonth( int year, int month) { int dim; dim = 31; if (month == 4 || month == 6 || month == 9 || month == 11) dim = 30; if (month == 2) { if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) dim = 29; else dim = 28; } return dim; } int daysinyear( int year) { int diy; if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) diy = 366; else diy = 365; return diy; } int dayofweek (int year, int month) { int dow; int days; int y, m; if (year >= 2000) { days = 0; for ( y = 2000; y < year; y++ ) { days = days + daysinyear(y); } for ( m = 1; m < month; m++ ) { days = days + daysinmonth(year, m); } dow = (6 + days) % 7; } else { days = 0; for ( m = month; m <= 12; m++ ) { days = days + daysinmo

  • C言語でのカレンダーの表示について質問です

    VisualStudioを使ってC言語でコンソールアプリケーションのカレンダーを作っています。 やりたいことは以下の通りです。 プログラムを起動すると、現在の月を中央に三ヶ月分のカレンダーを表示。 Lキーでカレンダー全体を一ヶ月前に変更 Rキーで一ヶ月後に変更 Dキーで三ヶ月前に変更 Uキーで三ヶ月後に変更 Aキーで一年前に変更 Bキーで一年後に変更 起動時にカレンダー表示するのはできているんですが、Rでカレンダーを進めていくと12月の次が13月、次が0月、その次が1月となってしまい、日付も上手いこと表示されません。 L,D,U,でも同じようになります。 また、A,Bで年を変えると日付が正確に表示されないです。 これらを正しく表示させる方法をご教授いただけますでしょうか。 #include <stdio.h> #include <conio.h> #include <time.h> #define KEY_ESC (0x1b) // エスケープ #define KEY_L (0x6c) // L #define KEY_D (0x64) // D #define KEY_R (0x72) // R #define KEY_U (0x75) // U #define KEY_A (0x61) // A #define KEY_B (0x62) // B time_t timer; // 現在の時刻 struct tm local; // 地方時 int curr_year; // 現在の年 int curr_mon; // 現在の月 int input; // どのキーが入力されたか int calendar[12][6][7]; // 月、最大の週、1週間 int year; // 年 int month; // 月 int day; // 日 int nweek; // その月が何周あるか int nday; // その月が何日まであるか int day_of_week; // その月が何曜日から始まるか void CalendarDisplay(void); void CalendarSet(void); void DayOfWeek(int, int, int); void NumOfDay(int); void KeyOperat(int); int LaepYear(int, int); int Zeller(int, int, int); int main(void) { CalendarDisplay(); while (1) { printf("ESCキーで終了\n\n"); input = _getch(); if(input == KEY_ESC) { break; } else if(input == KEY_L || input == KEY_D || input == KEY_R || input == KEY_U || input == KEY_A || input == KEY_B) { KeyOperat(input); } } return 0; } // カレンダーの表示 void CalendarDisplay(void) { // 現在時刻を取得 timer = time(NULL); // 地方時に変換 localtime_s(&local, &timer); // 現在の年 curr_year = local.tm_year + 1900; // 現在の月 curr_mon = local.tm_mon + 1; CalendarSet(); } // 当月とその±1の月を設定 void CalendarSet(void) { for (month = curr_mon-1; month <= curr_mon+1; month++) { // 12月を超えたら次の年に if(curr_mon > 12) { curr_mon = 1; month = 1; curr_year += 1; } // 1月を超えたら前の年に else if(curr_mon < 1) { curr_mon = 12; month = 12; curr_year -= 1; } // その月が何日まであるか求めて代入 nday = LaepYear(curr_year, month); // その月が何曜日から始まるか day_of_week = Zeller(curr_year, month, 1); nweek = 0; for (day = 1; day <= nday; day++) { calendar[month-1][nweek][day_of_week] = day; //printf("day-> %d ", day); if (++day_of_week == 7) { day_of_week = 0; nweek++; } } } DayOfWeek(month, curr_mon, curr_year); } // 月と曜日の表示 void DayOfWeek(int m, int cur_m, int cur_y) { for (m = cur_m-1; m <= cur_m-1; m++) { printf("%d%6d月%16d%6d月%16d%6d月\n" "日 月 火 水 木 金 土  " "日 月 火 水 木 金 土  " "日 月 火 水 木 金 土\n", cur_y, m, cur_y, m + 1, cur_y, m + 2); NumOfDay(&calendar[m-1]); } } // 日数の表示 void NumOfDay(int calendar[][6][7]) { int max_w = 0, nmon = 0, w = 0; // その月の最大週の間 for (max_w = 0; max_w < 6; max_w++) { // 3ヶ月分表示 for (nmon = 0; nmon < 3; nmon++) { for (w = 0; w < 7; w++) { if (calendar[nmon][max_w][w]) { printf("%2d ", calendar[nmon][max_w][w]); } else { printf(" "); } } printf(" "); } printf("\n"); } } // キー操作でカレンダーの表示を変える void KeyOperat(int input) { do { switch(input) { case KEY_L: curr_mon -= 1; break; case KEY_R: curr_mon += 1; break; case KEY_D: curr_mon -= 3; break; case KEY_U: curr_mon += 3; break; case KEY_A: curr_year -= 1; break; case KEY_B: curr_year += 1; break; default : input = !input; break; } } while(!input); CalendarSet(); } // うるう年の判定 int LaepYear(int y, int m) { const int num_day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (m == 2 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) { return 29; } return num_day[m - 1]; } // ツェラーの公式から曜日を求める int Zeller(int y, int m, int d) { int dow;   // 1月と2月の場合、それぞれ前年の13月、14月とする if(m == 1 || m == 2) { y--; m = m + 12; } dow = (y + y/4 - y/100 + y/400 + (13*m+8)/5 + d) % 7; return dow; }

  • C言語でのカレンダープログラミングについて。

    今、C言語でカレンダーについてのプログラミングを行っています。 まず月を入力し表示させる。 その年がうるう年かどうか判定させる。 日曜~土曜を表示し、指定した月の日数を曜日に合わせて表示させる。 2008年1月1日は火曜日。 という事実を利用してよい。 という条件で、うるう年かの判定はあるものの年は2008年で固定です。 曜日、指定した月の日数を出すところまでいきましたが。 曜日に合わせて出す事ができません。 今は1月に合わせて、1日が火曜日のところに出るように配置してます。 が、次の週になったときにどのように改行指定すればいぃのかわりません。 また、来月以降の月初めの曜日指定もわからないです。 途中までのプログラムはこれです。 醜いかもしれませんが。 #include <stdio.h> int main(void) { int year=2008; int month,hantei=0,a,b,c; int uru[12]={31,29,31.30,31,30,31,31,30,31,30,31};/*うるう年の時の各月ごとの日数*/ int normal[12]={31,28,31,30,31,30,31,31,30,31,30,31};/*通常の年の各月ごとの日数*/ printf("表示させる月を入力してください。:"); scanf("%d",&month); if(year%4==0||year%100==0||year%400==0) { hantei=1;/*その年がうるう年かどうかを判定し、うるう年なら「hantei」に「1」が代入*/ } if(hantei==1)/*うるう年の場合。*/ { printf("%d年 %d月\n",year,month); printf("%d日\n",uru[month-1]); } else/*通常の年の場合。*/ { printf("%d年 %d月\n",year,normal[month-1]); printf("%d\n",normal[month-1]); } printf("日 月 火 水 木 金 土\n");/*曜日*/ if(hantei==1)/*うるう年の場合*/ { for(b=0;b<=7;b++) { printf(" ");/*火曜日のところまでスペースを空ける*/ } for(c=1;c<=uru[month-1];c++)/*日数表示*/ { printf("%2d ",c); } } return(0); } まだあまりC言語に詳しくないので、簡単に説明できる範囲で御願いします。 説明がわかりにくいかもしれませんが、すいません。

  • 配列を使ってカレンダーを表示したい。

    配列を使った場合のカレンダー表示方法で質問します。 現在、 Calendarクラスを使い、配列に日を入れていくようにしているのですが Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at CalendarSample.main(CalendarSample.java:33) のようなエラーが生じます。 以下のような手順でで考えることは可能なのでしょうか? よろしくお願いします。 Calendar cal = Calendar.getInstance(); //最終日 int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //月の週の合計数 int week = cal.get(Calendar.WEEK_OF_MONTH); //日を格納する配列 int[][] date = new int[week][7]; int n = 0; for(int d=1;d<=max;d++){ //年月日の設定 cal.set(2011,6,d); int youbi = cal.get(Calendar.DAY_OF_WEEK); date[n][youbi] =d; //日が、土曜になったら、配列の列を変える if(cal.get(Calendar.DAY_OF_WEEK)==6){ n+=1; } }

  • C言語

    #include <stdio.h> int main(void) { int month, days; puts("月を入力してください:"); scanf("%d",&month); // if(month == 2) days = 28; if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); days = 31; else days=30; printf("%d月は%d日あります。\n", month, days); return(0); } // 月と日数の場合わけをしたいのですがうまくいきません。どこをなおせばいいのか誰か教えてください!

  • C言語

    #include <stdio.h> int main(void) { int month, days; puts(\"月を入力してください:\"); scanf(\"%d\",&month); // if(month == 2) days = 28; if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12); days = 31; else days=30; printf(\"%d月は%d日あります。\\n\", month, days); return(0); } // 月と日数の場合わけをしたいのですがうまくいきません。どこをなおせばいいのか誰か教えてください!

  • java Calendar作成

    javaでのカレンダー作成についての質問です。 表示イメージ(_はすべて半角スペースの意です) year年month月(今回は2016年04月) _日_月_火_水_木_金_土 _______________1__2_ _3__4__5__6__7__8__9_ 10_11_12_13_14_15_16_ 17_18_19_20_21_22_23_ 24_25_26_27_28_29_30_ public class CalShow { public static void main(String[] args) { java.util.Calendar cal = java.util.Calendar.getInstance(); int year = Integer.parseInt(args[0].substring(0, 4)); int month = Integer.parseInt(args[0].substring(4)); cal.set(java.util.Calendar.YEAR, year); cal.set(java.util.Calendar.MONTH, month - 1); cal.set(java.util.Calendar.DAY_OF_MONTH, 1); //日曜日=1で土曜日=7まで int week = cal.get(java.util.Calendar.DAY_OF_WEEK); //月末日 int lastDay = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH); System.out.println( year + "年" + month + "月"); System.out.println("_日_月_火_水_木_金_土"); //日付のない日数分回す for (int i = 1; i < week ; i ++) { System.out.print("___(半角3マス)"); } String empty; //1日から月末日まで、カレンダーを表示する for (int day = 1 ; day <= lastDay ; day ++) { empty = ""; if (day < 10) { empty = "_"; } System.out.print( empty + day + "_"); ●if ( == java.util.Calendar.SATURDAY) { System.out.println(""); } } } } ●部分で土曜日なら改行、としたいのですが、 上でint weekで土曜日=7と判明しているため、 if ( week == java.util.Calendar.SATURDAY) { とか if ( day % 7 == java.util.Calendar.SATURDAY) { など色々考え付くものを書き込んでいるのですが全く上手く動作しません。 どうしたらよいのかご教授お願いいたします・・・;;

    • ベストアンサー
    • Java