用C语言编写万年历程序

合集下载

C++_万年日历代码

C++_万年日历代码

C++实现控制台日历/*思路是1800 年1 月1 日是星期三当然你也可以自己定义某一天为起始日期然后求从某一天到1800 .1.1 的天数然后与7求余就是星期数*/#include<iostream>#include<iomanip>using namespace std;class Calen{private :int SpaceSize; // 输出长度setw()大小bool IsLeapYear(int year){ // 判断是否是闰年return ( ( 0 == year % 4 && 0 != year %100 ) || ( 0 == year % 400 ) );}int GetMonthDayNumber(int year , int month){ //返回某一年某一月天数int daynumber ;if( 2 == month ) { //二月单独处理if( IsLeapYear( year ) )daynumber = 29 ;elsedaynumber = 28 ;}else{if( ( month < 8 && 1 == month % 2) || ( month >= 8 && 0 == month % 2 ) ) // 八月前奇数月为31 天八月后偶数为31 天daynumber = 31 ;if( ( month < 8 && 0 == month % 2) || ( month >= 8 && 1 == month % 2 ) ) // 八月前偶数月30 天八月后奇数月30 天daynumber = 30 ;}return daynumber;}public:int NowToDay(int year,int month ,int day); // 从某一天到1800 年1 月1 日有多少天bool InputDayIsTrue( int year , int month , int day ); // 判断输入的日期是否合法int FindDayWeekend( int year , int month ,int day ); //查找某一天是星期几int DisplayMonth( int year , int month ); //显示某一年的某一月的日历int DisplayYear( int year ); //显示某一年的日历};int main(){Calen calener;int year,month,day;cout<< "输入某一天输出当是星期几格式\"年月日\""<<endl;cin >> year >> month >> day ;if( true == calener.InputDayIsTrue( year,month,day ) ){calener.FindDayWeekend( year,month,day ) ;}else{cout<< "The Input Day Is Error" << endl;}cout <<"输入某一年输入出某一年的日历"<< endl;cin >> year ;if( true == calener.InputDayIsTrue( year,1,1 ) ){calener.DisplayYear( year );}else{cout<< "The Input Day Is Error" << endl;}cout<< "输入某一年的某一月打印该月的日历"<< endl;cin >> year >> month ;if( true == calener.InputDayIsTrue( year,month,1 ) ){calener.DisplayMonth( year , month );}else{cout<< "The Input Day Is Error" << endl;}return 0;}int Calen :: NowToDay(int year,int month ,int day){int DiDay , i ;for( i = 1800 , DiDay = 0 ; i < year ; i++) // 闰年单独+ 366 天if( IsLeapYear( i ) )DiDay += 366 ;elseDiDay += 365 ;for( i = 1 ; i < month ; i++ )DiDay += GetMonthDayNumber( year , i );DiDay += day - 1 ;return DiDay;}bool Calen :: InputDayIsTrue( int year , int month , int day ){int TureDayNumber ;if( year < 1800 || month > 13 || day > 31 ){ // 如果输入的日期小于1800 年大于13 月大于31 天返回假return false;}else{TureDayNumber = GetMonthDayNumber( year, month ); // 如果输入的day 比该月的实际day 大返回错误if( day > TureDayNumber )return false ;}return true;}int Calen ::DisplayMonth(int year,int month ){int Excur , i , TotalDay ;SpaceSize = 4 ;Excur = ( NowToDay(year,month,1 ) + 3 ) % 7 ; // 首先计算这个月的1 日是星期几然后在这个基础上自加天数cout << year << "年" << month << "月" << endl;cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;cout << setw( SpaceSize ) <<"六" << endl;for( i = 1 ; i <= Excur ; i++)cout<< setw( SpaceSize ) << " "; //首先输出多余的空格TotalDay = GetMonthDayNumber( year , month );for( i = 1 ; i <= TotalDay ; i++){ // 然后自加,当然长度大于七的时候换行cout << setw( SpaceSize ) << i ;if( 0 == ( i + Excur ) % 7 ) //换行条件cout<< endl;}cout << endl ;return 0;}int Calen ::FindDayWeekend(int year , int month ,int day ){ //查找某一天是星期几int TotalDay ;char Weekend[ 7 ][ 3 ]={{"日"},{"一"},{"二"},{"三"},{"四"},{"五"},{"六"} } ;TotalDay = NowToDay(year,month,day) ;cout << year <<"年"<<month<<"月"<<day<<"日是星期" << Weekend[( TotalDay + 3 ) % 7 ]<<endl;return 0;}int Calen ::DisplayYear ( int year ){int i , j, k , n , month1 ,month2 ,month3 ; // 按照一个季度输出日历month1 month2 month3 是该月应该有天数int Excur1 , Excur2 , Excur3 ; // i , j , k 分别表示三个月的天数值Excur1,Excur2,Excur3 表示偏移量int sea;char Months[12][10]={{"一月"},{"二月"},{"三月"},{"四月"},{"五月"},{"六月"},{"七月"},{"八月"},{"九月"},{"十月"},{"十一月"},{"十二月"}};SpaceSize = 3 ;cout<< year << " 年" << endl;for( sea = 1 ; sea <= 12 ; sea += 3 ){ // 分四个季度显示Excur1 = ( NowToDay(year,sea , 1 ) + 3 ) % 7 ; // 分别计算3 个月的偏移量Excur2 = ( NowToDay(year,sea+1 , 1 ) + 3 ) % 7 ;Excur3 = ( NowToDay(year,sea+2 , 1 ) + 3 ) % 7 ;month1 = GetMonthDayNumber( year ,sea ); // 分别计算3个月的天数month2 = GetMonthDayNumber( year ,sea + 1 );month3 = GetMonthDayNumber( year ,sea + 2 ); //显示三个月的星期cout << Months[ sea - 1 ] << setw( 21 ) <<" " <<Months[ sea ] << setw( 20 ) <<" " <<Months[ sea + 1 ] << endl;cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;cout << setw( SpaceSize ) <<"六" << setw( SpaceSize ) <<" ";cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;cout << setw( SpaceSize ) <<"六" << setw( SpaceSize ) <<" ";cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;cout << setw( SpaceSize ) <<"六" << endl;for( n = 1 ; n <= Excur1 ; n++) // 第一行单独处理首先输出多余的空格然后在输出天数cout<< setw( SpaceSize ) << " "; // i ,j , k 单独保存当前的日期for( n ; n <= 7 ; n++)cout<< setw( SpaceSize ) << n - Excur1 ;cout<< setw( SpaceSize ) << " ";i = n - Excur1 ;for( n = 1 ; n <= Excur2 ; n++)cout<< setw( SpaceSize ) << " ";for( n ; n <= 7 ; n++)cout<< setw( SpaceSize ) << n - Excur2 ;cout<< setw( SpaceSize ) << " ";j = n - Excur2 ;for( n = 1 ; n <= Excur3 ; n++)cout<< setw( SpaceSize ) << " ";for( n ; n <= 7 ; n++)cout<< setw( SpaceSize ) << n - Excur3 ;cout<< endl ;k = n - Excur3 ;while ( i <= month1 || j <= month2 || k <= month3){ // 当三个月的日期都大于应有的天数后循环结束for( n = i ; i < n + 7; i++) // 接下来的7 天输出一次。

万年历c语言源程序

万年历c语言源程序

万年历c语言源程序#include stdio.h #include windows.h #include wincon.h #includestring.h #includestdlib.h #includeconio.h #includetime.h #includemath.h #define NULL 0*****_SCREEN_BUFFER_INFO csbiInfo; extern *****API HWND WINAPI GetConsoleWindow(); time_t T;struct tm * now; int min,sec;int hour,nowyear,nowmonth,nowday; void ti2() { time ( T ); now = localtime ( T ); hour=now-tm_hour%12; min=now-tm_min-15; sec=now-tm_sec-15; }void ti(){ time ( T ); now = localtime ( T ); hour=now-tm_hour; min=now-tm_min; sec=now-tm_sec; nowyear=now-tm_year+1900; nowmonth=now-tm_mon+1; nowday=now-tm_mday; }int yearbak=0; void clock_sz(); int pd( int year);void huizs(int line_head_month); void hzdy(int year,int nowmonth_hz); void menu(); void clear();int main(int argc, char* argv[]) { int x=0; ti();clock_sz(); hzdy(nowyear,nowmonth); menu(); return 0; }void clear() { int i; HWND hwnd; HANDLE hOut; HDC hdc;HBRUSH hbr; COORD pos={1,4}; hwnd=GetConsoleWindow(); hdc = GetDC(hwnd); hOut = GetStdHandle(STD_OUTPUT_HANDLE); hbr= CreateSolidBrush(RGB(0,0,0)); SelectObject(hdc,hbr); Rectangle(hdc,0,65,670,470); SetConsoleCursorPosition(hOut, pos); for(i=0;i i++) printf(“ \n "); SetConsoleCursorPosition(hOut, pos); Rectangle(hdc,0,65,670,470); ReleaseDC(hwnd, hdc); } void menu() { int year=0,xyear=0,x, xy=0,i ; struct re { int top; int left; int right; int bottom; }rt; HWND hwnd; HDC hdc,dc; HBRUSH hbr; COORD pos3_1 = {1, 4},pos3_2= {1, 1},pos = {1, 4},pos2_3 = {1, 6},pos2_4 = {1, 7}; INPUT_RECORD mouseRec; DWORD state = 0, res=0; POINT pt; HPEN pn;HANDLE hOut, hIn;hwnd=GetConsoleWindow();//得到控制台窗口句柄hdc = GetDC(hwnd); //得到画图设备句柄dc = GetDC(hwnd);//画刷hbr= CreateSolidBrush(RGB(255,0,0)); SelectObject(dc,hbr);for(rt.left=20,i=2;ii++) rt[i].left=rt[i-1].left+50;for(rt.right=50,i=2;ii++) rt[i].right=rt[i-1].right+50; //定位按钮for(i=0;ii++) rt[i].top=40; for(i=0;ii++) rt[i].bottom=60;hOut = GetStdHandle(STD_OUTPUT_HANDLE); hIn=GetStdHandle(STD_INPUT_HANDLE);pn=CreatePen(PS_SOLID,1,RGB(255,0,255)); GetClientRect(hwnd, rt); SelectObject(hdc, pn);SetBkMode(hdc,*****RENT); MoveToEx(hdc,1,35,NULL); LineTo(hdc,620,35); LineTo(hdc,620,62); LineTo(hdc,1,62); LineTo(hdc,1,35);pn=CreatePen(PS_SOLID,2,RGB(255,255,0));SelectObject(hdc, pn);MoveToEx(hdc,5,75,NULL); LineTo(hdc,45,75);hbr= CreateSolidBrush(RGB(0,0,190)); SelectObject(dc,hbr); for(i=1;ii++) Rectangle(dc,rt[i].left,rt[i].top,rt[i].right,rt[i].bottom);SetTextColor(hdc,RGB(0,255,255));TextOut(hdc,280,10,TEXT("万年历"),6);//输出文本SetTextColor(hdc,RGB(255,255,0));////设颜色TextOut(hdc,rt.left,rt.top,TEXT(" 1"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 2"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 3"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 4"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 5"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 6"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 7"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 8"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 9"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 0"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT("确定"),4);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT("清除"),4);//输出文本TextOut(hdc,40,350,TEXT("提示:双击窗口退出"),18);//输出文本SetConsoleCursorPosition(hOut, pos3_1); ShowWindow(hwnd,SW_SHOW); SetConsoleTextAttribute(hOut, 10); for(;;) { SetConsoleTextAttribute(hOut, 10); ReadConsoleInput(hIn, mouseRec, 1, res); Sleep(100); if (mouseRec.EventType == MOUSE_EVENT) { if (mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK) break; if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_*****) { GetCursorPos(pt); ScreenToClient(hwnd,pt); if(pt.x20 pt.x50 pt.yrt.top pt.yrt.bottom) { if(xyear4) printf("1"); xyear++; if(xyear==1) year=1*1000; if(xyear==2) year=year+1*100; if(xyear==3) year=year+1*10; if(xyear==4) { year=year+1; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.top pt.yrt.bottom) { if(xyear4) printf("2"); xyear++; if(xyear==1) year=2*1000; if(xyear==2) year=year+2*100;if(xyear==3) year=year+2*10; if(xyear==4){ year=year+2;printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom) { if(xyear4) printf("3"); xyear++; if(xyear==1) year=3*1000; if(xyear==2) year=year+3*100; if(xyear==3) year=year+3*10; if(xyear==4){ year=year+3; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("4"); xyear++; if(xyear==1) year=4*1000; if(xyear==2) year=year+4*100; if(xyear==3) year=year+4*10; if(xyear==4){ year=year+4; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("5"); xyear++; if(xyear==1) year=5*1000; if(xyear==2) year=year+5*100; if(xyear==3) year=year+5*10; if(xyear==4){ year=year+5;pt.yrt.top pt.yrt.top pt.yrt.topprintf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("6"); xyear++; if(xyear==1) year=6*1000; if(xyear==2) year=year+6*100; if(xyear==3) year=year+6*10; if(xyear==4){ year=year+6; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("7"); xyear++; if(xyear==1) year=7*1000; if(xyear==2) year=year+7*100; if(xyear==3) year=year+7*10;if(xyear==4){ year=year+7; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("8"); xyear++; if(xyear==1) year=8*1000; if(xyear==2) year=year+8*100; if(xyear==3) year=year+8*10; if(xyear==4){ year=year+8; printf("\n你选择的年为%d",year);pt.yrt.top pt.yrt.top pt.yrt.top} continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("9"); xyear++; if(xyear==1) year=9*1000; if(xyear==2) year=year+9*100; if(xyear==3) year=year+9*10; if(xyear==4){ year=year+9; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("0"); xyear++; if(xyear==1) year=0*1000; if(xyear==2) year=year+0*100; if(xyear==3) year=year+0*10; if(xyear==4){ year=year+0; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear=1year!=0) { clear(); yearbak=year; SetConsoleCursorPosition(hOut, pos3_2); printf("公元%d年",yearbak); SetConsoleCursorPosition(hOut, pos3_1); for(i=1;ii+=3) huizs(i);pt.yrt.top pt.yrt.top pt.yrt.topx=MessageBox(NULL,"是否显示后六个月?", "提示",MB_YESNO); if(x==6) { SetConsoleTextAttribute(hOut, 14); clear(); for(i=7;ii+=3) huizs(i); } } else printf("输入不合法,请点击清除重新输入"); continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.top pt.yrt.bottom){ xyear=0; year=0; clear(); clock_sz(); hzdy(nowyear,nowmonth);pn=CreatePen(PS_SOLID,2,RGB(255,255,0)); SelectObject(hdc, pn);//选画笔MoveToEx(hdc,5,75,NULL); LineTo(hdc,45,75); SetConsoleCursorPosition(hOut, pos); continue; } }; } } ReleaseDC(hwnd,hdc); }int pd( int year) { int pr=0; if((year%100==0)(year%400==0)) pr=1; else if(year%4==0year%100!=0) pr=1; else pr=0; return pr; } void huizs(int line_head_month) { int i=0,j=0,k=0,month=0,n=0,daysum=0,jlw=0;int monthhead={0},mlt={1,1,1}; int endline=0,test=0; int year=0; year=yearbak; for(i=1;iyear;i++) { if(pd(i)) daysum+=366; else daysum+=365; } j=daysum%7; monthhead=j; for(month=1;monthmonth++) { if(pd(year)month==2) n=29; else if(pd(year)==0month==2) n=28; else if(month==4||month==6||month==9||month==11) n=30; else n=31; monthhead[month]=(monthhead[month-1]+n)%7; } while(test=5){ for(month=line_head_month;month=(line_head_month+2);mo nth++) { if(month%3==1endline==0) { printf("\t%d月\t\t\t%d月\n",month,month+1,month+2); printf("一二三四五六日一二三四五六日三四五六日\n"); } if(endline==0) { for(k=0;kmonthhead[month-1];k++) printf(" "); j=monthhead[month-1]; } if(endline==0) i=1; else i=mlt[month-1]; for(;ji++,j++) { if(pd(year)month==2) n=29; else if(pd(year)==0month==2) n=28;%d月一二else if(month==4||month==6||month==9||month==11) n=30; else n=31; if(in) { for(k=0;kk++) printf(" "); break; } printf("%-3.0d",i); } printf(" "); mlt[month-1]=i; if(month%3==0) { printf("\n"); endline=1; } j=0; } test++; } }void hzdy(int year,int nowmonth_hz) { int i=0,j=0,k=0,month=0,n=0,daysum=0,jlw=0; int monthhead={0},mlt={1,1,1}; int endline=0,test=0; for(i=1;iyear;i++) { if(pd(i)) daysum+=366; else daysum+=365; } j=daysum%7; monthhead=j; for(month=1;monthmonth++) { if(pd(year)month==2) n=29; else if(pd(year)==0month==2) n=28;//平年else if(month==4||month==6||month==9||month==11) n=30; elsen=31; monthhead[month]=(monthhead[month-1]+n)%7; } if(pd(year)nowmonth_hz==2) n=29;//闰年else if(pd(year)==0nowmonth_hz==2) n=28;//平年else if(nowmonth_hz==4||nowmonth_hz==6||nowmonth_hz==9||no wmonth_hz==11) n=30;//4,6,9,11月else n=31; printf("\n\n\n\n\n\n\n\n 当前时间:%d年%d月%d日%d时%d分\n\n",nowyear,nowmonth,nowday,hour,min); printf("\t一二三四五六日\n\t"); for(k=0;kmonthhead[nowmonth_hz-1];k++) printf(" "); j=monthhead[nowmonth_hz-1]; for(i=1;ii++,j++) { if(j==7) { printf("\n\t"); j=0; } printf("%-3.0d",i); } printf("\n"); } void clock_sz() { float a,b,c; float x=0,y=0,ex=100,ey=160,radian=0,oldx,oldy,radius=90; float mx=0,my=0,oldmx,oldmy,mradius=40; float hx=0,hy=0,oldhx,oldhy,hradius=20; intkd_r1,kd_r2,kd_radius=170,kd_radius1,old_kd_x,old_kd_y,kd_radi an=0,k=0,q=0,oldk,oldq; HWND hwnd; HANDLE hOut; HDC hdc; *****F C1=RGB(0,0,0),C2=RGB(0,0,0); HBRUSH hbr,hbr1,hbr2; HPENhpn,gpn,rpn,ppn2,ypn,bpn,kdpn,rkdpn,dotpn,blue,black,blkdpn, pkdpn,hpn2; hwnd=GetConsoleWindow(); hdc = GetDC(hwnd);hOut = GetStdHandle(STD_OUTPUT_HANDLE); hbr= CreateSolidBrush(RGB(0,100,0)); hbr1= CreateSolidBrush(C1); hbr2= CreateSolidBrush(RGB(0,0,150)); hpn2=CreatePen(PS_SOLID,1,RGB(255,0,0));rpn=CreatePen(PS_SOLID,1,RGB(255,0,0));hpn=CreatePen(PS_SOLID,1,RGB(0,255,255));blue=CreatePen(PS_SOLID,3,RGB(0,0,255));bpn=CreatePen(PS_SOLID,1,RGB(255,255,255));gpn=CreatePen(PS_SOLID,2,RGB(0,255,0));pkdpn=CreatePen(PS_SOLID,3,RGB(0,255,255));kdpn=CreatePen(PS_SOLID,1,RGB(255,255,0));rkdpn=CreatePen(PS_SOLID,4,RGB(255,0,0));blkdpn=CreatePen(PS_SOLID,6,RGB(0,0,0));black=CreatePen(PS_SOLID,4,C1); ex=400;//时钟圆心位置ey=230;while(kd_radian=360){ if(kd_radian%5==0) { if(kd_radian%90==0){ kd_radius=130; SelectObject(hdc,rkdpn); } else{ kd_radius=125; SelectObject(hdc,pkdpn); } } else { kd_radius=130; SelectObject(hdc, kdpn); kd_radius1=120; } y=kd_radius*sin(kd_radian/180.0*3.14);x=kd_radius*cos(kd_radian/180.0*3.14);MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+x,ey+y); y=kd_radius1*sin(kd_radian/180.0*3.14);x=kd_radius1*cos(kd_radian/180.0*3.14); old_kd_x=x; old_kd_y=y; SelectObject(hdc,blkdpn); MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+old_kd_x,ey+old_kd_y); kd_radian+=6; };SetTextColor(hdc,RGB(0,255,0));}TextOut(hdc,ex+kd_radius+8,ey-7,TEXT("3"),1);TextOut(hdc,ex-4,ey+kd_radius+3,TEXT("6"),1); TextOut(hdc,ex-kd_radius-15,ey-5,TEXT("9"),1); TextOut(hdc,ex-7,ey-kd_radius-18,TEXT("12"),2); radius=110;mradius=90;//分针半径hradius=70;//时针半径x=0,y=0; radian=0;SetTextColor(hdc,RGB(0,255,225));////设颜色SetBkColor(hdc,RGB(0,0,0));////设颜色ti2();a=sec*6/180.0*3.14; b=min*6/180.0*3.14;c=(hour*30.0-90.0+(min+15)/2.0)/180.0*3.14; SetTextColor(hdc,RGB(0,255,255)); my=mradius*sin(b); mx=mradius*cos(b); SelectObject(hdc, gpn);MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+mx,ey+my); LineTo(hdc,ex+mx,ey+my); hy=hradius*sin(c); hx=hradius*cos(c);SelectObject(hdc, blue);MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+hx,ey+hy); ReleaseDC(hwnd, hdc);。

C语言万年历代码

C语言万年历代码

屌神NO.1#include <stdio.h>#include <stdlib.h>#include <stdlib.h>#include <conio.h> /清屏函数的头文件*/#define X " Sun Mon Tue Wed Thu Fri Sat"void menu();int getday(int year,int month,int date);int isleap(int year);void print(int n);int day(int year);void printcalendar(int year);void menu(){system("cls"); /*清除屏幕上所有文字*/printf("\nTHIS IS THE MENU OF THE PROGRAM !");printf("\nYOU CAN CHOOSE THE NUMBER FOR THE FUNCTIOM:");printf("\n\n");printf("1 Find the day by year-month-date.\n");printf("2 Find out whether the year you input is a leap year.\n");printf("3 Print the calendar of the year you input.\n");printf("4 Exit.\n\n");printf("Input your choice:");}/*函数定义,将在主函数中调用*/int getday(int year,int month,int date){int isleap(int year);int flag,s,i;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int cont=0;flag=isleap(year);if(flag==1){a[2]++; /*修改二月份天数*/}for(i=1;i<month;i++){cont=cont+a[i];}cont=cont+date;s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont;return s%7;}/*利用公式求星期几*/int isleap(int year){if(year%4==0&&year%100||year%400==0){return 1;}else{return 0;}}/*判定闰年*/void print(int n){int i;for(i=0;i<n;i++) printf(" ");}/*打印空格,排版使用*/int day(int year){long a,b;if(year<=2000){a=2000-year;b=6-(a+a/4-a/100+a/400)%7;return b;}else{a=year-2000;b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6;return b%7;}}/*打印日历函数*/void printcalendar(int year){int i,j,k,m,n,f1,f2,d;int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};printf("\nThe calendar of the year %d.\n\n",year);d=day(year);if(isleap(year)==1){a[2]++;}for(i=1;i<=12;i+=2){m=0; n=0; f1=0; f2=0;switch(i){case 1:printf(" Januray 1 ");break;case 3:printf(" March 3 ");break;case 5:printf(" May 5 ");break;case 7:printf(" July 7 "); break;case 9:printf(" September 9 ");break;case 11:printf(" Nevember 11 ");break;}print(21); /*打印空格,隔开奇偶月份名*/switch(i+1){case 2:printf(" February 2 "); break;case 4:printf(" April 4 "); break;case 6:printf(" June 6 "); break;case 8:printf(" August 8 "); break;case 10:printf(" October 10 "); break;case 12:printf(" December 12"); break;}printf("\n");printf(X); /*宏调用,打印星期字符串*/print(6); /*打印空格,隔开奇偶月份数据*/printf(X);printf("\n");for(j=0;j<6;j++){if(j==0){print(d*4); /*每个日期数采用个空格的宽度,即4字节。

C语言实训--万年历源代码

C语言实训--万年历源代码

C语言实训--万年历源代码# include#include#include#include#include# define YEAR 365int numday[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31}};int leap(int year);int calday(int y,int m);int counterdays(int year,int month,int day);int week(int year,int month,int day);int eachyeartotal(int year,int month,int day);int maxDay(int y,int month);void print(int y,int m,int w);void festival (int year,int month,int day);int IsLeap(int y);int guanli(int i);void rili(int year,int month);int rili1();void rili2();void rili3();void rili4();void rili5();void fan();void show(int year,int month,int date,int sec,int mini,int hour);int main(){int i;printf("***************************************\n"); printf("1.查询日历,输入年份:\n");printf("2.查询该月日历及对应星期,输入年份和月份:\n"); printf("3.查询星期几及节日,输入年月日:\n");printf("4.距您输入日期还有多少天,输入日期:\n"); printf("5.通过键盘上下控制不同年月日:\n");scanf("%d",&i);guanli(i);return 0;}int guanli(int i){switch(i){case 1:rili1();return main();case 2:rili2();return main();case 3:rili3();return main();case 4:rili4();return main();case 5:rili5();return main();}return 0;}int rili1(){int year;int n,i;printf("输入年份:");scanf("%d",&year);printf("the calendar of the year%d\n",year);printf("\n");printf("=============================\n"); for(i=1;i<13;i++){printf(" %d月份\n",i);rili(year,i);}n = leap(year);return 0;}void rili2(){int year,month,w;printf("输入年月:");scanf("%d%d",&year,&month);w=calday(year,month);print(year,month,w);}void rili3(){int year,month,day,number;printf("输入年月日:");scanf("%d%d%d",&year,&month,&day);festival(year,month,day);number= week(year,month,day);printf("星期%d",number);}void rili4(){int year,month,day;printf("输入年月日:");scanf("%d%d%d",&year,&month,&day); eachyeartotal( year,month,day);counterdays(year,month,day);}void rili5(){int year,month,date,sec,mini,hour;fan();show(year,month,date,sec,mini,hour);}int leap(int year){int n=(year%4==0 && year%100!=0 || year%100!=0); return n;}void rili(int year,int month){int i,day,first_day,flag,year1,year2;int temp = 6;flag =leap(year);year1=year/100;year2=year%100;if(year>2000){year1=year1-20;day = year1/4;day+=(YEAR+24)*year1+YEAR*year2+(year2-1)/4;if(year>2000)day++;switch(month){case 1:day; break;case 2:day+=31;break;case 3:day+=59+flag;break;case 4:day+=90+flag;break;case 5:day+=120+flag;break;case 6:day+=151+flag;break;case 7:day+=181+flag;break;case 8:day+=212+flag;break;case 9:day+=243+flag;break;case 10:day+=273+flag;break;case 11:day+=304+flag;break;case 12:day+=334+flag;break;}day+=temp;first_day=day%7;}else{year1 = 20-1-year;year2=100-year2-1;day+=(YEAR+24)*year1+YEAR*year2+year2/4; switch(month){case 12:day+=31;break;case 11:day+=61;break;case 10:day+=92;break;case 9:day+=122;break;case 8:day+=153;break;case 7:day+=184;break; case 6:day+=214;break; case 5:day+=245;break; case 4:day+=275;break; case 3:day+=306;break; case 2:day+=334+flag;break; case 1:day+=365+flag;break; }day-=temp;day = day%7;first_day=(7-day)%7;}switch(month){case 1:case 2:month=28+flag;break; case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:month=30;break; case 12:month =31;break;}for(i=1;i<4*first_day;i++) printf(" ");for(i=0;i<month;i++)< p="">{printf("%4d",i+1);if((first_day+i)%7==6)printf("\n");}printf("\n");printf("===============================\n");}///////////////////////////////////////////////////////////int counterdays(int year,int month,int day){int year1,days0,days1,month1,days,day1;time_t timep;struct tm*p;time(&timep);p=gmtime(&timep);year1=(1900+p->tm_year);month1=(1+p->tm_mon);day1=(p->tm_mday);days0=(year-1)*365 + (year-1)/4 - (year-1)/100 + (year-1)/400 +eachyeartotal (year,month,day);days1=(year1 - 1)*365+(year1-1)/4-(year1-1)/100 + (year1 - 1)/400 +eachyeartotal (year1,month1,day1);days=days0-days1;printf("\n今天距%d年%d月%d日还有%d天\n\n",year,month,day,days);return days;}int eachyeartotal(int year,int month,int day){int i,sum=0;for(i=1;i<month;i++)< p="">{sum+=maxDay(year,i);}sum+=day;return sum;}int IsLeap(int y){if(y%4==0 && y%100!=0 || y%400==0) return 1; else return 0;}int maxDay(int y,int month){int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};if(IsLeap(y))a[1]=29;return a[month-1];}///////////////////////////////////////void festival (int year,int month,int day){if (month==1 && day==1) printf (",这天是元旦");if (month==2 && day==14) printf (",这天是情人节"); if (month == 3)switch(day){case 8:printf (",这天是妇女节"); break;case 12:printf (",这天是植树节");}if (month==1 && day==1) printf (",这天是愚人节");if(month == 5)switch (day){case 1:printf(",这天是劳动节");break;case 4:printf(",这天是青年节");}if (month==6 && day==1) printf (",这天是儿童节");if (month==7 && day==1) printf (",这天是建党节");if (month==8 && day==1) printf (",这天是建军节");if (month==9 && day==10) printf (",这天是教师节");if (month==10 && day==1) printf (",这天是国庆节");printf("\n");}int week(int year,int month,int day){int number = (year-1)+(year-1)/4 - (year-1)/100+(year-1)/400+eachyeartotal(year,month,day);number = number%7;return number;}/////////////////////////////////////////////void print(int y,int m,int w){int i,l;printf("Calendar %d-%02d\n",y,m);printf("---------------------\n");printf("Su Mo Tu We Th Fr Sa \n");printf("---------------------\n");for (i=0;i<w;i++)< p=""> printf(" ");if (IsLeap(y))l=1;elsel=0;for (i=1;i<=numday[l][m-1];i++) {printf("%-3d",i);w++;if (w%7==0)printf("\n");}if (w%7) printf("\n");printf("---------------------\n"); }int calday(int y,int m){int d=0,w;while (y>1910){if (IsLeap(y)) d+=366;else d+=365;y--;}while (m>1){ --m;d+=numday[0][m-1];}w=(d+1)%7;return w;}//////////////////////////////////////////void show(int year,int month,int date,int sec,int mini,int hour) {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++)< p="">{if(leap(i))days += 366;}else{days += 365;}}for(i=1;i<month;i++)< p="">{if(i==2 && leap(year)){days+=29;}else{days+=month_day[i];}}printf(" %s.%d.%d %d:%d:%d \n",month_str[month],date,year,hour,mini,sec);printf(" 一二三四五六日\n");wdays = days % 7;for( j = 0; j < wdays; j++){printf(" ");}if(month == 2 && leap(year)){mdays=29;}else{mdays= month_day[month];}for(i=1;i<=mdays;i++){if( i > 1 && days % 7 == 0 ){printf("\n");}printf("%4d",i);days=days+1; }printf("\n---------------------------\n\n\n");}{time_t rawtime;struct tm *info;int year,month,date,sec,mini,hour; char ch;time ( &rawtime );info = localtime ( &rawtime );year =info->tm_year + 1900; month =info->tm_mon + 1;date =info->tm_mday;hour =info->tm_hour;mini =info->tm_min;sec =info->tm_sec;while(1){show(year,month,date,sec,mini,hour); printf("↑......上一年\n\n");printf("↓......下一年\n\n");printf("←....前一个月\n\n");pr intf("→....后一个月\n\n");printf("Esc.......退出\n\n");ch=getch();switch(ch){case 27://Ecs exit(0);case -32://Navigatorch=getch();if(ch==77){//Rightyear+=(month==12)?1:0;month=month%12+1;}else if(ch==75){year-=(month==1)?1:0; month=(month-2+12)%12+1; }else if(ch==72)year--;else if(ch==80)year++;system("cls");}}</month;i++)<></year;i++)<></w;i++)<></month;i++)<></month;i++)<>。

CC++程序设计——万年历(完整代码+实验报告)

CC++程序设计——万年历(完整代码+实验报告)

2、主要函数流程图: (1)类的构造函数:用于给类的对象赋值。提示用户输入数据,并具有较强的错误输 入数据检查功能。(参见图 3-1) (2)闰年判断函数:判断哪些年份为闰年。 (图 3-2)
正文 第 2 页(共 18 页)
开 始
开 始
year%4==0

输入待查询的 年份


year%100==0
* ","***** ","***** ","***** ","***** ","***** "," * ","***** ","***** "," * ","***** ","***** ","
"***** ","
=====================公元"<<year<<"年日历====================="<<endl; =====================公元"<<year<<"年日历====================="<<endl;
/*实现 3 维数组图案的输出*/ for(int g=0;g<5;g++) { cout<<" fout<<" for(int i=0;i<4;i++) for(int h=0;h<7;h++) { cout<<ss[g][num[i]][h]; fout<<ss[g][num[i]][h]; } cout<<endl; fout<<endl; } }

C语言课程设计万年历 完整版

C语言课程设计万年历  完整版

目录一引言 (2)二系统功能和数据说明 (3)一)功能简介 (3)二)程序中的数据说明 (3)三程序总体设计及流程图 (4)一)应用到的c语言 (4)二)程序的总框架 (5)四功能模块设计及调试 (5)一)算法说明 (5)1.总天数的算法 (5)2.计算输入日期是星期几 (6)3.对输入信息的汇总 (8)4..界面的控制 (10)二)调试结果 (11)五程序清单 (12)六结束语 (17)一引言通过大一上学期对C语言的学习,了解到了很多C语言的相关知识。

学习的过程有很多困惑但是当自己能够独立的看懂,能过独立的完成一个简单的程序时,心中就会收获无限的喜悦和成就感。

我可以里哟哦那个它看懂一些简单的程序,编写一些简单的计算程序,更多的是学会了一种思想——编程,它让我在去思考很多日常生活中的事物是怎么样通过一个个小小的函数实现功能的,激发我对探究的兴趣。

C语言是近年在国内外得到迅速推广应用的一种语言。

C语言功能丰富,表达能力强,使用灵活方便,应用面广,目标程序效率高,可移植性好,既具有高级语言的优点,又具有低级语言的许多特点。

因此,C语言特别适合于编写各种软件。

在这次的课程设计中我将把日常生活中最经常接触的——日期的查询利用C语言的程序编成一个简单的日历。

通过这个小小的日历可以实现很多功能。

在程序中你能看到很多熟悉的C语言关键字,同时也加入了很多自己课外了解到的一些关键字。

在不断的调试中最终才获得最为完整的程序。

接下来就是我的C 语言课程设计的具体内容来了二系统功能和数据说明(一)功能简介在我们的日常生活中能接触到很多不同类型的日历,在日历上我们通常希望它能简介明了的给我们最想要的日期信息。

在我的万年历当中,就是将日历,月历做的简单明了,很方便我们的使用。

下面是它要实现的一些基本功能:用C语言编写万年历1、输入年份,判断是否为闰年2、输入年月日,判断改日为星期几3、输入年份,打出12个月历,输入月份,打出该月的日历4、要求用多个函数实现[名称]万年历[修改]1、对输入的日期进行容错处理2、增加和修改为英文的月份和星期显示3、采用指针形式的weeks和month数组(二)程序中的数据说明①int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};这是定义的关于每个月天数的数组,根据大小月以及二月分的特殊情况将每个月的天数最为数组中的元素存入数组当中。

51单片机c语言电子万年历完整程序

51单片机c语言电子万年历完整程序

该程序为51单片机c语言电子万年历#include"reg52.h"//#include<stdio.h>#define uchar unsigned char#define uint unsigned intsbit lcden=P3^4;sbit lcdrs=P3^5;sbit DATA=P0^7;sbit RST=P0^5;sbit SCLK=P0^6;sbit menu=P3^0; //菜单sbit add=P3^1; //加一sbit dec=P3^7; //减一sbit led0=P1^0;sbit led1=P1^1;sbit led2=P1^2;sbit led3=P1^3;sbit ds=P3^2;//sbit beep=P3^3;uint temp;float f_temp;uint warn_l1=270;uint warn_l2=250;uint warn_h1=300;uint warn_h2=320;uint get_temp();void delayms(uint x);void write_com(uchar com);void write_data(uchar date);void init();void dis_temp(uint t);void Write1302(uchar dat);void WriteSet1302(uchar Cmd,uchar dat);uchar Read1302(void);uchar ReadSet1302(uchar Cmd);void Init_DS1302(void);void DisplaySecond(uchar x);void DisplayMinute(uchar x);void DisplayHour(uchar x);void DisplayDay(uchar x);void DisplayMonth(uchar x);void DisplayYear(uchar x);void DisplayWeek(uchar x);void dis_temp(uint t);void read_date(void);void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num);void key_scan(void);char code table[]="0123456789" ;uchar code table2[]= "TUEWESTHUFRISATSUNMON"; uchar second,minute,hour,day,month,year,week,count=0; uchar ReadValue,num,time;void delayms(uint x){uint i,j;for(i=x;i>0;i--)for(j=110;j>0;j--);}////////////////////////////////////////////////////////////void write_com(uchar com){lcdrs=0;P2=com;delayms(5);lcden=1;delayms(5);lcden=0;}void write_data(uchar date){lcdrs=1;P2=date;delayms(5);lcden=1;delayms(5);lcden=0;}void init(){lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);}/////////////////////////////////////////////////////////////////void Write1302(uchar dat){uchar i;SCLK=0; //拉低SCLK,为脉冲上升沿写入数据做好准备 delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续写8个二进制位数据DATA=dat&0x01; //取出dat的第0位数据写入1302delayms(2); //稍微等待,使硬件做好准备SCLK=1; //上升沿写入数据delayms(2); //稍微等待,使硬件做好准备SCLK=0; //重新拉低SCLK,形成脉冲dat>>=1; //将dat的各数据位右移1位,准备写入下一个数据位 }}void WriteSet1302(uchar Cmd,uchar dat){RST=0; //禁止数据传递SCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输delayms(2); //稍微等待,使硬件做好准备Write1302(Cmd); //写入命令字Write1302(dat); //写数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递}uchar Read1302(void){uchar i,dat;delayms(2); //稍微等待,使硬件做好准备for(i=0;i<8;i++) //连续读8个二进制位数据dat>>=1; //将dat的各数据位右移1位,因为先读出的是字节的最低位if(DATA==1) //如果读出的数据是1dat|=0x80; //将1取出,写在dat的最高位SCLK=1; //将SCLK置于高电平,为下降沿读出delayms(2); //稍微等待SCLK=0; //拉低SCLK,形成脉冲下降沿delayms(2); //稍微等待}return dat; //将读出的数据返回}uchar ReadSet1302(uchar Cmd){uchar dat;RST=0; //拉低RSTSCLK=0; //确保写数居前SCLK被拉低RST=1; //启动数据传输Write1302(Cmd); //写入命令字dat=Read1302(); //读出数据SCLK=1; //将时钟电平置于已知状态RST=0; //禁止数据传递return dat; //将读出的数据返回}void Init_DS1302(void){WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令WriteSet1302(0x80,((0/10)<<4|(0%10))); //根据写秒寄存器命令字,写入秒的初始值WriteSet1302(0x82,((59/10)<<4|(59%10))); //根据写分寄存器命令字,写入分的初始值WriteSet1302(0x84,((23/10)<<4|(23%10))); //根据写小时寄存器命令字,写入小时的初始值WriteSet1302(0x86,((28/10)<<4|(28%10))); //根据写日寄存器命令字,写入日的初始值 WriteSet1302(0x88,((2/10)<<4|(2%10))); //根据写月寄存器命令字,写入月的初始值WriteSet1302(0x8c,((14/10)<<4|(14%10))); //nian//WriteSet1302(0x8a,((4/10)<<4|(4%10)));}/////////////////////////////////////////////////////////////////void DisplaySecond(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);write_com(0x80+0x48);write_data(' ');dis_temp(get_temp());}void DisSecond(uchar x){uchar i,j;ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);i=x/10;j=x%10;write_com(0x80+0x46);write_data(i+0x30);write_com(0x80+0x47);write_data(j+0x30);}void DisplayMinute(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x80+0x43);write_data(i+0x30);write_com(0x80+0x44);write_data(j+0x30);write_com(0x80+0x45);write_data(':');}void DisplayHour(uchar x){uchar i,j;i=x/10;j=x%10;write_com(0x80+0x40);write_data(i+0x30);write_com(0x80+0x41);write_data(j+0x30);write_com(0x80+0x42);write_data(':');}void DisplayDay(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x89);write_data(i+0x30);write_com(0x8a);write_data(j+0x30); }void DisplayMonth(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x86);write_data(i+0x30); write_com(0x87);write_data(j+0x30);write_com(0x88);write_data('/');}void DisplayYear(uchar x) {uchar i,j;i=x/10;j=x%10;write_com(0x81);write_data(2+0x30);write_com(0x82);write_data(0+0x30);write_com(0x83);write_data(i+0x30);write_com(0x84);write_data(j+0x30);write_com(0x85);write_data('/');}void DisplayWeek(uchar x){ uchar i;x=x*3;// write_com(0x8c);write_data(table2[x]);// write_com(0x8d);write_data(table2[x+1]);// write_com(0x8e);write_data(table2[x+2]);write_com(0x8c);for(i=0;i<3;i++){write_data(table2[x]);x++;}}void read_date(void){ReadValue = ReadSet1302(0x81);second=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = ReadSet1302(0x83);minute=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x85);hour=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x87);day=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x89);month=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = ReadSet1302(0x8d);year=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue=ReadSet1302(0x8b); //读星期week=ReadValue&0x07;DisplayYear(year);DisplayMonth(month);DisplayDay(day);DisplayWeek(week);DisplayHour(hour);DisplayMinute(minute);DisplaySecond(second);dis_temp(get_temp()); ///温度显示key_scan(); ///按键检测}void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num){newval=ReadSet1302(newaddr); //读取当前时间newval=((newval&0x70)>>4)*10+(newval&0x0f); //将bcd码转换成十进制 if(flag) //判断是加一还是减一{newval++;switch(s1num){ case 1: if(newval>99) newval=0;DisplayYear(newval);break;case 2: if(newval>12) newval=1;DisplayMonth(newval);break;case 3: if(newval>31) newval=1;DisplayDay(newval);break;case 4: if(newval>6) newval=0;DisplayWeek(newval);break;case 5: if(newval>23) newval=0;DisplayHour(newval);break;case 6: if(newval>59) newval=0;DisplayMinute(newval);break;case 7: if(newval>59) newval=0;DisplaySecond(newval);break;default:break;}}else{newval--;switch(s1num){ case 1: if(newval==0) newval=99;DisplayYear(newval);break;case 2: if(newval==0) newval=12;DisplayMonth(newval);break;case 3: if(newval==0) newval=31;DisplayDay(newval);break;case 4: if(newval<0) newval=6;DisplayWeek(newval);break;case 5: if(newval<0) newval=23;DisplayHour(newval);break;case 6: if(newval<0) newval=59;DisplayMinute(newval);break;case 7: if(newval<0) newval=59;DisplaySecond(newval);break;default:break;}}WriteSet1302((newaddr-1),((newval/10)<<4)|(newval%10)); //将新数据写入寄存器}//////////////////////////////////////void dsreset(void){uint i;ds=0;i=103;while(i>0)i=4;while(i>0)i--;}bit tempreadbit(void){uint i;bit dat;ds=0;i++;ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return(dat);}uchar tempread(void){uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tempreadbit();dat=(j<<7)|(dat>>1); }return(dat);}void tempwritebyte(uchar dat) {bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb){ds=0;i++;i++;ds=1;i=8;while(i>0)i--;}else{ds=0;i=8;while(i>0)i--;ds=1;i++;i++;}}}void tempchange(void) {dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0x44);key_scan(); //////按键函数}uint get_temp(){uchar a,b;tempchange(); //////温度函数dsreset();delayms(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread();b=tempread();temp=b;temp<<=8;temp=temp|a;f_temp=temp*0.0625;temp=f_temp*10+0.5;f_temp=f_temp+0.05;return temp;}//////void dis_temp(uint t){uchar n1,n2,n3;n1=t/100;n2=t%100/10;n3=t%100%10;DisSecond(second); ///秒显示 write_com(0x80+0x49);write_data(table[n1]);//delayms(5);write_com(0x80+0x4a);write_data(table[n2]);//delayms(5);write_com(0x80+0x4b);write_data('.');// delayms(5);write_com(0x80+0x4c);write_data(table[n3]);//delayms(5);write_com(0x80+0x4d);write_data('^');//delayms(5);write_com(0x80+0x4e);write_data('C');//delayms(5);DisSecond(second); ////秒显示}/*********************液晶显示*****************/ void warn(uint s,uchar led){uchar i;i=s;// beep=0;P1=~(led);while(i--){dis_temp(get_temp());}// beep=1;P1=0xff;i=s;while(i--){dis_temp(get_temp());}}void deal(uint t){uchar i;if((t>warn_l2)&&(t<=warn_l1)){warn(40,0x01);}else if(t<=warn_l2){warn(10,0x03);}else if((t<warn_h2)&&(t>=warn_h1)) {warn(40,0x04);}else if(t>=warn_h2){warn(10,0x0c);}else{i=40;while(i--){dis_temp(get_temp());DisSecond(second);}}}///////////////////////////////////////void main(){init();Init_DS1302();while(1){tempchange();read_date();deal(temp);key_scan();}}////******************************************* void key_scan(void){// uchar miao,s1num=0;uchar s1num=0;if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;while(1){if(menu==0){delayms(5);if(menu==0){while(!menu);s1num++;}}// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,miao|0x80);write_com(0x0f);//光标闪射if(s1num==1){ //year=ReadSet1302(0x8d);write_com(0x80+4); //年光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(year,1,0x8d,1);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(year,0,0x8d,1);}}}if(s1num==2){//month=ReadSet1302(0x89);write_com(0x80+7); //月光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(month,1,0x89,2);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(month,0,0x89,2);}}}if(s1num==3){ //day=ReadSet1302(0x87);write_com(0x80+10);//日光标{delayms(3);if(add==0){ while(!add);turn_val(day,1,0x87,3);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(day,0,0x87,3); //写入日寄存器 }}}if(s1num==4){ //week=ReadSet1302(0x8b);write_com(0x80+14); //星期光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(week,1,0x8b,4);}}if(dec==0){delayms(3);{ while(!dec);turn_val(week,0,0x8b,4);}}}if(s1num==5){// hour=ReadSet1302(0x85)write_com(0x80+0x40+1); //时光标if(add==0){delayms(3);if(add==0){ while(!add);turn_val(hour,1,0x85,5);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(hour,0,0x85,5);}}}if(s1num==6)//调时间分{ // minute=ReadSet1302(0x83);write_com(0x80+0x40+4);if(add==0){delayms(5);if(add==0){ while(!add);turn_val(minute,1,0x83,6); //写入分寄存器}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(minute,0,0x83,6); //写入分寄存器}}}if(s1num==7)//调时间秒{// second=ReadSet1302(0x81);write_com(0x80+0x40+7);//秒光标if(add==0){delayms(3);if(add==0){ while(!add);if(second==0x60)second=0x00;turn_val(second,1,0x81,7);}}if(dec==0){delayms(3);if(dec==0){ while(!dec);turn_val(second,0,0x81,7);}}}if(s1num==8){// miao=ReadSet1302(0x81);// second=miao;// WriteSet1302(0x80,second&0x7f);s1num=0;//s1num清零//write_com(0x0c);//光标不闪烁//break;}}}}}。

c++万年历及公历农历转换源代码2

c++万年历及公历农历转换源代码2

#include <stdio.h>#include <stdlib.h>#include <conio.h>#define X " Sun Mon Tue Wed Thu Fri Sat"#define P printf#define B break#define FP fprintfstruct date /*日期*/{int year;int month;int date;};struct taglunarcal /**/{int basedays;int intercalation;int monthdays[13];}dalist[]={{31,8,0,1,0,0,1,0,1,1,0,1,1,0,1},{50,0,0,1,0,0,1,0,1,0,1,1,1,0,0},{39,0,1,0,1,0,0,1,0,1,0,1 ,1,1,0},{29,5,0,1,0,1,0,0,1,0,0,1,1,0,1},{47,0,1,1,0,1,0,0,1,0,0,1,1,0,0},{35,0,1,1,0,1,1,0,0,1,0,1,0,1,0},{25,4,0,1,1,0,1,0,1,0,1,0,1,0,1},{4 4,0,0,1,0,1,0,1,1,0,1,0,1,0,0},{33,0,1,0,0,1,1,0,1,0,1,1,0,1,0},{22,2,0,1,0,0,1,0,1,0,1,1,1,0,1},{41,0,0,1,0,0,1,0,1,0,1,1,1,0,0},{3 0,6,1,0,1,0,0,1,0,0,1,1,0,1,1},{49,0,1,0,1,0,0,1,0,0,1,1,0,1,0},{37,0,1,1,0,1,0,0,1,0,0,1,0,1,0},{26,5,1,1,0,1,0,1,0,1,0,1,0,0,1},{4 5,0,1,0,1,1,0,1,0,1,0,1,0,1,0},{35,0,0,1,0,1,0,1,1,0,1,0,1,0,0},{23,2,1,0,0,1,0,1,1,0,1,1,0,1,0},{42,0,1,0,0,1,0,1,0,1,1,0,1,1,0},{3 2,7,0,1,0,0,1,0,0,1,1,0,1,1,1},{51,0,0,1,0,0,1,0,0,1,1,0,1,1,0},{39,0,1,0,1,0,0,1,0,0,1,0,1,1,0},{28,5,1,0,1,1,0,0,1,0,0,1,0,1,1},{4 7,0,0,1,1,0,1,0,1,0,0,1,0,1,0},{36,0,0,1,1,0,1,1,0,1,0,1,0,0,0},{24,4,1,0,1,0,1,1,0,1,1,0,1,0,1},{44,0,0,0,1,0,1,0,1,1,0,1,1,0,0},{3 3,0,1,0,0,1,0,1,0,1,0,1,1,1,0},{23,2,0,1,0,0,1,0,0,1,0,1,1,1,1},{41,0,0,1,0,0,1,0,0,1,0,1,1,1,0},{30,6,0,1,1,0,0,1,0,0,1,0,1,1,0},{4 8,0,1,1,0,1,0,1,0,0,1,0,1,0,0},{37,0,1,1,1,0,1,0,1,0,0,1,0,1,0},{26,5,0,1,1,0,1,1,0,1,0,1,0,0,1},{45,0,0,1,0,1,1,0,1,0,1,1,0,1,0},{3 5,0,0,0,1,0,1,0,1,1,0,1,1,0,0},{24,3,1,0,0,1,0,0,1,1,0,1,1,1,0},{42,0,1,0,0,1,0,0,1,0,1,1,1,0,0},{31,7,1,1,0,0,1,0,0,1,0,1,1,0,1},{5 0,0,1,1,0,0,1,0,0,1,0,1,0,1,0},{39,0,1,1,0,1,0,1,0,0,1,0,1,0,0},{27,6,1,1,0,1,1,0,1,0,0,1,0,1,0},{46,0,1,0,1,1,0,1,0,1,0,1,0,1,0},{3 6,0,0,1,0,1,0,1,1,0,1,0,1,0,0},{25,4,1,0,1,0,1,0,1,0,1,1,0,1,1},{44,0,0,0,1,0,0,1,0,1,1,1,0,1,0},{33,0,1,0,0,1,0,0,1,0,1,1,0,1,0},{2 2,2,1,1,0,0,1,0,0,1,0,1,0,1,1},7,0,1,0,1,1,0,1,0,1,0,1,0,1,0},{27,5,0,1,0,1,0,1,0,1,1,0,1,0,1},{45,0,0,1,0,0,1,1,0,1,1,0,1,0,0},{34,0,1,0,1,0,0,1,0,1,1,0,1,1,0},{2 4,3,0,1,0,1,0,0,1,0,1,0,1,1,1},{43,0,0,1,0,1,0,0,1,0,1,0,1,1,0},{31,8,1,0,1,0,1,0,0,1,0,1,0,1,0},{49,0,1,1,1,0,1,0,0,1,0,1,0,1,0},{3 9,0,0,1,1,0,1,0,1,0,1,0,1,0,0},{28,6,1,0,1,0,1,1,0,1,0,1,0,1,0},{46,0,1,0,1,0,1,0,1,1,0,1,0,1,0},{36,0,0,1,0,0,1,0,1,1,0,1,1,0,0},{2 5,4,1,0,1,0,0,1,0,1,0,1,1,1,0},{44,0,1,0,1,0,0,1,0,1,0,1,1,1,0},{33,0,0,1,0,1,0,0,1,0,0,1,1,0,0},{21,3,1,1,1,0,1,0,0,1,0,0,1,1,0},{4 0,0,1,1,0,1,1,0,0,1,0,1,0,1,0},{30,7,0,1,0,1,1,0,1,0,1,0,1,0,1},{48,0,0,1,0,1,0,1,1,0,1,0,1,0,0},{37,0,1,0,0,1,0,1,1,0,1,1,0,1,0},{2 7,5,0,1,0,0,1,0,1,0,1,1,1,0,1},{46,0,0,1,0,0,1,0,1,0,1,1,0,1,0},{34,0,1,0,1,0,0,1,0,0,1,1,0,1,0},{23,4,1,1,0,1,0,0,1,0,0,1,1,0,1},{4 2,0,1,1,0,1,0,0,1,0,0,1,0,1,0},{31,8,1,1,0,1,0,1,0,1,0,0,1,0,1},{49,0,1,0,1,1,0,1,0,1,0,1,0,0,0},{38,0,1,0,1,1,0,1,1,0,1,0,1,0,0},{2 8,6,1,0,0,1,0,1,1,0,1,1,0,1,0},{47,0,1,0,0,1,0,1,0,1,1,0,1,1,0},{36,0,0,1,0,0,1,0,0,1,1,0,1,1,0},{25,4,1,0,1,0,0,1,0,0,1,0,1,1,1},{4 4,0,1,0,1,0,0,1,0,0,1,0,1,1,0},{33,0,1,0,1,1,0,0,1,0,0,1,0,1,1},{51,0,0,1,1,0,1,0,1,0,0,1,0,1,0},{40,0,0,1,1,0,1,1,0,1,0,1,0,0,0},{2 9,6,1,0,1,0,1,1,0,1,1,0,1,0,0},{48,0,1,0,1,0,1,0,1,1,0,1,1,0,0},{37,0,1,0,0,1,0,0,1,1,0,1,1,1,0},{27,5,0,1,0,0,1,0,0,1,0,1,1,1,1},{4 6,0,0,1,0,0,1,0,0,1,0,1,1,1,0},{35,0,0,1,1,0,0,1,0,0,1,0,1,1,0},{23,3,0,1,1,0,1,0,1,0,0,1,0,1,0},{41,0,1,1,1,0,1,0,1,0,0,1,0,1,0},{3 1,8,0,1,1,0,1,0,1,1,0,0,1,0,1},{50,0,0,1,0,1,1,0,1,0,1,1,0,0,0},{38,0,1,0,1,0,1,0,1,1,0,1,1,0,0},{28,5,1,0,0,1,0,0,1,1,0,1,1,0,1},{4 7,0,1,0,0,1,0,0,1,0,1,1,1,0,0},{36,0,1,1,0,0,1,0,0,1,0,1,1,0,0},{24,4,1,1,0,1,0,1,0,0,1,0,1,0,1},{43,0,1,1,0,1,0,1,0,0,1,0,1,0,0},{3 2,0,1,1,0,1,1,0,1,0,0,1,0,1,0},{22,2,0,1,0,1,1,0,1,0,1,0,1,0,1},{40,0,0,1,0,1,0,1,1,0,1,0,1,0,0},{29,7,1,0,1,0,1,0,1,0,1,1,0,1,1},{4 9,0,0,0,1,0,0,1,0,1,1,1,0,1,0},{38,0,1,0,0,1,0,0,1,0,1,1,0,1,0},{26,5,1,1,0,0,1,0,0,1,0,1,0,1,1},{45,0,1,0,1,0,1,0,0,1,0,1,0,1,0},{4 5,0,1,0,1,1,0,1,0,0,1,0,1,0,0},{23,4,1,0,1,1,1,0,1,0,1,0,1,0,1},{41,0,1,0,1,0,1,1,0,1,0,1,0,1,0},{31,9,0,1,0,1,0,1,0,1,1,1,0,1,1},{5 0,0,0,1,0,0,1,0,1,1,1,0,1,0,0},{39,0,1,0,1,0,0,1,0,1,1,0,1,1,0},{28,6,0,1,0,1,0,0,0,1,0,1,1,1,0},{47,0,0,1,0,1,0,0,1,0,1,0,1,1,0},{3 6,0,1,0,1,0,1,0,0,1,0,0,1,1,0},{25,4,0,1,1,1,1,0,0,1,0,1,0,1,1},{43,0,0,1,1,0,1,0,1,0,1,0,1,0,0},{32,0,1,0,1,0,1,1,0,1,0,1,0,1,0},{2 2,2,0,1,0,1,1,0,1,1,0,1,0,1,1},{41,0,0,1,0,0,1,0,1,1,0,1,1,0,0},{29,6,1,0,1,0,0,1,1,0,1,1,1,0,1},{48,0,1,0,1,0,0,1,0,0,1,1,1,0,0},{3 7,0,1,1,0,1,0,0,1,0,0,1,1,0,0},{26,5,1,1,1,0,1,0,1,0,0,1,1,0,1},{44,0,1,1,0,1,0,1,0,1,0,0,1,1,0},{34,0,0,1,0,1,1,0,1,0,1,0,1,0,0},{2 3,3,0,1,1,1,0,1,1,0,1,0,1,0,1},{42,0,1,0,0,1,0,1,1,0,1,1,0,1,0},{31,7,0,1,0,0,1,0,1,1,1,1,0,1,1},{50,0,0,1,0,0,1,0,1,0,1,1,0,1,0},{3 9,0,1,0,1,0,0,1,0,0,1,1,0,1,0},4,5,1,1,0,1,1,1,0,1,0,1,0,0,1},{43,0,1,0,1,1,0,1,0,1,1,0,1,0,0},{32,0,0,1,0,1,0,1,1,0,1,1,0,1,0},{22,2,0,1,0,1,0,1,0,1,1,0,1,1,1},{4 1,0,0,1,0,0,1,0,0,1,1,0,1,1,0},{30,7,1,0,1,0,0,1,0,1,0,1,1,1,0},{48,0,1,0,1,0,0,1,0,0,1,0,1,1,0},{37,0,1,0,1,0,1,0,1,0,0,1,0,1,0},{2 6,5,1,0,1,1,0,0,1,0,0,1,0,1,0},{45,0,0,1,1,0,1,1,0,1,0,0,1,0,0},{33,0,1,0,1,0,1,1,0,1,1,0,1,0,0},{23,3,0,1,0,0,1,0,1,1,0,1,1,0,0},{4 2,0,1,0,0,1,0,0,1,1,0,1,1,1,0},{32,8,0,1,0,0,1,0,0,1,1,1,1,1,1},{50,0,0,1,0,0,1,0,0,1,0,1,1,1,0},{39,0,0,1,1,0,0,1,0,0,1,0,1,1,0},{2 8,6,0,1,1,0,1,0,0,0,1,0,1,0,0},{46,0,1,1,1,0,1,0,1,0,0,1,0,1,0},{35,0,0,1,1,0,1,0,1,1,0,0,1,0,0},{24,4,1,0,1,0,0,1,1,0,1,1,0,0,0},{4 3,0,1,0,1,0,1,0,1,0,1,1,1,0,0},{33,0,1,0,0,1,0,0,1,0,1,1,1,0,0},{21,3,1,1,0,1,0,0,1,0,1,1,1,0,0},{40,0,1,1,0,0,1,0,0,1,0,1,1,0,0},{2 9,7,1,1,0,1,0,1,0,1,0,1,0,1,0},{48,0,1,1,0,1,0,1,0,0,1,0,1,0,0},{36,0,1,1,0,1,1,0,1,0,0,1,0,1,0},{26,5,0,1,0,1,1,1,0,1,0,1,0,1,1},{4 5,0,0,1,0,1,0,1,1,0,1,0,1,0,0},{34,0,1,0,1,0,0,1,1,0,1,1,0,1,0},{23,4,0,1,0,1,0,1,0,1,1,1,0,1,1},{42,0,0,1,0,1,0,0,1,0,1,1,0,1,0},{3 1,8,1,0,1,0,1,0,0,1,1,0,1,1,1},{50,0,1,0,1,0,1,0,0,1,0,1,0,1,0},{38,0,1,0,1,1,0,1,0,0,1,0,1,0,0},{27,6,1,0,1,1,0,1,1,0,1,0,1,0,1},{4 6,0,1,0,1,0,1,1,0,1,0,1,0,1,0},{36,0,0,1,0,1,0,1,0,1,1,0,1,0,0},{24,4,1,0,1,0,1,0,1,1,1,0,1,0,0},{43,0,1,0,1,0,0,1,0,1,1,0,1,1,0},{3 3,0,0,1,0,1,0,0,1,0,1,0,1,1,0},{22,3,1,0,1,1,0,0,1,0,0,1,1,1,1},{40,0,0,1,1,0,1,0,0,1,0,0,1,1,0},{29,7,0,1,1,1,0,0,1,1,0,0,1,1,1},{4 8,0,0,1,1,0,1,0,1,0,1,0,1,0,0},{37,0,1,0,1,0,1,1,0,1,0,1,0,1,0},{26,5,0,1,0,0,1,0,1,1,0,1,0,1,1},{45,0,0,1,0,0,1,0,1,1,0,1,1,0,0},{3 4,0,1,0,1,0,0,1,0,1,0,1,1,1,0},{24,4,0,1,0,1,0,1,0,0,1,1,1,0,1},{41,0,1,1,0,1,0,0,1,0,0,1,1,0,0},{30,8,1,1,1,0,1,0,0,1,0,1,1,0,1},{4 9,0,1,1,0,1,0,1,0,1,0,0,1,0,0},{38,0,1,1,0,1,1,0,1,0,1,0,1,0,0},{27,6,0,1,1,0,1,0,1,0,1,0,1,0,0},{46,0,0,1,0,1,0,1,1,0,1,1,0,1,0},{3 6,0,0,1,0,0,1,0,1,0,1,1,1,0,0},{25,4,1,0,1,0,1,0,0,1,1,1,0,1,0},{43,0,1,0,1,0,0,1,0,0,1,1,0,1,0},{32,0,1,0,1,0,0,0,0,1,0,1,0,1,0},{2 1,2,1,1,1,1,0,0,1,0,0,1,0,1,1},{40,0,1,1,0,1,0,1,0,1,0,0,1,0,0}};getleap( int year ) /*1代表闰年*/{if ( year % 400 == 0 )return 1;else if ( year % 100 == 0 )return 0;else if ( year % 4 == 0 )return 1;elsereturn 0;}print(int n){int i;for(i=0;i<n;i++) printf(" ");}int day(int year){long a,b;if(year<=2000){a=2000-year;b=6-(a+a/4-a/100+a/400)%7; return b;}else{a=year-2000; b=(a+1+(a-1)/4-(a-1)/100+(a-1)/400)%7+6; return b%7; }}solardate(struct date * input) /*公历年第几天*/{int i,number=0,solarcal[12]={31,28,31,30,31,30,31,31,30,31,30,31}; number=getleap(input->year);for(i=0;i<input->month-1;i++){number+=solarcal[i];}return number+input->date;}lunardate(struct date *input)/*农历年第几天*/{int i,leap=0,number=0;if(dalist[input->year-1900].intercalation==0)leap=0;else if(input->month==dalist[input->year-1900].intercalation){printf(" 0 not leap month\n 1 leap month");scanf("%d",&leap);}else if(input->month>dalist[input->year-1900].intercalation) leap=1;for(i=0;i<input->month+leap-1;i++)number+=dalist[input->year-1900].monthdays[i]+29;return number+input->date;}lunarmonth(int year,int *mon){int i,j;mon[0]=dalist[year].monthdays[0]+29;for(i=1,j=1;i<12;i++,j++){if(i==dalist[year].intercalation){mon[i-1]+=dalist[year].monthdays[j]+29;j++;}mon[i]=mon[i-1]+dalist[year].monthdays[j]+29;}}main(){int i,j,k,m,n,f1,f2,year,d,y,ly,cord,greday,lunday,diff,add,leap,tlylm[12],lastylm[12],a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31},SolarDays[2][12] = {{31,59,90,120,151,181,212,243,273,304,334,365},{31,60,91,121,152,182,213,244,274,305,335, 366}};char fname[12]={"0"}; FILE *fw;struct date input;do{printf("\n--------------------------------------------\n");printf(" 请选择");printf("\n--------------------------------------------\n");printf(" 1 公历转换成农历");printf("\n--------------------------------------------\n");printf(" 2 农历转换成公历");printf("\n--------------------------------------------\n");printf(" 3 万年历");printf("\n--------------------------------------------\n");printf(" 4 退出");printf("\n--------------------------------------------\n");printf("\n 请选择: 1,2,3,4\n");scanf("%d",&cord);clrscr();switch(cord){case 1:{printf("\n(公历) 请输入年.月.日(yyyy.mm.dd) \n");scanf("%d.%d.%d",&input.year,&input.month,&input.date);/*输入年月日*/y=input.year;greday=solardate(&input);/*公历年第几天*/diff=greday-dalist[y-1900].basedays;if(diff>0)lunarmonth(y-1900,tlylm);else{y-=1;lunarmonth(y-1900,tlylm);diff+=tlylm[11];}for(i=0;tlylm[i]<diff;i++);if(i!=0)diff-=tlylm[i-1];printf("(农历) %d.%d.%d",y,i+1,diff);break;}clrscr();case 2:{printf("\n(农历) 请输入年.月.日(yyyy.mm.dd) \n");scanf("%d.%d.%d",&input.year,&input.month,&input.date);y=input.year;ly=getleap(y);lunday=lunardate(&input);/*农历年第几天*/add=lunday+dalist[y-1900].basedays;if(add-365+ly>0){add-=365+ly;y+=1;}for(i=0;SolarDays[ly][i]<add;i++);if(i!=0)add-=SolarDays[ly][i-1];printf("(公历) %d.%d.%d",y,i+1,add);break;}case 3:{system("cls");P(" 请输入你所要查看的年份:");scanf("%d",&year);fw=fopen(fname,"w+");if(fw==NULL){P(" can not open file."); return;}P(" The calendar of the year %d.\n",year);FP(fw,"The calendar of the year %d.\n",year);d=day(year);if(year%4==0&&year%100||year%400==0) a[2]++;for(i=1;i<=12;i+=2){m=0; n=0; f1=0; f2=0;switch(i){case 1:P(" Januray 1 "); FP(fw," Januray 1 "); B; case 3:P(" March 3 "); FP(fw," March 3 "); B; case 5:P(" May 5 "); FP(fw," May 5 "); B; case 7:P(" July 7 "); FP(fw," July 7 "); B; case 9:P(" September 9 "); FP(fw," September 9 "); B; case 11:P(" Nevember 11 "); FP(fw," Nevember 11 "); B; }print(21);for(k=0;k<21;k++) fprintf(fw," ");switch(i+1){case 2:P(" February 2 "); FP(fw," February 2 ");B; case 4:P(" April 4 "); FP(fw," April 4 ");B;case 6:P(" June 6 "); FP(fw," June 6 ");B; case 8:P(" August 8 "); FP(fw," August 8 "); B; case 10:P(" October 10 "); FP(fw," October 10 ");B; case 12:P(" December 12"); FP(fw," December 12");B;}P("\n");fprintf(fw,"\n");P(X); fprintf(fw,X); print(6);fprintf(fw," "); P(X); fprintf(fw,X);fprintf(fw,"\n"); P("\n");for(j=0;j<6;j++){if(j==0){print(d*4); for(k=0;k<d*4;k++) fprintf(fw," ");for(k=0;k<7-d;k++){P("%4d",++m); fprintf(fw,"%4d",m);}print(6); fprintf(fw," ");d+=a[i]%7; d%=7;print(d*4); for(k=0;k<d*4;k++) fprintf(fw," ");for(k=0;k<7-d;k++){P("%4d",++n); fprintf(fw,"%4d",n);}P("\n"); fprintf(fw,"\n");}else{for(k=0;k<7;k++){if(m<a[i]){P("%4d",++m); fprintf(fw,"%4d",m);}else{print(4); fprintf(fw,"");}if(m==a[i]) f1=1;}print(6); fprintf(fw," ");for(k=0;k<7;k++){if(n<a[i+1]){P("%4d",++n);fprintf(fw,"%4d",n);}else{print(4); fprintf(fw,"");}if(n==a[i+1]) f2=1;}P("\n"); fprintf(fw,"\n");if(f1&&f2) B;}}d+=a[i+1]%7; d%=7; P(" "); fprintf(fw," "); if(i==5){getch();system("cls");}}fclose(fw);getch();break;}case 4:{exit(0);}}}while(cord<=4);}。

