以下是一个基于上述要求的 C++ 程序示例:

#include <iostream>
#include <vector>

using namespace std;

class Warehouse {
private:
    int capacity; // 库房容量
    bool isOccupied; // 库房状态,true 表示已占用,false 表示空闲
    int rent; // 每个库房日租金
    int inTime; // 入库时间

public:
    Warehouse(int cap, int r) {
        capacity = cap;
        isOccupied = false;
        rent = r;
        inTime = 0;
    }

    // 入库操作
    bool storeGoods(int weight, int time) {
        if (weight <= capacity && !isOccupied) {
            capacity -= weight;
            isOccupied = true;
            inTime = time;
            return true;
        }
        return false;
    }

    // 出库操作
    int releaseGoods(int time) {
        if (isOccupied) {
            int duration = time - inTime;
            int cost = duration * rent;
            // 释放库房
            capacity += (capacity == 0) ? 0 : 1;
            isOccupied = false;
            return cost;
        }
        return 0;
    }

    // 获取库房容量
    int getCapacity() {
        return capacity;
    }
};

class WarehouseManager {
private:
    vector<Warehouse> warehouses;

public:
    WarehouseManager() {}

    // 添加库房
    void addWarehouse(int cap, int rent) {
        warehouses.emplace_back(cap, rent);
    }

    // 货物入库
    bool storeGoods(int weight, int time) {
        int minCapacity = INT_MAX;
        int minIndex = -1;
        for (int i = 0; i < warehouses.size(); i++) {
            if (warehouses[i].getCapacity() >= weight && !warehouses[i].storeGoods(weight, time)) {
                continue;
            }
            if (warehouses[i].getCapacity() < minCapacity) {
                minCapacity = warehouses[i].getCapacity();
                minIndex = i;
            }
        }
        if (minIndex != -1) {
            warehouses[minIndex].storeGoods(weight, time);
            return true;
        }
        return false;
    }

    // 货物出库
    int releaseGoods(int time) {
        int totalCost = 0;
        for (int i = 0; i < warehouses.size(); i++) {
            totalCost += warehouses[i].releaseGoods(time);
        }
        return totalCost;
    }
};

int main() {
    WarehouseManager manager;

    // 添加三个库房
    manager.addWarehouse(10, 100);
    manager.addWarehouse(20, 200);
    manager.addWarehouse(30, 300);

    // 货物入库
    bool success = manager.storeGoods(15, 1); // 应选择第一个库房
    if (!success) {
        cout << '入库失败' << endl;
    }

    // 货物出库
    int cost = manager.releaseGoods(5); // 结算费用
    cout << '费用:' << cost << endl;

    return 0;
}

在上述程序中,Warehouse 类表示一个库房,包含了库房的容量、状态、日租金和入库时间等属性,并实现了入库和出库的操作。WarehouseManager 类表示库房的管理类,可以添加库房、进行货物入库和出库操作。在主函数中,创建了一个 WarehouseManager 对象并添加了三个库房,然后进行了货物入库和出库操作,并输出了费用。

C++ 仓库管理系统:实现库房租赁和货物存取

原文地址: https://www.cveoy.top/t/topic/cH6p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录