程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言問答 >> C語言 打印日歷

C語言 打印日歷

編輯:C語言問答

C語言 打印日歷

#include <stdio.h>
main()
{ int i,j,k,year,week;
int day[]={31,28,31,30,31,30,31,31,30,31,30,31};
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0))
day[1]++;
week=((year-1)+(year-1)/4-(year-1)/100+(year-1)/400)%7;
for(i=0;i<12;i++)
{
printf("*Mon Tue Wed Thu Fri Sat Sun\n");
for(k=0;k<week;k++) printf(" ");
for(j=1;j<=day[i];j++)
{
printf(" %3d",j);
if(++week>=7)
{
printf("\n");
week=week%7;
}
if(i==10)
}
printf("\n");
}
}

輸出後十月末尾多了一行怎麼改?

最佳回答:

#include <stdio.h>
void main()
{
int i,j,k,year,week;
int day[]={31,28,31,30,31,30,31,31,30,31,30,31};
char month[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};

printf("Input year you want to print:");
scanf("%d",&year);

if((year%4==0&&year%100!=0)||(year%400==0)) day[1]++; //判斷是否閏年
week=(35+year+year/4-year/100+year/400)%7; //計算當年的1月1日是周幾的公式

for(i=0;i<12;i++)
{
printf("\n%s\n",month[i]); //輸出月份的英文
printf("Sun Mon Tue Wed Thu Fri Sat\n"); //輸出表頭
for(k=0;k<week;k++) printf(" "); //每月一號對齊它的星期數
for(j=1;j<=day[i];j++)
{
printf("%3d ",j);
if(++week>=7)
{
printf("\n");
week=week%7;
}
}
printf("\n");
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved