⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rgytese.c

📁 这是一个用c语言做的红绿灯代码
💻 C
字号:
/* Note:Your choice is C IDE */
#include "stdio.h" 
#include "conio.h"
#include "dos.h"
#include "io.h"
#include "time.h"  

#define state1 0x21					/*100001  南红东绿*/
#define state2 0x22					/*100010  南红东黄*/
#define state3 0x0a					/*001100  南绿东红*/
#define state4 0x14					/*010100  南黄东红*/
#define state5 0x12					/*010010  南北东西全部黄灯*/

static int base0=0x220;

int Red1=27;                        /*这是交通不算拥挤时所用的转换时间*/
int Yellow1=3;

int Red2=20;                        /*这是交通拥挤时所用的转换时间*/
int Yellow2=10;

int timecount=0;        	        /*计数器*/    
int state=0;						/*保存红绿灯的状态*/

main()
{
	/*以下是用户程序*/  

	struct tm *local;               /*定义local指针变量,用来保存本地时间*/
	
	time_t ts,te;                   /*ts保存前一时刻,te保存当前时刻*/
	ts=time(NULL);                  /*让这两个时间变量赋初值,某一时刻的时间*/
	te=time(NULL);                                   
	
	while(1)
	{
		/*以下是用户程序*/                      
										
		te=time(NULL);                  /*不断地让te获得当前时间*/     
		local=localtime(&te);           /*利用函数根据计算机时间转换成本地时间*/

		if((te-ts)>=1)           	 	/*这是用来如果两者相差大于等于1秒,就让计数器加1.*/
		{                               /*但是注意这个前提是:while()循环在1秒内就能够完成*/
			ts=te;						/*让ts保存当前时间*/
			timecount=timecount+1;      /*计数器加1*/
		} 
		 
		switch (local->tm_hour)       	/*根据本地计算机时间,判断红绿灯的控制*/
		{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 22:
			case 23:        			/*时间如果是在22点到凌晨4点之间,东西南北路口全黄灯亮*/
					printf("010010  ALL Yellow\n");			
					delay(3000);        /*推迟3秒*/
					break;
			case 5:
			case 6:					    /*这是交通不拥挤的时间,传入转换快的变量*/
					if (0<=timecount && timecount<=Red2)  /*时间在0到20秒时候,显示状态1*/
						state=1;
					else if(Red2<timecount && timecount<=(Red2+Yellow2)) /*时间在20到30秒时候,显示状态2*/
					 	state=2;
					else if((Red2+Yellow2)<timecount && timecount<=(2*Red2+Yellow2))/*时间在30到50秒时候,显示状态3*/
						state=3;
					else if((2*Red2+Yellow2)<timecount && timecount<=2*(Red2+Yellow2))/*时间在50到60秒时候,显示状态4*/
						state=4;      
					else if(2*(Red2+Yellow2)<timecount)/*时间在超过60秒时候,返回状态1,时间计数器归0*/
						{
							state=1;
							timecount=0;
						}
					else
						; 
					break;
			case 7:
			case 8:
			case 9:                     /*这是早上交通拥挤的时间,传入转换慢的变量*/                        
					if (0<=timecount && timecount<=Red1)/*时间在0到27秒时候,显示状态1*/
						state=1;
					else if(Red1<timecount && timecount<=(Red1+Yellow1))/*时间在27到30秒时候,显示状态2*/
					 	state=2;
					else if((Red1+Yellow1)<timecount && timecount<=(2*Red1+Yellow1))/*时间在30到57秒时候,显示状态3*/
						state=3;
					else if((2*Red1+Yellow1)<timecount && timecount<=2*(Red1+Yellow1))/*时间在57到60秒时候,显示状态4*/
						state=4;
					else if(2*(Red1+Yellow1)<timecount)/*时间在超过60秒时候,返回状态1,时间计数器归0*/
						{
							state=1;
							timecount=0;
						}
					else
						;
					break; 
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
			case 16:                   /*这是交通不拥挤的时间,传入转换快的变量*/					                        
					if (0<=timecount && timecount<=Red2)  /*时间在0到20秒时候,显示状态1*/
						state=1;
					else if(Red2<timecount && timecount<=(Red2+Yellow2)) /*时间在20到30秒时候,显示状态2*/
					 	state=2;
					else if((Red2+Yellow2)<timecount && timecount<=(2*Red2+Yellow2))/*时间在30到50秒时候,显示状态3*/
						state=3;
					else if((2*Red2+Yellow2)<timecount && timecount<=2*(Red2+Yellow2))/*时间在50到60秒时候,显示状态4*/
						state=4;      
					else if(2*(Red2+Yellow2)<timecount)/*时间在超过60秒时候,返回状态1,时间计数器归0*/
						{
							state=1;
							timecount=0;
						}
					else
						;  
					break;
			case 17:
			case 18:
			case 19:				   /*这是下午交通拥挤的时间,传入转换慢的变量*/					                        
					if (0<=timecount && timecount<=Red1)/*时间在0到27秒时候,显示状态1*/
						state=1;
					else if(Red1<timecount && timecount<=(Red1+Yellow1))/*时间在27到30秒时候,显示状态2*/
					 	state=2;
					else if((Red1+Yellow1)<timecount && timecount<=(2*Red1+Yellow1))/*时间在30到57秒时候,显示状态3*/
						state=3;
					else if((2*Red1+Yellow1)<timecount && timecount<=2*(Red1+Yellow1))/*时间在57到60秒时候,显示状态4*/
						state=4;
					else if(2*(Red1+Yellow1)<timecount)/*时间在超过60秒时候,返回状态1,时间计数器归0*/
						{
							state=1;
							timecount=0;
						}
					else
						;
					break;
			case 20:
			case 21:                   /*这是交通不拥挤的时间,传入转换快的变量*/
					if (0<=timecount && timecount<=Red2)  /*时间在0到20秒时候,显示状态1*/
						state=1;
					else if(Red2<timecount && timecount<=(Red2+Yellow2)) /*时间在20到30秒时候,显示状态2*/
					 	state=2;
					else if((Red2+Yellow2)<timecount && timecount<=(2*Red2+Yellow2))/*时间在30到50秒时候,显示状态3*/
						state=3;
					else if((2*Red2+Yellow2)<timecount && timecount<=2*(Red2+Yellow2))/*时间在50到60秒时候,显示状态4*/
						state=4;      
					else if(2*(Red2+Yellow2)<timecount)/*时间在超过60秒时候,返回状态1,时间计数器归0*/
						{
							state=1;
							timecount=0;
						}
					else
						; 
					break;
			default :
					break;	
		}
		
		switch (state)
		{
			case 1:
					printf("100001  south RED est GREN\n");/*输出状态1,保持此状态27秒*/ 
					delay(27000);
					break;
			case 2:              
					printf("100010  south RED est YELLOW\n");/*输出状态2,保持此状态3秒*/ 
					delay(3000);
					break;
			case 3:           
					printf("001100  south GREEN est RED\n");/*输出状态3,保持此状态27秒*/ 
					delay(27000);
					break;
			case 4:              
					printf("010100  south YELLOW est RED\n");/*输出状态4,保持此状态3秒*/ 
					delay(3000);                    			
					break;
			default :
					break;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -