当前位置: 首页 > 知识库问答 >
问题:

C语言中的房间管理程序#

卫建义
2023-03-14

以下节目旨在复制一个基本的酒店客房预订方案。创建hotel number的实例后,通过调用checkIn()方法,程序检查是否有可用的房间,如果有,则保留房间。

但是,如果在分配完所有房间后,有人从一个房间(通过checkOut()方法)退房,例如1号房间,然后试图入住该房间,则该方法无效。当调用hasRoomsAvailable()方法时会出现此问题,在这种情况下,由于currentRoomNumber是5,该方法会返回false而不是true。

在不改变每种方法的时间复杂度的情况下,有人能建议如何解决这个问题,最好是如何改进设计吗?

using System;

namespace HotelManagement
{
    //Hotel class
    public class Hotel
    {
        private bool[] available;
        private int totalNumberOfRooms;
        private int currentRoomNumber;

        // constructor to set number of rooms
        private Hotel(int totalNumberOfRooms)
        {
            this.totalNumberOfRooms = totalNumberOfRooms;
            available = new bool[totalNumberOfRooms];

            for (int i = 0; i < totalNumberOfRooms; i++)   
                available[i] = true;
        }

        //Returns true if room is available
        private bool hasRoomsAvailable()
        {
            if (currentRoomNumber < totalNumberOfRooms &&
                available[currentRoomNumber])
                return available[currentRoomNumber];
            else
                return false;
        }

        //Time Complexity: O(1)

        //Checks if there's at least one room available and it reserves it
        private int checkIn()
        {
            if (hasRoomsAvailable())
            {
                available[currentRoomNumber] = false;
                return ++currentRoomNumber;
            }
            else
                return -1;
        }
        //Time Complexity: O(1)

        //Check out method
        private void checkOut(int roomNumber)
        {
            if (roomNumber <= totalNumberOfRooms && roomNumber != -1)
            {
                if (available[roomNumber - 1] == false)
                {
                    available[roomNumber - 1] = true;
                    Console.WriteLine("Check out room : {0}", roomNumber);
                }
                else
                    Console.WriteLine("Invalid Check Out : {0}", roomNumber);
            }
            else
                Console.WriteLine("Incorrect room number : {0}", roomNumber);
        }
        //Time Complexity: O(1)

        public static void Main(string[] args)
        {
            //Create an instance of Hotel with 5 rooms
            Hotel hotel = new Hotel(5);
            int roomNum = -1;

            if (hotel.hasRoomsAvailable())
            {
                roomNum = hotel.checkIn();
                Console.WriteLine("Room Allocated is: {0}", roomNum);

                roomNum = hotel.checkIn();
                Console.WriteLine("Room Allocated is: {0}", roomNum);

                roomNum = hotel.checkIn();
                Console.WriteLine("Room Allocated is: {0}", roomNum);

                roomNum = hotel.checkIn();
                Console.WriteLine("Room Allocated is: {0}", roomNum);

                roomNum = hotel.checkIn();
                Console.WriteLine("Room Allocated is: {0}", roomNum);
            }

            hotel.checkOut(1);
            roomNum = hotel.checkIn();

            Console.WriteLine("Room Allocated is: {0}", roomNum);

            //pause program output on console
            Console.ReadLine();
        }
    }
}

共有3个答案

吴缪文
2023-03-14

如果我能很好地理解你,你就需要一个代码,无论你有多少个房间,它都不能扫描房间以很好地缩放。我建议您对房间号使用哈希集

public class Hotel
{
    private HashSet<int> avalibe;
    private HashSet<int> busy;


    //constructor to set number of rooms
    public Hotel(int totalNumberOfRooms)
    {
        avalibe = new HashSet<int>();
        for (int i = 1; i <= totalNumberOfRooms; i++)
            avalibe.Add(i);
        busy = new HashSet<int>();
    }

    //Returns true if room is available
    public bool hasRoomsAvailable()
    {
        return avalibe.Count > 0;
    }
    //Time Complexity: O(1)

    //Checks if there's at least one room available and it reserves it
    public int checkIn()
    {
        if (hasRoomsAvailable())
        {
            var result = avalibe.First();
            avalibe.Remove(result);
            busy.Add(result);
            return result;
        }
        else
            return -1;
    }
    //Time Complexity: O(1)

    //Check out method
    public void checkOut(int roomNumber)
    {
        if (!busy.Contains(roomNumber))
        {
            Console.WriteLine("Incorrect room number : {0}", roomNumber);
            return;
        }
        busy.Remove(roomNumber);
        avalibe.Add(roomNumber);
    }


}
夏侯弘量
2023-03-14

在登记入住时添加条件以检查每个房间的可用性。

需要使用System. Linq添加命名空间的引用;

private bool hasRoomsAvailable()
    {
        if (currentRoomNumber < totalNumberOfRooms && available[currentRoomNumber])
            return available[currentRoomNumber];
        else if (available.Where(x => x == true).Count() > 0)
        {
            currentRoomNumber = Array.IndexOf(available, available.Where(x => x == true).First());
            return available[currentRoomNumber];
        }
        else
            return false;
    }
侯令雪
2023-03-14

可用的bool[]足以找到一个可以入住的房间。维护currentRoomNumber会带来更多的困难。

public class Hotel
{
    private bool[] available;
    private int totalNumberOfRooms;        

    // constructor to set number of rooms
    public Hotel(int totalNumberOfRooms)
    {
        this.totalNumberOfRooms = totalNumberOfRooms;
        available = new bool[totalNumberOfRooms];

        for (int i = 0; i < totalNumberOfRooms; i++)   
            available[i] = true;
    }

    //Returns true if room is available
    public bool hasRoomsAvailable()
    {
        return available.Any(room => room);
    }

    //Time Complexity: O(1)

    //Checks if there's at least one room available and it reserves it
    public int checkIn()
    {
        for(int room = 0; room < totalNumberOfRooms; room++)
        {
            if (available[room])
            {
                available[room] = false;
                return room + 1;
            }
        }

        return -1;
    }
    //Time Complexity: O(1)

    //Check out method
    public void checkOut(int roomNumber)
    {
        if (roomNumber <= totalNumberOfRooms && roomNumber > 0)
        {
            if (available[roomNumber - 1] == false)
            {
                available[roomNumber - 1] = true;
                Console.WriteLine("Check out room : {0}", roomNumber);
            }
            else
                Console.WriteLine("Invalid Check Out : {0}", roomNumber);
        }
        else
            Console.WriteLine("Incorrect room number : {0}", roomNumber);
    }
}
public static void Main(string[] args)
{
    //Create an instance of Hotel with 5 rooms
    Hotel hotel = new Hotel(5);
    int roomNum = -1;

    if (hotel.hasRoomsAvailable())
    {
        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);
    }

    hotel.checkOut(1);
    roomNum = hotel.checkIn();

    Console.WriteLine("Room Allocated is: {0}", roomNum);

    Console.ReadLine();
}

具有O(1)复杂性的解决方案,它使用队列来存储可用房间:

public class Hotel
{
    private Queue<int> rooms;
    private int totalNumberOfRooms;        

    // constructor to set number of rooms
    public Hotel(int totalNumberOfRooms)
    {
        this.totalNumberOfRooms = totalNumberOfRooms;
        rooms = new Queue<int>();

        for (int i = 1; i <= totalNumberOfRooms; i++)   
            rooms.Enqueue(i);
    }

    //Returns true if room is available
    public bool hasRoomsAvailable()
    {
        return rooms.Count > 0;
    }

    //Time Complexity: O(1)

    //Checks if there's at least one room available and it reserves it
    public int checkIn()
    {
        if (rooms.Count > 0)
            return rooms.Dequeue();

        return -1;
    }
    //Time Complexity: O(1)

    //Check out method
    public void checkOut(int roomNumber)
    {
        if (roomNumber <= totalNumberOfRooms && roomNumber > 0)
        {
            Console.WriteLine("Check out room : {0}", roomNumber);
            rooms.Enqueue(roomNumber);
        }
        else
            Console.WriteLine("Incorrect room number : {0}", roomNumber);
    }
}

public static void Main(string[] args)
{
    //Create an instance of Hotel with 5 rooms
    Hotel hotel = new Hotel(5);
    int roomNum = -1;

    if (hotel.hasRoomsAvailable())
    {
        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);

        roomNum = hotel.checkIn();
        Console.WriteLine("Room Allocated is: {0}", roomNum);
    }

    hotel.checkOut(4);
    hotel.checkOut(2);
    roomNum = hotel.checkIn();
    Console.WriteLine("Room Allocated is: {0}", roomNum);

    roomNum = hotel.checkIn();

    Console.WriteLine("Room Allocated is: {0}", roomNum);

    //pause program output on console
    Console.ReadLine();
}
 类似资料:
  • 本文向大家介绍C语言实现学生信息管理程序,包括了C语言实现学生信息管理程序的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C语言实现学生信息管理程序的具体代码,供大家参考,具体内容如下 目前只有5个功能 1. 增加学生信息 2. 删除学生信息 3. 查询某学生信息 4. 列出所有学生信息 5. 按出生日期先后输出学生信息 代码: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希

  • 游戏管理类 成员变量 变量 类型 名称 备注 gameId number 游戏id roomId number 房间id mId string 当前用户的openId ownerId string 房主的openId status number 房间状态 1 已创建房间,2 游戏中房间 environment number 网络环境 NETWORK_ENVIRONMENT_QQ_RELEASE /

  • 本文向大家介绍C语言图书管理系统课程设计,包括了C语言图书管理系统课程设计的使用技巧和注意事项,需要的朋友参考一下 这是本人大一第二学期初C语言课程设计的作品,嘿嘿,本来以为已经找不到原稿了,今天无意中竟然在QQ网络硬盘中找到了当初的teta版,发布于此,以作纪念。 C 源代码如下: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 推荐首先阅读 内存管理 Objective-C 中的内存分配 在 Objective-C 中,对象通常是使用 alloc 方法在堆上创建的。 [NSObject alloc] 方法会在对堆上分配一块内存,按照NSObject的内部结构填充这块儿内存区域。 一旦对象创建完成,就不可能再移动它了。因为很可能有很多指针都指向这个对象,这些指针并没有被追踪。因此没有办法在移动对象的位置之后更新全部的这些指

  • 本文向大家介绍C语言实现超市管理系统,包括了C语言实现超市管理系统的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C语言实现超市管理系统的具体代码,供大家参考,具体内容如下 超市管理系统 1.包括管理员和顾客 2.管理员有登入,录入商品信息,显示所有商品信息功能 3.顾客有购物车和结算功能 4.购物车具有显示商品列表,显示购物车商品,往购物车添加商品的功能 函数 建立库存函数 在购物

  • 本文向大家介绍C语言实现职工管理系统,包括了C语言实现职工管理系统的使用技巧和注意事项,需要的朋友参考一下 小编找到了一个特别好的职工管理系统,分享给大家一份C语言职工管理系统的具体实现代码,供大家参考,也谢谢这位网友的分享,具体内容如下 person.cpp person.h 更多学习资料请关注专题《管理系统开发》。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。