萬年歷c語言編程
❶ 用c語言怎麼編寫萬年歷
C語言編寫萬年歷的源代碼如下:
#include <stdio.h>
int week(int y,int m,int d);
int main()
{
char wk[7][3] = {{"天"},{"一"},{"二"},{"三"},{"四"},{"五"},{"六"}};
int year, month, day, w;
scanf("%d %d %d", &year, &month, &day);
w = week(year, month, day);
printf("%d年%d月%d日 星期%s", year, month, day, wk[w]);
}
int week(int y,int m,int d)
{
static int r[13]={0,0,3,3,6,1,4,6,2,5,0,3,5};
int c,w;
y %= 400;
if ((y==0||y%4==0&&y%100!=0)&&m<3)
c = 5;
else
c = 6;
w = (y+y/4-y/100+r[m]+d+c)%7;
return w;
}
(1)萬年歷c語言編程擴展閱讀
電子時鍾C語言版的源代碼如下:
#include<stdio.h>
#include<time.h>
#include<string.h>
int main()
{
char str[50];
time_t t;
struct tm *nt;
while(1)
{
system("cls");//清屏函數
t=time(NULL);
nt=localtime(&t);
strftime(str,50,"%Y-%m-%d %H:%M:%S 「,nt);
printf("現在時刻:%s ",str);
}
getch();
return 0;
}
❷ C語言編寫萬年歷~~速度
/*C語言編程萬年歷
要求輸入年月,判斷是否閏年;
輸入年月日,判斷星期幾;
輸入年份,打出12個月的月歷;
輸入年份,月份,列印出本月日歷;
要求用多個函數實現。*/
#include<stdio.h>
#include<time.h>
#include<string.h>int calendar[12][6][7];/*月歷*/
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satarday"};
char* Monthname[]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
int monthday[]={31,28,31,30,31,30,31,31,30,31,30,31};
char* menu[]={/*操作菜單*/
"1.Input a year number,check whether it's a leap year.\n",
"2.Input year,month,day,check the weekday.\n",
"3.Input year,month,output the calendar of that month.\n",
"4.Input year,output all the month calendar.\n",
"0.Exit.\n"
};/*判斷參數year傳遞的年份是否是閏年*/
int IsLeapyear(int year)
{
if(!(year%4)&&year%100||!(year%400)) return 1;
else return 0;
}/*輸入年月日,判斷星期幾,利用Zeller公式w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
w是結果星期數,y是年份的後兩位,c是年份的前兩位,m是月份,3≤m≤14,也就是當
m≤2時,要算到前一年的13月份和14月份,最後要將w對7取模
*/
int WeekDay(int year,int month,int day)
{
int w,y,c,m,d;
c=year/100;
if(month<3){
m=12+month;
y=year%100-1;
}
else{
m=month;
y=year%100;
}
d=day;
w=y+y/4+c/4-2*c+26*(m+1)/10+d-1;
return (w%7+7)%7;
}/*輸入年份,月份,列印出本月的日歷*/
void Monthly(int year,int month)
{
int weekday,i,j;
if(month==2)
if(IsLeapyear(year)) monthday[1]+=1;
weekday=WeekDay(year,month,1);
printf("%s\n",Monthname[month-1]);
printf("Sun. Mon. Tue. Wed. Thu. Fri. Sat.\n");
for(i=1,j=weekday;i<=monthday[month-1];i++,j++){
calendar[month-1][j/7][j%7]=i;
}
for(i=0;i<6;i++){
for(j=0;j<7;j++)
{
if(calendar[month-1][i][j]==0) printf("%5c",' ');
else printf("%-5d",calendar[month-1][i][j]);
}
printf("\n");
}
}void allMonth(int year) /*輸入年份,列印出12個月的月歷*/
{
int i;
for(i=1;i<=12;i++){
Monthly(year,i);
getch();/*按任意鍵繼續執行*/
}
}void main(void)
{
int year,month,day,i,n,weekday;
memset(&calendar,sizeof(calendar),0); /*初始化月歷*/
for(i=0;i<5;i++)
printf("%s",menu[i]);
while(1){
printf("Please choose the menu:");
scanf("%d",&n);
printf("\n");
switch(n){
case 1:
printf("Please input year:");
scanf("%d",&year);
if(IsLeapyear(year)) printf("\n%d is leap year.\n",year);
else printf("%d isn't leap year.\n",year);
break;
case 2:
printf("Please input year month day:");
scanf("%d%d%d",&year,&month,&day);
printf("\n");
weekday=WeekDay(year,month,day);
printf("That day is %s\n",week[weekday]);
break;
case 3:
printf("Please input year month,then it output a calendar of that month:");
scanf("%d%d",&year,&month);
printf("\n");
Monthly(year,month);
break;
case 4:
printf("Please input year,then it will output the calendar of that year:");
scanf("%d",&year);
printf("\n");
allMonth(year);
break;
case 0:
return;
default:
printf("The number you input is invalid.\n");
break;
}
}
getch(); /*按任意鍵,程序退出*/
}這個程序是可用的~~希望能幫到你~~~
❸ 鎬庝箞鐢–璇璦緙栧啓涓囧勾鍘嗙▼搴
涓囧勾鍘
#include "stdio.h" /* Required for MS-DOS use */
#define ENTER 0x1C0D /* Enter key */
int year, month, day;
static char *days[8] = {" ","Sunday ","Monday ","Tuesday ",
"Wednesday","Thursday ","Friday ","Saturday "};
struct TIMEDATE {
int year; /* year 1980..2099 */
int month; /* month 1=Jan 2=Feb, etc. */
int day; /* day of month 0..31 */
int hours; /* hour 0..23 */
int minutes; /* minute 0..59 */
int seconds; /* second 0..59 */
int hsecs; /* 1/100ths of second 0..99 */
char dateline[47]; /* date & time together */
};
static struct TIMEDATE today;
main()
{
char cmonth[3];
char cday[3];
char cyear[5];
double getdays();
double daynumb, numbnow;
int weekday, retcode, dayer, i;
dayer = datetime(&today);
clrscn();
for (i=0;i<3;++i)cmonth[i]='\0';
for (i=0;i<3;++i)cday[i]='\0';
for (i=0;i<5;++i)cyear[i]='\0';
putstr(5,8,14,"Enter date in MM DD YYYY format:");
while (retcode != ENTER)
{
retcode = bufinp(5,41,13,2,cmonth);
if (retcode != ENTER) retcode = bufinp(5,44,13,2,cday);
if (retcode != ENTER) retcode = bufinp(5,47,13,4,cyear);
}
year = atoi(&cyear);
month = atoi(&cmonth);
day = atoi(&cday);
daynumb = getdays(year, month, day);
numbnow = getdays(today.year, today.month, today.day);
weekday = weekdays(daynumb);
if (numbnow - daynumb == 0)
printf("\n\n%02d-%02d-%d is",month, day, year);
if (numbnow - daynumb > 0)
printf("\n\n%02d-%02d-%d was",month, day, year);
if (numbnow - daynumb < 0)
printf("\n\n%02d-%02d-%d will be",month, day, year);
printf(" a %s\n",days[weekday]);
} /* end MAIN */
/************************************************************
* GETDAYS - From integer values of year (YYYY), month *
* (MM) and day (DD) this subroutine returns a *
* double float number which represents the *
* number of days since Jan 1, 1980 (day 1). *
* This routine is the opposite of GETDATE. *
************************************************************/
double getdays(year, month, day)
int year, month, day;
{
int y,m;
double a,b,d, daynumb;
double floor(),intg();
/**********************************
** make correction for no year 0 **
**********************************/
if (year < 0) y = year + 1;
else y = year;
/*********************************************************
** Jan and Feb are months 13 and 14 in this calculation **
*********************************************************/
m = month;
if (month < 3)
{
m = m + 12;
y = y - 1;
}
/**************************
** calculate Julian days **
**************************/
d = floor(365.25 * y) + intg(30.6001 * (m + 1)) + day - 723244.0;
/**********************************************
** use Julian calendar if before Oct 5, 1582 **
**********************************************/
if (d < -145068.0) daynumb = d;
/*************************************
** otherwise use Gregorian calendar **
*************************************/
else
{
a = floor(y / 100.0);
b = 2 - a + floor(a / 4.0);
daynumb = d + b;
}
return(daynumb);
} /* end GETDAYS */
/********************************************************
* GETDATE - This routine takes a double float number *
* representing the number of days since Jan 1,*
* 1980 (day 1) and returns the year month and *
* day as pointer integers *
* This routine is the opposite of GETDAYS *
********************************************************/
getdate(numb)
double numb;
{
double a,aa,b,c,d,e,z;
double date;
date = numb;
z = intg(date + 2444239.0);
if (date < -145078.0) a = z;
else
{
aa = floor((z - 1867216.25) / 36524.25);
a = z + 1 + aa - floor(aa/4.0);
}
b = a + 1524.0;
c = intg((b - 122.1) / 365.25);
d = intg(365.25 * c);
e = intg((b - d) / 30.6001);
day = b - d - intg(30.6001 * e);
if (e > 13.5) month = e - 13.0;
else month = e - 1.0;
if (month > 2) year = c - 4716.0;
else year = c - 4715.0;
if (year < 1) --year;
return;
} /* end GETDATE */
/********************************************************
* WEEKDAYS - This routine takes a double float number *
* representing the number of days since Jan 1,*
* 1980 (day 1) and returns the day of the week*
* where 1 = Sunday, 2 = Tuesday, etc. *
********************************************************/
int weekdays(numb)
double numb;
{
double dd;
int day;
dd = numb;
while (dd > 28000.0) dd = dd - 28000.0;
while (dd < 0) dd = dd + 28000.0;
day = dd;
day = ((day + 1) % 7) + 1;
return(day);
}
/********************************************************
* FRACT - This routine takes a double float number *
* and returns the fractional part as a double *
* float number *
********************************************************/
double fract(numb)
double numb;
{
int inumb;
double fnumb;
while (numb < -32767) numb += 32767;
while (numb > 32767) numb -= 32767;
inumb = numb;
fnumb = inumb;
return(numb-fnumb);
} /* end FRACT */
/********************************************************
* FLOOR - This routine takes a double float number *
* and returns the next smallest integer *
********************************************************/
double floor(numb)
double numb;
{
double fract(), intg();
double out;
out = intg(numb);
if (numb < 0 && fract(numb) != 0) out -= 1.0;
return(out);
} /* end FLOOR */
/********************************************************
* INTG - This routine takes a double float number *
* and returns the integer part as a double *
* float number *
********************************************************/
double intg(numb)
double numb;
{
double fract();
return(numb - fract(numb));
} /* end INTG */
❹ C語言萬年歷代碼
#include"stdio.h"
#include"stdlib.h"
main( )
{
int Year,Month; //年、月
int FirstDay_Year,FirstDay_Month; //某年的第一天是星期幾,某年某月的第一天是星期幾(范圍是0~6,其中0代表星期日)
int IsLeapYear; //是否為閏年,0表示不是閏年,1表示是閏年
int i,d,y; //臨時變數
char YN; //Yes No,程序是否要繼續
int Days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//Days[1~12]存儲每個月有多少天,其中二月的天數是可變的(閏年29天,平年28天),這里初始化為28天
printf(" C語言簡單萬年歷\n"); //列印標題
XunHuan: //循環標號(可以通過goto跳轉到這里)
printf("請輸入年份<0000~9999>: "); //提示輸入年份(0~9999)
scanf("%d",&Year); //把輸入的年份賦值給變數Year
printf("請輸入月份<0~12>: "); //提示輸入月份(1~12)
scanf("%d",&Month); //把輸入的月份賦值給變數Month
y=Year;
FirstDay_Year=5*(y/4)+(y%4)-(y/100)+(y/400);//蔡勒公式(計算某年的第一天是星期幾)
IsLeapYear=(y%4==4&&y%100!=100||y%400==0)?1:0;
//判斷是否為閏年
Days[2]=(IsLeapYear==1)?29:28; //閏年二月29天,非閏年二月28天
for(i=1,d=0;i<Month;i++)
d=d+Days[i];
FirstDay_Month=(d+FirstDay_Year)%7; //當月的第一天是星期幾(0代表星期日)
printf("\n****************************************************\n");
printf("\t\t公元 %d 年 %2d 月",Year,Month); //列印年月
printf("\n****************************************************\n");
printf(" 星期日 星期一 星期二 星期三 星期四 星期五 星期六\n");
//列印星期表頭
for(i=0;i<FirstDay_Month;i++)
printf("%7c",' '); //當某月的第一天不是星期日時列印空格佔位
for(d=1;d<=Days[Month];d++) //循環,從每個月的第一天開始列印
{
printf("%7d",d);
if(((d+FirstDay_Month)%7)==0 && d<Days[Month])
printf("\n"); //當輸出了星期六而且還未輸出所有天數時,換行
}
printf("\n****************************************************\n");
printf("\n");
printf("是否繼續(Y/N)?\n");
scanf("%c",&YN);
scanf("%c",&YN);
if(YN=='Y' || YN=='y')
goto XunHuan;
}
❺ 萬年歷(C語言編程)
#include<stdio.h>
#include<time.h>
int
leap
(int
year)//判斷閏年
{
if(year%4==0&&year%100!=0||year%400==0)
return
1;
else
return
0;
}
int
days_month
(int
month,int
year)//判斷月
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return
31;
if(month==4||month==6||month==9||month==11)
return
30;
if(month==2&&leap(year)==1)
return
29;
else
return
28;
}
int
firstday(int
month,int
year)//判斷年
{
int
w;
w=(1+2*month+3*(month+1)/5+year+year/4+year/400-year/100)%7+1;
return
w;
}
main()
{
//調用系統時間
time_t
tval;
struct
tm
*now;
tval
=
time(NULL);
now
=
localtime(&tval);
printf("現在時間:
%4d年
%d月
%02d日
%d:%02d:%02d\n",
now->tm_year+1900,
now->tm_mon+1,
now->tm_mday,
now->tm_hour,
now->tm_min,
now->tm_sec);
//調用結束
int
i,j=1,k=1,a,b,month,year;
b=days_month(now->tm_mon+1,now->tm_year+1900);
a=firstday
(now->tm_mon+1,now->tm_year+1900);
printf("
Sun
Mon
Tue
Wed
Thu
Fri
Sat
\n");
if(a==7)
{
for(i=1;i<=b;i++)
{
printf("%4d",i);
if(i%7==0)
{
printf("\n");
}
}
}
if(a!=7)
{
while
(j<=4*a)
{
printf("
");
j++;
}
for(i=1;i<=b;i++)
{
printf("%4d",i);
if(i==7*k-a)
{
printf("\n");
k++;
}
}
}
printf("\n");
}
❻ 用C語言怎麼編寫這個萬年歷
核心演算法:星期(0~6)=(年份系數+月份系數+日期)%7;
其中,年份系數我以2007年做參照,已知2007年,年份系數是0,往前往後分別是遞減和遞增。增減規律:非閏年為±1,閏年1~2月-2或+1,閏年3~12月+2或者-1。
其中,12個月份系數對應:0,3,3,6,1,4,6,2,5,0,3,5。
根據以上規律,只要利用循環,就可以的到對應系數和星期。
日歷實現翻頁功能,按鍵盤左右方向鍵可以翻譯查看當年其他月份,按向上方向鍵可以返回菜單。
下面是代碼:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#define XY 2007
#define X07 0//以2007年年系數0做參照
int isLeapYear(int year);//判斷是否是閏年,是返回1,否返回0
int addyx(int yx,int n);//年系數自增,0~6,n:自增的跨度,返回自增後的年系數
int getYX(int year,int month);//獲得年系數0~6
int getMX(int month);//獲得月系數
int getWeek(int yx,int mx,int day);//通過年月系數(yx:年系數;mx:月系數)及日期獲取星期
char *getStrWeek(int yx,int mx,int day);//通過年月系數(yx:年系數;mx:月系數)及日期獲取星期對應的中文字元串
int getMaxDay(int year,int month);//通過年月獲取當月最大天數
void prCalendar(int year,int month);//列印日歷
int showMeun();//列印主菜單
int getDuration(int year,int month,int day);//通過日期獲取距今天數
int calendar[6][7];
int main()
{
//int year,month,day,yx,mx;
while(1)
{
if(!showMeun())
break;
}
return 0;
}
int showMeun()//列印主菜單
{
int n,year,month,day,yx,mx;
while(1)
{
system("cls");
printf("1、輸入年份,顯示該年每個月的日歷 ");
printf("2、輸入年月,顯示該月的天數 ");
printf("3、輸入日期,顯示距離今天的天數 ");
printf("4、輸入日期,查詢這一天是星期幾 ");
printf("0、結束程序 ");
n=-1,year=0,month=0,day=0;
while(n<0 || n>4)
{
printf("請輸入選擇的菜單序號:");
scanf("%d",&n);
}
switch(n)
{
case 1:while(year<=0 || year>9999)
printf("請輸入年份:"),scanf("%d",&year);
prCalendar(year,1);
break;
case 2:while(year<=0 || year>9999 || month<=0 || month>12)
printf("請輸入年份及月份:"),scanf("%d%d",&year,&month);
printf("%4d年%2d月共有%d天 按任意鍵繼續。。。。。。 ",year,month,getMaxDay(year,month)),getch();
break;
case 3:while(year<=0 || year>9999 || month<=0 || month>12 || day<0 || day>getMaxDay(year,month))
printf("請輸入日期:"),scanf("%d%d%d",&year,&month,&day);
printf("距離今日有%d天 按任意鍵繼續。。。。。。 ",getDuration(year,month,day)),getch();
break;
case 4:while(year<=0 || year>9999 || month<=0 || month>12 || day<0 || day>getMaxDay(year,month))
printf("請輸入日期:"),scanf("%d%d%d",&year,&month,&day);
yx=getYX(year,month);
mx=getMX(month);
printf("%4d年%2d月%2d日是%s 按任意鍵繼續。。。。。。 ",year,month,day,getStrWeek(yx,mx,day)),getch();
break;
case 0:return 0;
}
}
return n;
}
void prCalendar(int year,int month)//列印日歷
{
char c1,c2;
int i,j,*p=NULL,yx,mx,cnt,w,maxDay;
while(1)
{
p=&calendar[0][0];
yx=getYX(year,month),mx=getMX(month);
cnt=0,w=getWeek(yx,mx,1);
maxDay=getMaxDay(year,month);
for(i=0;i<6;i++)
for(j=0;j<7;j++)
{
if(cnt<w)
p++,cnt++;
calendar[i][j]=0;
}
for(i=1;i<=maxDay;i++)
*p=i,p++;
system("cls");
printf(" %4d 年%2d月 ",year,month);
printf("日 一 二 三 四 五 六 ");
for(i=0;i<6;i++,printf(" "))
for(j=0;j<7;j++)
if(calendar[i][j]==0)
printf(" ");
else
printf("%2d ",calendar[i][j]);
printf("<- 按方向鍵向左或向右翻頁 -> ");
printf(" 按向上方向鍵返回主菜單 ");
c1=getch();
c2=getch();
if(c1==-32 && c2==75)//左鍵
{
if(month==1)
month=12;
else
month--;
}
if(c1==-32 && c2==77)//右鍵
{
if(month==12)
month=1;
else
month++;
}
if(c1==-32 && c2==72)//上鍵
{
showMeun();
break;
}
}
}
int getDuration(int year,int month,int day)//通過日期獲取距今天數
{
int i,y,m,d,sum=0,sum2=0,minY,maxY,minM,maxM,minD,maxD;
time_t tt;
struct tm *tmp;
time(&tt);
tmp=localtime(&tt);
y=1900+tmp->tm_year,m=1+tmp->tm_mon,d=tmp->tm_mday;//獲取當前日期的年月日
if(year<y)
minY=year,maxY=y,minM=month,maxM=m,minD=day,maxD=d;
if(year>y)
minY=y,maxY=year,minM=m,maxM=month,minD=d,maxD=day;
if(year!=y)
{
for(i=minY;i<maxY;i++)//按相差年份累加(不包含右端最大年份)
if(isLeapYear(i))
sum+=366;//閏年
else
sum+=365;
for(i=1;i<minM;i++)//扣除左端年份已過月份對應天數
sum-=getMaxDay(minY,i);
sum-=minD;//扣除左端當月已過天數
for(i=1;i<maxM;i++)//累加右端最大年份已過月份對應天數(不包含最大年份當月)
sum+=getMaxDay(maxY,i);
sum+=maxD;//累加右端當月已過天數
}
else//如果年份相同,累加兩邊一年內已過天數,求差值
{
for(i=1;i<month;i++)
sum+=getMaxDay(month,i);
sum+=day;
for(i=1;i<m;i++)
sum2+=getMaxDay(m,i);
sum2+=d;
return abs(sum2-sum);
}
return sum;
}
int getMaxDay(int year,int month)//通過年月獲取當月最大天數
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year) && month==2)
return days[month-1]+1;
return days[month-1];
}
char *getStrWeek(int yx,int mx,int day)//通過年月系數(yx:年系數;mx:月系數)及日期獲取星期對應的中文字元串
{
int w;
static char week[7];
memset(week,0,7);
strcpy(week,"星期");
w=getWeek(yx,mx,day);
switch(w)
{
case 0:strcat(week,"日");break;
case 1:strcat(week,"一");break;
case 2:strcat(week,"二");break;
case 3:strcat(week,"三");break;
case 4:strcat(week,"四");break;
case 5:strcat(week,"五");break;
case 6:strcat(week,"六");break;
}
return week;
}
int getWeek(int yx,int mx,int day)//通過年月系數(yx:年系數;mx:月系數)及日期獲取星期對應的數值0~6
{
return (yx+mx+day)%7;
}
int getMX(int month)//獲得月系數
{
int xmonth[12]={0,3,3,6,1,4,6,2,5,0,3,5};//月系數:1-12月,每月的系數。
return xmonth[month-1];
}
int getYX(int year,int month)//獲得年系數0~6
{
int yx=0,nyear=XY,isly,flag;
if(nyear==year)
return 0;
if(nyear<year)
flag=0,nyear++;
else
flag=1,nyear--;
while(1)
{
isly=isLeapYear(nyear);
if(!isly){//非閏年年系數累加1
if(!flag)
yx=addyx(yx,1);
else
yx=addyx(yx,-1);
}
if(isly && month>=1 && month<=2){//閏年1~2月年系數累加1,3`12月年系數累加2
if(!flag)
yx=addyx(yx,1);
else
yx=addyx(yx,-2);
}
else if(isly && month>=3 && month<=12){
if(!flag)
yx=addyx(yx,2);
else
yx=addyx(yx,-1);
}
if(nyear==year)
break;
if(!flag && nyear<year)
nyear++;
if(flag && nyear>year)
nyear--;
}
return yx;
}
int addyx(int yx,int n)//年系數自增,0~6,n:自增的跨度,返回自增後的年系數
{
if(n>0)
{
if(yx+n<=6)
return yx+n;
if(yx+n>6)
return yx+n-7;
}
if(n<0)
{
if(yx+n>=0)
return yx+n;
if(yx+n<0)
return yx+n+7;
}
return -1;
}
int isLeapYear(int year)//判斷是否是閏年,是返回1,否返回0
{
if((year%4==0 && year%100!=0)||(year%400==0))
return 1;
return 0;
}
❼ 用c語言編寫萬年歷,要求輸出任意一年的某月,能顯示出這個月的日歷
1、首先要判斷一個年份是閏年還是平年,用一個子程序來做。