プロが教える店舗&オフィスのセキュリティ対策術

C言語のプログラミングで年と月を入力するとその月のカレンダーを表示するプログラムを完成させて貰いたいです!
西暦一年一月一日を月曜日としてカレンダーは日曜日から始まるものです!

#include <stdio.h>

int main(void)
{
int year,month;
int month[12]={31,28,31,30,31,30,31,31,30,
31,30,31};//それぞれの月の日の配列
int cut = 0;
int year,month,i;

int total=0;

scanf("%d",&year);//年を入力
printf("%d年");//入力された年の値を出力

scanf("%d",&month);//月を入力
printf("%d月");//入力された月の値を出力

printf(" SUN MON TUE WED THU FRI STA \n")
total=total+1;

for(i=1; i<year; i++){
total=+365;

if(year%400==0 || (year%100 != 0 && year%4 == 0))
total++;

for(i=0;i<month-1;i++){
total+=month[1];

if(month==2){
if(year%400 == 0 || (year%100 != 0 && year%4 == 0)){
      day = 29;
    }
    else{
      day = 28;
    }
}

for(i=1;cnt=0; i<=total%7; i++, cnt++)
printf(" ");
for(i=1; i<=month[month-1]; i++,cnt++)

if(cnt %7 == 0)
printf("\n");
printf("%3d",i);
}
printf("\n");
return 0;
}

A 回答 (3件)

丁度数日前、(今どき・苦笑)Pascalの勉強をしてて、それに似たような問題があったので、C言語に翻訳してみました。


参考までに。

/* ここから */

#include <stdio.h>
#include <stdlib.h>

typedef int yeartype;
typedef enum {
    Jan, Feb, Mar, Apr, May, Jun,
    Jul, Aug, Sep, Oct, Nov, Dec,
} monthtype;
typedef enum {
    Sun, Mon, Tue, Wed, Thu, Fri, Sat,
} weektype;
typedef int daytype;

int isLeapyear(yeartype year) {
 return (year % 4 == 0) & (year % 100 != 0) || (year % 400 == 0);
}

daytype getdays(yeartype year, monthtype month) {
 switch(month) {
 case Jan: case Mar: case May: case Jul: case Aug: case Oct: case Dec:
  return 31;
 case Feb: if (isLeapyear(year)) {
  return 29;
 } else {
  return 28;
 }
 default: return 30;
 }
}

daytype sum_of_days(yeartype year, monthtype month, daytype day) {
 monthtype m;
 daytype d = 0;
 switch(month) {
 case Jan: return day;
 case Feb: return getdays(year, Jan) + day;
 default:
 for (m = Jan; m < month; m++) {
  d += getdays(year, m);
 }
 return d + day;
 }
}

weektype day_of_week(yeartype year, monthtype month, daytype day) {
 yeartype y;
 daytype d = 0;
 for (y = 1; y < year; y++) {
 if (isLeapyear(y)) {
  d = (d + 366) % 7;
 } else {
  d = (d + 365) % 7;
 }
 }
 d += sum_of_days(year, month, day);
 switch (d % 7) {
 case 0: return Sun; case 4: return Thu;
 case 1: return Mon; case 5: return Fri;
 case 2: return Tue; case 6: return Sat;
 default: return Wed;
 }
}

void print(yeartype year, monthtype month) {
 int column, end_of_month;
 int i;
 puts(" SUN MON TUE WED THU FRI SAT");
 end_of_month = getdays(year, month);
 column = day_of_week(year, month, 1) + 1;
 if (column != 1) {
  for (i = 1; i < column; i++) printf(" ");
 }
 i = 1;
 while (!(i > end_of_month)) {
  while (!(column > 7 || i > end_of_month)) {
   printf("%4d", i); i++; column++;
  }
  puts(""); column = 1;
 }
}

int main(void) {
 int i, j, k;
 char s0[5], s1[3];
 puts("enter year");
 scanf("%4s%*[^\n]%*c", s0);
 i = atoi(s0);
 puts("enter month (1...12)");
 scanf("%2s%*[^\n]%*c", s1);
 k = atoi(s1);
 if ((k < 1) || (k > 12)) goto EXIT;
 switch(k) {
 case 1: j = Jan; break; case 5: j = May; break; case 9: j = Sep; break;
 case 2: j = Feb; break; case 6: j = Jun; break; case 10: j = Oct; break;
 case 3: j = Mar; break; case 7: j = Jul; break; case 11: j = Nov; break;
 case 4: j = Apr; break; case 8: j = Aug; break; default: j = Dec;
 }
 print(i, j);
 EXIT:
 return EXIT_SUCCESS;
}
    • good
    • 0

> int year,month;


> int month[12]={31,28,31,30,31,30,31,31,30,

monthという変数を何回も宣言、定義しています。
いらない宣言を削除したり、変数名を変更して下さい。

せめて、コンパイル通る程度まで修正してから質問する方が良いと思います。
    • good
    • 3

プログラミング言語学習の練習問題などでよくあるものですから、とりあえずGoogleなどで「カレンダー作成プログラム C言語」くらいのキーワードで検索してみましょう。


解説ページが幾つもヒットするはずです。
入出力仕様がご自身の考えるものとちょっと異なる場合は、そこだけ考えればOKでしょう。

参考まで。
    • good
    • 3

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!