【问题标题】:C++ - Program with a linked list and a class that executes one function at a timeC++ - 具有链表和一次执行一个函数的类的程序
【发布时间】:2018-12-09 04:50:11
【问题描述】:

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

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

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

编辑:为了明确大学项目的这一部分,我不能使用标准库中准备好的东西,比如 (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;
            }

【问题讨论】:

  • 注意 C++ 已经有一个你可能想要使用的链表std::list
  • @MartinYork 不幸的是,这是大学项目的一部分,我不能使用从 std 准备好的东西,我需要手动实现它们
  • 这对大学来说是完全合理的。值得注意的是问题的一部分。

标签: c++ class linked-list octal


【解决方案1】:

第一:

    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)

【讨论】:

    【解决方案2】:

    供参考:

    1. 输出 id/s 与输入不同(但也相似)

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

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

    1. 有两种不同的打印功能,但在运行程序时只执行一个(如果我注释了另一个,它们都可以运行)

    就像大卫的回答一样,只需将 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多