近来有打算从新找工做,还没提离职,投了几家公司简历,其中一家比较中意的公司给发了面试题,其实,好像是好几天前的事了,主要是Gmail邮箱不多用,因此一直都没去看,今天看到题目给解了。c++
题目以下:面试
假设咱们是中国国家航天局人员,当玉兔号离开嫦娥三号以后,咱们须要可以控制玉兔号在月球上开展探测工做。咱们先假定虹湾区是一个很大的平原,咱们在虹湾区创建一个坐标轴,以下图:c#
玉兔号离开嫦娥三号后,根据自身安装的定位系统能够知道本身的初始位置,咱们记为 X0 , Y0 ; 同时玉兔号也能够知道当前它的朝向,如东、西、南、北(暂时只考虑这四个方向)。函数
中国国家航天局会向玉兔号发送指令,咱们先暂定为3种:单元测试
一)设计一个玉兔号的主程序,可以接收中国国家航天局发送过来的指令序列(如FFLFRFLL),执行该指令序列以后,玉兔号可以走到正确的位置,并知道当前正确的位置。(如:玉兔号初始位置为 (0,0),方向朝东,执行指令 FFLFRFLL以后,位置为 (3,1) 方向朝西)学习
二)主程序中,不容许出现switch case语句,也不容许出现else关键字,也不容许使用三元表达式,if关键字出现的次数要求在5次如下(0-4次)测试
三)主程序能够用任何语言编写,如Java、C#、Ruby、Python、PHP等this
四)在所选语言容许的状况下,请编写相应的单元测试 spa
思路:通常有多条件的,咱们会选择使用if/else、switch /case ,但题目明确规定 不能使用,if也限定次数,很天然就想到委托(c++里面的函数指针),还有怎么实现根据输入自动选择哪一种操做,这就想到了字典(键/值).设计
贴出代码:
public delegate void OperatorDelegate(); static void Main(string[] args) { string instruct = ""; Detector YuTu3 = new Detector(0, 0, (int)Diretion.East); var Dictory = new System.Collections.Generic.Dictionary<string, OperatorDelegate>(); Dictory["F"] = new OperatorDelegate(YuTu3.DealFont); Dictory["L"] = new OperatorDelegate(YuTu3.DealLeft); Dictory["R"] = new OperatorDelegate(YuTu3.DealRight); while ("exit" != (instruct = Console.ReadLine())) { if (Dictory.ContainsKey(instruct)) { Dictory[instruct](); } }; YuTu3.Print(); } //探测器 class Detector { delegate void DelegateFont(); private int x; private int y; private int direction; private const int MaxDirection = 4;//常量表示当前有4个方向 private Dictionary<string, DelegateFont> Dictionary = new Dictionary<string, DelegateFont>(); //构造函数 public Detector(int x, int y, int direction) { this.x = x; this.y = y; this.direction = direction; Dictionary["0"] = new DelegateFont(NorthAdd); Dictionary["1"] = new DelegateFont(EastAdd); Dictionary["2"] = new DelegateFont(SouthAdd); Dictionary["3"] = new DelegateFont(WestAdd); } /// <summary> /// 逆时针 /// </summary> public void DealLeft() { direction = (direction - 1 + MaxDirection) % MaxDirection; } /// <summary> /// 顺时针 /// </summary> public void DealRight() { direction = (direction + 1) % MaxDirection; } public void DealFont() { //没有使用委托实现 //if (direction == (int)Diretion.North) { ++y; return; } //if (direction == (int)Diretion.South) { --y; return; } //if (direction == (int)Diretion.West) { --x; return; } //++x; //使用委托+字典实现 if (Dictionary.ContainsKey(direction.ToString())) { //调用委托 Dictionary[direction.ToString()](); } } public void Print() { Console.WriteLine("x:" + x + ",y:" + y); Console.WriteLine("Direction:" + (Diretion)direction); Console.ReadKey(); } private void NorthAdd() { ++y; } private void SouthAdd() { --y; } private void WestAdd() { --x; } private void EastAdd() { ++x; } } enum Diretion { North = 0, East = 1, South = 2, West = 3 }
使用两个委托+字典替换if/else ,switch/case,这样作的好处就是容易维护和新增数据,当代码须要添加第四种操做,第五种操做的时候,不须要改动调用的方法,只须要在探测器类里面填加操做,键值新增数据便可。
同时也把c#高级的委托跟字典再复习了一遍,收获颇多。不断学习,不断总结,不断成长。