⑴ 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(); /*按任意鍵,程序退出*/
}這個程序是可用的~~希望能幫到你~~~
⑵ 用C語言編寫萬年歷
#include <stdio.h>
#include <process.h>
int day_s();
int year_s();
int week_s();
void output_month();
void menu();
void output_year();
int year,month;
int main()
{
printf("請輸入年月以列印該月日歷!\n");
printf("年:");
scanf("%d",&year);
printf("月:");
scanf("%d",&month);
system("cls");
printf("\t\t公元%d年\n",year);
output_month();
menu();
return 0;
}
int day_s()
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;break;
case 4:
case 6:
case 9:
case 11:return 30;break;
case 2:
{
if(year%4==0&&year%100!=0||year%400==0) return 29;
else return 28;
break;
}
}
}
int year_s()
{
int s=1;
switch(month-1)
{
case 11:s+=30;
case 10:s+=31;
case 9:s+=30;
case 8:s+=31;
case 7:s+=31;
case 6:s+=30;
case 5:s+=31;
case 4:s+=30;
case 3:s+=31;
case 2:
{
if(year%4==0&&year%100!=0||year%400==0)s+=29;
else s+=28;
}
case 1:s+=31;
}
return s;
}
int week_s()
{
int s=1,n;
n=(year-1)%400;
for(int i=1;i<=n;i++)
{
if(year%4==0&&year%100!=0||year%400==0)
s+=2;
else s+=1;
}
s+=year_s();
s%=7;
return s;
}
void output_month()
{
int week,day,i;
printf("\t\t公元%d月\n",month);
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
day=day_s();
week=week_s();
for(i=1;i<=week;i++)
{
printf(" \t");
}
for(i=1;i<=day;i++)
{
printf("%d",i);
if((i+week)%7==0) printf("\n");
else printf("\t");
}
for(int j=1;j<=42-week-i;j++)
{
printf("\t");
}
printf("\n\n");
}
void output_year()
{
printf("\t\t公元%d年\n",year);
for(int i=1;i<=12;i++)
{
month=i;
output_month();
}
printf("1查看下年\n");
printf("2查看上年\n");
printf("3重新輸入年月\n");
printf("0退出\n");
printf("請選擇:");
int n;
scanf("%d",&n);
system("cls");
switch(n)
{
case 1:year+=1;output_year();break;
case 2:year-=1;output_year();break;
case 3:main();break;
case 0:;break;
}
}
void menu()
{
printf("1查看下個月\n");
printf("2查看上個月\n");
printf("3查看本年全部日歷\n");
printf("4重新輸入年月\n");
printf("0退出\n");
printf("請選擇:");
int n;
scanf("%d",&n);
system("cls");
switch(n)
{
case 1:
{
if(month+1==13)
{
year+=1;
month=1;
}
else month+=1;
printf("\t\t公元%d年\n",year);
output_month();
menu();
break;
}
case 2:
{
if(month-1==0)
{
year-=1;month=12;
}
else
{
month-=1;
printf("\t\t公元%d年\n",year);
output_month();
menu();
}
break;
}
case 3:output_year();break;
case 4:main();break;
case 0:break;
}
}
⑶ c語言設計萬年歷
/*不過除了沒有查詢某年某月某日是這一年的第幾天。。和判斷這一年的生肖外,都能滿足你的要求。
加點金幣幫你完善點!*/
#include<stdio.h>
int
Swiss(int
Years)
//判斷是否是閏年
{
if(!(Years%100))
{
Years=Years/100;
}
if(Years%4)
{
return
0;
}
else
{
return
1;
}
}
int
Number(int
Yearsa,int
Yearsb)
//已知兩個年份,求出兩個年份之間閏年的個數
{
int
i=Yearsa+1;
int
mou=0;
do{
if(Swiss(i))
{
mou++;
}
i++;
}while(i<Yearsb);
return
mou;
}
int
Mvalue(int
Years,int
Month,int
Day)
//已知年月日,求出某年某月某日是星期幾
{
int
M[12]={0,3,3,6,1,4,6,2,5,0,3,5};
//月值
int
N=6;
//年值初始化
int
a;
if(Years<2006)
//求年值,年值以2006年為基數
{
N=6-((2006-Years)%7)-Number(Years,2006);
if(Swiss(Years))
{
if(Month<3)
{
N--;
}
}
}
else
if(Years>2006)
{
N=((Years-2006)%7)-1+Number(2006,Years);
if(Swiss(Years))
{
if(Month>2)
{
N++;
}
}
}
a=(Day+M[Month-1]+N)%7;
//某年某月某日是星期幾=(日值+月值+年值)%7
return
a;
}
int
Amonth(int
Month)
//已知月,求出這個月是大月還是小月
{
switch(Month)
{
case
1:
case
3:
case
5:
case
7:
case
8:
case
10:
//1,3,5,7,8,10,12是大月,沒月31天
case
12:return
1;
case
4:
case
6:
case
9:
case
11:return
0;
//4,6,9,11是小月,每月30天
case
2:return
2;
//二月份
}
return
-1;
}
void
main
()
{
int
Dtable[7][7];
int
i,j;
int
Years=9999;
int
Month=12;
int
Day=1;
int
b;
for(i=0;i<7;i++)
//初始化數組
{
Dtable[0][i]=i;
}
for(i=1;i<7;i++)
{
for(j=0;j<7;j++)
{
Dtable[i][j]=0;
}
}
i=Mvalue(Years,Month,Day);
switch(Amonth(Month))
{
case
0:b=30;break;
case
1:b=31;break;
case
2:if(Swiss(Years))b=29;else
b=28;break;
//閏年2月29天,平年二月28天
default:b=-1;break;
}
for(;i<7;i++)
{
Dtable[1][i]=Day++;
}
for(i=2;i<7;i++)
{
for(j=0;j<7;j++)
//建造日歷表
{
if(Day<=b)
{
Dtable[i][j]=Day++;
}
else
{
continue;
}
}
}
printf("%d:%d\n",Years,Month);
for(i=0;i<7;i++)
//輸出日歷表
{
for(j=0;j<7;j++)
{
printf("%3d",Dtable[i][j]);
}
printf("\n");
}
getch();
//完畢!
}
⑷ c語言萬年歷程序
已經編譯運行確認過。
#include <stdio.h>
#include <stdlib.h>
int leap(int); /*判斷是否為閏年*/
int md(int m,int y); /*返回本月的天數*/
void prtweek(void); /*輸出表頭*/
int fw(int m,int y); /*返回本月一號是星期幾*/
void prtmonth(int m); /*輸出月份表頭*/
int main(void)
{
int y,m,i,test,week;
printf("請輸入您要查詢的年份: ");
scanf("%d",&y);
for (m=1; m<=12; m++) {
test=1;
prtmonth(m);
prtweek();
week=fw(m,y);
if (week!=6)
for (i=0; i<=week; i++) {
putchar('\t');
test++;
}
for (i=1; i<=md(m,y); i++,test++) {
printf("%d\t",i);
if (test%7==0) {
putchar('\n');
}
}
putchar('\n');
system("pause");//暫停,以便觀察輸出
}
return 0;
}
int leap(int y)
{
return (y%4==0&&y%100||y%400==0)?1:0;
}
int md(int m,int y)
{
int d;
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d=31;break;
case 4:
case 6:
case 9:
case 11:d=30;break;
case 2:d=leap(y)?29:28;break;
}
return d;
}
void prtweek(void)
{
printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n","Sun","Mon","Tue","Wed","Thur","Fri","Sat");
}
int fw(int m,int y)
{
int f;
if (m<3) {
m+=12;
y--;
}
f=(1+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
return f;
}
void prtmonth(int m)
{
switch (m) {
default:
case 1:puts("===January===");break;
case 2:puts("===February===");break;
case 3:puts("===March===");break;
case 4:puts("===April===");break;
case 5:puts("===May===");break;
case 6:puts("===June===");break;
case 7:puts("===July===");break;
case 8:puts("===August===");break;
case 9:puts("===September===");break;
case 10:puts("===October===");break;
case 11:puts("===November===");break;
case 12:puts("===December===");break;
}
}
⑸ c語言編寫萬年歷
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
typedef struct //定義一個接受輸入日期的結構體
{
int year;
int month;
int day;
}time2;
/*列印菜單*/
void menu()
{
system("cls");
printf("||===========================================||\n");
printf(" 1、輸出系統所對應的本月月歷。\n");
printf(" 2、查找某年某月。輸出其對應的月歷。\n");
printf(" 3、查找具體日期是星期幾?\n");
printf(" 0、退出本程序。\n");
printf("||===========================================||\n");
}
int year_leap(int year)//判斷輸入的年是否是閏年?
{
int i=0;
if(year%4==0&&year%100!=0||year%400==0)
i=1; //是閏年 為真。
return i;
}
int month_day(int year,int month)
{
int i;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//判斷本月是否為大月?
return i=31;
if(month==4||month==6||month==9||month==11) //判斷本月是否為小月?
return i=30;
if(month==2&&year_leap(year)) //判斷本月是否為閏年的二月?
return i=29;
else
return i=28;
}
/*查找每一天所對應的是星期幾?*/
int day_search(int year,int month,int day)
{
int c=0;
float s;
int m;
for(m=1;m<month;m++)
c=c+month_day(year,m);
c=c+day;
s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c;
return ((int)s%7); //返回要查找的日期是星期幾,
}
/*判斷輸入的年月是否符合習慣*/
time2 panan_year_month(int year,int month)
{
time2 b;
b.year=year;
b.month=month;
if(year<0)
{
system("cls");
printf("你輸入的年份有錯!!!\n請重新輸入年份:"); //判斷輸入的year是否符合習慣?
scanf("%d",&b.year);
}
if(month>12||month<1) //判斷輸入的month是否符合習慣?
{
system("cls");
printf("輸入的月份有錯!!!\n請重新輸入月份:");
scanf("%d",&b.month);
}
return b;
}
//輸出所查找的那天是星期幾?
void print_weekday()
{
FILE *fp;
char string[7];
time2 time,b;
system("cls");
printf("請輸入你要查找的年月日:");
scanf("%d%d%d",&time.year,&time.month,&time.day); //輸入年月日
if((fp=fopen("weekday.txt","r"))==NULL) //判斷能否打開文件
{
printf("cannot open this file\n");
exit(0);
}
b=panan_year_month(time.year,time.month);
if(time.day<1||time.day>month_day(time.year,time.month)) //判斷輸入的day是否符習慣,若不,則重新輸入。
{
system("cls");
printf("你輸入的日期有錯!!!\n請重新輸入:");
scanf("%d",&b.day);
}
else
b.day=time.day;
time=b;
fseek(fp,6*day_search(time.year,time.month,time.day),0);
fread(string,6,1,fp);
string[6]='\0';
printf("%d/%d/%d/是%s\n",time.year,time.month,time.day,string);
fclose(fp);
getch();
}
/*列印一個月的月歷。*/
void print_month(int a,int b)
{
int i,j=1,k=1;
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");
}
/*列印系統當月的月歷*/
void print_system_month()
{
int a,b;
system("cls");
/*調用系統時間*/
time_t tval;
struct tm *now;
tval=time(NULL);
now=localtime(&tval);
printf("現在時間: %4d年 %d月 %02d日\n",now->tm_year+1900, now->tm_mon+1, now->tm_mday);//結束調用系統時間,並輸出當前的系統月歷。
b=month_day(now->tm_year+1900,now->tm_mon+1);
a=day_search(now->tm_year+1900,now->tm_mon+1,1);
print_month(a,b);
getch();
}
/*輸出要查找的年月所對應的月歷。*/
void print_any_month()
{
time2 time;
int a,b;
system("cls");
printf("請輸入你要查找的年份:");
scanf("%d%d",&time.year,&time.month);
time=panan_year_month(time.year,time.month); //判斷輸入的月份是否正確?若不正確,那就重新輸入,並把修改後的結果賦給time
b=month_day(time.year,time.month);
a=day_search(time.year,time.month,1);
system("cls");
print_month(a,b); //輸出月歷
getch();
}
void print_xx() //輸出歡迎用語!
{
system("cls");
printf("||================================歡迎下次光臨!===============================||\n");
}
void choice() //選擇功能選項
{
int i;
while(1)
{
menu();
printf("請輸入你要選擇的功能所對應的數:\n");
i=getch();
if(i=='0') break; //判斷輸入的數是否為零,若是,退出循環。
switch(i)
{
case '1': print_system_month();break;
case '2': print_any_month();break;
case '3': print_weekday();break;
default: printf("沒有你要選擇的功能選項!!!!!\n");getch();
}
}
print_xx(); //調用函數輸出,致謝語。
}
/*在主函數中通過choice()函數調用其他功能函數。*/
void main()
{
choice();
printf("\n");
}
⑹ 萬年歷(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語言程序設計萬年歷怎麼編寫
#include <stdio.h>
int IsLeapYear(int);
main()
{
int i;
int day;
int year;
int temp;
int temp_i;
long int Year_days = 0;
int Year_Start = 1;
int Per_Year_Days;
int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29};
printf("Please enter the year: ");
scanf("%d",&year);
while(Year_Start < year)
{
if( IsLeapYear( Year_Start ) )
Per_Year_Days = 366;
else
Per_Year_Days = 365;
Year_days = Year_days + Per_Year_Days;
Year_Start++;
}
for( temp = 1; temp <=12; temp++ )
{
switch( temp )
{
case 1:
printf(" January(%d)\n",year);
break;
case 2:
printf(" February(%d)\n",year);
break;
case 3:
printf(" March(%d)\n",year);
break;
case 4:
printf(" April(%d)\n",year);
break;
case 5:
printf(" May(%d)\n",year);
break;
case 6:
printf(" June(%d)\n",year);
break;
case 7:
printf(" July(%d)\n",year);
break;
case 8:
printf(" August(%d)\n",year);
break;
case 9:
printf(" September(%d)\n",year);
break;
case 10:
printf(" October(%d)\n",year);
break;
case 11:
printf(" November(%d)\n",year);
break;
case 12:
printf(" December(%d)\n",year);
break;
}
i = Year_days % 7;
printf("Mon Tue Wed Thu Fri Sat Sun\n");
if( i != 0 )
for( temp_i = 0; temp_i < i; temp_i++)
printf(" ");
day = 1;
if( IsLeapYear(year) && temp == 2)
while( day <= month_day[12] )
{
if( day >1 )
if( Year_days % 7 == 0 )
printf("\n");
if( day >= 10 )
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;
}
else
while (day <= month_day[temp-1])
{
if( day > 1 )
if( Year_days % 7 == 0 )
printf("\n");
if( day >=10 )
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;
}
printf("\n");
if( getch() == 'q' )
exit(0);
}
getch();
}
int IsLeapYear( int year )
{
if ((year %4 == 0) && (year % 100 != 0) ||
(year % 400 == 0) )
return 1;
else
return 0;
}