RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-515714

enderline13's questions

Martin Hope
enderline13
Asked: 2023-10-25 03:40:25 +0000 UTC

无法从二进制文件中正确读取数字

  • 5
class PizzeriaDB {
public:
    ~PizzeriaDB() = default;
    std::vector<Pizza> getPizzasAvailable() const;
    void addClient(std::string, std::string);
    void addEmployee(const std::string& name);
    void deleteEmployee(const std::string& s);
    void addPizza(const std::string& name, double price);
    void deletePizza(const std::string& s);
    void setAdminKey(const std::string& s);
    bool AdminIsValid(const std::string&) const;
    bool ClientIsValid(const std::string&, const std::string&) const;
    void newOrder(const Order& o);
    void complete_order();
    void GetDB();
    void SaveDB();
private:
    std::string AdminKey = "superadmin";
    std::vector<Pizza> availablePizzas;
    std::vector<Employee> workers;
    std::map<std::string, std::string> clientData;
    std::queue<Order> current_orders;
};

class Employee {
public:
    Employee() = default;
    Employee(const std::string& name, bool free = true);
    void doWork(const Order& o);
    bool isFree() const;
    std::string getName() const;
    void SetName(const std::string& n) {
        name = n;
    }

    void SetFree(bool f) {
        free = f;
    }

    // Методы для получения данных
    const std::string& GetName() const {
        return name;
    }

    bool IsFree() const {
        return free;
    }
private:
    std::string name;
    bool free = 1;
};

class Pizza {
public:
    Pizza() = default;
    ~Pizza() {};
    Pizza(const std::string& type, double price, int amount = 1);
    std::string getType() const;
    double getPrice() const;
    int getAmount() const;
    std::string getName() const;
    void SetPizzaType(const std::string& type) {
        pizza_type = type;
    }
    void SetPrice(double p) {
        price = p;
    }
    void SetAmount(unsigned int a) {
        amount = a;
    }
    const std::string& GetPizzaType() const {
        return pizza_type;
    }
    double GetPrice() const {
        return price;
    }
    unsigned int GetAmount() const {
        return amount;
    }
private:
    std::string pizza_type;
    double price = 1;
    unsigned int amount = 1;
};

void PizzeriaDB::SaveDB() {
    // Запись PizzaCatalogue
    out.open("PizzaCatalogue.bin", std::ios::binary | std::ios::out);
    if (!out) {
        std::cout << "Failed to open PizzaCatalogue.bin for writing." << std::endl;
        return;
    }
    size_t pizzaCount = availablePizzas.size();
    out.write((char*)&pizzaCount, sizeof(size_t));
    for (const auto& pizza : availablePizzas) {
        size_t typeLen = pizza.GetPizzaType().size();
        out.write((char*)&typeLen, sizeof(size_t));
        const std::string& pizzaType = pizza.GetPizzaType();
        out.write(pizzaType.c_str(), typeLen);
        double price = pizza.GetPrice();
        out.write((char*)&price, sizeof(double));
    }
    out.close();

    // Запись WorkersDB
    out.open("WorkersDB.bin", std::ios::binary | std::ios::out);
    if (!out) {
        std::cout << "Failed to open WorkersDB.bin for writing." << std::endl;
        return;
    }
    size_t workerCount = workers.size();
    out.write((char*)&workerCount, sizeof(size_t));
    for (const auto& worker : workers) {
        size_t nameLen = worker.GetName().size();
        out.write((char*)&nameLen, sizeof(size_t));
        out.write(worker.GetName().c_str(), nameLen);
    }
    out.close();
}

