您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > C++项目-路口交通灯控制程序源代码
分两个文件【红绿灯控制.cpp】【diytime.h】存放,并将其放到同一文件夹下编译C++编译环境////////////////红绿灯控制.cpp////////////////#includestdio.h#includewindows.h#includemath.h#includeiostream#includediytime.husingnamespacestd;HWNDGetConsoleHwnd(void){#defineMY_BUFSIZE1024HWNDhwndFound;charpszNewWindowTitle[MY_BUFSIZE];charpszOldWindowTitle[MY_BUFSIZE];GetConsoleTitle(pszOldWindowTitle,MY_BUFSIZE);wsprintf(pszNewWindowTitle,%d/%d,GetTickCount(),GetCurrentProcessId());SetConsoleTitle(pszNewWindowTitle);Sleep(40);hwndFound=FindWindow(NULL,pszNewWindowTitle);SetConsoleTitle(pszOldWindowTitle);return(hwndFound);}voidmain(void){#defineWEST_EAST50,50,100,100#defineSOUTH_NORTH350,50,300,100intminute=0,second=0;intminute1=0,second1=0;intir=1,i;cout输入倒计时时间(单位:秒,3600秒):endl;cinsecond;system(cls);cout东西方向南北方向endlendl;if(second59){minute=second/60;second=second%60;}minute1=minute;second1=second;HPENhPen=CreatePen(0,5,RGB(200,256,256));HBRUSHhBrushy=CreateSolidBrush(RGB(255,255,0));HBRUSHhBrushg=CreateSolidBrush(RGB(0,255,0));HBRUSHhBrushr=CreateSolidBrush(RGB(255,0,0));HBRUSHhBrushb=CreateSolidBrush(RGB(0,0,0));HWNDhConsole=GetConsoleHwnd();HDCpdcg=GetDC(hConsole);HDCpdcr=GetDC(hConsole);HDCpdcy=GetDC(hConsole);HDCpdcb=GetDC(hConsole);HPENhOldPen=(HPEN)SelectObject(pdcg,hBrushg);hOldPen=(HPEN)SelectObject(pdcr,hBrushr);hOldPen=(HPEN)SelectObject(pdcy,hBrushy);hOldPen=(HPEN)SelectObject(pdcb,hBrushb);while(1){if(ir){Ellipse(pdcg,WEST_EAST);Ellipse(pdcr,SOUTH_NORTH);i=destime_n(minute,second,3);Ellipse(pdcb,WEST_EAST);outputtime1(0,3);printf(\r);Sleep(500);Ellipse(pdcg,WEST_EAST);outputtime2(0,3);printf(\r);Sleep(500);Ellipse(pdcb,WEST_EAST);outputtime1(0,2);printf(\r);Sleep(500);Ellipse(pdcg,WEST_EAST);outputtime2(0,2);printf(\r);Sleep(500);Ellipse(pdcb,WEST_EAST);outputtime1(0,1);printf(\r);Sleep(500);Ellipse(pdcg,WEST_EAST);outputtime2(0,1);printf(\r);Sleep(500);Ellipse(pdcy,WEST_EAST);outputtime1(0,0);printf(\r);Sleep(1000);ir=0;}else{Ellipse(pdcg,SOUTH_NORTH);Ellipse(pdcr,WEST_EAST);i=destime_n(minute,second,3);Ellipse(pdcb,SOUTH_NORTH);outputtime1(0,3);printf(\r);Sleep(500);Ellipse(pdcg,SOUTH_NORTH);outputtime2(0,3);printf(\r);Sleep(500);Ellipse(pdcb,SOUTH_NORTH);outputtime1(0,2);printf(\r);Sleep(500);Ellipse(pdcg,SOUTH_NORTH);outputtime2(0,2);printf(\r);Sleep(500);Ellipse(pdcb,SOUTH_NORTH);outputtime1(0,1);printf(\r);Sleep(500);Ellipse(pdcg,SOUTH_NORTH);outputtime2(0,1);printf(\r);Sleep(500);Ellipse(pdcy,SOUTH_NORTH);outputtime1(0,0);printf(\r);Sleep(1000);ir=1;}}ReleaseDC(hConsole,pdcr);ReleaseDC(hConsole,pdcg);ReleaseDC(hConsole,pdcb);ReleaseDC(hConsole,pdcy);}////////////////diytime.h////////////////#includewindows.h#includeiostream#includestdio.h#ifndefTIMH_H#defineTIMH_Husingnamespacestd;//时间输出函数voidoutputtime1(intminute,intsecond){if(minute10)printf(0);//cout'0';printf(%d,minute);//coutminute;printf(:);//cout':';if(second10)printf(0);//cout'0';printf(%d,second);//coutsecond;}voidoutputtime2(intminute,intsecond){if(minute10)printf(0);//cout'0';printf(%d,minute);//coutminute;printf();//cout'';if(second10)printf(0);//cout'0';printf(%d,second);//coutsecond;}//倒计时函数(到3秒)intdestime_3(intminute,intsecond){intdottrans=1;inttimetrans=2;while(second!=-1&&minute!=-1){if(dottrans==1){outputtime1(minute,second);dottrans=0;timetrans--;}else{outputtime2(minute,second);dottrans=1;timetrans--;}if(timetrans==0){second--;if(second==-1){minute--;second=59;}timetrans=2;}printf(\r);//cout'\r';Sleep(500);if(minute==0&&second==3)return1;}return0;}//倒计时函数(差1秒)intdestime_1(intminute,intsecond){intdottrans=1;inttimetrans=2;while(second!=-1&&minute!=-1){if(dottrans==1){outputtime1(minute,second);dottrans=0;timetrans--;}else{outputtime2(minute,second);dottrans=1;timetrans--;}if(timetrans==0){second--;if(second==-1){minute--;second=59;}timetrans=2;}printf(\r);//cout'\r';Sleep(500);if(minute==0&&second==0)return1;}return0;}//倒计时函数(正常)intdestime_0(intminute,intsecond){intdottrans=1;inttimetrans=2;while(second!=-1&&minute!=-1){if(dottrans==1){outputtime1(minute,second);dottrans=0;timetrans--;}else{outputtime2(minute,second);dottrans=1;timetrans--;}if(timetrans==0){second--;if(second==-1){minute--;second=59;}timetrans=2;}printf(\r);//cout'\r';Sleep(500);}return0;}//倒计时函数(到n秒)intdestime_n(intminute,intsecond,intn){intdottrans=1;inttimetrans=2;while(second!=-1&&minute!=-1){if(dottrans==1){outputtime1(minute,second);dottrans=0;timetrans--;}else{outputtime2(minute,second);dottrans=1;timetrans--;}if(timetrans==0){second--;if(second==-1){minute--;second=59;}timetrans=2;}printf(\r);//cout'\r';Sleep(500);if(minute==0&&second==n)return1;}return0;}#endif
本文标题:C++项目-路口交通灯控制程序源代码
链接地址:https://www.777doc.com/doc-3700811 .html