C語言實(shí)現(xiàn)萬年歷源碼
更新時(shí)間:2019年10月30日 11:43:34 作者:不是培訓(xùn)筆記了
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)萬年歷源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)萬年歷的具體代碼,供大家參考,具體內(nèi)容如下
主函數(shù)所在源碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int GetWeek(int year,int month,int day);//求今天是周幾。周四就返回 4 。周日 返回 0;非法返回 -1;
int GetDaysInMonth(int year,int month);/*求指定月總共多少天*/
int CreateMonthData(int MonthDay[6][7],int year,int month);
void PrintMonth(int MonthDay[6][7]);
int main()
{
int MDate[6][7] = {{0}};
int y = 0;
int m = 0;
int ret = 0;
printf("Plear input year month:\n");
scanf("%d%d",&y,&m);
if(m <= 0 || m > 12)
{
printf("Your month is invalid\n");
return 1;
}
ret = CreateMonthData(MDate,y,m);
if(ret == 0)
{
PrintMonth(MDate);
}
return 0;
}
int CreateMonthData(int MonthDay[6][7],int year,int month)
{
int week = GetWeek(year,month,1);//返回第幾周。
int day = 1;
int i = 0;
int j = 0;
int daysInMonth = GetDaysInMonth(year,month);//當(dāng)月天數(shù)。
if(week < 0)
{
printf("GetWeek Failed\n");
return -1;
}
/*給第0行賦值*/
for(j = 0;j < 7;j++)
{
if(j < week)
{
MonthDay[0][j] = 0;
}
else
{
MonthDay[0][j] = day;
day++;
}
}
/*給第1~5賦值*/
for(i = 1;i < 6;i++)
{
for(j = 0;j < 7;j++)
{
if(day > daysInMonth)
{
MonthDay[i][j] = 0;
}
else
{
MonthDay[i][j] = day;
day++;
}
}
}
return 0;
}
/*將二維數(shù)組中本月日期按如下形式顯示:以2017年1月為例*/
/*
日 一 二 三 四 五 六
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 0 0 0 0
0 0 0 0 0 0 0
*/
void PrintMonth(int MonthDay[6][7])
{
int i=0;
int j=0;
printf(" 日 一 二 三 四 五 六\n");
for(i=0;i<6;i++)
{
for(j=0;j<7;j++)
{
printf("%2d ",MonthDay[i][j]);
}
printf("\n");
}
}
第二個(gè)文件源碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//潤年
int LeapDays[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
//非閏年
int CommonDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
//閏年返回 1 ,否則返回 0;
int IsLeapYear(int year)
{
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 1;
}
else
{
return 0;
}
}
// 輸入非法 返回 0;否則返回 1;
int IsValidDate(int year,int month,int day)
{
int ret = 1;
if(month < 1 || month > 12 || day < 1 || year <= 0)
{
return 0;
}
if(IsLeapYear(year))
{
if(day > LeapDays[month - 1])
{
ret = 0;
}
}
else
{
if(day > CommonDays[month - 1])
{
ret = 0;
}
}
return ret;
}
/*求這一天是當(dāng)年的第多少天*/
int GetDaysInYear(int year,int month,int day)
{
int sum=0;//總天數(shù)
int isrun=IsLeapYear(year);//閏年返回 1 ;否則返回 0;
int i=0;
int j=0;
if(isrun)
{
for(i=0;i<month-1;i++)
{
sum=sum+LeapDays[i];
}
}
else
{
for(i=0;i<month-1;i++)
{
sum=sum+CommonDays[i];
}
}
sum=sum+day;
return sum;
}
/*求指定月總共多少天*/
int GetDaysInMonth(int year,int month)
{
int isrun=IsLeapYear(year);//閏年返回 1 ;否則返回 0;
if(isrun)
{
return LeapDays[month-1];
}
else
{
return CommonDays[month-1];
}
}
//求今天是周幾。周四就返回 4 。周日 返回 0;非法返回 -1;
int GetWeek(int year,int month,int day)
{
int sum = 0;
if(0 == IsValidDate(year,month,day))
{
printf("Input date is invalid\n");
return -1;
}
sum = year -1;
sum = sum + sum / 4 - sum / 100 + sum / 400;
sum = sum + GetDaysInYear(year,month,day);
return sum % 7;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ new與malloc和delete及free動(dòng)態(tài)內(nèi)存管理及區(qū)別介紹
這篇文章主要介紹了深入理解C++中的new/delete和malloc/free動(dòng)態(tài)內(nèi)存管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
函數(shù)指針的強(qiáng)制類型轉(zhuǎn)換實(shí)現(xiàn)代碼
函數(shù)指針的強(qiáng)制類型轉(zhuǎn)換實(shí)現(xiàn)代碼。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-10-10
優(yōu)先隊(duì)列(priority_queue)的C語言實(shí)現(xiàn)代碼
本文簡要介紹一種基于數(shù)組二叉堆實(shí)現(xiàn)的優(yōu)先隊(duì)列,定義的數(shù)據(jù)結(jié)構(gòu)和實(shí)現(xiàn)的函數(shù)接口說明如下2013-10-10
C++ 關(guān)于STL中sort()對(duì)struct排序的方法
本篇文章介紹了,關(guān)于STL中sort()對(duì)struct排序的方法。需要的朋友參考下2013-04-04

