C++ - 带有链表和一次执行一个函数的 class 的程序

C++ - Program with a linked list and a class that executes one function at a time

我正在编写一些简单的 C++ 管理程序,它有一个仓库 class,其中包含存储为链表的产品列表。 它的输出有两个问题:

  1. 输出id/s与输入
  2. 不同(但也相似)
  3. 有两个不同的打印函数,但只有一个在 运行 运行程序时执行(如果我注释了另一个,它们都可以 运行)

由于程序编译没有任何错误,我试图逐行调试它,但似乎无法弄清楚

编辑:要明确大学项目的这一部分,我不能使用标准库中准备好的东西,比如 (std::vector, std::list, ...) 我需要实现链接手工列出

    #include <iostream>
    #include <iomanip>      // std::setw

            struct product {
                int id;
                int num;
                product* next;
            };

            class warehouse{
            private:
                product* list = new product;
            public:
                warehouse() = default;

                //adding a product to warehouse
                void AddProduct(const int id,const int boxes) {
                    auto* item = new product;
                    auto* tmp = new product;
                    // copy the head of the linked list
                    tmp = list;
                    item->id = id;
                    item->num = boxes;
                    item->next = tmp;
                    //add the the new product at the beginning
                    list = item;
                }

                //print all products
                void printlist() {
                    int i=0;
                    product* tmp;
                    tmp = list;
                    while(list) {
                        i++;
                        std::cout << "item n." << i << "\tid: " << tmp->id << " number of items: " << tmp->num << std::endl;
                        tmp = tmp -> next;
                    }
                }

                //print products that have less than 50 box and need resupply
                void SupplyReport(){
                    product* tmp = new product;
                    tmp = list;
                    int i=0;
                    while(list) {
                        if (tmp->num <= 50) {
                            i++;
                            std::cout << i << ". id:" << tmp->id << std::setw(20) << "N. of Boxes:" << tmp->num << std::endl;
                        }
                        tmp = tmp -> next;
                    }
                    if (i==0)
                        std::cout << "No product/s need re-supply";
                }
            };

            int main(){
                /* Problems:
                 * Generating random id instead of using the given values
                 * Execute only one function at a time meaning if I commented printlist it's print the supply report as expected
                 */
                warehouse w1;
                w1.AddProduct(005,50);
                w1.AddProduct(007,70);
                w1.AddProduct(055,30);
                w1.printlist();
                w1.SupplyReport();
                return 0;
            }

第一个:

    private:
        product* list = new product;

这很奇怪。你为什么要创建一个无意义的 product 并让 list 指向它?

下一个:

            auto* tmp = new product;
            // copy the head of the linked list
            tmp = list;

您希望 tmp 指向 list 还是指向您创建和分配的 new product?它可以做这两件事中的任何一件,但不能同时做这两件事——它只是一个指针。你想让它指向什么?

下一个:

        void printlist() {
            int i=0;
            product* tmp;
            tmp = list;
            while(list) {
                i++;
                std::cout << "item n." << i << "\tid: " << tmp->id << " number of items: " << tmp->num << std::endl;
                tmp = tmp -> next;
            }
        }

你有 while(list),但你想要 while(tmp)

最后一个:

        void SupplyReport(){
            product* tmp = new product;
            tmp = list;
            int i=0;
            while(list) {
                if (tmp->num <= 50) {
                    i++;
                    std::cout << i << ". id:" << tmp->id << std::setw(20) << "N. of Boxes:" << tmp->num << std::endl;
                }
                tmp = tmp -> next;
            }
            if (i==0)
                std::cout << "No product/s need re-supply";
        }

同样,您 tmp 指向 new products,然后将其设置为等于 list。你想让 tmp 指向相同的东西 list 指向吗?或者你想让它指向 new product 吗?它不能两者兼顾。

当你想要 while(tmp) 时,你又得到了 while(list)

供参考:

  1. Output id/s different (but also similar) to the input

解决方案是简单地避免使用第一个数字 'zero' 的变量,或者可能将它们转换回十进制

此行为背后的原因是编译器将以“0”开头的值视为八进制文字!这就是为什么输出不同但不是随机的。我不知道这个 "feature",只是希望所有 ID 看起来都一样

  1. there're two different print functions but only one execute when running the program (both of them can run if I commented the other)

就像 David 的回答一样,通过将 while(list) 更改为 while(tmp) 解决了这个问题,因为我认为它们基本相同。

固定码为:

#include <iostream>
#include <iomanip>      // std::setw

struct product {
    int id;
    int num;
    product* next;
};

class warehouse{
private:
    product* list;
public:

    warehouse() = default;

    //adding a product to warehouse
    void AddProduct(const int id,const int boxes) {
        auto* item = new product;
        product* tmp = list;
        item->id = id;
        item->num = boxes;
        item->next = tmp;
        //add the the new product at the beginning
        list = item;
    }

    //print all products
    void printlist() {
        int i=0;
        product* tmp= list;
        while(tmp) {
            i++;
            std::cout << "item n." << i << "\tid: " << tmp->id << std::setw(20) << " number of items: " << tmp->num << std::endl;
            tmp = tmp -> next;
        }
    }

    //print products that have less than 50 box and need resupply
    void SupplyReport(){
        product* tmp = list;
        int i=0;
        while(tmp) {
            if (tmp->num <= 50) {
                i++;
                std::cout << i << ". id:" << tmp->id << std::setw(20) << "N. of Boxes:" << tmp->num << std::endl;
            }
            tmp = tmp -> next;
        }
        if (i==0)
            std::cout << "No product/s need re-supply";
    }
};

int main(){
    warehouse w1{};
    w1.AddProduct(5,50);
    w1.AddProduct(7,70);
    w1.AddProduct(55,30);
    w1.printlist();
    w1.SupplyReport();
    return 0;
}