4、模拟智能交通信号灯系统
设计一个智能信号灯系统,在十字路口控制东西南北四个方向交通信号,当然是红灯停绿灯行了,一般情况是四个方向每次绿灯亮30秒,只不过在绿灯熄灭后亮3秒黄灯再转红灯。现要求系统能够根据各个方向路口来的车流情况动态分配信号灯在来车多的方向绿灯可以綖长30秒时间,为此在各个路口设有车流信号统计器,可以向系统动态发送车流信息。
看到题目我首先想到的是先写出红绿灯的程序先,因为红绿灯的变化是这个程序最基本的东西,
一开始我就写出了红绿灯秒数的变化,一开始是不会清屏,问同学就知道清屏是ststem("cls")
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{while(1)
{int g;
for(g=30;g>=0;g--)
{
cout<<" >>>>>>>> 绿灯<<<<<<<< 剩 "<<g<<" 秒"<<'\n';
Sleep (1000);
system ("cls");
}
int y=3;
for(;y>=0;y--)
{
cout<<" >>>>>>>> 黄灯<<<<<<<< 剩 "<<y<<" 秒"<<'\n';
Sleep (1000);
system ("cls");
}
int r=33;
for(;r>=0;r--)
{
cout<<" >>>>>>>> 红灯<<<<<<<< 剩 "<<r<<" 秒"<<'\n';
Sleep (1000);
system ("cls");
}}
return 0;
}
这个是一个方向的红绿灯计时的程序,运行出一个方向的红绿计时。
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int green=30,yellow=4,red[4]={1,34,67,100};
int greena,greenb,greenc,greend;
int yellowa,yellowb,yellowc,yellowd;
int reda,redb,redc,redd;
int i=0,j,k,l,a;
while(1)
{
greena=30;
greenb=30;greenc=greend=0;
yellowa=yellowb=yellowc=yellowd=0;
reda=red[0];
redb=red[1];
redc=red[2];
redd=red[3];
for(a=132;a>0;a--)
{
// if(a%33==0&&a>=33)
cout<<"\n------西路口-----\n";
if(greena>0)
{
cout<<" >>> 绿灯 <<< "<<greena<<" 秒\n";
greena--;
if(greena==0)
yellowa=yellow;
}
if(yellowa>0)
{
if(yellowa==4)
yellowa--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowa<<" 秒\n";
yellowa--;
}
if(yellowa==0)
{
i=3;
reda=red[i];
}
}
if(reda>0)
{
if(reda==red[i])
reda--;
else
{
cout<<" >>> 红灯 <<< "<<reda<<" 秒\n";
reda--;
}
if(reda==0)
{
greena=green;
i--;
}
}
/********************************************************************/
cout<<"\n------南路口-----\n";
if(redb>0)
{
cout<<" >>> 红灯 <<< "<<redb<<" 秒\n";
redb--;
}
if(redb==0)
{
if(greenb>0)
{
cout<<" >>> 绿灯 <<< "<<greenb<<" 秒\n";
greenb--;
}
if(greenb==0)
yellowb=4;
}
if(yellowb>0)
{
cout<<" >>> 黄灯 <<< "<<yellowb<<" 秒\n";
yellowb--;
if(yellowa==0)
redb=red[i+1];
}
/********************************************************************/
cout<<"\n------东路口-----\n";
if(greenc>0)
{
cout<<" >>> 绿灯 <<< "<<greenc<<" 秒\n";
greenc--;
}
if(yellowc>0)
{
cout<<" >>> 黄灯 <<< "<<yellowc<<" 秒\n";
yellowc--;
}
if(redc>0)
{
cout<<" >>> 红灯 <<< "<<redc<<" 秒\n";
redc--;
}
/********************************************************************/
cout<<"\n------北路口-----\n";
if(greend>0)
{
cout<<" >>> 绿灯 <<< "<<greend<<" 秒\n";
greend--;
}
if(yellowd>0)
{
cout<<" >>> 黄灯 <<< "<<yellowd<<" 秒\n";
yellowd--;
}
if(redd>0)
{
cout<<" >>> 红灯 <<< "<<redd<<" 秒\n";
redd--;
}
cout<<'\n';
Sleep(100);
system("cls");
}
}
return 0;
}
暂时弄好了第一个方向的所有时间的计算,还有3个方向还没有弄好,其他方向的有时候不会显示出来时间,还有第二个方向红灯显示的时候连着黄灯会
一起跳动秒数。Sleep(100);是为了检验时间跳动的时候可以快一点。暂时做这么多,明天上实验课继续做。
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int green=30,yellow=4,red[4]={1,34,67,100};
int greena,greenb,greenc,greend;
int yellowa,yellowb,yellowc,yellowd;
int reda,redb,redc,redd;
int i=0,j,k,l,a;
while(1)
{
greena=30;greenb=30;greenc=greend=0;
yellowa=yellowb=yellowc=yellowd=0;
reda=red[0];
redb=red[1];
redc=red[2];
redd=red[3];
for(a=132;a>0;a--)
{
// if(a%33==0&&a>=33)
cout<<"\n------西路口-----\n";
if(greena>0)
{
cout<<" >>> 绿灯 <<< "<<greena<<" 秒\n";
greena--;
if(greena==0)
yellowa=yellow;
}
if(yellowa>0)
{
if(yellowa==4)
yellowa--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowa<<" 秒\n";
yellowa--;
}
if(yellowa==0)
{
i=3;
reda=red[i];
}
}
if(reda>0)
{
if(reda==red[i])
reda--;
else
{
cout<<" >>> 红灯 <<< "<<reda<<" 秒\n";
reda--;
}
if(reda==0)
{
greena=green;
i--;
}
}
/********************************************************************/
cout<<"\n------南路口-----\n";
if(redb>0)
{
cout<<" >>> 红灯 <<< "<<redb<<" 秒\n";
redb--;
}
if(redb==0)
{
if(greenb>0)
{
if(greenb==30)
greenb--;
else
cout<<" >>> 绿灯 <<< "<<greenb<<" 秒\n";
greenb--;
if(greenb==0)
yellowb=yellow;
}
}
if(yellowb>0)
{
if(yellowb==4)
yellowb--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowb<<" 秒\n";
yellowb--;
}
if(yellowb==0)
{
redb=red[3];
break;
}
}
/********************************************************************/
cout<<"\n------东路口-----\n";
if(greenc>0)
{
cout<<" >>> 绿灯 <<< "<<greenc<<" 秒\n";
greenc--;
}
if(yellowc>0)
{
cout<<" >>> 黄灯 <<< "<<yellowc<<" 秒\n";
yellowc--;
}
if(redc>0)
{
cout<<" >>> 红灯 <<< "<<redc<<" 秒\n";
redc--;
}
/********************************************************************/
cout<<"\n------北路口-----\n";
if(greend>0)
{
cout<<" >>> 绿灯 <<< "<<greend<<" 秒\n";
greend--;
}
if(yellowd>0)
{
cout<<" >>> 黄灯 <<< "<<yellowd<<" 秒\n";
yellowd--;
}
if(redd>0)
{
cout<<" >>> 红灯 <<< "<<redd<<" 秒\n";
redd--;
}
cout<<'\n';
Sleep(100);
system("cls");
}
}
return 0;
}
终于完成第二个方向的计时。
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int green=30,yellow=4,red[4]={1,34,67,100};
int greena,greenb,greenc,greend;
int yellowa,yellowb,yellowc,yellowd;
int reda,redb,redc,redd;
int i=0,j,k,l,a;
while(1)
{
greena=greenb=greenc=greend=30;
yellowa=yellowb=yellowc=yellowd=0;
reda=red[0];
redb=red[1];
redc=red[2];
redd=red[3];
for(a=132;a>0;a--)
{
// if(a%33==0&&a>=33)
cout<<"\n------西路口-----\n";
if(greena>0)
{
cout<<" >>> 绿灯 <<< "<<greena<<" 秒\n";
greena--;
if(greena==0)
yellowa=yellow;
}
if(yellowa>0)
{
if(yellowa==4)
yellowa--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowa<<" 秒\n";
yellowa--;
}
if(yellowa==0)
{
i=3;
reda=red[i];
}
}
if(reda>0)
{
if(reda==red[i])
reda--;
else
{
cout<<" >>> 红灯 <<< "<<reda<<" 秒\n";
reda--;
}
if(reda==0)
{
greena=green;
i--;
}
}
/********************************************************************/
cout<<"\n------南路口-----\n";
if(redb>0)
{
cout<<" >>> 红灯 <<< "<<redb<<" 秒\n";
redb--;
}
if(redb==0)
{
if(greenb>0)
{
if(greenb==30)
greenb--;
else
cout<<" >>> 绿灯 <<< "<<greenb<<" 秒\n";
greenb--;
if(greenb==0)
yellowb=yellow;
}
}
if(yellowb>0)
{
if(yellowb==4)
yellowb--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowb<<" 秒\n";
yellowb--;
}
if(yellowb==0)
redb=red[3];
}
/********************************************************************/
cout<<"\n------东路口-----\n";
if(redc>0)
{
cout<<" >>> 红灯 <<< "<<redc<<" 秒\n";
redc--;
}
if(redc==0)
{
if(greenc>0)
{
if(greenc==30)
greenc--;
else
cout<<" >>> 绿灯 <<< "<<greenc<<" 秒\n";
greenc--;
if(greenc==0)
yellowc=yellow;
}
}
if(yellowc>0)
{
if(yellowc==4)
yellowc--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowc<<" 秒\n";
yellowc--;
}
if(yellowc==0)
redc=red[3];
}
/********************************************************************/
cout<<"\n------北路口-----\n";
if(redd>0)
{
cout<<" >>> 红灯 <<< "<<redd<<" 秒\n";
redd--;
}
if(redd==0)
{
if(greend>0)
{
if(greend==30)
greend--;
else
cout<<" >>> 绿灯 <<< "<<greend<<" 秒\n";
greend--;
if(greend==0)
yellowd=yellow;
}
}
if(yellowd>0)
{
if(yellowd==4)
yellowd--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowd<<" 秒\n";
yellowd--;
}
if(yellowd==0)
redd=red[3];
}
/********************************************************************/
cout<<'\n';
Sleep(100);
system("cls");
}
}
return 0;
}
四个方向的正确运行。
老师我尽力只能做到这里了,不会做下去了。
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int n;
int carnum,carnum1,carnum2,carnum3;
cout<<"请输入西路口的车流量数"<<endl;
cin>>carnum;
if(carnum>30)
{
int green=60,yellow=4,red[5]={1,64,95,126,100};
int greena,greenb,greenc,greend;
int yellowa,yellowb,yellowc,yellowd;
int reda,redb,redc,redd;
int i=0,a;
while(1)
{
greena=60;greenb=greenc=greend=30;
yellowa=yellowb=yellowc=yellowd=0;
reda=red[0];
redb=red[1];
redc=red[2];
redd=red[3];
for(a=162;a>0;a--)
{
cout<<"\n------西路口-----\n";
if(greena>0)
{
cout<<" >>> 绿灯 <<< "<<greena<<" 秒\n";
greena--;
if(greena==0)
yellowa=yellow;
}
if(yellowa>0)
{
if(yellowa==4)
yellowa--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowa<<" 秒\n";
yellowa--;
}
if(yellowa==0)
{
i=4;
reda=red[4];
}
}
if(reda>0)
{
if(reda==red[i])
reda--;
else
{
cout<<" >>> 红灯 <<< "<<reda<<" 秒\n";
reda--;
}
if(reda==0)
{
greena=green;
i--;
}
}
/********************************************************************/
cout<<"\n------南路口-----\n";
if(redb>0)
{
cout<<" >>> 红灯 <<< "<<redb<<" 秒\n";
redb--;
}
if(redb==0)
{
if(greenb>0)
{
if(greenb==30)
greenb--;
else
cout<<" >>> 绿灯 <<< "<<greenb<<" 秒\n";
greenb--;
if(greenb==0)
yellowb=yellow;
}
}
if(yellowb>0)
{
if(yellowb==4)
yellowb--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowb<<" 秒\n";
yellowb--;
}
if(yellowb==0)
redb=red[3];
}
/********************************************************************/
cout<<"\n------东路口-----\n";
if(redc>0)
{
cout<<" >>> 红灯 <<< "<<redc<<" 秒\n";
redc--;
}
if(redc==0)
{
if(greenc>0)
{
if(greenc==30)
greenc--;
else
cout<<" >>> 绿灯 <<< "<<greenc<<" 秒\n";
greenc--;
if(greenc==0)
yellowc=yellow;
}
}
if(yellowc>0)
{
if(yellowc==4)
yellowc--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowc<<" 秒\n";
yellowc--;
}
if(yellowc==0)
redc=red[3];
}
/********************************************************************/
cout<<"\n------北路口-----\n";
if(redd>0)
{
cout<<" >>> 红灯 <<< "<<redd<<" 秒\n";
redd--;
}
if(redd==0)
{
if(greend>0)
{
if(greend==30)
greend--;
else
cout<<" >>> 绿灯 <<< "<<greend<<" 秒\n";
greend--;
if(greend==0)
yellowd=yellow;
}
}
if(yellowd>0)
{
if(yellowd==4)
yellowd--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowd<<" 秒\n";
yellowd--;
}
if(yellowd==0)
{
redd=red[4];
system("cls");
main();
}
}
/
cout<<'\n';
Sleep(100);
system("cls");
}
}
return 0;
}
//------------/-------//
else
{
int green=30,yellow=4,red[4]={1,34,65,96};
int greena,greenb,greenc,greend;
int yellowa,yellowb,yellowc,yellowd;
int reda,redb,redc,redd;
int i=0,a;
while(1)
{
greena=greenb=greenc=greend=30;
yellowa=yellowb=yellowc=yellowd=0;
reda=red[0];
redb=red[1];
redc=red[2];
redd=red[3];
for(a=132;a>0;a--)
{
cout<<"\n------西路口-----\n";
if(greena>0)
{
cout<<" >>> 绿灯 <<< "<<greena<<" 秒\n";
greena--;
if(greena==0)
yellowa=yellow;
}
if(yellowa>0)
{
if(yellowa==4)
yellowa--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowa<<" 秒\n";
yellowa--;
}
if(yellowa==0)
{
i=3;
reda=red[i];
}
}
if(reda>0)
{
if(reda==red[i])
reda--;
else
{
cout<<" >>> 红灯 <<< "<<reda<<" 秒\n";
reda--;
}
if(reda==0)
{
greena=green;
i--;
}
}
/********************************************************************/
cout<<"\n------南路口-----\n";
if(redb>0)
{
cout<<" >>> 红灯 <<< "<<redb<<" 秒\n";
redb--;
}
if(redb==0)
{
if(greenb>0)
{
if(greenb==30)
greenb--;
else
cout<<" >>> 绿灯 <<< "<<greenb<<" 秒\n";
greenb--;
if(greenb==0)
yellowb=yellow;
}
}
if(yellowb>0)
{
if(yellowb==4)
yellowb--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowb<<" 秒\n";
yellowb--;
}
if(yellowb==0)
redb=red[3];
}
/********************************************************************/
cout<<"\n------东路口-----\n";
if(redc>0)
{
cout<<" >>> 红灯 <<< "<<redc<<" 秒\n";
redc--;
}
if(redc==0)
{
if(greenc>0)
{
if(greenc==30)
greenc--;
else
cout<<" >>> 绿灯 <<< "<<greenc<<" 秒\n";
greenc--;
if(greenc==0)
yellowc=yellow;
}
}
if(yellowc>0)
{
if(yellowc==4)
yellowc--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowc<<" 秒\n";
yellowc--;
}
if(yellowc==0)
redc=red[3];
}
/********************************************************************/
cout<<"\n------北路口-----\n";
if(redd>0)
{
cout<<" >>> 红灯 <<< "<<redd<<" 秒\n";
redd--;
}
if(redd==0)
{
if(greend>0)
{
if(greend==30)
greend--;
else
cout<<" >>> 绿灯 <<< "<<greend<<" 秒\n";
greend--;
if(greend==0)
yellowd=yellow;
}
}
if(yellowd>0)
{
if(yellowd==4)
yellowd--;
else
{
cout<<" >>> 黄灯 <<< "<<yellowd<<" 秒\n";
yellowd--;
}
if(yellowd==0)
{ system("cls");
main();
redd=red[3];
}
}
/
cout<<'\n';
Sleep(100);
system("cls");
}
}
return 0;
}
}
我只能做这么多了,尽力了。剩下的实在是不会。