C语言万年历代码

C语言万年历代码

else if(ch=='n'||ch=='N')
{ month+=1;
if(month>12) {month=1;year+=1;}
}
printf("\t\t********e退出 p上一月 n下一月********");
}
void first_set(int *year,int *month)
{ do{
printf ("please input the year(1~9999):");
if (choose=='y'||choose=='Y') main();
}
days_of_month(int x,int y)
{
int z;
switch (y)
{case 1:
case 3:
case 5:
case 7:
case 8:
printf ("please input the month(1~12):");
scanf ("%d",month);
if(*month<1||*month>12) printf ("WANNING:ERROR,please input again!\n");
printf ("\n\n********************************************************************************\n\n");
自己做的日历(C语言)
本帖最后由 kekeyu 于 2010-8-15 16:59 编辑

万年历程序源代码

万年历程序源代码

#include<iostream>#include<iomanip>#include <ctime>using namespace std;int dayofmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dd[12]={ 31,28,31,30,31,30,31,31,30,31,30,31 };class datee{private:int d,m,y;public:int isLeap( int y );int getDayPerMonth( int y , int m );int getNumDay( datee D );bool legalDate( datee D );void count();};/*ÅжÏÊÇ·ñÊÇÈòÄê*/int datee::isLeap( int y ){if( !(y%4)&&y%100 || !(y%400) )return 1;return 0;}/*¼ÆË㲻ͬÇé¿ö¶þÔ·ݵÄÌìÊý*/int datee::getDayPerMonth( int y , int m ){if( m==2 )return dd[1]+isLeap( y );return dd[m-1];}/*¼ÆËãʱ¼ä²î*/int datee::getNumDay( datee D ){int ret=0;int i;for( i=1 ; i<=D.y ; i++ )ret+=365+isLeap( D.y );for( i=1 ; i<D.m ; i++ )ret+=getDayPerMonth( D.y , i );ret+=D.d;return ret;}/*ÔËÓò¼¶ûÐͺ¯ÊýÅжÏÊäÈëÊý¾ÝµÄÕýÎó*/bool datee::legalDate( datee D ){if( D.y<0||D.y>9999 )return false;if( D.m<1||D.m>12 )return false;if( D.d<1||D.d>getDayPerMonth( D.y , D.m ) )return false;return true;}void datee::count(){long day;datee sysDate,date;cout<<"please input what date you want to compared:"<<endl; cout<<"year month day"<<endl;cout<<"the one:";cin>>date.y>>date.m>>date.d;while( !legalDate(date) ){cout<<"illegal date,input again: "<<endl;cin>>date.y>>date.m>>date.d;}cout<<"the tow:";cin>>sysDate.y>>sysDate.m>>sysDate.d;while( !legalDate(sysDate) ){cout<<"illegal system date,input again: "<<endl;cin>>sysDate.y>>sysDate.m>>sysDate.d;}day=getNumDay(date)-getNumDay(sysDate);if(day<0)day=-day;cout<<"the time difference is :"<<endl<<day<<endl;}class Date{private:int year;int month;int day;public:void SetDate();int IsLeap(int year);void ShowDate();int getdays(int year,int month,int day);void showweek();void printmonth();void printyear();void input(){cin>>year>>month;while(month<1||month>12){cout<<"illegal month,please input again:"<<endl;cin>>year>>month;panduan();}}void panduan(){if(month<1||month>12){cout<<"illegal month,please input again:"<<endl;cin>>year>>month;}}};void Date::SetDate(){int yy,mm,dd;cout<<"please input the date:"<<endl<<"year month date"<<endl;cin>>yy>>mm>>dd;if(yy<=0){ cout<<"Year value error!"<<endl;}if(mm<=0&&mm>12){cout<<"Month value error!"<<endl;exit(1);}year=yy;month=mm;switch(month){case 4:case 6:case 9:case 11:{if(dd>=1&&dd<=30)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}} break;case 2:{if(IsLeap(year)){if(dd>=1&&dd<=29)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}}else{if(dd>=1&&dd<=28)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}}}break;default:{if(dd>=1&&dd<=31)day=dd;else{cout<<"Day value error!"<<endl;cout<<"please input again:"<<endl;cout<<"year month date"<<endl;cin>>yy>>mm>>dd;day=dd;}}}}int Date::IsLeap(int year){if(year%400==0||(year%100!=0&&year%4==0))return 1;elsereturn 0;}void Date::ShowDate(){cout<<year<<"-"<<month<<"-"<<day<<" ";}int Date::getdays(int year,int month,int day){int i,sum=0;for(i=1;i<year;i++){if(IsLeap(i))sum+=366;elsesum+=365;}if(IsLeap(year))dayofmonth[2]=29;for(i=1;i<month;i++)sum+=dayofmonth[i];sum+=day;return sum;}void Date::showweek(){int s;s=getdays(year,month,day)%7;switch(s){case 0:cout<<"Sunday"<<endl;break;case 1:cout<<"Monday"<<endl;break;case 2:cout<<"Tuesday"<<endl;break;case 3:cout<<"Wednesday"<<endl;break;case 4:cout<<"Thursday"<<endl;break;case 5:cout<<"Friday"<<endl;break;case 6:cout<<"Saturday"<<endl;break;}}void Date::printmonth(){if(year%4==0)cout<<" "<<year<<" "<<month<<" "<<"(ÈòÄê)"<<endl;elsecout<<" "<<year<<" "<<month<<" ¡¡"<<"(·ÇÈòÄê)"<<endl; cout<<"ÈÕ "<<"Ò» "<<"¶þ "<<"Èý"<<" ËÄ "<<"Îå "<<"Áù "<<endl;cout<<"Su "<<"Mo "<<"Tu "<<"We "<<"Th "<<"Fr "<<"Sa "<<endl;int s=getdays(year,month,1);s=s%7;for(int m=0;m<s;m++)cout<<" ";cout<<setw(2)<<setfill(' ')<<"1"<<" ";if(IsLeap(year)) dayofmonth[2]=29;int yue=dayofmonth[month];for(int i=2;i<8-s;i++)cout<<setw(2)<<setfill(' ')<<i<<' ';cout<<endl;int j;for(i=8-s,j=1;i<=yue;i++,j++){cout<<setw(2)<<setfill(' ')<<i<<' ';if(j%7==0)cout<<endl;}cout<<endl;}void Date::printyear(){int y,m;cin>>y;year=y;if(year%4==0)cout<<"------the year is "<<year<<"£¨ÈòÄ꣩"<<"----------"<<endl;elsecout<<"------the year is "<<year<<"£¨·ÇÈòÄ꣩"<<"---------"<<endl;for( m=1;m<13;m++) //×ܹ²12¸öÔ·Ý{cout<<" ***********************"<<endl;cout<<endl<<setw(5)<<m<<""<<year<<"Äê"<<setw(5)<<m<<"Ô·Ý"<<endl;cout<<" "<<"ÈÕ "<<"Ò» "<<"¶þ "<<"Èý"<<" ËÄ "<<"Îå "<<"Áù "<<endl;cout<<" "<<"Su "<<"Mo "<<"Tu "<<"We "<<"Th "<<"Fr "<<"Sa "<<endl;month=m;int s=getdays(year,month,1);s=s%7;for(int m=0;m<s;m++)cout<<" ";cout<<" "<<setw(2)<<setfill(' ')<<"1"<<" ";if(IsLeap(year)) dayofmonth[2]=29;int yue=dayofmonth[month];for(int i=2;i<8-s;i++)cout<<setw(2)<<setfill(' ')<<i<<' ';cout<<endl<<" ";int j;for(i=8-s,j=1;i<=yue;i++,j++){cout<<setw(2)<<setfill(' ')<<i<<' ';if(j%7==0)cout<<endl<<" ";}cout<<endl<<" ***********************"<<endl;}}class m{private:int a,b,number;public:void months(){if (b==1||b==3||b==5||b==7||b==8||b==10||b==12){number=31;}elseif(b==4||b==6||b==9||b==11){number=30;}elseif(b==2){if(a%4==0){number=29;}else{number=28;}}cout<<number<<endl;}void month(){cin>>a>>b;if(b>0&&b<13)months();else{cout<<"error!"<<endl;cout<<"please input again:"<<endl;cin>>a>>b;months();}}};int main(){int choose;char ch;Date mydate;system("title »¶Ó-ʹÓÃÍòÄêÀú!");while(choose!=0){cout<<"************************************************************"<<endl;cout<<" »¶Ó-ʹÓÃÍòÄêÀú! Ô¸ÄúʹÓõÄ˳ÐÄ£¬¿ªÐÄ!"<<endl;/*»ñȡϵͳʱ¼ä*/time_t timen;time(&timen);tm* t_tm = localtime(&timen);cout<<" ϵͳµ±Ç°Ê±¼äΪ: "<<t_tm->tm_year+1900<<"Äê"<<t_tm->tm_mon+1<<"ÔÂ"<<t_tm->tm_mday<<"ÈÕ"<<endl;cout<<"ÇëÑ¡Ôñ¡°0¡«5¡±"<<endl;cout<<"1£º¼ÆËãʱ¼ä²î 2£º²éѯijÔÂÌìÊý"<<endl;cout<<"3£º²éѯijÈÕÐÇÆÚ 4£ºÏÔʾijÄêÈÕÀú"<<endl;cout<<"5£º²éѯijÄêijÔÂÔÂÀú"<<endl;cout<<"0£ºÍ˳ö"<<endl;cout<<"******************************************************** ****"<<endl;cin>>choose;switch(choose) //¹¦ÄÜÑ¡Ôñ{case 1:datee myday;myday.count();/*ÅжÏÊÇ·ñÇåÆÁ²¢ÏÔʾÖ÷²Ëµ¥*/cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");//ÇåÆÁ£¬²¢·µ»ØÖ÷²Ëµ¥system("COLOR a1");break;}elsebreak; //ÏÔʾÖ÷²Ëµ¥case 2:cout<<"please input what you wangt to know the year,month:";m a;a.month();cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 1b");break;}elsebreak;case 3:mydate.SetDate();mydate.ShowDate();mydate.showweek();mydate.printmonth();cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 6e");break;}elsebreak;case 4:cout<<"please input what years you want to know£º";cout<<endl;mydate.printyear();cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 3d");break;}elsebreak;case 5:cout<<"please input the year,month what you want to know."<<endl;mydate.input();mydate.printmonth();cout<<"please choose whether or not return the menu 'y' or other keys"<<endl;cin>>ch;if(ch=='y'){system("cls");system("COLOR 55");break;}elsebreak;case 0:cout<<"ллʹÓÃÍòÄêÀúϵͳ£¬Çë¹Ø±Õ!!!!!"<<endl;exit(0);break;default:cout<<"ÄúÊäÈëÓÐÎó£¬ÇëÖØÐÂÊäÈë"<<endl;}}return 0;}。

C语言程序设计 输出任意年份的日历

C语言程序设计  输出任意年份的日历
for(q=i1;q<7;q++)/*格式对齐的控制使第二列的日历每行的第一个书从同一位置开始*/
printf("");
printf("");
i2=0;flag2=0;
for(day2=p-(W+t[month-1])%7;day2<=t[month]&&flag2==0;day2++)
{i2++;printf("%4d",day2);
{
if(month<3)
{c=((int)(year-1)/100);
Y=(year-1)-((int)((year-1)/100))*100;
w=((int)(c/4))-2*c+Y+((int)(Y/4))+((int)(13*(month+13)/5));
W=(w+70)%7;
}
else if(month>=3)
if(i2%7==0) flag2=1;}
printf("\n");
}
}
}
{c=((int)(year/100));
Y=year-((int)(year/100))*100;
w=((int)(c/4))-2*c+Y+((int)(Y/4))+((int)(13*(month+1)/5));
W=(w+70)%7;
}
printf("\n\n%d%d\n",month,month+1);printf("日一二三四五六日一二三四五六\n");i1=W;/*一行一行的输出*/

万年历c语言代码讲解

万年历c语言代码讲解

万年历c语言代码讲解摘要:一、万年历C语言代码讲解简介1.万年历的概念及用途2.C语言代码讲解的重要性二、C语言代码基础1.C语言简介2.C语言基本语法3.C语言的数据类型与变量三、万年历C语言代码实现1.计算闰年2.计算月份的天数3.计算日期的星期4.输出万年历四、万年历C语言代码的优化与拓展1.代码性能优化2.功能的拓展与实现五、总结1.万年历C语言代码讲解的意义2.对编程能力的提升正文:一、万年历C语言代码讲解简介万年历是一种记录了公历日期的工具,它可以帮助我们快速查询公历日期对应的农历、节日、提醒等信息。

C语言是一种广泛应用于计算机领域的编程语言,掌握C语言编程对于学习其他编程语言有很大的帮助。

本文将详细讲解万年历C语言代码的实现过程,帮助读者更好地理解C语言编程。

二、C语言代码基础1.C语言简介C语言是一种高级编程语言,由丹尼斯·里奇(Dennis Ritchie)于20世纪70年代在贝尔实验室开发。

C语言具有良好的性能和可移植性,被广泛应用于操作系统、嵌入式系统、硬件驱动等领域。

2.C语言基本语法C语言的基本语法包括变量声明、数据类型、运算符、控制结构、函数等。

了解这些基本语法对于编写C语言代码至关重要。

3.C语言的数据类型与变量C语言的数据类型包括整型、浮点型、字符型等。

变量是存储数据的容器,通过变量名来表示。

声明变量时需要指定变量的数据类型和存储空间。

三、万年历C语言代码实现1.计算闰年闰年是指公历年份可以被4整除但不能被100整除的年份,或者是可以被400整除的年份。

通过编写一个函数,我们可以判断一个年份是否为闰年。

2.计算月份的天数根据公历规定,每个月的天数不同。

通过编写一个函数,我们可以计算指定月份的天数。

3.计算日期的星期通过编写一个函数,我们可以根据公历日期计算对应的星期。

4.输出万年历根据用户输入的日期范围,输出对应的万年历信息。

四、万年历C语言代码的优化与拓展1.代码性能优化在实现万年历C语言代码的过程中,可以通过优化算法、使用更高效的函数等方式提高代码性能。

C++实现万年历(原创)

C++实现万年历(原创)

C++实现万年历(原创)任务要求功能要求:(1)提供菜单⽅式选择,假定输⼊的年份在1940-2040年之间。

(2)输⼊⼀个年份,输出是在屏幕上显⽰该年的⽇历。

(3)输⼊年⽉,输出该⽉的⽇历,如图2-1所⽰: 图2- 1 ⽉⽇历(4)输⼊年份、⽉份、⽇期,计算得到的是这⼀天据今天有多少天,星期⼏。

(5)输⼊公历的年⽉⽇,输出农历年⽉⽇。

(6)输⼊农历节⽓,输出当年农历的年⽉⽇及公历年⽉⽇。

可以假定只涉及年份是1940年到2040年。

总体设计将⽂件三部分 Date.h、main.cpp、Date.cpp1) Date.h:包括函数的声明。

2) main.cpp:包含调⽤与模拟交互界⾯的函数代码3) Date.cpp://输⼊输⼊⼀个年份,输出是在屏幕上显⽰该年的⽇历。

void print_annual_calendar(int year)//输⼊年⽉,输出该⽉的⽇历。

void print_monthly_calendar(int year,int month)//输⼊年份、⽉份、⽇期,计算得到的是这⼀天据今天有多少天,星期⼏。

void print_number_week(int year,int month ,int day)//输⼊公历,显⽰农历。

void gregorian_to_lunar (int year,int month,int day)//输⼊农历节⽓,输出当年农历的年⽉⽇和公历年⽉⽇。

void solar_term(char *str)详细设计说明(1) 查询距今多少天,星期⼏?①计算某⼀⽇期到1939年1⽉1⽇的天数:getDaysTo1939(int year, int month, int day)1. 判断是否是闰年(平年365天,闰年366天),函数如表4-1所⽰:表4- 1 isLeapYear函数定义函数bool isLeapYear(int year)返回值若year年是闰年返回true,否则返回false参数说明year参数是要判断的年份1. 判断某年某⽉有多少天?函数如表4-2所⽰:表4- 2 getDaysOfMonth函数定义函数int getDaysOfMonth(int year,int month)返回值返回year年month⽉的天数参数说明year、month参数分别是要查询的年、⽉例如:计算2020年8⽉14⽇到1939年1⽉1⽇的天数。

万年历程序(不要嫌程序长,功能很齐全,而且对初学者很有帮助)

万年历程序(不要嫌程序长,功能很齐全,而且对初学者很有帮助)

#include <REG51.H>#include <intrins.h>#define uint unsigned int#define uchar unsigned charvoid Stopwatch(); //声明秒表函数void Clock(); //声明闹钟函数void Output(); //声明输出函数void important(); //声明重要函数(相当于mian()函数)void TLimit(); //声明调节温度上下限函数sbit DS1302_CLK = P1^7; //实时时钟时钟线引脚sbit DS1302_IO = P1^6; //实时时钟数据线引脚sbit DS1302_RST = P1^5; //实时时钟复位线引脚sbit wireless_1 = P3^0; //温度过上限标志sbit wireless_2 = P3^1; //温度适中标志sbit wireless_3 = P3^2; //温度过下限标志sbit fan = P3^4; //开风扇标志sbit clock=P3^5; //开关闹钟sbit setclock = P3^6; //闹钟开启控制sbit stopwatch = P3^7; //秒表开启控制sbit ACC0 = ACC^0;sbit ACC7 = ACC^7;char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; //秒,分,时到日,月,年位闪的计数sbit Set = P2^0; //模式切换键sbit Up = P2^1; //加法按钮sbit Down = P2^2; //减法按钮sbit out = P2^3; //立刻跳出调整模式按钮sbit DQ = P1^0; //温度传送数据IO口char c3,c2=0;char c=0,LowerBonud=20,UpperLimit=30;char a1=0,a2=0,a3=0,a4=0;char k[8]={'0','0',':','0','0',':','0','0'};void InitTimer0(void)//产生10ms时间间隔{TMOD = (TMOD & 0XF0) | 0X01; //定时器0工作在模式1,16Bit定时器模式TH0 = (65536 - 8000)/256;TL0 = (65536 - 8000)%256;TR0 =1; ET0 =1;//开启定时器和中断EA = 1;}char done,count,temp,flag,up_flag,down_flag; //uchar temp_value; //温度值uchar TempBuffer[5],week_value[2],h[8]; //unsigned char NL[5];void show_time(); //液晶显示程序/***********1602液晶显示部分子程序****************///Port Definitions**********************************************************sbit LcdRs = P2^5;sbit LcdRw = P2^6;sbit LcdEn = P2^7;sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口//内部等待函数************************************************************************** unsigned char LCD_Wait(void){LcdRs=0;LcdRw=1; _nop_();LcdEn=1; _nop_();LcdEn=0;return DBPort;}//向LCD写入命令或数据************************************************************#define LCD_COMMAND 0 // Command#define LCD_DATA 1 // Data#define LCD_CLEAR_SCREEN 0x01 // 清屏#define LCD_HOMING 0x02 // 光标返回原点void LCD_Write(bit style, unsigned char input){LcdEn=0;LcdRs=style;LcdRw=0; _nop_();DBPort=input; _nop_();//注意顺序LcdEn=1; _nop_();//注意顺序LcdEn=0; _nop_();LCD_Wait();}//设置显示模式************************************************************#define LCD_SHOW 0x04 //显示开#define LCD_HIDE 0x00 //显示关#define LCD_CURSOR 0x02 //显示光标#define LCD_NO_CURSOR 0x00 //无光标#define LCD_FLASH 0x01 //光标闪动#define LCD_NO_FLASH 0x00 //光标不闪动void LCD_SetDisplay(unsigned char DisplayMode){LCD_Write(LCD_COMMAND, 0x08|DisplayMode);}//设置输入模式************************************************************ #define LCD_AC_UP 0x02#define LCD_AC_DOWN 0x00 // default#define LCD_MOVE 0x01 // 画面可平移#define LCD_NO_MOVE 0x00 //defaultvoid LCD_SetInput(unsigned char InputMode){LCD_Write(LCD_COMMAND, 0x04|InputMode);}//初始化LCD************************************************************ void LCD_Initial(){LcdEn=0;LCD_Write(LCD_COMMAND,0x38); //8位数据端口,2行显示,5*7点阵LCD_Write(LCD_COMMAND,0x38);LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //开启显示, 无光标LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC递增, 画面不动}//液晶字符输入的位置************************void GotoXY(unsigned char x, unsigned char y){if(y==0)LCD_Write(LCD_COMMAND,0x80|x);if(y==1)LCD_Write(LCD_COMMAND,0x80|(x-0x40));}//将字符输出到液晶显示void Print(unsigned char *str){while(*str!='\0')//while(*str!='\0'){LCD_Write(LCD_DATA,*str);str++;}}/***********DS1302时钟部分子程序******************/typedef struct SYSTEMTIME{unsigned char Second;unsigned char Minute;unsigned char Hour;unsigned char Week;unsigned char Day;unsigned char Month;unsigned char Year;unsigned char YearH;unsigned char YearL;unsigned char day;unsigned char month;unsigned char year;unsigned char DateString[11];unsigned char NLDateString[12];unsigned char TimeString[9];}SYSTEMTIME; //定义的时间类型SYSTEMTIME CurrentTime; //#define AM(X) X#define PM(X) (X+12) // 转成24小时制#define DS1302_SECOND 0x80 //时钟芯片的寄存器位置,存放时间#define DS1302_MINUTE 0x82#define DS1302_HOUR 0x84#define DS1302_WEEK 0x8A#define DS1302_DAY 0x86#define DS1302_MONTH 0x88#define DS1302_YEAR 0x8C#define DS1302_YEARH 0xC0void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) {unsigned char i;ACC = d;for(i=8; i>0; i--){DS1302_IO = ACC0; //相当于汇编中的RRCDS1302_CLK = 1;DS1302_CLK = 0;ACC = ACC >> 1;}}unsigned char DS1302OutputByte(void) //实时时钟读取一字节(内部函数){unsigned char i;for(i=8; i>0; i--){ACC = ACC >>1; //相当于汇编中的RRCACC7 = DS1302_IO;DS1302_CLK = 1;DS1302_CLK = 0;}return(ACC);}void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据{DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr); // 地址,命令DS1302InputByte(ucDa); // 写1Byte数据DS1302_CLK = 1;DS1302_RST = 0;}unsigned char Read1302(unsigned char ucAddr) //读取DS1302某地址的数据{unsigned char ucData;DS1302_RST = 0;DS1302_CLK = 0;DS1302_RST = 1;DS1302InputByte(ucAddr|0x01); // 地址,命令ucData = DS1302OutputByte(); // 读1Byte数据DS1302_CLK = 1;DS1302_RST = 0;return(ucData);}void DS1302_GetTime(SYSTEMTIME *Time) //获取时钟芯片的时钟数据到自定义的结构型数组{unsigned char ReadV alue;ReadValue = Read1302(DS1302_SECOND);Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_MINUTE);Time->Minute = ((ReadV alue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_HOUR);Time->Hour = ((ReadV alue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_DAY);Time->Day = ((ReadV alue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_WEEK);Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadV alue&0x0F);ReadValue = Read1302(DS1302_MONTH);Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);ReadValue = Read1302(DS1302_YEAR);Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadV alue&0x0F);Time->YearH = 20;}void DateToStr(SYSTEMTIME *Time) //将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]{ if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为2007/07/22{Time->DateString[0] = Time->YearH/10 + '0';Time->DateString[1] = Time->YearH%10 + '0';Time->DateString[2] = Time->Year/10 + '0';Time->DateString[3] = Time->Year%10 + '0';}else{Time->DateString[0] = ' ';Time->DateString[1] = ' ';Time->DateString[2] = ' ';Time->DateString[3] = ' ';}Time->DateString[4] = '/';if(hide_month<2){Time->DateString[5] = Time->Month/10 + '0';Time->DateString[6] = Time->Month%10 + '0';}else{Time->DateString[5] = ' ';Time->DateString[6] = ' ';}Time->DateString[7] = '/';if(hide_day<2){Time->DateString[8] = Time->Day/10 + '0';Time->DateString[9] = Time->Day%10 + '0';}else{Time->DateString[8] = ' ';Time->DateString[9] = ' ';}if(hide_week<2){week_value[0] = Time->Week%10 + '0'; //星期的数据另外放到week_value[]数组里,跟年,月,日的分开存放,因为等一下要在最后显示}else{week_value[0] = ' ';}week_value[1] = '\0';Time->DateString[10] = '\0'; //字符串末尾加'\0' ,判断结束字符}void TimeToStr(SYSTEMTIME *Time) //将时,分,秒数据转换成液晶显示字符放到数组TimeString[];{ if(hide_hour<2){Time->TimeString[0] = Time->Hour/10 + '0';Time->TimeString[1] = Time->Hour%10 + '0';h[0]=Time->TimeString[0];h[1]=Time->TimeString[1];}else{Time->TimeString[0] = ' ';Time->TimeString[1] = ' ';}Time->TimeString[2] = ':';h[2]=Time->TimeString[2];if(hide_min<2){Time->TimeString[3] = Time->Minute/10 + '0';Time->TimeString[4] = Time->Minute%10 + '0';h[3]=Time->TimeString[3];h[4]=Time->TimeString[4];}else{Time->TimeString[3] = ' ';Time->TimeString[4] = ' ';}Time->TimeString[5] = ':';h[5]=Time->TimeString[5];if(hide_sec<2){Time->TimeString[6] = Time->Second/10 + '0';Time->TimeString[7] = Time->Second%10 + '0';h[6]=Time->TimeString[6];h[7]=Time->TimeString[7];}else{Time->TimeString[6] = ' ';Time->TimeString[7] = ' ';}Time->DateString[8] = '\0';if ((a1==h[4])&&(a2==h[3])&&(a3<=h[7])&&((a4==h[6])||(a4==h[6]-1))) {clock=0;c=10;}}void Initial_DS1302(void) //时钟芯片初始化{unsigned char Second=Read1302(DS1302_SECOND);if(Second&0x80) //判断时钟芯片是否关闭{Write1302(0x8e,0x00); //写入允许Write1302(0xC0,0x20);Write1302(0x8c,0x09); //以下写入初始化时间日期:2007/07/25.星期: 3. 时间: 23:59:55 Write1302(0x88,0x06);Write1302(0x86,0x05);Write1302(0x8a,0x07); //xingqiWrite1302(0x84,0x23);Write1302(0x82,0x59);Write1302(0x80,0x55);Write1302(0x8e,0x80); //禁止写入}}/***********ds18b20子程序*************************//***********ds18b20延迟子函数(晶振12MHz )*******/void delay_18B20(unsigned int i){while(i--);}/**********ds18b20初始化函数**********************/void Init_DS18B20(void){unsigned char x=0;DQ = 1; //DQ复位delay_18B20(8); //稍做延时DQ = 0; //单片机将DQ拉低delay_18B20(80); //精确延时大于480usDQ = 1; //拉高总线delay_18B20(14);x=DQ; //稍做延时后如果x=0则初始化成功x=1则初始化失败delay_18B20(20);}/***********ds18b20读一个字节**************/unsigned char ReadOneChar(void){uchar i=0;uchar dat = 0;for (i=8;i>0;i--){DQ = 0; // 给脉冲信号dat>>=1; //数据右移一位DQ = 1; // 给脉冲信号if(DQ)dat|=0x80; //按位或,取最高位delay_18B20(4);}return(dat);}/*************ds18b20写一个字节****************/void WriteOneChar(uchar dat){unsigned char i=0;for (i=8; i>0; i--){DQ = 0;DQ = dat&0x01; //取最低位delay_18B20(5);DQ = 1; //上升沿将数据送入dat>>=1;}}/**************读取ds18b20当前温度************/void ReadTemp(void){unsigned char a=0;unsigned char b=0;unsigned char t=0;Init_DS18B20();WriteOneChar(0xCC); // 跳过读序号列号的操作WriteOneChar(0x44); // 启动温度转换delay_18B20(100); // this message is wery importantInit_DS18B20();WriteOneChar(0xCC); //跳过读序号列号的操作WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器)前两个就是温度delay_18B20(100);a=ReadOneChar(); //读取温度值低位b=ReadOneChar(); //读取温度值高位temp_value=b<<4; //左移四位,扩大16倍,变为一个字节的高四位temp_value+=(a&0xf0)>>4;//取温度值的地位的高四位,右移四位后与温度的高四位相加得到温度值if (temp_value>=UpperLimit) {wireless_1 =0;fan=1;}else {wireless_1 =1;fan=0;}if ((temp_value>LowerBonud)&&(temp_value<UpperLimit)) wireless_2 =0;else wireless_2 =1;if (temp_value<=LowerBonud) wireless_3 =0;else wireless_3 =1;}void temp_to_str() //温度数据转换成液晶字符显示{TempBuffer[0]=temp_value/10+'0'; //十位TempBuffer[1]=temp_value%10+'0'; //个位TempBuffer[2]=0xdf; //温度符号TempBuffer[3]='C';TempBuffer[4]='\0';}void Delay1ms(unsigned int count){unsigned int i,j;for(i=0;i<count;i++)for(j=0;j<120;j++){if (!stopwatch) {c++;Stopwatch();}if (!setclock) {c2++;Clock();}if ((!out)&&(!Set)) {TLimit();}if (!out) clock=1; //关闭闹钟}}/*延时子程序*/void mdelay(uint delay){ uint i;for(;delay>0;delay--)for(i=0;i<32;i++); //1ms延时.}/*****************************阳历转换阴历***********************************/ code unsigned char year_code[597]={0x04,0xAe,0x53, //1901 00x0A,0x57,0x48, //1902 30x55,0x26,0xBd, //1903 60x0d,0x26,0x50, //1904 90x0d,0x95,0x44, //1905 120x46,0xAA,0xB9, //1906 150x05,0x6A,0x4d, //1907 180x09,0xAd,0x42, //1908 210x24,0xAe,0xB6, //19090x04,0xAe,0x4A, //19100x6A,0x4d,0xBe, //19110x0A,0x4d,0x52, //19120x0d,0x25,0x46, //19130x5d,0x52,0xBA, //19140x0B,0x54,0x4e, //19150x0d,0x6A,0x43, //19160x29,0x6d,0x37, //19170x09,0x5B,0x4B, //19180x74,0x9B,0xC1, //19190x04,0x97,0x54, //19200x0A,0x4B,0x48, //19210x5B,0x25,0xBC, //19220x06,0xA5,0x50, //19230x06,0xd4,0x45, //19240x4A,0xdA,0xB8, //19250x02,0xB6,0x4d, //19260x09,0x57,0x42, //19270x24,0x97,0xB7, //19280x04,0x97,0x4A, //19290x66,0x4B,0x3e, //19300x0d,0x4A,0x51, //19310x0e,0xA5,0x46, //19320x56,0xd4,0xBA, //19330x05,0xAd,0x4e, //19340x02,0xB6,0x44, //19350x39,0x37,0x38, //19360x09,0x2e,0x4B, //19370x0C,0x95,0x53, //1939 0x0d,0x4A,0x48, //1940 0x6d,0xA5,0x3B, //1941 0x0B,0x55,0x4f, //1942 0x05,0x6A,0x45, //1943 0x4A,0xAd,0xB9, //1944 0x02,0x5d,0x4d, //1945 0x09,0x2d,0x42, //1946 0x2C,0x95,0xB6, //1947 0x0A,0x95,0x4A, //1948 0x7B,0x4A,0xBd, //1949 0x06,0xCA,0x51, //1950 0x0B,0x55,0x46, //1951 0x55,0x5A,0xBB, //1952 0x04,0xdA,0x4e, //1953 0x0A,0x5B,0x43, //1954 0x35,0x2B,0xB8, //1955 0x05,0x2B,0x4C, //1956 0x8A,0x95,0x3f, //1957 0x0e,0x95,0x52, //1958 0x06,0xAA,0x48, //1959 0x7A,0xd5,0x3C, //1960 0x0A,0xB5,0x4f, //1961 0x04,0xB6,0x45, //1962 0x4A,0x57,0x39, //1963 0x0A,0x57,0x4d, //1964 0x05,0x26,0x42, //1965 0x3e,0x93,0x35, //1966 0x0d,0x95,0x49, //1967 0x75,0xAA,0xBe, //1968 0x05,0x6A,0x51, //1969 0x09,0x6d,0x46, //1970 0x54,0xAe,0xBB, //1971 0x04,0xAd,0x4f, //1972 0x0A,0x4d,0x43, //1973 0x4d,0x26,0xB7, //1974 0x0d,0x25,0x4B, //1975 0x8d,0x52,0xBf, //1976 0x0B,0x54,0x52, //1977 0x0B,0x6A,0x47, //1978 0x69,0x6d,0x3C, //1979 0x09,0x5B,0x50, //1980 0x04,0x9B,0x45, //19810x0A,0x4B,0x4d, //19830xAB,0x25,0xC2, //19840x06,0xA5,0x54, //19850x06,0xd4,0x49, //19860x6A,0xdA,0x3d, //19870x0A,0xB6,0x51, //19880x09,0x37,0x46, //19890x54,0x97,0xBB, //19900x04,0x97,0x4f, //19910x06,0x4B,0x44, //19920x36,0xA5,0x37, //19930x0e,0xA5,0x4A, //19940x86,0xB2,0xBf, //19950x05,0xAC,0x53, //19960x0A,0xB6,0x47, //19970x59,0x36,0xBC, //19980x09,0x2e,0x50, //1999 294 0x0C,0x96,0x45, //2000 297 0x4d,0x4A,0xB8, //20010x0d,0x4A,0x4C, //20020x0d,0xA5,0x41, //20030x25,0xAA,0xB6, //20040x05,0x6A,0x49, //20050x7A,0xAd,0xBd, //20060x02,0x5d,0x52, //20070x09,0x2d,0x47, //20080x5C,0x95,0xBA, //20090x0A,0x95,0x4e, //20100x0B,0x4A,0x43, //20110x4B,0x55,0x37, //20120x0A,0xd5,0x4A, //20130x95,0x5A,0xBf, //20140x04,0xBA,0x53, //20150x0A,0x5B,0x48, //20160x65,0x2B,0xBC, //20170x05,0x2B,0x50, //20180x0A,0x93,0x45, //20190x47,0x4A,0xB9, //20200x06,0xAA,0x4C, //20210x0A,0xd5,0x41, //20220x24,0xdA,0xB6, //20230x04,0xB6,0x4A, //20240x69,0x57,0x3d, //20250x0d,0x26,0x46, //2027 0x5e,0x93,0x3A, //2028 0x0d,0x53,0x4d, //2029 0x05,0xAA,0x43, //2030 0x36,0xB5,0x37, //2031 0x09,0x6d,0x4B, //2032 0xB4,0xAe,0xBf, //2033 0x04,0xAd,0x53, //2034 0x0A,0x4d,0x48, //2035 0x6d,0x25,0xBC, //2036 0x0d,0x25,0x4f, //2037 0x0d,0x52,0x44, //2038 0x5d,0xAA,0x38, //2039 0x0B,0x5A,0x4C, //2040 0x05,0x6d,0x41, //2041 0x24,0xAd,0xB6, //2042 0x04,0x9B,0x4A, //2043 0x7A,0x4B,0xBe, //2044 0x0A,0x4B,0x51, //2045 0x0A,0xA5,0x46, //2046 0x5B,0x52,0xBA, //2047 0x06,0xd2,0x4e, //2048 0x0A,0xdA,0x42, //2049 0x35,0x5B,0x37, //2050 0x09,0x37,0x4B, //2051 0x84,0x97,0xC1, //2052 0x04,0x97,0x53, //2053 0x06,0x4B,0x48, //2054 0x66,0xA5,0x3C, //2055 0x0e,0xA5,0x4f, //2056 0x06,0xB2,0x44, //2057 0x4A,0xB6,0x38, //2058 0x0A,0xAe,0x4C, //2059 0x09,0x2e,0x42, //2060 0x3C,0x97,0x35, //2061 0x0C,0x96,0x49, //2062 0x7d,0x4A,0xBd, //2063 0x0d,0x4A,0x51, //2064 0x0d,0xA5,0x45, //2065 0x55,0xAA,0xBA, //2066 0x05,0x6A,0x4e, //2067 0x0A,0x6d,0x43, //2068 0x45,0x2e,0xB7, //20690x8A,0x95,0xBf, //20710x0A,0x95,0x53, //20720x0B,0x4A,0x47, //20730x6B,0x55,0x3B, //20740x0A,0xd5,0x4f, //20750x05,0x5A,0x45, //20760x4A,0x5d,0x38, //20770x0A,0x5B,0x4C, //20780x05,0x2B,0x42, //20790x3A,0x93,0xB6, //20800x06,0x93,0x49, //20810x77,0x29,0xBd, //20820x06,0xAA,0x51, //20830x0A,0xd5,0x46, //20840x54,0xdA,0xBA, //20850x04,0xB6,0x4e, //20860x0A,0x57,0x43, //20870x45,0x27,0x38, //20880x0d,0x26,0x4A, //20890x8e,0x93,0x3e, //20900x0d,0x52,0x52, //20910x0d,0xAA,0x47, //20920x66,0xB5,0x3B, //20930x05,0x6d,0x4f, //20940x04,0xAe,0x45, //20950x4A,0x4e,0xB9, //20960x0A,0x4d,0x4C, //20970x0d,0x15,0x41, //20980x2d,0x92,0xB5, //2099};//月份数据表code unsigned char day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};code unsigned int day_code2[3]={0x111,0x130,0x14e};/**************************************************************************** * 子函数,用于读取数据表中农历月的大月或小月,如果该月为大返回1,为小返回0****************************************************************************/ bit GetMoonDay(unsigned char month_p,unsigned short table_addr){switch (month_p){case 1:if((year_code[table_addr]&0x08)==0) return(0);else return(1);case 2:if((year_code[table_addr]&0x04)==0) return(0);else return(1);case 3:if((year_code[table_addr]&0x02)==0) return(0);else return(1);case 4:if((year_code[table_addr]&0x01)==0) return(0);else return(1);case 5:if((year_code[table_addr+1]&0x80)==0) return(0);else return(1);case 6:if((year_code[table_addr+1]&0x40)==0) return(0);else return(1);case 7:if((year_code[table_addr+1]&0x20)==0) return(0);else return(1);case 8:if((year_code[table_addr+1]&0x10)==0) return(0);else return(1);case 9:if((year_code[table_addr+1]&0x08)==0) return(0);else return(1);case 10:if((year_code[table_addr+1]&0x04)==0) return(0);else return(1);case 11:if((year_code[table_addr+1]&0x02)==0) return(0);else return(1);case 12:if((year_code[table_addr+1]&0x01)==0) return(0);else return(1);case 13:if((year_code[table_addr+2]&0x80)==0) return(0);else return(1);}return(0);}/****************************************************************************** ***************************** 函数名称:GetChinaCalendar** 功能描述:公农历转换(只允许1901-2099年)** 输入: year 公历年** month 公历月** day 公历日** p 储存农历日期地址** 输出: 1 成功** 0 失败** 作者: Campo** 修改: 赖皮** 日期: 2007年02月06日**-------------------------------------------------------------------------------------------------------** 修改人:** 日期:**------------------------------------------------------------------------------------------------------******************************************************************************* *************************/unsigned char GetChinaCalendar(unsigned int year,unsigned char month,unsigned char day){unsigned char temp1,temp2,temp3,month_p,yearH,yearL;unsigned char flag_y;unsigned short temp4,table_addr;yearH=year/100; yearL=year%100;if((yearH!=19)&&(yearH!=20)) return(0);// 定位数据表地址if(yearH==20) table_addr=(yearL+100-1)*3;else table_addr=(yearL-1)*3;// 取当年春节所在的公历月份temp1=year_code[table_addr+2]&0x60;temp1>>=5;// 取当年春节所在的公历日temp2=year_code[table_addr+2]&31;// 计算当年春年离当年元旦的天数,春节只会在公历1月或2月if(temp1==1) t emp3=temp2-1;else temp3=temp2+31-1;// 计算公历日离当年元旦的天数if (month<10) temp4=day_code1[month-1]+day-1;else temp4=day_code2[month-10]+day-1;// 如果公历月大于2月并且该年的2月为闰月,天数加1if ((month>2)&&(yearL%4==0)) temp4++;// 判断公历日在春节前还是春节后if (temp4>=temp3){temp4-=temp3;month=1;month_p=1;flag_y=0;if(GetMoonDay(month_p,table_addr)==0) temp1=29; //小月29天else temp1=30; //大小30天// 从数据表中取该年的闰月月份,如为0则该年无闰月temp2=year_code[table_addr]/16;while(temp4>=temp1){temp4-=temp1;month_p++;if(month==temp2){NL[4]='R';flag_y=~flag_y;if(flag_y==0) {month++;NL[4]=' ';}}else {month++;NL[4]=' ';}if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;}day=temp4+1;}// 公历日在春节前使用下面代码进行运算else{temp3-=temp4;if (yearL==0){yearL=100-1;yearH=19;}else yearL--;table_addr-=3;month=12;temp2=year_code[table_addr]/16;if (temp2==0) {month_p=12;NL[4]=' ';}else {month_p=13;NL[4]='R';}flag_y=0;if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;while(temp3>temp1){temp3-=temp1;month_p--;if(flag_y==0) month--;if(month==temp2) flag_y=~flag_y;if(GetMoonDay(month_p,table_addr)==0) temp1=29;else temp1=30;}day=temp1-temp3+1;}/////////////////////////NL[0]=yearH;NL[1]=yearL;NL[2]=month;NL[3]=day;return(1);}void zhuan(SYSTEMTIME *Time){GetChinaCalendar(Time->YearH*100+Time->Year,Time->Month,Time->Day);Time->day = NL[3];Time->month =NL[2];//将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为2007/07/22{Time->NLDateString[0] = NL[0]/10 + '0';Time->NLDateString[1] = NL[0]%10 + '0';Time->NLDateString[2] = NL[1]/10 + '0';Time->NLDateString[3] = NL[1]%10 + '0';}else{Time->NLDateString[0] = ' ';Time->NLDateString[1] = ' ';Time->NLDateString[2] = ' ';Time->NLDateString[3] = ' ';}Time->NLDateString[4] = '/';if(hide_month<2){Time->NLDateString[5] =NL[4] ;Time->NLDateString[6] = Time->month/10 + '0';Time->NLDateString[7] = Time->month%10 + '0';}else{Time->NLDateString[6] = ' ';Time->NLDateString[7] = ' ';}Time->NLDateString[8] = '/';if(hide_day<2){Time->NLDateString[9] = Time->day/10 + '0';Time->NLDateString[10] = Time->day%10 + '0';}else{Time->NLDateString[9] = ' ';Time->NLDateString[10] = ' ';}Time->NLDateString[11] = '\0'; //字符串末尾加'\0' ,判断结束字符//////////////////////}//显示天干生肖void lcd_digan(){temp = NL[1]%12;switch(temp){case 0: GotoXY(3,0); Print("Chen LongYear");//龙break;case 1: GotoXY(3,0); Print("Si She ear");//蛇break;case 2: GotoXY(3,0); Print("Wu Ma Year");//马break;case 3: GotoXY(3,0); Print("Wei YangYear"); //羊break;case 4: GotoXY(3,0); Print("Chen Hou Year"); //猴break;case 5: GotoXY(3,0); Print("You Ji Year"); //鸡break;case 6: GotoXY(3,0); Print("Xu Gou Year"); //狗break;case 7: GotoXY(3,0); Print("Hai Zhu Year"); //猪break;case 8: GotoXY(3,0); Print("Zi Shu Year"); //鼠break;case 9: GotoXY(3,0); Print("Chou Niu Year"); //牛break;case 10:GotoXY(3,0); Print("Yin Hu Year"); //虎break;case 11:GotoXY(3,0); Print("Mao Tu Year"); //兔break;}}//显示天支void lcd_tianzhi(){temp = NL[1]%10;switch(temp){case 0: GotoXY(0,0); Print("Gen");//庚break;case 1: GotoXY(0,0); Print("Xin");//辛break;case 2: GotoXY(0,0); Print("Ren");//壬break;case 3: GotoXY(0,0); Print("Gui"); //癸?break;case 4: GotoXY(0,0); Print("Jia"); //甲break;case 5: GotoXY(0,0); Print("Yi"); //乙break;case 6: GotoXY(0,0); Print("Bin"); //丙break;case 7: GotoXY(0,0); Print("Din"); //丁break;case 8: GotoXY(0,0); Print("Wu"); //戊break;case 9: GotoXY(0,0); Print("Ji");//己break;}}void outkey() //跳出调整模式,返回默认显示{ uchar Second;if(out==0)//if(out==0||wireless_1==1) //{ mdelay(4);count=0;hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;Second=Read1302(DS1302_SECOND);Write1302(0x8e,0x00); //写入允许Write1302(0x80,Second&0x7f);Write1302(0x8E,0x80); //禁止写入done=0;while(out==0);//while(wireless_1==1);}}////////////////////////////////////////////////////////////////////////////////////////////////////////////void Upkey()//升序按键{Up=1;if(Up==0)//if(Up==0||wireless_2==1){mdelay(4);switch(count){case 1:temp=Read1302(DS1302_SECOND); //读取秒数temp=temp+1; //秒数加1up_flag=1; //数据调整后更新标志if((temp&0x7f)>0x59) //超过59秒,清零temp=0;break;case 2:temp=Read1302(DS1302_MINUTE); //读取分数temp=temp+1; //分数加1up_flag=1;if(temp>0x59) //超过59分,清零temp=0;break;case 3:temp=Read1302(DS1302_HOUR); //读取小时数temp=temp+1; //小时数加1up_flag=1;if(temp>0x23) //超过23小时,清零temp=0;break;case 4:temp=Read1302(DS1302_WEEK); //读取星期数temp=temp+1; //星期数加1up_flag=1;if(temp>0x7)temp=1;break;case 5:temp=Read1302(DS1302_DAY); //读取日数temp=temp+1; //日数加1up_flag=1;if(temp>0x31)temp=1;break;case 6:temp=Read1302(DS1302_MONTH); //读取月数temp=temp+1; //月数加1up_flag=1;if(temp>0x12)temp=1;break;case 7:temp=Read1302(DS1302_YEAR); //读取年数temp=temp+1; //年数加1up_flag=1;if(temp>0x85)temp=0;break;default:break;}while(Up==0);//while(wireless_2==1);}}////////////////////////////////////////////////////////////////////////////////////////////////////////////void Downkey()//降序按键{Down=1;if(Down==0)//if(Down==0||wireless_3==1){mdelay(4);switch(count){case 1:temp=Read1302(DS1302_SECOND); //读取秒数temp=temp-1; //秒数减1down_flag=1; //数据调整后更新标志if(temp==0x7f) //小于0秒,返回59秒temp=0x59;break;case 2:temp=Read1302(DS1302_MINUTE); //读取分数temp=temp-1; //分数减1down_flag=1;if(temp==-1)temp=0x59; //小于0秒,返回59秒break;case 3:temp=Read1302(DS1302_HOUR); //读取小时数temp=temp-1; //小时数减1down_flag=1;if(temp==-1)temp=0x23;break;case 4:temp=Read1302(DS1302_WEEK); //读取星期数temp=temp-1; //星期数减1down_flag=1;if(temp==0)temp=0x7;;break;case 5:temp=Read1302(DS1302_DAY); //读取日数temp=temp-1; //日数减1down_flag=1;if(temp==0)temp=31;break;case 6:temp=Read1302(DS1302_MONTH); //读取月数temp=temp-1; //月数减1down_flag=1;if(temp==0)temp=12;break;case 7:temp=Read1302(DS1302_YEAR); //读取年数temp=temp-1; //年数减1down_flag=1;if(temp==-1)temp=0x85;break;default:break;}while(Down==0);//while(wireless_3==1);}}void Setkey()//模式选择按键{Set=1;if(Set==0)//if(Set==0||wireless_4==1){mdelay(4);count=count+1; //Setkey按一次,count就加1done=1; //进入调整模式while(Set==0);//while(wireless_4==1);}}void keydone()//按键功能执行{ uchar Second;if(flag==0) //关闭时钟,停止计时{ Write1302(0x8e,0x00); //写入允许temp=Read1302(0x80);Write1302(0x80,temp|0x80);Write1302(0x8e,0x80); //禁止写入flag=1;。

c语言万年历并判断是否为闰年课程设计程序

c语言万年历并判断是否为闰年课程设计程序

【导言】C语言是一门被广泛使用的计算机编程语言,它的应用范围涵盖了各个领域。

而在计算机程序设计课程中,常常会有关于时间和日期的相关内容。

今天我们将讨论一个关于C语言编程的课程设计题目——C 语言万年历并判断是否为闰年程序设计。

【一、问题描述】1. 万年历的定义:所谓万年历,是指能够展示任意年份的日历,并且准确反映该年的月份和日期分布。

2. 闰年的定义:闰年是指公历中普通年份为365天,而闰年为366天的年份。

闰年有特定的判断规则。

【二、程序设计目标】1. 设计一个C语言程序,能够根据用户输入的年份,打印出该年的日历。

2. 在打印日历的判断该年份是否为闰年,并给出判断结果。

【三、程序设计思路】1. 确定程序的输入和输出:输入:用户输入一个年份输出:打印该年的日历,并给出是否为闰年的判断结果2. 了解计算闰年的规则:- 能被4整除但不能被100整除的年份为闰年- 能被400整除的年份也为闰年3. 设计程序的逻辑:- 首先根据输入的年份计算该年的日历,并以直观的形式输出- 然后根据闰年的判断规则,判断该年份是否为闰年,并给出判断结果【四、程序设计实现】以下是C语言程序的设计实现:```c#include <stdio.h>// 判断闰年的函数int isLeapYear(int year) {return (year 4 == 0 year 100 != 0) || (year 400 == 0);}// 打印日历的函数void printCalendar(int year) {// 实现日历打印的逻辑// 省略具体实现细节printf("这里是d年的日历\n", year);}int m本人n() {// 用户输入年份int year;printf("请输入年份:");scanf("d", year);// 打印该年的日历printCalendar(year);// 判断该年是否为闰年if (isLeapYear(year)) {printf("d年是闰年\n", year);} else {printf("d年不是闰年\n", year);}return 0;}```【五、程序设计测试】接下来我们对设计的C语言程序进行测试,例如输入2020年,程序将输出2020年的日历,并给出判断结果。

万年历代码

万年历代码
注意:
“Visual Studio 2008 命令提示”自动设置了 Visual C++ 编译器和所需的任何库的正确路径。应使用它而不是使用普通的“命令提示符”窗口。
在命令提示符下,键入notepad simple.cpp,并按 Enter。
在系统提示是否创建新文件时,单击“是”。
在记事本中,键入下列各行:
else
cout<<"\t";
}
cout<<endl;
cout<<"--------------------------------------------------\n";
}
作者:…烟花飘飘…2008-7-9 22:46 回复此发言
--------------------------------------------------------------------------------
使用命名空间
2008要求较严格 .h是C语言里的东西,所以包含头文件时应该用
#include <iostream>
using namespace std;
创建新项目:
在“文件”菜单上,指向“新建”,然后单击“项目”。
在“Visual C++”项目类型中,单击“Win32”,然后单击“Win32 控制台应用程序”。
4 回复:费了一个小时写的◣万年历◢,o(∩_∩)o..烟花飘飘
用API了,不错
ShineFancyRay
作者:120.128.149.*2008-7-9 23:14 回复此发言
--------------------------------------------------------------------------------

单片机万年历程序代码

单片机万年历程序代码

单片机万年历程序代码以下是一个示例单片机万年历的程序代码:```c#include <reg51.h>typedef unsigned char uchar;typedef unsigned int uint;sbit K1 = P2^0; // 显示年份sbit K2 = P2^1; // 显示月份sbit K3 = P2^2; // 显示日期uchar code year_tab[] = {31,28,31,30,31,30,31,31,30,31,30,31}; uchar code week_tab[] = {0x06,0x07,0x01,0x02,0x03,0x04,0x05}; uchar year, month, day, week;void delay(uint ms){uint i, j;for(i=ms;i>0;i--)for(j=110;j>0;j--);}uchar getKey(){if(K1==0) {delay(5);if(K1==0)return 1;while(!K1);}if(K2==0) {delay(5);if(K2==0)return 2;while(!K2);}if(K3==0) {delay(5);if(K3==0)return 3;while(!K3);}return 0;}void display(uchar num) {P0 = num;delay(1);P0 = 0x00;}void init(){TMOD=0x01;TH0=0xFC;TL0=0x67;EA=1;ET0=1;TR0=1;}void main(){init();while(1) {uchar key = getKey();if(key == 1) {year++;if(year == 100)year = 0;}else if(key == 2) {month++;if(month == 13)month = 1;}else if(key == 3) {day++;if(day > year_tab[month-1]) { day = 1;}}display(year / 10);display(year % 10);display(month / 10);display(month % 10);display(day / 10);display(day % 10);display(week);}}void timer0() interrupt 1{TH0=0xFC;TL0=0x67;week++;if(week == 7)week = 0;}```该代码的主要思路是通过外部三个按键模拟年、月和日的调节,通过一个定时器不断更新星期的计数,然后将年、月、日和星期分别在数码管上显示出来。

万年历代码c语言

万年历代码c语言

万年历代码c语言万年历是一种实用的日历工具,它可以根据年、月、日来显示当天的日期信息,并可以切换到其他日期来查询对应的日期信息。

在编写万年历的代码时,我们需要考虑输入的年份是否为闰年、每个月的天数、以及每个月第一天是星期几等等。

以下是一份使用C语言编写万年历的参考代码:```c#include <stdio.h>// 判断是否为闰年int isLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }// 获取某年某月的天数int getMonthDays(int year, int month) {int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return days[month - 1];}// 获取某年某月第一天是星期几int getFirstDayOfWeek(int year, int month) {int day = 1;for (int i = 1800; i < year; i++) {if (isLeapYear(i)) {day = (day + 366) % 7;} else {day = (day + 365) % 7;}}for (int i = 1; i < month; i++) {day = (day + getMonthDays(year, i)) % 7;}return day;}int main() {int year, month;printf("请输入年份:");scanf("%d", &year);printf("请输入月份:");scanf("%d", &month);// 判断输入是否合法if (year < 1800 || month < 1 || month > 12) {printf("输入的年份或月份不合法!\n");return 0;}// 获取某年某月的天数和第一天是星期几int days = getMonthDays(year, month);int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印万年历printf("日一二三四五六\n");for (int i = 0; i < firstDayOfWeek; i++) {printf(" ");}for (int i = 1; i <= days; i++) {printf("%2d ", i);if ((firstDayOfWeek + i) % 7 == 0) {printf("\n");}}printf("\n");return 0;}```以上代码实现了一个简单的万年历功能,在控制台输出给定年份和月份的月历。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《C程序设计》课程设计报告2011-2012学年第二学期设计题目:万年历的设计指导教师:李素若完成时间:2012年6月1日至2011年6月26日第1组报告成员姓名专业班级学号李晓龙计算机科学与技术一班2011404010101王帅计算机科学与技术一班2011404010102郑乐计算机科学与技术一班2011404010103赵清泉计算机科学与技术一班2011404010104朱叶广计算机科学与技术一班2011404010105方欢计算机科学与技术一班2011404010106成绩评定:分任课教师签字:2012年6月21日目录目录目录 (2)1序言 (3)2设计题目 (3)3问题定义 (4)4总体分析与设计 (5)4.1总体流程图 (5)4.2算法中涉及的知识点 (6)4.3详细设计 (6)5调试与测试 (9)6参考文献 (11)7总结与心得 (11)1序言我们经过了一个学期的c语言课程的学习,对计算机变成所需的c语言有了大体的了解,能够看懂和编写一些简单的c语言程序,为了更好的加深同学们对c语言的认识,提升同学们的c语言时间编程能力,所以进行了c语言的课程设计。

本程序是一个万年历程序,可以在程序中任意的输入公元年份,通过程序的将该年份的各种信息形象直观的显示出来,达到c语言程序编写所体现的方便快捷的功效。

同时,通过对该程序c语言的编写,能够在训炼我们在课本中学到各种知识点,并将其应用其中。

2设计题目:1设计编写一个C程序—“万年历”。

2输入任一年将显示出该年的所有月份日期,对应的星期。

3下一行显示星期,从周日到周六,中英文都可以。

4下一行开始显示日期从1号开始,并按其是周几实际情况与上面的星期数垂直对齐。

5月份:中文英文都可以,注意闰年情况。

3问题定义(或系统分析):1要输入任一年显示出该年的所有月份日期,应该先设计具体的输出格式,如下:the calendar of the year2008.JanuarySun Mon Tue Wed Thu Fri Sat12345678910111213141516171819202122232425262728293031FebruarySun Mon Tue Wed Thu Fri Sat1234567891011121314151617181920212223242526272829=============================================2判断是不是闰年(year%4!=0||year%100==0&&year%400!=0)满足条件就是平年,把一年中的所有月份分为四类(28,29,30,31)。

3求出一年中的第一天是周几(year+(year-1)/4-(year-1)/100+(year-1)/400)%7。

4有指针数组存放十二个月的英文和星期的英文缩写。

5用FOR循环输出各月份的日历4总体设计与详细设计:4.1总体流程图开始定义整型变year,i=0,j=0,a,n,m=1,k=0和函数定义一个指针变量**p定义两个字符型数输入年份,yeara=(year+(year-1)/4-(year-1)/100+(year-1)/400)i++,i<4n=judge(year,i+1),p=mon输出月份回Nj++,j<输出星期回(a+m)%7==YN输出回车n==2&&m==2YYn==3&&m==2 break,退出本次循环break,退出本次循环n==4&&m==3break,退出本次循环a=(a+m)%7回车NYYY Y4.2算法中涉及的知识点1定义整型,字符型,指针变量,字符型数组和指针数组。

数组的初始化和引用和指针的使用。

2函数的定义、声名、调用以及函数的返回值。

3程序的三种基本结构:顺序,选择,循环。

if语句的嵌套,for语句的嵌套。

4赋值语句的使用。

5输入(scanf)输出(printf)函数的应用。

6算术运算符、关系运算符和逻辑运算符的应用.4.3详细设计:#include<stdio.h>int judge(int year,int month){if(month==1||month==3||month==5||month==7||month==8||mon th==10||month==12)return(1);else if(month==2){if(year%4!=0||year%100==0&&year%400!=0)return(2);else return(3);}else return(4);}int main(){int year,i,j,a,n,m,k;char**p;char* week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};char*month[]={"January","February","March","April","May", "June","July","August","September","October","November","De cember"};printf("please inter the year:");scanf("%d",&year);printf("\n");printf("the calendar of the year%d.",year);printf("\n");a=((year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400+1)%7; for(i=0;i<12;i++){n=judge(year,i+1);p=month+i;printf("%s\n",*p);printf("\n");for(j=0;j<7;j++){p=week+j;printf("%6s",*p);}printf("\n");for(k=0;k<a;k++)printf("");for(m=1;m<32;m++){printf("%6d",m);if((a+m)%7==0)printf("\n");if(n==1&&m==31)break;else if(n==2&&m==28)break;else if(n==3&&m==29)break;else if(n==4&&m==30)break;}a=(a+m)%7;printf("\n");printf("============================================");printf("\n");}}5调试与测试:1.登陆界面图4-1登陆界面提供了四个选项进行选择,并提示输入后的操作方法;2.显示某年全年日历图4-2输入指令后,依次打印出每个月份的日历。

并显示平年或者闰年;3.显示某月日历5参考文献[1]谭浩强著《C语言程序设计》北京:清华大学出版社2005[2]百度知道6总结与心得:1.C语言是目前流行的计算机日历程序,比较典型的是Windows各版本中的日历程序以及基础于该程序所开发的各种应用程序中的日历程序。

然而,这些程序都千篇一律的局限在一个很短的时间范围内。

但是,在很多情况下,特别是在众多的科学研究领域中,一个时间跨度较大的日历程序是很有参考价值的。

C程序设计语言充分发挥了其自身无与伦比的优越性,用极短的程序文本填补了这一领域的空白,同时用铁的事实强有力的告诉世人:C程序设计语言作为计算机语言家族的新生事物,其发展前景是极为广阔的。

2.经过上一个学期对《C程序设计》的学习,我们学习了理论知识,了解了C语言程序设计的思想,这些知识都为我们的下一步学习打下了坚实的基础。

通过课程设计,一方面是为了检查我们一个学期来我们学习的成果,另一方面也是为了让我们进一步的掌握和运用它,同时也让我们认清自己的不足之处和薄弱环节,加以弥补和加强。

3.通过对c语言万年历的设计进一步的巩固了用c语言编写程序,并且有利于更好的掌握c语言!4.在万年历的编写过程中也体会到了做事情一顶要细心、认真。

更加知道了要掌握好基础知识。

还有体会到了成功的感觉!在万年历的设计过程中更加体会到了团队合作的重要性,知道了只有团队合作才会更好的完成设计!也体会到以后在工作中团队合作的必要性和重要性!5.通过本项课程设计也培养了我独立思考、综合运用所学有关相应知识的能力,掌握工程软件设计的基本方法,强化上机动手编程能力,闯过理论与实践相结合的难关!6.由于C语言是近年在国内外得到迅速推广应用的一种语言。

C语言功能丰富,表达能力强,使用灵活方便,应用面广,目标程序效率高,可移植性好,既具有高级语言的优点,又具有低级语言的许多特点。

通过这次的c语言程序设计更加了解了c语言的好处和其可用性!7.在这次课程设计中也知道了自己的动手能力不强有待进一部的提高!在设计过程中不能够把书本上的知识与实践相结合,这也就增加了设计不好该程序的想法!在设计过程中的一次次设计错误增加了我放弃的想法!不过经过大家的努力终于完成了课程设计!完成该程序后想起自己以前的每一次对自己失去信心,就觉得并不是在知识掌握上打败了,而是自己对自己缺乏信心!只要自己对自己不失去信心相信就可以完成那些以前认为完成不了的事情!也让我懂得了要想成功首先就必须有很强的自信心!懂得了自己以后要在做任何事情时都要自信!通过本次课程设计我第一次通过自己的实践对书本上的知识进行了与生活相关的运用,使我受益匪浅,我将不断地学习c语言知识,不断完善自己,提升自己对c语言的理解力。

相关文档
最新文档