您的位置:首页 > 其它

综合程序题目>>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;
}


不繁忙的时候:





繁忙的时候:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: