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字节。

万年历代码

万年历代码
for(i=0,j=0;i<=99;i++)
{if(shuori[0][i]!=0 || zhongqi[0][i]!=0) printf("\n第%d个:",i+1);
if(shuori[0][i]!=0) {printf("\t朔日在:");count4(shuori[0][i],0);}
if (M>=11) result=result-7;
else if (M>=9) result=result-6;
else if (M>=6) result=result-5;
else if (M>=4) result=result-4;
else if (M>=2) result=result-3;
{date[2][j]=shuori[1][i];
#include "stdio.h"
#include "math.h"
int count_day(int Y,int M,int D)//根据日期,求出在年历里是第几天
{return (Y-1)*365+count(Y,M)+count2(M)+D;}
int count(int Y,int M)//到今天为止有多少2月28日;但当前月少于3月,不把当月2月28号不用加进去
else printf("%d\n",RESULT);
printf("是第%d次润年!\n",count(Y,5));
printf("是这一年的第%d天!\n",count2(M)+D);

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++)<>。

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语言农历表算法

C语言农历表算法

#include<stdio.h>void DateTrans(char *chDate,int *nYear,int *nMonth,int *nDay);// 1int IsLeapYear(int nYear);// 2int GetWeekOfFirstday(int nYear);// 3int GetWeek(int nYear,int nMonth,int nDay,int nWeekOfFirstday);// 4void PrintCalendar(int nWeek,int nDay,int nMonthDays,char *chDate);// 5void DateTrans(char *chDate,int *nYear,int *nMonth,int *nDay)// 1{*nYear=(chDate[0]-'0')*1000+(chDate[1]-'0')*100+(chDate[2]-'0')*10+chDate[3] -'0';*nMonth=(chDate[5]-'0')*10+chDate[6]-'0';*nDay=(chDate[8]-'0')*10+chDate[9]-'0';}int IsLeapYear(int nYear)// 2{if(nYear%4==0)return 1;elsereturn 0;}int GetWeekOfFirstday(int nYear)// 3{if(nYear>2000)return ((nYear-2001)*365+(nYear-2001)/4+1)%7;else if(nYear<2000)return 6-((2000-nYear)*365+(2000-nYear)/4)%7;elsereturn 6;}int GetWeek(int nYear,int nMonth,int nDay,int nWeekOfFirstday)// 4{int nDaysYear[]={31,28,31,30,31,30,31,31,30,31,30,31};int nDaysLeapYear[]={31,29,31,30,31,30,31,31,30,31,30,31};int i,sum=0;if(nYear%4==0){for(i=0;i<(nMonth-1);i++){sum+=nDaysLeapYear[i];}return (sum+nDay+nWeekOfFirstday-1)%7;}else{for(i=0;i<(nMonth-1);i++){sum+=nDaysYear[i];}return (sum+nDay+nWeekOfFirstday-1)%7;}}void PrintCalendar(int nWeek,int nDay,int nMonthDays,char *chDate) // 5{int i,j;printf("the calender of this month as following:\n");printf("*********************************\n");printf(" SUN MON TUE WEN THU FRI STA\n");for(i=1,j=1;j<=nMonthDays;i++){if(i<=nWeek+1)printf(" ");else{printf("%4d",j);j++;}if(i%7==0)printf("\n");}printf("\n********************************\n");printf("OK!\n");}main(){char chDate[11],i=0,j,isleapyear;int nYear,nMonth,nDay;int nWeekOfFirstday;int nMonthDays;int nWeek;char *Week;int nDaysYear[]={31,28,31,30,31,30,31,31,30,31,30,31};int nDaysLeapYear[]={31,29,31,30,31,30,31,31,30,31,30,31};printf("请输入你要查找的日期,如(2001\\01\\01或2001.01.01:\n或者是你想要日历的月份:如(2000\\01或2000.01):\n");do{scanf("%c",&chDate[i]);i++;}while(chDate[i-1]!='\n');if(i==11)chDate[i-1]='\0';elsefor(j=8;j<11;j++)chDate[j]='0';DateTrans(chDate,&nYear,&nMonth,&nDay);// 1while(nYear<=0||nMonth<1||nMonth>12){printf("查询年月chDate非法\n");return 0;}nWeekOfFirstday = GetWeekOfFirstday(nYear);// 3nWeek=GetWeek(nYear,nMonth,nDay,nWeekOfFirstday);// 4isleapyear=IsLeapYear(nYear);// 2if(isleapyear==1)nMonthDays=nDaysLeapYear[nMonth-1];else if(isleapyear==0)nMonthDays=nDaysYear[nMonth-1];if(i==11){while(nDay<1||nDay>nMonthDays){printf("查询日期chDate非法\n");return 0;}switch(nWeek){case 0:Week="SUNDAY";break;case 1:Week="MONDAY";break;case 2:Week="TUESDAY";break;case 3:Week="WEDNESDAY";break;case 4:Week="THUESDAY";break;case 5:Week="FRIDAY";break;case 6:Week="SATERDAY";break;}printf("This day (%s) is %s\n",chDate,Week);printf("OK!\n");}elsePrintCalendar(nWeek,nDay,nMonthDays,chDate); //5 }。

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[]={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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;}。

万年历源代码

万年历源代码
LCD_Write(LCD_COMMAND, 0x04|InputMode); }
//初始化 LCD************************************************************
void LCD_Initial()
{
LcdEn=0; LCD_Write(LCD_COMMAND,0x38);
#define LCD_SHOW
0x04 //显示开
#define LCD_HIDE
0x00 //显示关
#define LCD_CURSOR #define LCD_NO_CURSOR
#define LCD_FLASH #define LCD_NO_FLASH
0x02 0x00
//显示光标 //无光标
{
Time->DateString[0] = '2';
Time->DateString[1] = '0';
Time->DateString[2] = Time->Year/10 + '0';
#include <REG52.H> #include <intrins.h> //#include "LCD1602.h" //#include "DS1302.h"
#define uint unsigned int #define uchar unsigned char sbit DS1302_CLK = P1^7; sbit DS1302_IO = P1^6; sbit DS1302_RST = P1^5;
0x01 0x00
//光标闪动 //光标不闪动

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语言代码的过程中,可以通过优化算法、使用更高效的函数等方式提高代码性能。

万年历程序代码

万年历程序代码

/*****************************************************4字LED点阵屏+DS1302万年历电子钟C 程序*******************************************************DS1302 接线图Vcc2 CLK I/O /RST| | | |-------------------| 8 7 6 5 || DS1302 || || 1 2 3 4 |-------------------| | | |VCC1 GND1 脚接+5V 2,3脚32768HZ晶振4脚接地5脚接S51的P02 6脚接S51的P01 7接S51的P008脚接后备电源,可以接老计算机主板上的3.6V电池,也可以通过二级管隔离接一个大容量电解电容电压在2.5V以上即可维持595连级输出数据,138行驱动。

*///(本程序引用网上程序,有点BUG经过修改后可以使用,经过修改调整加上了温度显示功能,节日提醒功能和家人生日提醒功能。

//程序没有有效简化)07电气工程,付春平!//手动添加定时器2寄存器定义sfr T2CON = 0xC8;(special function register)sfr TL2 = 0xCC;sfr TH2 = 0xCD;sfr RCAP2L = 0xCA;sfr RCAP2H = 0xCB;sbit TF2 = T2CON^7;sbit EXF2 = T2CON^6;sbit RCLK = T2CON^5;sbit TCLK = T2CON^4;sbit EXEN2 = T2CON^3;sbit TR2 = T2CON^2;sbit C_T2 = T2CON^1;sbit CP_RL2= T2CON^0;//sbit ET2 =0xAD;#include<A T89x51.H>#include <intrins.h>#define uchar unsigned char#define uint unsigned intunsigned char irtime;//红外用全局变量bit irokk;unsigned char IRcord[2];unsigned char irdata[17];unsigned char data temp_data[2] = {0x00,0x00} ;void Ircordpro(void);uchar code hanzi[]; //汉字字模uchar code hanzi1[]; //汉字字模uchar code timer[18][16]; //0~9数字uchar code sw[]; //138驱动数据void Show_word(); //待机显示按三秒间隔分别显示年、月日、星期、时分秒。

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年的日历,并给出判断结果。

单片机万年历程序代码

单片机万年历程序代码

单片机万年历程序代码以下是一个示例单片机万年历的程序代码:```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;}```该代码的主要思路是通过外部三个按键模拟年、月和日的调节,通过一个定时器不断更新星期的计数,然后将年、月、日和星期分别在数码管上显示出来。

万年历

万年历

编写一个万年历程序,能显示日历,查询任意日期。

日历初始化和显示。

能根据系统日期进行初始化,并显示当前月份的月历,并突出显示当前日期。

日期查询,可以输入一个日期,然后查询并正确显示日期所在月份的月历,并突出显示查询日期,如果输入日期不正确给出提示。

#include<stdio.h>#include<time.h>#include<iostream.h>#include"万年历.h"int main(){int y,m,d,h,min,sec;//系统时间参数int cho;//用户选项int yt,mt,dt;//拥护输入的时间参数while(1){cout<<"≡≡≡≡≡≡万年历菜单选项≡≡≡≡≡";cout<<"请按编号选择功能";cout<<" ⑴查询当前时间";cout<<" ⑵显示本月月历";cout<<" ⑶查找给定日期对应的月历";cout<<" ⑷退出万年历";cin>>cho;Initdate(y,m,d,h,min,sec);switch(cho){case 1: cout<<"当前时间为<<y<<"."<<m<<"."<<d<<"."<<" "<<h<<":"<<min<<":"<<sec<<" ";week(y,m,d);break;case 2: printmonth(y,m,d);break;case 3: cout<<"请输入所要查找的日期,格式如2000 7 5";cin>>yt<<mt<<dt;search(yt,mt,dt);break;case 4: exit(1);break;default :cout<<"输入错误,请重新输入";}}return;}void week(y,m,d)//日期对于的星期{int c,w;c=y/100+1;if(m<=2) {m=m+12;y=y-1;}w=(y+y/4+c/4-2*c+26*(m+1)/10+d-1)%7;if(w=0) cout("Sun");else if (w==1)cout<<"Mon";else if (w==2)cout<<"Tue";else if (w==3)cout<<"wed";else if (w==4)cout<<"Thu";else if (w=5)cout<<"Fri";else cout<<"Sta"; }void Initdate(&y,&m,&d,&h,&min,&sec)//根据系统时间初始化万年历{time_t t;time(&t);struct tm * local_time = localtime(&t);sec=local_time->tm_sec;min=local_time->tm_min ;h=local_time->tm_hour ;d=local_time->tm_mday ;m=local_time->tm_mon+1;y=local_time->tm_year+1900;}void printmonth(y,m,d)//打印出给定时间对于月历{int ym[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};int leap,i;leap=(y%4==0&&y%100!=0||y%400==0)for(i=1;i<d;i++)cout<<i<<" "<<week(y,m,i)<<endl;cout<<"★" ;for(i=d;i<=ym[leap][m];i++)cout<<i<<" "<<week(y,m,i)<<endl;}int judge(y,m,d)//判断给定时间是否存在{int ym[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};int leap,i;if(y<=0||m<1||m>12)return 0;leap=(y%4==0&&y%100!=0||y%400==0);if(d<1||d>ym[leap][m])return 0;else return 1;}int search(y,m,d)//查找打印给定时间对应阅历{if(judge(y,m,d)==0)cout<<"error date";else printmonth(y,m,d);}2011-8-9 16:29最佳答案// abc4.cpp : 定义控制台应用程序的入口点。

万年历代码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)。

该代码在VC++6.0编译器下运行,只要按照编译器给出的说明操作,就能将万年历显示在调试窗口中了……………………………………由于制作时间匆忙,很多注释都来不及写了,对于出学者可以慢慢看,代码很简单的,都放在了主函数里,如果有兴趣的话,可以把这些代码放在几个文件里,这样会好看点,加油了!
#include<stdio.h>
#include<math.h>
void main()
{
int m,i,j,n=0,y=2010,wo,w,a;
printf("input the form of: year-month:\t");
scanf("%d-%d",&y,&m); //输入格式的决定
if(y%4==0&&y%100!=0||y%400==0)
a=29;
else a=28; //闰年的判断及2月日数的确定
if(m<1||m>12){
printf("month is not exit,please input later!\n");
exit(0);
}
wo=(y+(y-1)/4-(y-1)/100+(y-1)/400)%7; //1月1日的算法wo表示
for(i=0;i<55;i++)
printf("*"); //*的输入
printf("\n");
for(j=1;j<=7;j++)
printf("星期%d\t",j); //星期的输入
printf("\n"); //表前的建造
switch(m){
case 1:for(i=1;i<wo;i++){
printf("\t");
n++;
}
for(j=1;j<=31;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 1 month case 2:w=(wo+31)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=a;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 2 month case 3:w=(wo+31+a)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=31;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 3 month case 4:w=(wo+31+a+31)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=30;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 4 month
case 5:w=(wo+31+a+31+30)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=31;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 5 month
case 6:w=(wo+31+a+31+30+31)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=30;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 6 month
case 7:w=(wo+31+a+31+30+31+30)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=31;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 7 month
case 8:w=(wo+31+a+31+30+31+30+31)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=31;j++){
printf("\n");
printf("%d\t",j);
n++;
}
break; // 8 month
case 9:w=(wo+31+a+31+30+31+30+31+31)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=30;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 9 month
case 10:w=(wo+31+a+31+30+31+30+31+31+30)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=31;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 10 month
case 11:w=(wo+31+a+31+30+31+30+31+31+30+31)%7;
for(i=1;i<w;i++){
printf("\t");
n++;
}
for(j=1;j<=30;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 11 month
case 12:w=(wo+31+a+31+30+31+30+31+31+30+31+30)%7;
for(i=1;i<w;i++){
n++;
}
for(j=1;j<=31;j++){
if(n%7==0)
printf("\n");
printf("%d\t",j);
n++;
}
break; // 12 month
}
printf("\n");
for(i=0;i<55;i++)
printf("*");
printf("\n"); //表尾的处理}。

相关文档
最新文档