void PizzeriaDB::GetDB() {
    // Чтение PizzaCatalogue
    in.open("PizzaCatalogue.bin", std::ios::binary);
    if (!in) {
        std::cout << "Failed to open PizzaCatalogue.bin for reading." << std::endl;
        return;
    }
    if (in.peek() == std::ifstream::traits_type::eof()) {
        // Файл пуст, пропускаем чтение
        return;
    }
    size_t pizzaCount;
    in.read((char*)&pizzaCount, sizeof(size_t));
    availablePizzas.resize(pizzaCount);
    for (size_t i = 0; i < pizzaCount; ++i) {
        Pizza pizza;
        size_t typeLen;
        in.read((char*)&typeLen, sizeof(size_t));
        std::string pizzaType(typeLen, '\0');
        in.read(&pizzaType[0], typeLen);
        pizza.SetPizzaType(pizzaType);
        double price;
        in.read((char*)&price, sizeof(double));
        pizza.SetPrice(price);
        availablePizzas[i] = pizza;
    }
    in.close();

    // Чтение WorkersDB
    in.open("WorkersDB.bin", std::ios::binary);
    if (!in) {
        std::cout << "Failed to open WorkersDB.bin for reading." << std::endl;
        return;
    }
    if (in.peek() == std::ifstream::traits_type::eof()) {
        // Файл пуст, пропускаем чтение
        return;
    }
    size_t workerCount;
    in.read((char*)&workerCount, sizeof(size_t));
    workers.resize(workerCount);
    for (size_t i = 0; i < workerCount; ++i) {
        Employee employee;
        size_t nameLen = 20;
        in.read((char*)&nameLen, sizeof(size_t));   
        std::string name(nameLen, '\0');
        in.read(&name[0], nameLen);
        employee.SetName(name);
        bool isFree;
        in.read((char*)&isFree, sizeof(bool));
        employee.SetFree(isFree);
        workers[i] = employee;
    }
    in.close();
}

对于披萨,一切都被记录下来并很好地阅读。但对于员工来说,就是第二个不读名字大小的员工。输入一个随机的大数字(可能是size_t限制),然后由于内存不足而无法构造字符串。本质上,披萨和工人的写入和读取算法是相同的。

c++
  • 1 个回答
  • 23 Views
Martin Hope
enderline13
Asked: 2022-09-04 15:48:23 +0000 UTC

如何在收到号码后告诉程序它在哪个号码系统中?C++

  • 0

在此处输入图像描述

我正在做 Stroustrup 书中的一项任务。以某种方式可以通过前缀为自己确定数字系统,但是如何将其告诉程序,然后将其翻译成您需要的?

c++
  • 2 个回答
  • 81 Views
Martin Hope
enderline13
Asked: 2022-08-20 15:41:19 +0000 UTC

运算符重载 == 无权访问私有字段 [关闭]

  • 0
关闭 这个问题是题外话。目前不接受回复。

寻求调试帮助的问题(“为什么这段代码不起作用? ”)应该包括期望的行为、具体的问题或错误,以及在问题中重现它的最少代码。没有明确描述问题的问题对其他访问者毫无用处。请参阅如何创建一个最小的、独立的和可重现的示例。

1 个月前关闭。

改进问题
bool operator== (book a, book b) {
    if (a.code == b.code) { return true; }
    else { return false; }
}

重载应该按一个字段比较对象,但该字段对其关闭。

c++ перегрузка-операторов
  • 1 个回答
  • 27 Views
Martin Hope
enderline13
Asked: 2022-08-19 18:22:58 +0000 UTC

程序跳过第二个对象的数据读取方法。C++

  • -1

当我为第一个对象输入数据时,一切都是标准的,但是当我转到第二个对象(start2)的方法时,编译器愚蠢地跳过了读取。

class Name_Pairs {
public:
    vector<string> name;
    vector<double> age;
    void read_names();
    void read_ages();
    void sort_name();
};
void Name_Pairs::read_names() {
    string h;
    for (int i = 0; cin >> h;) {
        if (h == "stop") { break; }
        name.push_back(h);
    }
}
void Name_Pairs::read_ages() {
    int h;
    for (int i = 0; cin >> h;) {
        age.push_back(h);
    }
}
int main()
{
    Name_Pairs start;
    Name_Pairs start2;
    start.read_names();
    start.read_ages();
   

     start2.read_names();
     start2.read_ages();

}
c++ объекты
  • 1 个回答
  • 29 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5