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

 

 

/**************************************
************自动化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;
}


 

 

不繁忙的时候:

 

 

繁忙的时候:

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值