模拟实现十字路口的交通灯管理系统逻辑,具体需求如下:· 异步随机生成按照各个路线行驶的车辆。 例如: 由南向而来去往北向的车辆---- 直行车辆 由西向而来去往南向的车辆---- 右转车辆 由东向而来去往南向的车辆---- 左转车辆 · 信号灯忽略黄灯,只考虑红灯和绿灯。 · 应考虑左转车辆控制信号灯,右转车辆不受信号灯控制。 · 具体信号灯控制逻辑与现实生活中普通交通灯控制逻辑相同,不考虑特殊情况下的控制逻辑。 注:南北向车辆与东西向车辆交替放行,同方向等待车辆应先放行直行车辆而后放行左转车辆。· 每辆车通过路口时间为1秒(提示:可通过线程Sleep的方式模拟)。 · 随机生成车辆时间间隔以及红绿灯交换时间间隔自定,可以设置。 · 不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。 (一)Road类
1.每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
2.在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
3.在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
源码如下: using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace Program01
{
public class Road
{
Queue<string> vechicles = new Queue<string>();
private string name;
public Road(string name)
{
this.name = name;
new Thread(new ThreadStart( delegate
{
for (int i = 0; i < 1000; i++)
{
Thread.Sleep((new Random().Next(10)+1)*1000);
vechicles.Enqueue(name + "_"+ (i + 1));
}
})).Start();
Timer timer = new Timer(new TimerCallback(delegate
{
if (vechicles.Count>0)
{
bool lighted = Lamp.Lamps[this.name].Lighted;
if (lighted)
{
Console.WriteLine(vechicles.Dequeue()+" is traversing !");
}
}
}), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
}
}
}(二)Lamp类 1.系统中有12个方向上的灯,在程序的其他地方要根据灯的名称就可以获得对应的灯的实例对象。 2.每个Lamp对象中的亮黑状态用lighted变量表示,选用S2N、S2W、E2W、E2N这四个方向上的Lamp对象依次轮询变亮,Lamp对象中还要有一个oppositeLampName变量来表示它们相反方向的灯,再用一个nextLampName变量来表示此灯变亮后的下一个变亮的灯。这三个变量用构造方法的形式进行赋值,因为枚举元素必须在定义之后引用,所以无法再构造方法中彼此相互引用,所以,相反方向和下一个方向的灯用字符串形式表示。
3.增加让Lamp变亮和变黑的方法:light和blackOut,对于S2N、S2W、E2W、E2N这四个方向上的Lamp对象,这两个方法内部要让相反方向的灯随之变亮和变黑,blackOut方法还要让下一个灯变亮。
4.除了S2N、S2W、E2W、E2N这四个方向上的Lamp对象之外,其他方向上的Lamp对象的nextLampName和oppositeLampName属性设置为null即可,并且S2N、S2W、E2W、E2N这四个方向上的Lamp对象的nextLampName和oppositeLampName属性必须设置为null,以便防止light和blackOut进入死循环。using System;
using System.Collections.Generic;
using System.Text;namespace Program01
{
public class Lamp
{ //使用字典存放交通的变化逻辑;
private static Dictionary<string, Lamp> lamps = new Dictionary<string, Lamp>();
public static Dictionary<string, Lamp> Lamps
{
get { return Lamp.lamps; }
} //静态构造函数,引用静态成员之前创建,相对于单例;
static Lamp()
{
lamps.Add("S2N", new Lamp("S2N","N2S", "S2W", false));
lamps.Add("S2W", new Lamp("S2W","N2E", "E2W", false));
lamps.Add("E2W", new Lamp("E2W","W2E", "E2S", false));
lamps.Add("E2S", new Lamp("E2S","W2N", "S2N", false)); lamps.Add("N2S", new Lamp("N2S",null, null, false));
lamps.Add("N2E", new Lamp("N2E",null, null, false));
lamps.Add("W2E", new Lamp("W2E",null, null, false));
lamps.Add("W2N", new Lamp("W2N",null, null, false)); lamps.Add("S2E", new Lamp("S2E",null, null, true));
lamps.Add("E2N", new Lamp("E2N",null, null, true));
lamps.Add("N2W", new Lamp("N2W",null, null, true));
lamps.Add("W2S", new Lamp("W2S",null, null, true));
}
//S2N,S2W,E2W,E2S,
//N2S,N2E,W2E,W2N,
//S2E,E2N,N2W,W2S
private string name;
private string opposite;
private string next;
private bool lighted; public bool Lighted
{
get { return lighted; }
}
private Lamp(string name,string opposite, string next, bool lighted)
{
this.name = name;
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
public void light()
{
this.lighted = true;
if (opposite!=null)
{
lamps[opposite].light();
}
System.Console.WriteLine(this.name + " lamp is green,下面总共应该有6个方向能看到汽车穿过!");
}
public Lamp blackOut()
{
this.lighted = false;
if (opposite!=null)
{
lamps[opposite].blackOut();
}
Lamp nextLamp = null;
if (next!=null)
{
nextLamp = lamps[next];
System.Console.WriteLine("绿灯从" + this.name + "-------->切换为" + this.next);
nextLamp.light();
}
return nextLamp;
} }
}
(三)LampController类
1.LampController构造方法中要设定第一个为绿的灯。
2.LampController对象将当前灯变绿,然后启动一个定时器,每隔10秒将当前灯变红和将下一个灯变绿。
源码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace Program01
{
public class LampController
{
private Lamp currentLamp;
public LampController()
{
currentLamp = Lamp.Lamps["S2N"];
currentLamp.light(); Timer timer = new Timer(new TimerCallback(delegate {
currentLamp = currentLamp.blackOut();
}),null,TimeSpan.FromSeconds(10),TimeSpan.FromSeconds(10)); }
}
}(四)Program类
1.用for循环创建出代表12条路线的对象。
2.接着创建出LampController对象。
源码如下: using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace Program01
{
class Program
{
static void Main(string[] args)
{ string[] directions = new string[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
};
for (int i = 0; i < directions.Length; i++)
{
new Road(directions[i]);
}
new LampController();
}
}
}
1.每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
2.在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
3.在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
源码如下: using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace Program01
{
public class Road
{
Queue<string> vechicles = new Queue<string>();
private string name;
public Road(string name)
{
this.name = name;
new Thread(new ThreadStart( delegate
{
for (int i = 0; i < 1000; i++)
{
Thread.Sleep((new Random().Next(10)+1)*1000);
vechicles.Enqueue(name + "_"+ (i + 1));
}
})).Start();
Timer timer = new Timer(new TimerCallback(delegate
{
if (vechicles.Count>0)
{
bool lighted = Lamp.Lamps[this.name].Lighted;
if (lighted)
{
Console.WriteLine(vechicles.Dequeue()+" is traversing !");
}
}
}), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
}
}
}(二)Lamp类 1.系统中有12个方向上的灯,在程序的其他地方要根据灯的名称就可以获得对应的灯的实例对象。 2.每个Lamp对象中的亮黑状态用lighted变量表示,选用S2N、S2W、E2W、E2N这四个方向上的Lamp对象依次轮询变亮,Lamp对象中还要有一个oppositeLampName变量来表示它们相反方向的灯,再用一个nextLampName变量来表示此灯变亮后的下一个变亮的灯。这三个变量用构造方法的形式进行赋值,因为枚举元素必须在定义之后引用,所以无法再构造方法中彼此相互引用,所以,相反方向和下一个方向的灯用字符串形式表示。
3.增加让Lamp变亮和变黑的方法:light和blackOut,对于S2N、S2W、E2W、E2N这四个方向上的Lamp对象,这两个方法内部要让相反方向的灯随之变亮和变黑,blackOut方法还要让下一个灯变亮。
4.除了S2N、S2W、E2W、E2N这四个方向上的Lamp对象之外,其他方向上的Lamp对象的nextLampName和oppositeLampName属性设置为null即可,并且S2N、S2W、E2W、E2N这四个方向上的Lamp对象的nextLampName和oppositeLampName属性必须设置为null,以便防止light和blackOut进入死循环。using System;
using System.Collections.Generic;
using System.Text;namespace Program01
{
public class Lamp
{ //使用字典存放交通的变化逻辑;
private static Dictionary<string, Lamp> lamps = new Dictionary<string, Lamp>();
public static Dictionary<string, Lamp> Lamps
{
get { return Lamp.lamps; }
} //静态构造函数,引用静态成员之前创建,相对于单例;
static Lamp()
{
lamps.Add("S2N", new Lamp("S2N","N2S", "S2W", false));
lamps.Add("S2W", new Lamp("S2W","N2E", "E2W", false));
lamps.Add("E2W", new Lamp("E2W","W2E", "E2S", false));
lamps.Add("E2S", new Lamp("E2S","W2N", "S2N", false)); lamps.Add("N2S", new Lamp("N2S",null, null, false));
lamps.Add("N2E", new Lamp("N2E",null, null, false));
lamps.Add("W2E", new Lamp("W2E",null, null, false));
lamps.Add("W2N", new Lamp("W2N",null, null, false)); lamps.Add("S2E", new Lamp("S2E",null, null, true));
lamps.Add("E2N", new Lamp("E2N",null, null, true));
lamps.Add("N2W", new Lamp("N2W",null, null, true));
lamps.Add("W2S", new Lamp("W2S",null, null, true));
}
//S2N,S2W,E2W,E2S,
//N2S,N2E,W2E,W2N,
//S2E,E2N,N2W,W2S
private string name;
private string opposite;
private string next;
private bool lighted; public bool Lighted
{
get { return lighted; }
}
private Lamp(string name,string opposite, string next, bool lighted)
{
this.name = name;
this.opposite = opposite;
this.next = next;
this.lighted = lighted;
}
public void light()
{
this.lighted = true;
if (opposite!=null)
{
lamps[opposite].light();
}
System.Console.WriteLine(this.name + " lamp is green,下面总共应该有6个方向能看到汽车穿过!");
}
public Lamp blackOut()
{
this.lighted = false;
if (opposite!=null)
{
lamps[opposite].blackOut();
}
Lamp nextLamp = null;
if (next!=null)
{
nextLamp = lamps[next];
System.Console.WriteLine("绿灯从" + this.name + "-------->切换为" + this.next);
nextLamp.light();
}
return nextLamp;
} }
}
(三)LampController类
1.LampController构造方法中要设定第一个为绿的灯。
2.LampController对象将当前灯变绿,然后启动一个定时器,每隔10秒将当前灯变红和将下一个灯变绿。
源码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace Program01
{
public class LampController
{
private Lamp currentLamp;
public LampController()
{
currentLamp = Lamp.Lamps["S2N"];
currentLamp.light(); Timer timer = new Timer(new TimerCallback(delegate {
currentLamp = currentLamp.blackOut();
}),null,TimeSpan.FromSeconds(10),TimeSpan.FromSeconds(10)); }
}
}(四)Program类
1.用for循环创建出代表12条路线的对象。
2.接着创建出LampController对象。
源码如下: using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace Program01
{
class Program
{
static void Main(string[] args)
{ string[] directions = new string[]{
"S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"
};
for (int i = 0; i < directions.Length; i++)
{
new Road(directions[i]);
}
new LampController();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货