软件工程师笔试题-玉兔号控制程序

来源:互联网 发布:软件测试工程师前景 编辑:程序博客网 时间:2024/04/28 13:02

背景介绍:

201312142111分,嫦娥三号在月球正面的虹湾以东地区着陆。

20131215日凌晨,嫦娥三号搭载的玉兔号月球探测器成功与嫦娥三号进行器件分离。

题目:

假设我们是中国国家航天局人员,当玉兔号离开嫦娥三号之后,我们需要能够控制玉兔号在月球上开展探测工作。我们先假定虹湾区是一个很大的平原,我们在虹湾区建立一个坐标轴,如下图:

玉兔号离开嫦娥三号后,根据自身安装的定位系统可以知道自己的初始位置,我们记为 X0 , Y0 ; 同时玉兔号也可以知道当前它的朝向,如东、西、南、北(暂时只考虑这四个方向)。

中国国家航天局会向玉兔号发送指令,我们先暂定为3种:

1. F : 当玉兔号接收到这条指令之后,会向前移动一个坐标单位的距离

2. L : 当玉兔号接受到这条指令之后,会原地向左旋转90

3. R : 当玉兔号接收到这条指令之后,会原地向右旋转90

要求:

一) 设计一个玉兔号的主程序,能够接收中国国家航天局发送过来的指令序列(如FFLFRFLL),执行该指令序列之后,玉兔号能够走到正确的位置,并知道当前正确的位置。(如:玉兔号初始位置为 (0,0),方向朝东,执行指令 FFLFRFLL之后,位置为 (3,1) 方向朝西)

二) 主程序中,不允许出现switch case语句,也不允许出现if或者else关键字,也不允许使用三元表达式。

 

三) 主程序可以用任何语言编写,如JavaC#RubyPythonPHP

 

在所选语言允许的情况下,请编写相应的单元测试

----------------------------------------------------------------------

 参考了一些c#资料,本人做的是asp.net(c#) b/s 于是将他做成web项目来测试,使用vs2010 .net 4.0 运行运行结果如下图所示:

Detector.cs 文件
-------------------------------------------------------
using System.Collections.Generic;/// <summary>/// 玉兔号探测器/// </summary>public class Detector{    delegate void DelegateFront();    private int x;    private int y;    private int direction;    private const int MaxDirection = 4; //常量表示当前共有4个方向    private Dictionary<string, DelegateFront> Dictionary = new Dictionary<string, DelegateFront>();    //构造函数    public Detector(int x, int y, int direction)    {        this.x = x;        this.y = y;        this.direction = direction;        Dictionary["0"] = new DelegateFront(GoNorth);//向北        Dictionary["1"] = new DelegateFront(GoEast);//向东        Dictionary["2"] = new DelegateFront(GoSouth);//向南        Dictionary["3"] = new DelegateFront(GoWest);//向西    }    /// <summary>    /// 向左转    /// </summary>    public void DealLeft()    {        direction = (direction - 1 + MaxDirection) % MaxDirection;    }    /// <summary>    /// 向右转    /// </summary>    public void DealRight()    {        direction = (direction + 1) % MaxDirection;    }    /// <summary>    /// 向前    /// </summary>    public void DealFront()    {        //使用委托,字典实现        if (Dictionary.ContainsKey(direction.ToString()))        {            //调用委托            Dictionary[direction.ToString()]();        }    }    public string WriteResult()    {        string strResult = "";        strResult += "坐标是:x:" + x + ",y:" + y;        strResult += "<br />";        strResult += "方向朝:" + (Direction)direction;        return strResult;    }    private void GoNorth()    {        ++y;    }    private void GoSouth()    {        --y;    }    private void GoWest()    {        --x;    }    private void GoEast()    {        ++x;    }}public enum Direction{    North = 0,    East = 1,    South = 2,    West = 3}


 

-----------------------
default.aspx.cs文件
----------------------
using System;using System.Collections.Generic;public partial class _default : System.Web.UI.Page{    public delegate void OperatorDelegate();    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {                   }    }    protected void btnPost_Click(object sender, EventArgs e)    {        //一串指令        string instruct = this.TxtInstruct.Text.Trim().ToUpper();        Detector YuRabbit3 = new Detector(0, 0, (int)Direction.East);        var Dictory = new System.Collections.Generic.Dictionary<string, OperatorDelegate>();        Dictory["F"] = new OperatorDelegate(YuRabbit3.DealFront);        Dictory["L"] = new OperatorDelegate(YuRabbit3.DealLeft);        Dictory["R"] = new OperatorDelegate(YuRabbit3.DealRight);        if (instruct != string.Empty)        {            string subInstruct =string.Empty;            for (int i = 0; i < instruct.Length; i++)            {                subInstruct = instruct.Substring(i, 1);                if (Dictory.ContainsKey(subInstruct))                {                    Dictory[subInstruct]();                }            }            this.ltResult.Text = YuRabbit3.WriteResult();        }        else        {            this.ltResult.Text = "您输入的指令不正确";        }    }}


 

0 0
原创粉丝点击