c语言万年历编程解释

来源:百度知道 编辑:UC知道 时间:2024/06/04 02:24:35
#include <stdio.h>

int leap(int year )
{
if ((year %4 == 0) && (year % 100 != 0)
|| (year % 400 == 0))
{
return 1;
}
return 0;
}

void show(int year,int month)
{
const char month_str[][4]={"","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,wdays,mdays,days;

for(i=1,days=0;i<year;i++)
{
if(leap(i))
{
days += 366;
}
else
{
days += 365;
}
}
for(i=1;i<month;i++)
{
if(i==2 && leap(year))
{
days+=29;
}
else
{
days+=month_day[i];
}
}

printf(" %s (%d)&

首先从主函数入手
main()
{
int year,month; //初始化参数

printf("输入年和月份:"); //在屏幕上输入提示
scanf("%d%d",&year,&month); //输入年和月
show(year,month); //显示函数
}

然后进入show()函数中进行一系列的运算。

其实这个都挺简单的了,如果需要详细解答,加我。

哎 都一个样

#include <stdio.h>

int leap(int year ) //平闰年的判断
{
if ((year %4 == 0) && (year % 100 != 0) || (year % 400 == 0))
{
return 1;
}
return 0;
}

void show(int year,int month) //显示指定年制定月的日历
{
//定义12个月的英文字符串
const char month_str[][4]={"","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
//定义各个月的天数,二月为28天, 平年
const int month_day[]={30,31,28,31,30,31,30,31,31,30,31,30,31};
//分别定义:
int i;//该变量在后续代码中经常被使用,为计数变量