c語言農歷萬年歷
『壹』 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語言怎麼編寫萬年歷
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;
}
(2)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語言編寫萬年歷
#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語言代碼
除了天數差計算,基本上是完成了你所提出的功能(天數差計算其實已經提供了年內序數計算方方法,可以在此基礎上完善即可)。包含星期計算、公歷轉農歷、農歷轉公歷、節氣查詢。數據與演算法均為原創,絕非網上復制而來。附消態瞎件中有源代碼,同時附帶農歷數據提取工具,可提取1600到7000年之間的任意年份區間,提取後的數據為壓縮數據,佔用空間小,同時數據包含數九、三伏、入梅和出梅,非常適合寫一份完整的萬年歷。
吧。提取碼:ooj7『伍』 C語言萬年歷
PIC16F877A LCD1602液晶式萬年歷
默認分類 2011-03-23 23:27:51 閱讀10 評論0 字型大小:大中小 訂閱
#include<pic.h>#define uchar unsigned char#define uint unsigned int#define RS RA1#define RW RA2#define E RA3#define s1 RB1#define s2 RB2#define s3 RB3 __CONFIG(0x1832);int Hours=0;//定義小時int Minutes=0;//定義分鍾int Seconds=0;//定義秒int Day=20;int Months=03;int Years=2011;const char Table[]="0123456789"; ////1602:0-9 數字uint Y[16]; //8位元組的顯示緩沖區uchar set_2=0;int incone(int n);int decone(int m);void delay(uint x)///延時 { uint a,b; for(a=x;a>0;a--) for(b=30;b>0;b--); }
void interrupt time0 (){uchar temp,intnum; T0IF=0; TMR0=63; intnum++; if(intnum==40) { intnum=0; Seconds++; if(Seconds==60) { Seconds=0; Minutes++; if(Minutes==60) { Minutes=0; Hours++; if(Hours==24) { Hours=0; Day++; if(Day==31) { Day=0; Months++; if(Months==12) { Months=0; Years++; } } } } } } }
void write_Directive(uchar a)//寫指令{RS=0;RW=0;delay(1);E=0;PORTD=a;delay(1);E=1;delay(1);E=0;delay(1);}
void write_Data(uchar a)//寫數據{RS=1;RW=0;delay(1);E=0;PORTD=a;delay(1);E=1;delay(1);E=0;delay(1);
}void init()//初始化{ ADCON1=0X07; PORTB=0xFF; TRISB=0XFF; PORTC=0x00; TRISC=0X00; TRISD=0; TRISA=0; OPTION=0X06; TMR0=63; INTCON=0XA0;Y[14]=':';delay(15);write_Directive(0x38);delay(5);write_Directive(0x38);delay(5);write_Directive(0x38);write_Directive(0x01);write_Directive(0x02);//初始化後數據地址為0x80;即第一行,第一個位置write_Directive(0x0c);//不顯示游標}
void write_Data_String()//顯示時間函數{ Y[0]=Hours/10;//轉換成10進制顯示Y[1]=Hours%10;Y[2]=Minutes/10;Y[3]=Minutes%10;Y[4]=Seconds/10;Y[5]=Seconds%10;Y[6]=Years/1000;Y[7]=Years/100%10;Y[8]=Years/10%10;Y[9]=Years%10;//Y[6]=Years/1000;//Y[7]=Years%1000/100;//DispBuf[8]=Years%100/10;//DispBuf[9]=Years%10;Y[10]=Months/10;Y[11]=Months%10;Y[12]=Day/10;Y[13]=Day%10;Y[14]=':';Y[15]='.';Y[0]=Table[Y[0]];//查表取字元Y[1]=Table[Y[1]];Y[2]=Table[Y[2]];Y[3]=Table[Y[3]];Y[4]=Table[Y[4]];Y[5]=Table[Y[5]];Y[6]=Table[Y[6]];//查表取字元Y[7]=Table[Y[7]];Y[8]=Table[Y[8]];Y[9]=Table[Y[9]];Y[10]=Table[Y[10]];Y[11]=Table[Y[11]];Y[12]=Table[Y[12]];Y[13]=Table[Y[13]];write_Directive(0x80+0x04);//第一行,第五個位置地址
write_Data(Y[0]);//第一行,第五個位置write_Data(Y[1]);//第一行,第六個位置
write_Data(Y[14]);//第一行,第七個位置
write_Data(Y[2]);//第一行,第八個位置write_Data(Y[3]);//第一行,第九個位置
write_Data(Y[14]);//第一行,第十個位置
write_Data(Y[4]);//第一行,第十一個位置write_Data(Y[5]);//第一行,第十二個位置
write_Directive(0x80+0x43);//第二行,第四個位置地址
write_Data(Y[6]);//第二行,第四個位置write_Data(Y[7]);//第二行,第五個位置write_Data(Y[8]);//第二行,第六個位置write_Data(Y[9]);//第二行,第七個位置
write_Data(Y[15]);//第二行,第八個位置
write_Data(Y[10]);//第二行,第九個位置write_Data(Y[11]);//第二行,第十個位置
write_Data(Y[15]);//第二行,第十一個位置
write_Data(Y[12]);//第二行,第十二個位置write_Data(Y[13]);//第二行,第十三個位置}void key_set() //時間按鍵設置{if(s1 == 0){ delay(20); if(s1 == 0) { set_2++; while(!s1); if(set_2 == 8) { set_2 = 0; } }}}void set_time() //設置時間函數{ if(set_2==1) { RC0=1; } if(set_2==2) ////s2即RB1按1次調秒 { Seconds=incone(Seconds); if(Seconds>59) { Seconds=0; } Seconds=decone(Seconds); if(Seconds<0) { Seconds=59; } } if(set_2==3) ////s2即RB1按2次調分 { Minutes=incone(Minutes); if(Minutes>59) { Minutes=0; } Minutes=decone(Minutes); if(Minutes<0) { Minutes=59; } } if(set_2==4) ////s2即RB1按3次調時 { Hours=incone(Hours); if(Hours>23) { Hours=0; } Hours=decone(Hours); if(Hours<0) { Hours=23; } } if(set_2==5) ////s2即RB1按4次調天 { Day=incone(Day); if(Day>31) { Day=1; } Day=decone(Day); if(Day<1) { Day=31; } } if(set_2==6) ////s2即RB1按4次調月 { Months=incone(Months); if(Months>12) { Months=1; } Months=decone(Months); if(Months<1) { Months=12; } } if(set_2==7) ////s2即RB1按5次調年 { Years=incone(Years); if(Years>2050) { Years=1986; } Years=decone(Years); if(Years<1986) { Years=2010; } } }
int incone(int n) //加「1」函數{ if(s2==0) { delay(20); if(s2==0) { n++; while(!s2); write_Data_String(); } }}
int decone(int m) //減「1」函數{ if(s3==0) { delay(20); if(s3==0) { m--; while(!s3); write_Data_String(); } }}void main(){init();while(1) { key_set(); set_time(); write_Data_String(); }}