• 締切済み

javaでカレンダー作成

西暦年号・月を入力して、その月のカレンダーを出力するというものです。 <処理例> 指定日のカレンダーを出力します。 西暦年を入力:2007 月を入力:6 日 月 火 水 木 金 土 ______________________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 というものを出力したいです。下線は何もないという意味です。(見にくかったらすみません) 一応途中のプログラム載せます。 ~~~の部分を教えてください。 お願いします。 import java.io.*; public class Ex01a{ public static void main ( String[] args ) throws IOException{ int year, month, day, i, youbiNum=0; String ss; BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); System.out.println("指定月のカレンダーを出力します"); System.out.print("西暦年を入力:"); ss = kbd.readLine(); year = getInt(ss); System.out.print("月を入力:"); ss = kbd.readLine(); month = getInt(ss); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ day = 1; youbiNum=getyoubi(year,month,day); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } public static void printCal(int maxDay, int youbiNum){ int cal[] = new int[43]; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public static int getyoubi(int year, int month, int day){ int youbiNum, y1, nissuu=0; y1 = year-1; nissuu += y1 + y1/4 - y1/100 + y1/400+getJulian(year, month, day); youbiNum = nissuu % 7; return youbiNum; } public static int getJulian(int year, int month, int day){ int julian; julian = day; while(--month > 0){ julian+=getDay(year,month); } return julian; } public static int getDay(int year, int month){ int mmdd[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int mmdd_day; mmdd[2] += uruu(year); mmdd_day = mmdd[month]; return mmdd_day; } public static int getInt(String ss){ try{ return Integer.parseInt(ss); } catch(Exception e){ return 0; } } public static int uruu(int yy){ if(yy%4==0 && yy%100!=0 || yy%400==0) return 1; else return 0; } }

  • Java
  • 回答数2
  • ありがとう数2

みんなの回答

  • aoi2008
  • ベストアンサー率42% (6/14)
回答No.2

#1です 課題か何かですか?? ちょっと求めている答えと違うかもしれませんが、以下にソースを載せます 何かしら読み取ってもらえれば幸いです ---------------サンプル--------------- import java.io.*; public class Ex01a { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("指定月のカレンダーを出力します"); System.out.print("西暦年を入力:"); String line = reader.readLine(); int year = toInt(line); System.out.print("月を入力:"); line = reader.readLine(); int month = toInt(line); printCal(year, month); } public static void printCal(int year, int month) { int firstDayOfWeek = getDayOfWeek(year, month, 1); int lastDay = getLastDay(year, month); System.out.println("日\t月\t火\t水\t木\t金\t土"); int dayOfWeek = 0; for(int i = 0; i < firstDayOfWeek; i++){ System.out.print(" \t"); dayOfWeek++; } for(int i = 1; i <= lastDay; i++){ System.out.print(i + "\t"); dayOfWeek++; if(dayOfWeek == 7){ System.out.println(); dayOfWeek = 0; } } } /* * 指定した年月日の曜日を求める。戻り値と曜日の関係は * 日曜日:0~土曜日:6 */ public static int getDayOfWeek(int year, int month, int day) { if(month <= 2){ month += 12; year--; } int h = year/100, y = year%100; // ツェラーの公式 int dayOfWeek = y + y/4 + h/4 - 2*h + 13*(month+1)/5 + day; return (dayOfWeek+6)%7; } // 指定した年・月の最終日を返す public static int getLastDay(int year, int month) { int[] maxDay = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if(isLeapYear(year)) maxDay[2]++; return maxDay[month]; } // 引数の文字列をint型に変換する。数字で無かった場合は0を返す。 public static int toInt(String ss) { try { return Integer.parseInt(ss); } catch (Exception e) { return 0; } } // 引数の年がうるう年かどうか判定する public static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0); } } -----------------終了-----------------

san232
質問者

お礼

ありがとうございます。 課題なんですよ。 これでなんとかやってみます。

  • aoi2008
  • ベストアンサー率42% (6/14)
回答No.1

こんにちは Javaにはjava.util.Calendarクラスという便利なクラスがあります。 もちろん質問者さんのように曜日計算などのメソッドを書いても良いですが、Calendarクラスを使ったほうが全体もすっきりとして良いと思います。 参考までに

参考URL:
http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/java/util/Calendar.html
san232
質問者

お礼

java.util.Calendarクラスというのがあるんですね。 できればそれを使わずに作成したいんです。 お願いします。

関連するQ&A

  • カレンダー作成

    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); } よろしくお願いします。

  • 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
  • javaのカレンダー作成について

    コマンドライン引数で渡された6桁の数字をカレンダーに起こすプログラムを作成しています。 以下のように書きました。 ↓ public class Calendar { 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); int week = cal.get(java.util.Calendar.DAY_OF_WEEK); //日曜始まり一週間のセット作成 int weekset = 0; if (week == cal.SUNDAY) { weekset = 0; } else if (week == cal.MONDAY) { weekset = 1; } else if (week == cal.TUESDAY) { weekset = 2; } else if (week == cal.WEDNESDAY) { weekset = 3; } else if (week == cal.THURSDAY) { weekset = 4; } else if (week == cal.FRIDAY) { weekset = 5; } else if (week == cal.SATURDAY) { weekset = 6; } //月末日 int lastDay = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH); //7日ごとに改行するカウンタ用意 int counter = 0; System.out.println( year + "年" + month + "月"); System.out.println(" 日 月 火 水 木 金 土"); //weekset分回す for (int i = 0; i < weekset; i++) { System.out.print(" "); counter ++; } //1日から月末日まで、カレンダーを表示する for (int day = 1 ; day <= lastDay; day ++) { if (day < 10) { System.out.print(" " + day + " "); } else { System.out.print( day + " "); } counter ++; if (counter == 7) { // 7日おきに改行する counter = 0; System.out.println(); } } } } きちんと動作してくれるのですが、2つ目のループ内の、 ************** for (int day = 1 ; day <= lastDay; day ++) { if (day < 10) { ●System.out.print(" " + day + " "); } else { ●System.out.print( day + " "); } ************** ●の部分の出力はループの外で一つに纏められるはずだと指摘されました。 dayの後ろの部分にだけなら、for文の前にString str = "";と宣言しておいて、 for (int day = 1 ; day <= lastDay; day ++) { str += " "; とすることで空白を付けられる気がしているのですが、前部分に空白を設定した上でまとめて出力する方法が分かりません。 お分かりになる方、どのようにすればよいのかご教授お願いいたします。 (また、上記の方法自体全く的外れということでしたらそれを含めてご教授お願いいたします・・・;;)

    • ベストアンサー
    • Java
  • プログラムの内容

    以下のプログラムは答えとして渡されたものなんですが、 コマンド引数を使ったプログラムの流れがよくわかりません。 簡単に解説してくれる方、いませんか? #include <stdio.h> int leapyear(int year){ if(year%400 == 0) return 1; else if(year%4 == 0 && year%100 != 0) return 1; else return 0; } void addmonth(int *day, int *month, int *year){ *day = 1; (*month)++; if(*month > 12) { *month = 1; (*year)++; } } int main(int argc, char *argv[]){ int year, month, day, a=0, max_days; int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; if(argc < 2) max_days = 10000; else max_days = atoi(argv[1]); printf("今日は西暦何年 何月 何日? "); scanf("%d%d%d",&year,&month,&day); while(a<max_days) { a++; day++; if(month == 2 && leapyear(year)) { if(day > 29) addmonth(&day, &month, &year); } else { if(day > month_days[month-1]) addmonth(&day, &month, &year); } } printf("今日から%d日後は、%d年%d月%d日です。",max_days, year, month, day); }

  • java Calendarクラス

    javaで月、日を入力してカレンダーを作成したのですが 年と月のsetでmonth-1はマジックナンバーなので直したいのですが どなたかわかる方教えてください。 package sample; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Test { private final static int firstday = 1; public static void main(String[] args) { //カレンダーのインスタンスを取得します Calendar cal = Calendar.getInstance(); //文字入力 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //年を取得 int year =0; //月を取得 int month =0; //最後の日付 int lastDay =0; //月初めの曜日を取得 int week =0; //年妥当性チェック boolean CheckYear = true; //月妥当性チェック boolean CheckMonth = true; try { //年妥当性チェック while(CheckYear){ System.out.println("年を入力してください"); //年を入力します year = Integer.parseInt(input.readLine()); //年が4桁の場合 if(String.valueOf(year).length()==4){ CheckYear = false; }else{ System.out.println("年は4桁で入力してください"); } } //月妥当性チェック while(CheckMonth){ System.out.println("月を入力してください"); //月を入力します month = Integer.parseInt(input.readLine()); //月が1~12の場合 if(month>=1&&month<=12){ CheckMonth = false; }else{ System.out.println("月1~12を入力してください"); } } }catch(IOException e){ System.out.println("数字以外は入力しないでください"); System.out.println("処理を中断します"); return; }catch (Exception a) { System.out.println("数字以外は入力しないでください"); System.out.println("処理を中断します"); return; } //年、月をセットします cal.set(year,month-1,cal.getActualMinimum(Calendar.DATE)); //月初めの曜日を取得 week = cal.get(Calendar.DAY_OF_WEEK); //年月を出力する System.out.println(String.valueOf(year)+"年"+String.valueOf(month)+"月"); //曜日を出力する System.out.println("日 月  火  水  木  金  土"); //最後の日付を取得する lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); /* * 最後の日付を取得する */ if(month==1||month==3||month==7||month==8||month==10||month==12) { lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); }else if(month==4||month==6||month==9||month==11){ lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); }else if(year%4==0&&month==2){ lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); }else if(year%4!=0&&month==2){ lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); } /* * カレンダーを出力する */ //最後の日付まで繰り返す // 最後の日付まで繰り返す for (int i = 1; i <= lastDay; i++) { // 1日とそれ以外で分岐する if (i == 1) { // 1日の曜日位置まで移動する for (int j = 1; j < cal.get(Calendar.DAY_OF_WEEK); j++) { System.out.print(" "); } } else { // 日付を増やす cal.add(Calendar.DAY_OF_MONTH, firstday); } // 1~9と10~で表示を変える if (i < 10) { System.out.print(" " + i); } else { System.out.print(" " + i); } // 土曜日になったら改行する if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { System.out.println(""); } } } }

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

    C言語課題で「三次元配列を定義して、与えられた年の1年間分のカレンダーを作成する」という課題があるのですが三次元配列を使い一年分のカレンダーがなかなか出来ません。 教えていただけるとありがたいです。 どうかよろしくお願いします!!!! 現状はこの状態です。 #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 }; char *weekday[] = { "日,月,火,水,木,金,土" }; //曜日 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; { } return total; } /*カレンダー*/ void karenda(int total) { int month, week, day; int box = total % 7; char cal[N_MONTH][WEEK][N_WEEK]; //3次元配列の宣言 for (month = 0; month < N_MONTH; month++) { printf("%d\n", (month + 1)); //月を入力 for (week = 0; week < WEEK; week++) { for (day = 0; day < N_WEEK; day++) { if (day < box){ printf(" "); } } } } } よろしくお願いします。

  • 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言語で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です。今作成中の、カレンダーを表示させるプログラムの一部なのですが、それを以下に書きます。 ――――――――――――――― int day_of_week(int year, int month, int day) { int a, i, days1 = 0, days2 = 0, alldays, wk; a = (year - 1)/4; days1 = a * 366 + (year - 1 - a)*365 - (year - 1)/100 + (year - 1)/400; for(i=1; i< month ; i++) days2 += day_of_month(year, i); alldays = days1 + days2 + day; wk = alldays % 7; return(wk); } int write_cal(int year, int month) { /* !!! */ } ――――――――――― 関数write_calで、関数day_of_weekの戻り値wkをつかいたいのですが、!!! にそのままday_of_week(int year, int month, int day)を入れると当然コンパイルエラーになります。初歩的なことをわすれているのかもしれませんが、ポインタなどもつかっていいのでやりかた教えてください! int write_cal(int year, int month, int day) にするというのはナシでお願いします

専門家に質問してみよう