题目如下:
题目:
假设我们是中国国家航天局人员,当玉兔号离开嫦娥三号之后,我们需要能够控制玉兔号在月球上开展探测工作。我们先假定虹湾区是一个很大的平原,我们在虹湾区建立一个坐标轴,如下图:
玉兔号离开嫦娥三号后,根据自身安装的定位系统可以知道自己的初始位置,我们记为 X0 , Y0 ; 同时玉兔号也可以知道当前它的朝向,如东、西、南、北(暂时只考虑这四个方向)。
中国国家航天局会向玉兔号发送指令,我们先暂定为3种:
F : 当玉兔号接收到这条指令之后,会向前移动一个坐标单位的距离
L : 当玉兔号接受到这条指令之后,会原地向左旋转90度
R : 当玉兔号接收到这条指令之后,会原地向右旋转90度
要求:
一)设计一个玉兔号的主程序,能够接收中国国家航天局发送过来的指令序列(如FFLFRFLL),执行该指令序列之后,玉兔号能够走到正确的位置,并知道当前正确的位置。(如:玉兔号初始位置为 (0,0),方向朝东,执行指令 FFLFRFLL之后,位置为 (3,1) 方向朝西)
二)主程序中,不允许出现switch case语句,也不允许出现else关键字,也不允许使用三元表达式,if关键字出现的次数要求在5次以下(0-4次)
三)主程序可以用任何语言编写,如Java、C#、Ruby、Python、PHP等
四)在所选语言允许的情况下,请编写相应的单元测试
思路:一般有多条件的,我们会选择使用if/else、switch /case ,但题目明确规定 不能使用,if也限定次数,很自然就想到委托(c++里面的函数指针),还有怎么实现根据输入自动选择哪种操作,这就想到了字典(键/值).
//窗口程序代码实现====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace YutuControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public class Move
{
delegate void DelegateFront();
//字典实现
private Dictionary<string, DelegateFront> Dictionary = new Dictionary<string, DelegateFront>();
private int direction;
private const int MaxDirection = 4;//常量表示当前有4个方向
public int x,y;
public void move(int x, int y, int direction)
{
//Turn_Direction = 0; //北
//Turn_Direction = 1; //东
//Turn_Direction = 2; //南
//Turn_Direction = 3; //西
this.x = x;
this.y = y;
direction = direction;
//使用委托+字典实现
Dictionary["0"] = new DelegateFront(NorthAdd);
Dictionary["1"] = new DelegateFront(EastAdd);
Dictionary["2"] = new DelegateFront(SouthAdd);
Dictionary["3"] = new DelegateFront(WestAdd);
}
/// <summary>
/// 逆时针
/// </summary>
public void DealLeft()
{
direction = (direction - 1 + MaxDirection) % MaxDirection;
}
/// <summary>
/// 顺时针
/// </summary>
public void DealRight()
{
direction = (direction + 1) % MaxDirection;
}
public void DealFront()
{
//没有使用委托实现
//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()]();
}
}
private void NorthAdd()
{
++y;
}
private void SouthAdd()
{
--y;
}
private void WestAdd()
{
--x;
}
private void EastAdd()
{
++x;
}
public string Print()
{
return "x:" + x + ",y:" + y;
// Console.WriteLine("x:" + x + ",y:" + y);
// Console.WriteLine("Direction:" + (Diretion)direction);
// Console.ReadKey();
}
// Singleton模式:
private static Move instance;
private Move() { }
public static Move Instance
{
get
{
if (instance == null)
{
instance = new Move();
}
return instance;
}
}
}
class Direction
{
int north;
int south;
int west;
int east;
public int North
{
get{return north;}
set{ north = 3;}
}
public int East
{
get { return east; }
set { east = 2; }
}
public int South
{
get { return south; }
set { south = 1; }
}
public int West
{
get { return west; }
set { west = 0; }
}
}
public delegate void OperatorDelegate();
public int X , Y ;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//字典实现
Direction direction = new Direction();
var Key = new System.Collections.Generic.Dictionary<string, OperatorDelegate>();
// Move YuTu3 = new Move(0, 0, direction.North);
// Move YuTu3 = new Move();
Move.Instance.move(X, Y, direction.North);
Key["F"] = new OperatorDelegate(Move.Instance.DealFront);
Key["L"] = new OperatorDelegate(Move.Instance.DealLeft);
Key["R"] = new OperatorDelegate(Move.Instance.DealRight);
string key = e.KeyValue.ToString();
if (key=="70")
{
string instruct = "F";
if (Key.ContainsKey(instruct))
{
Key[instruct]();
}
Y = Move.Instance.y;
X = Move.Instance.x;
label1.Text = string.Format("x: {0}"+"y: {1}:",X,Y);//Y.ToString();
}
}
}
}