程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#掌握台模仿電梯任務道理

C#掌握台模仿電梯任務道理

編輯:C#入門知識

C#掌握台模仿電梯任務道理。本站提示廣大學習愛好者:(C#掌握台模仿電梯任務道理)文章只能為提供參考,不一定能成為您想要的結果。以下是C#掌握台模仿電梯任務道理正文


天天高低樓都是乘坐電梯的,就想電梯的任務道理是甚麼呢?因而本身寫了個掌握台法式來模仿一下電梯的任務道理!
采取面向對象的編程思惟!將電梯拆解為兩部門;
第一部門就是每一個樓層的掌握器(每一個樓層都有叫梯按鈕的哈,一個向上一個向下)
第二部門就電梯間了。電梯間裡有樓層按鈕,你想上誰人樓層便可以按哪一個按鈕了!
技巧難點:狀況刷新、敕令次序、電梯運轉

焦點代碼一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Elevator
{
  /// <summary>
  /// 樓層類,每一個樓層有向上叫梯敕令和向下叫梯敕令
  /// </summary>
  public class Floor
  {
    Elevator elevator;
    /// <summary>
    /// 樓層號
    /// </summary>
    private int _iFloorNo;
 
    public int IFloorNo
    {
      get { return _iFloorNo; }
      set { _iFloorNo = value; }
    }
    /// <summary>
    /// 下行需求
    /// </summary>
    private bool _boolUp = false;
    /// <summary>
    /// 下行需求
    /// </summary>
    private bool _boolDown = false;
 
    private bool _boolStop = false;
 
    public bool BoolStop
    {
      get { return _boolStop; }
      set { _boolStop = value; }
    }
 
    #region 結構函數
     
    
    public Floor(int f,Elevator e)
    {
      _iFloorNo = f;
      elevator = e;
    }
 
    public Floor(int f)
    {
      _iFloorNo = f;
    }
 
    public Floor()
    {
    }
    #endregion
 
    /// <summary>
    /// 獲得本層能否停靠,能否為敕令層
    /// </summary>
    /// <returns>停靠true;過false;</returns>
    public bool GetStatus()
    {
      return _boolDown || _boolUp;
    }
 
    /// <summary>
    /// 下行前往1;下行前往-1;本層為目標地前往0;
    /// </summary>
    /// <returns></returns>
    public int GetStatusFlag()
    {
      if (_boolDown)
      {
        return -1;
      }
      else if(_boolUp)
      {
        return 1;
      }
      else if(_boolStop)
      {
        return 0;
      }
      else
      {
        return -999;
      }
 
    }
 
    /// <summary>
    /// 上樓敕令
    /// </summary>
    public void CommandUp()
    {
      _boolUp = true;
      elevator.GoToCommandFloor();
    }
 
    /// <summary>
    /// 下樓敕令
    /// </summary>
    public void CommandDown()
    {
      _boolDown = true;
      elevator.GoToCommandFloor();
    }
 
    /// <summary>
    /// 樓層達到狀況刷新
    /// </summary>
    public void Refresh()
    {
      _boolUp = false;
      _boolDown = false;
      _boolStop = false;
    }
 
 
    /// <summary>
    /// 樓層比擬看樓層號
    /// </summary>
    /// <param name="floor"></param>
    /// <returns></returns>
    public int Compare(Floor floor)
    {
      int result = 1;
      if (this._iFloorNo > floor._iFloorNo)
      {
        result = 1;
      }
      else if (this._iFloorNo < floor._iFloorNo)
      {
        result = -1;
      }
      else
      {
        result = 0;
      }
      return result;
    }
  }
}

焦點代碼二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace Elevator
{
  /// <summary>
  /// 電梯類,有樓層選擇辦法
  /// </summary>
  public class Elevator
  {
    #region 屬性
 
    /// <summary>
    /// 以後樓層
    /// </summary>
    public Floor _floorCurrent ;
    /// <summary>
    /// 全體樓層
    /// </summary>
    public List<Floor> _floorAll = new List<Floor>();
 
    #endregion
 
    /// <summary>
    /// 我想在所輸出參數樓層停滯
    /// </summary>
    /// <param name="floor">想要停滯的樓層</param>
    public void CommandStop(Floor floor)
    {
      floor.BoolStop = true;
      GoToTargetFloor();
    }
 
 
    /// <summary>
    /// 電梯向上運轉,運轉到floor層
    /// </summary>
    /// <param name="floor"></param>
    public void GoUp(Floor floor)
    {
      if (_floorCurrent.Compare(floor) < 0)
      {
      Thread.Sleep(300);
      Console.WriteLine("下行:" + _floorCurrent.IFloorNo);
      int index = _floorAll.IndexOf(_floorCurrent);
      _floorCurrent = _floorAll[index + 1];
      GoUp(floor);
      }
      else {
        Thread.Sleep(500);
        Reach(floor);
      }
    }
 
    /// <summary>
    /// 電梯向下運轉
    /// </summary>
    public void GoDown(Floor floor)
    {
      if (_floorCurrent.Compare(floor) > 0)
      {
        Thread.Sleep(300);
        Console.WriteLine("下行:" + _floorCurrent.IFloorNo);
        int index = _floorAll.IndexOf(_floorCurrent);
        _floorCurrent = _floorAll[index - 1];
        GoDown(floor);
      }
      else {
        Thread.Sleep(500);
        Reach(floor);
      }
    }
 
    /// <summary>
    /// 前去敕令層,輪回列表能否有敕令層
    /// </summary>
    public void GoToCommandFloor()
    {
      foreach (var item in _floorAll)
      {
        if (item.GetStatus())
        {
          if (_floorCurrent.Compare(item) < 0)
          {
            GoUp(item);
          }
          else if (_floorCurrent.Compare(item) > 0)
          {
            GoDown(item);
          }
        }
      }
    }
 
    /// <summary>
    /// 前去目的樓層
    /// </summary>
    public void GoToTargetFloor() {
 
      foreach (var item in _floorAll)
      {
        if (item.GetStatusFlag()==0)
        {
          if (_floorCurrent.Compare(item) < 0)
          {
            GoUp(item);
          }
          else if (_floorCurrent.Compare(item) > 0)
          {
            GoDown(item);
          }
        }
      }
    }
    
 
    /// <summary>
    /// 達到樓層敕令
    /// </summary>
    public void Reach(Floor f)
    {
      Console.WriteLine("電梯門翻開,停靠層:"+f.IFloorNo);
      f.Refresh();
    }
 
  }
}

焦點代碼三:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace Elevator
{
  class Program
  {
    static void Main(string[] args)
    {
       Elevator elevator = new Elevator();
      List<Floor> lfloor = new List<Floor>();
      Floor f1 = new Floor(1, elevator);
      Floor f2 = new Floor(2, elevator);
      Floor f3 = new Floor(3, elevator);
      Floor f4 = new Floor(4, elevator);
      Floor f5 = new Floor(5, elevator);
      Floor f6 = new Floor(6, elevator);
      Floor f7 = new Floor(7, elevator);
      Floor f8 = new Floor(8, elevator);
      Floor f9 = new Floor(9, elevator);
      Floor f10 = new Floor(10, elevator);
      lfloor.Add(f1);
      lfloor.Add(f2);
      lfloor.Add(f3);
      lfloor.Add(f4);
      lfloor.Add(f5);
      lfloor.Add(f6);
      lfloor.Add(f7);
      lfloor.Add(f8);
      lfloor.Add(f9);
      lfloor.Add(f10);
      
      elevator._floorAll = lfloor;
      //設置以後樓層
      elevator._floorCurrent = f1;
      //2樓叫梯 想下樓
      f2.CommandDown();
      //目的樓層為1樓
      elevator.CommandStop(f1);
      //4樓叫梯
      f4.CommandUp();
      //目的樓層為8樓
      elevator.CommandStop(f8);
 
      Console.ReadLine();
    }
  }
}

以上所述就是本文的全體內容了,願望對年夜家闇練控制C#可以或許有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved