C # written questions of software engineers

Source: Internet
Author: User
Tags switch case

Recently, I have been planning to find a new job. I have not submitted my resignation, but I have submitted my resume to several companies. One of my favorite companies has sent interview questions. In fact, it seems like a few days ago, the main reason is that Gmail mailbox is rarely used, so I haven't looked at it all the time. Today I can see the question. Question: Suppose we are a member of the Chinese National Space Administration (NASA). When Yutu leaves chang'e 3, we need to be able to control Yutu's detection on the moon. Let's assume that the dinghong Bay area is a great plain. We have established an axis in the Hongwan area. For example, after Yutu leaves chang'e 3, according to the positioning system we have installed, we can know our initial position, which is marked as X0 and Y0. At the same time, Yutu can also know its current orientation, for example, east, west, south, and North (only these four directions are considered for the time being ). The Chinese National Space Administration will send an instruction to Yutu. We will first tentatively set it to three types: F: When Yutu receives this instruction, it will move forward the distance of a coordinate unit L: after the Yutu receives this instruction, it will rotate 90 degrees R to the left: After the Yutu receives this instruction, it will rotate 90 degrees to the right. Requirement: 1) design a main program of Yutu to receive the sequence of commands (such as FFLFRFLL) sent by the Chinese National Space Administration. After executing the sequence, Yutu can go to the correct position, and know the current correct position. (For example, in the main program, the initial position of Yutu is (0, 0), the direction is to the east, after the command FFLFRFLL is executed, and the position is (3, 1) to the west, switch case statements, else keywords, and ternary expressions are not allowed. if keywords must be less than 5 times (0-4 times) the main program can be written in any language, such as Java, C #, Ruby, Python, PHP, etc.) when the selected language permits, please write the corresponding unit test ideas: if there are multiple conditions, we will choose to use if/else, switch/case, but the question clearly stipulates that it cannot be used, if also limits the number of times, naturally, I think of delegation (the function pointer in c ++) and how to automatically select which operation based on input. This reminds me of the Dictionary (key/value ). paste the code: copy the code public delegate void OperatorDelegate (); static void Main (string [] args) {str Ing instruct = ""; Detector YuTu3 = new Detector (0, 0, (int) Diretion. east); var Dictory = new System. collections. generic. dictionary <string, OperatorDelegate> (); Dictionary ["F"] = new OperatorDelegate (YuTu3.DealFont); Dictionary ["L"] = new OperatorDelegate (YuTu3.DealLeft ); dictory ["R"] = new OperatorDelegate (YuTu3.DealRight); while ("exit "! = (Instruct = Console. readLine () {if (Dictory. containsKey (instruct) {Dictory [instruct] () ;}}; YuTu3.Print () ;}// Detector class Detector {delegate void DelegateFont (); private int x; private int y; private int direction; private const int MaxDirection = 4; // Constant indicates that there are currently four directions of private Dictionary <string, DelegateFont> Dictionary = new Dictionary <string, delegateFont> (); // constructor 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> /// counterclockwise // </summary> public void DealLeft () {direction = (direction-1 + MaxDirection) % MaxDirection ;} /// <summary> /// clockwise /// </Summary> public void DealRight () {direction = (direction + 1) % MaxDirection;} public void DealFont () {// The delegate is not used for implementation. // if (direction = (int) Diretion. north) {++ y; return;} // if (direction = (int) Diretion. south) {-- y; return;} // if (direction = (int) Diretion. west) {-- x; return;} // ++ x; // use the delegate + Dictionary to implement if (Dictionary. containsKey (direction. toString () {// call the delegate 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} copy the code to replace if/else, switch/case with two delegates + dictionaries. The advantage of this is that it is easy to maintain and add data. When the code needs to be added Add the fourth operation. During the fifth operation, you do not need to modify the called method. You only need to add the operation in the detector class and add data to the key value. At the same time, I also reviewed the c # advanced delegation and dictionary, and gained a lot. Continuous learning, summary, and growth.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.