简体   繁体   English

C ++-带有链接列表和一次执行一个功能的类的程序

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

I'm writing some simple c++ management program that has a warehouse class with a list of products stored as Linked list. 我正在编写一些简单的c ++管理程序,该程序具有一个仓库类,其中包含作为链接列表存储的产品列表。 it has two problems with the output: 它的输出有两个问题:

  1. Output id/s different (but also similar) to the input 输出id / s与输入不同(但也相似)
  2. there're two different print functions but only one execute when running the program (both of them can run if I commented the other) 有两种不同的打印功能,但是在运行程序时只有一种可以执行(如果我注释了另一种,它们都可以运行)

Since the program compiles without any error, I tried to debug it line by line but can't seem to figure it out 由于该程序编译时没有任何错误,因此我尝试逐行调试它,但似乎无法弄清楚

Edit: To be clear this part of a college project and I can't use stuff ready from standard library like (std::vector, std::list, ...) I need to implement the linked list by hand 编辑:要清楚这是大学项目的这一部分,我无法使用标准库中的东西,如(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;
            }

First: 第一:

    private:
        product* list = new product;

This is strange. 这很奇怪。 Why are you creating a meaningless product and having list point to it? 您为什么要创建无意义的productlist指向?

Next: 下一个:

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

Do you want tmp to point to list or do you want it to point to a new product that you create and allocate? 您想让tmp指向list还是要指向您创建和分配的new product It can do either of these two things, but it can't do both -- it's only one pointer. 它可以做这两件事中的任何一个,但不能两者都做-它只是一个指针。 What do you want it to point to? 您要指向什么?

Next: 下一个:

        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;
            }
        }

You have while(list) , but you want while(tmp) . 您有while(list) ,但是想要while(tmp)

Last: 持续:

        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";
        }

Again, you have tmp point to a new products and then you set it equal to list . 同样,您已将tmp指向new products ,然后将其设置为等于list Do you want tmp to point to the same thing list points to? 您是否希望tmp指向list指向的相同内容? Or do you want it to point to a new product ? 还是您要它指向new product It can't do both. 它不能同时做到。

You again have while(list) when you want while(tmp) . 当您需要while(tmp)时,您又有了while(list) while(tmp)

For reference: 以供参考:

  1. Output id/s different (but also similar) to the input 输出id / s与输入不同(但也相似)

The solution's to simply avoid variables with the first digit 'zero' or maybe convert them back to decimal 解决方案是简单地避免使用具有第一位数字“零”的变量,或者将其转换回十进制

The reason behind this behavior was that compilers consider values that start with '0' as Octal literal! 此行为背后的原因是,编译器将以“ 0”开头的值视为八进制文字! which's why the output was different but not random. 这就是为什么输出不同但不是随机的原因。 I wasn't aware of this "feature" and just wanted all id's to look alike 我不知道这个“功能”,只是希望所有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) 有两种不同的打印功能,但是在运行程序时只有一种可以执行(如果我注释了另一种,它们都可以运行)

Like David's answer, solved just by changing while(list) to while(tmp) which slipped through my mind because I thought there're basically the same. 就像David的回答一样,只需将while(list)更改为while(tmp)即可解决,因为我认为这基本是相同的。

The fixed code is: 固定代码为:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM