综合程序题目>>4、模拟智能交通信号灯系统

来源:互联网 发布:湖南软件企业评估 编辑:程序博客网 时间:2024/06/11 11:51
 

 

/**************************************************自动化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;elsem=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--;elseyellowa=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--;elseyellowc=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--;elseyellowb=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--;elseyellowd=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);elsehuang(a);}elsehong(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;elsem=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;}


 

 

不繁忙的时候:

 

 

繁忙的时候:

 

0 0
原创粉丝点击