天天看點

綜合程式題目>>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;
}
           

不繁忙的時候:

綜合程式題目&gt;&gt;4、模拟智能交通信号燈系統
綜合程式題目&gt;&gt;4、模拟智能交通信号燈系統

繁忙的時候:

綜合程式題目&gt;&gt;4、模拟智能交通信号燈系統
綜合程式題目&gt;&gt;4、模拟智能交通信号燈系統