综合程序题目>>4、模拟智能交通信号灯系统
2014-06-22 00:16
363 查看
/************************************** ************自动化1124孙伟星*********** ***************************************/ #include<iostream> #include<cstdlib> #include<windows.h> using namespace std; int hong (int x) { cout<<"\t"<<">>>>> 红灯 <<<<<" <<"\t"<<x<<" 秒"; cout<<"\a"<<endl; x--; return x; } int lv (int x) { cout<<"\t"<<">>>>> 绿灯 <<<<<" <<"\t"<<x<<" 秒"; cout<<"\a"<<endl; x--; return x; } int huang (int x) { cout<<"\t"<<">>>>> 黄灯 <<<<<" <<"\t"<<x<<" 秒"; cout<<"\a"<<endl; x--; return x; } int main() { int k=0; while(1) { int m,n; n=rand()%100; //n由即将绿灯方向的车流信号统计器确定,这里用自由数暂代; //假定一个车流额定值为50辆; if(n<50) m=30; else m=60; int greena,greenb,greenc,greend; int yellowa,yellowb,yellowc,yellowd; int reda,redb,redc,redd; int a; reda=redc=redb=redd=m+3; greena=greenc=greenb=greend=m; yellowa=yellowb=yellowc=yellowd=4; if (k==0) //东西方向绿灯 { k=1; for(a=m+3;a>0;a--) { //十字路口交通灯的演示情况 cout<<"//////////////////////////////////////"<<endl; cout<<"\t"<<"十字路口的交通灯演示"; cout<<"\t"<<endl; cout<<"//////////////////////////////////////"<<endl; cout<<"//////////////////////////////////////"<<endl; //东边方向车道的情况 cout<<"\n\t-------东路口-------\n"; if (greena > 0) greena=lv(greena); if(greena==0) { if(yellowa>0) { if (yellowa==4) yellowa--; else yellowa=huang(yellowa); } } cout<<"\n//////////////////////////////////////"<<endl; //南边方向车道的情况 cout<<"\n\t-------南路口-------\n"; if(redb>0) { redb=hong(redb); } cout<<"\n//////////////////////////////////////"<<endl; //西边方向车道的情况 cout<<"\n\t-------西路口-------\n"; if (greenc > 0) greenc=lv(greenc); if(greenc==0) { if(yellowc>0) { if (yellowc==4) yellowc--; else yellowc=huang(yellowc); } } cout<<"\n//////////////////////////////////////"<<endl; //北边方向车道的情况 cout<<"\n\t-------北路口-------\n"; if(redd>0) { redd=hong(redd); } cout<<"\n//////////////////////////////////////"<<endl; Sleep(1000); system("cls"); } } else //南北方向绿灯 { k=0; for(a=m+3;a>0;a--) { //十字路口交通灯的演示情况 cout<<"//////////////////////////////////////"<<endl; cout<<"\t"<<"十字路口的交通灯演示"; cout<<"\t"<<endl; cout<<"//////////////////////////////////////"<<endl; cout<<"//////////////////////////////////////"<<endl; //东边方向车道的情况 cout<<"\n\t-------东路口-------\n"; if(reda>0) { reda=hong(reda); } cout<<"\n//////////////////////////////////////"<<endl; //南边方向车道的情况 cout<<"\n\t-------南路口-------\n"; if (greenb > 0) greenb=lv(greenb); if(greenb==0) { if(yellowb>0) { if (yellowb==4) yellowb--; else yellowb=huang(yellowb); } } cout<<"\n//////////////////////////////////////"<<endl; //西边方向车道的情况 cout<<"\n\t-------西路口-------\n"; if(redc>0) { redc=hong(redc); } cout<<"\n//////////////////////////////////////"<<endl; //北边方向车道的情况 cout<<"\n\t-------北路口-------\n"; if (greend > 0) greend=lv(greend); if(greend==0) { if(yellowd>0) { if (yellowd==4) yellowd--; else yellowd=huang(yellowd); } } cout<<"\n//////////////////////////////////////"<<endl; Sleep(1000); system("cls"); } } } return 0; }
浓缩精简版:
/************************************** ************自动化1124孙伟星*********** ***************************************/ #include<iostream> #include<cstdlib> #include<windows.h> using namespace std; void hong (int x) { cout<<"\t"<<">>>>> 红灯 <<<<<" <<"\t"<<x<<" 秒"; cout<<"\n"<<endl; } void lv (int x) { cout<<"\t"<<">>>>> 绿灯 <<<<<" <<"\t"<<x-3<<" 秒"; cout<<"\n"<<endl; } void huang (int x) { cout<<"\t"<<">>>>> 黄灯 <<<<<" <<"\t"<<x<<" 秒"; cout<<"\n"<<endl; } void ban() { cout<<"//////////////////////////////////////"<<endl; } void liang(int a,int k,int i) { cout<<"\n\t-------"; switch(i) { case 0:cout<<"东"; break; case 1:cout<<"南"; break; case 2:cout<<"西"; break; case 3:cout<<"北"; } cout<<"路口-------\n"; if ((k+i)%2==0) { if (a > 3) lv(a); else huang(a); } else hong(a); ban(); } int main() { int k,i,m,n,a; while(1) { for(k=0;k<2;k++) { n=rand()%100; //n由即将绿灯方向的车流信号统计器确定,这里用自由数暂代; if(n<50) //假定一个车流额定值为50辆; m=30; else m=60; for(a=m+3;a>0;a--) { //十字路口交通灯的演示情况 system("cls"); ban(); cout<<"\t十字路口的交通灯演示\n"; ban();ban(); //各边方向车道的情况 for (i=0;i<4;i++) liang(a,k,i); Sleep(1000); } } } return 0; }
不繁忙的时候:
繁忙的时候:
相关文章推荐
- 课程设计--模拟智能交通信号灯系统
- EBS系统<关于Oracle数据库的包的编译(对AP程序的)影响>
- android面试题目大全<第三部分>,java高级综合部分
- 智能交通信号灯管理系统
- 黑马程序员->交通灯系统
- 智能交通信号灯管理系统
- 程序员 7K面试题<一>---交通灯管理系统
- 黑马程序员 面试技巧<二>--->交通灯管理系统
- 面试项目<一>_交通灯管理系统
- 黑马程序员 Java基础 --->7K面试题之交通灯管理系统
- Windows核心编程<读书笔记四之程序>显示系统中进程、线程、模块详细信息
- <模拟电子学习1>Multisim 12.0 结构和仿真51最小的单芯片系统
- 电信系统方案 >> 电信综合营帐系统
- 谁能跟我编写一个程序(C语言)题目是编写一个函数计算sum(n)=1+2+3+。。。+n(n&gt;=1)
- 黑马程序员_<<交通灯管理系统>>
- VB.Net Winform程序的简单打包和部署--<机房收费系统>
- <深入理解计算机系统> 通过程序的机器级表示来理解函数栈
- 在linux中,让程序进入后台运行的命令—>daemon程序
- 关于“系统错误&H80004015(-2147467243).此类别是作为跟调用程序不同的安全ID运行的”的解决办法
- 利用Radrails开发Ruby on Rails程序入门指南------->成功