首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >库存系统代码库

库存系统代码库
EN

Code Review用户
提问于 2019-07-07 00:14:37
回答 1查看 124关注 0票数 1

我在这里找一个关于我目前的代码库的评论。

请:

  • 忽略使用命名空间std;这主要是出于教育目的,因此the使用命名空间std;对于这个代码基并不重要,因为它是一个为教育目的而拼凑在一起的项目。
  • 在下面我将指出的代码的某些部分Look,如果可能的话,查看其他部分,给我反馈,并告诉我如何可能改进/展示我。

在我的InventorySystem中,有一些代码可以读入如下结构的文本文件:

代码语言:javascript
复制
{
123,
A piece of cheese.,
10.19,
4,
}

{
321,
A country flag,
10.00,
1,
}

具体来说,它有Packet.h的结构,如下所示。

此代码:

代码语言:javascript
复制
string line, line2, line3, line4;
int num1;
double num2;
int num3;
while (getline(inFile, line)) {
    getline(inFile, line);
    line = line.substr(0, line.size() - 1);
    num1 = stoi(line);
    getline(inFile, line2);
    line2 = line2.substr(0, line2.size() - 1);
    getline(inFile, line3);
    line3 = line3.substr(0, line3.size() - 1);
    num2 = stod(line3);
    getline(inFile, line4);
    line4 = line4.substr(0, line4.size() - 1);
    num3 = stoi(line4);
    Packet importPacket(num1, line2, num2, num3);
    inventorySystem.insert(importPacket);
    getline(inFile, line);
    getline(inFile, line);
}
inFile.close();

int main()的开头,Inventory.cpp应该逐段阅读。但是看看代码,正如你所看到的,它看起来很笨重。你有什么改进的建议吗?注意,它删除了在每个属性末尾看到的",“,这样它就可以读取iddescriptionpricepartCount of Packet.h

还有这部分

代码语言:javascript
复制
else if (choice == 'a') {
    cout << "Archiving all the information." << endl;
    Sleep(1000);
    cout << "Archiving all the information.." << endl;
    Sleep(1000);
    cout << "Archiving all the information..." << endl;
    vector  getPackets = inventorySystem.extract();

    bOutFile.open("C:\\...Put_Your_Path_Here.dat", ios::out | ios::binary);
    for (int i = 0; i < getPackets.size(); ++i) {
        bOutFile.write((char*)&getPackets[i], sizeof(getPackets[i]));
    }
    bOutFile.close();
}

为了存档到二进制.dat,我使用二进制搜索树上的提取函数来获取其所有内容,并将其放入向量中,以便存档到.dat中。我不确定这是否是个好办法。还能改进吗?我仍然想提取,这样我就不必编写更多的代码了。为了提取BST的内容,这段代码还使用了大量的前置遍历。是很重复的。这段代码可以在int ()的if...choice“a”中找到。下面是如何从BST.cpp执行的:

代码语言:javascript
复制
vector  BST::extract() const {
    vector  result;

    if (root != nullptr) {
        extract(root, result);
    }

    return result;
}

void BST::extract(const Node *p, vector  &result) const {
    if (p != nullptr) {
        result.push_back(p->data);
        extract(p->llink, result);
        extract(p->rlink, result);
    }
}

它是递归的。就这样。但是如果你能看到代码库的其他部分,那也会很棒的!

这一部分用于<#>reference:

Inventory.cpp:

代码语言:javascript
复制
#include 
#include 
#include 
#include "windows.h" 

#include "BST.h"
#include "Packet.h"

using namespace std;

/*
    PURPOSE: Process inventory information for a parts warehouse.
    1. Read from txt. file used for storing information between sessions.
        *Txtfile can be empty.
    2. Pop up 5 choices in main menu: enter a new part to system, find and print data for a part when given the part ID number,
    and find and modify data for a part when given the part ID number, copy all information to a binary archive file, and quit.
*/

int main() {
    BST inventorySystem;
    ifstream inFile("C:\\...Put_Your_Path_Here.txt");
    ofstream outFile;
    fstream bOutFile;

    if (!inFile) {
        cerr << "ERROR: Unable to open the text file." << endl;
    }
    else {
        string line, line2, line3, line4;
        int num1;
        double num2;
        int num3;
        while (getline(inFile, line)) {
            getline(inFile, line);
            line = line.substr(0, line.size() - 1);
            num1 = stoi(line);
            getline(inFile, line2);
            line2 = line2.substr(0, line2.size() - 1);
            getline(inFile, line3);
            line3 = line3.substr(0, line3.size() - 1);
            num2 = stod(line3);
            getline(inFile, line4);
            line4 = line4.substr(0, line4.size() - 1);
            num3 = stoi(line4);
            Packet importPacket(num1, line2, num2, num3);
            inventorySystem.insert(importPacket);
            getline(inFile, line);
            getline(inFile, line);
        }
        inFile.close();


        char choice = 'z';

        while (choice != 'q') {
            cout << "-----------------------------------------------------" << endl;
            cout << "Program successfully loaded..." << endl;
            cout << "Welcome to the main menu..." << endl;
            cout << "-----------------------------------------------------" << endl;
            cout << "N: Enter new part into the system >>" << endl;
            cout << "F: Find a particular part >>" << endl;
            cout << "A: Archive the information >>" << endl;
            cout << "Q: Quit >>" << endl;
            cout << "Input your choice: ";
            cin >> choice;
            choice = tolower(choice);

            if (choice == 'n') {
                cout << "Enter the new part's I.D.: ";
                int partId = -1;
                cin >> partId;
                cin.ignore();; // Flushes the input stream and removes the new line(s) at the end of the stream for the upcoming getline.
                cout << "Enter a short description for this new part: ";
                string description = "";
                getline(cin, description);
                cout << "Enter the price of this new part: $";
                double price = 0.00;
                cin >> price;
                cout << "Enter how many parts the warehouse currently has: "; 
                int partCount = 0;
                cin >> partCount;

                if (partId >= 0 && price >= 0 && partCount >= 0) {
                    Packet packet(partId, description, price, partCount);
                    inventorySystem.insert(packet);
                    cout << "Attempted to enter the part into the SYSTEM." << endl;
                }
                else {
                    cout << "One or more inputs are invalid. You will be prompted back to the menu. Please enter a valid input!" << endl;
                    cout << "Usage: PartID should be greater than or equal to 0, price should be similar, and vice versa!" << endl;
                }
            }
            else if (choice == 'f') {
                cout << "Enter the part I.D. that you want to search the database for: ";
                int partId = -1;
                cin >> partId;
                Packet* getPacket = inventorySystem.search(partId);

                if (getPacket != nullptr) {
                    cout << "{" << endl;
                    cout << "I.D.: " << getPacket->getPartId() << endl;
                    cout << "Description: " << getPacket->getDescription() << endl;
                    cout << "Price: " << getPacket->getPrice() << endl;
                    cout << "Part Count: " << getPacket->getPartCount() << endl;
                    cout << "}" << endl;
                }
                else {
                    cout << "ERROR: System could not find the following I.D. as part of the inventory system." << endl;
                }
            }
            else if (choice == 'a') {
                cout << "Archiving all the information." << endl;
                Sleep(1000);
                cout << "Archiving all the information.." << endl;
                Sleep(1000);
                cout << "Archiving all the information..." << endl;
                vector  getPackets = inventorySystem.extract();

                bOutFile.open("C:\\...Put_Your_Path_Here.dat", ios::out | ios::binary);
                for (int i = 0; i < getPackets.size(); ++i) {
                    bOutFile.write((char*)&getPackets[i], sizeof(getPackets[i]));
                }
                bOutFile.close();
            }
        }

        vector  getPackets = inventorySystem.extract();

        outFile.open("C:\\...Put_Your_Path_Here.txt");
        for (int i = 0; i < getPackets.size(); ++i) {
            outFile << "{" << endl;
            outFile << getPackets[i]->getPartId() << "," << endl;
            outFile << getPackets[i]->getDescription() << "," << endl;
            outFile << getPackets[i]->getPartCount() << "," << endl;
            outFile << getPackets[i]->getPrice() << "," << endl;
            outFile << "}" << endl << endl;
        }
        outFile.close();
    }
    system("pause");
}

BST.h:

代码语言:javascript
复制
#pragma once

#include "Packet.h"
#include 

using namespace std;

class BST {
    struct Node {
        Node() : rlink(nullptr), llink(nullptr) {};
        ~Node() {};

        Packet *data;
        Node *rlink, *llink;
    };
public:
    BST();
    void insert(Packet &p);
    void insert(Node *&p, Node *newNode);
    Packet* search(int id);
    vector  extract() const;
    void preorderTraversal() const;
    void destroyTree();
    ~BST();
private:
    Node * root;
    void destroyTree(Node *&p);
    Packet* search(const Node *p, int id);
    void extract(const Node *p, vector  &result) const;
    void preorderTraversal(const Node *p) const;
};

BST.cpp:

代码语言:javascript
复制
#include "BST.h"
#include 

BST::BST() : root(nullptr) {}

void BST::insert(Packet &p) {
    if (search(p.getPartId()) == nullptr) {
        Node *newNode = new Node;
        newNode->data = new Packet(p);
        insert(root, newNode);
    }
    else {
        cout << "Insertion failed because such packet has already been found to exist." << endl;
    }
}

void BST::insert(Node *&p, Node *newNode) {
    if (p == nullptr) {
        p = newNode;
    }
    else if (p->data->getPartId() > newNode->data->getPartId()) {
        insert(p->llink, newNode);
    }
    else {
        insert(p->rlink, newNode);
    }
}

Packet* BST::search(int id) {
    return search(root, id);
}

Packet* BST::search(const Node *p, int id) {
    if (p == nullptr) {
        return nullptr;
    }
    else if (p->data->getPartId() == id) {
        return p->data;
    }
    else if (p->data->getPartId() < id) {
        return search(p->rlink, id);
    }

    return search(root->llink, id);
}

vector  BST::extract() const {
    vector  result;

    if (root != nullptr) {
        extract(root, result);
    }

    return result;
}

void BST::extract(const Node *p, vector  &result) const {
    if (p != nullptr) {
        result.push_back(p->data);
        extract(p->llink, result);
        extract(p->rlink, result);
    }
}

void BST::preorderTraversal() const {
    if (root == nullptr) {
        cerr << "There is no tree.";
    }
    else {
        preorderTraversal(root);
    }
}

void BST::preorderTraversal(const Node *p) const {
    if (p != nullptr) {
        cout << p->data->getPartId() << " ";
        preorderTraversal(p->llink);
        preorderTraversal(p->rlink);
    }
}

void BST::destroyTree(Node *&p) {
    if (p != nullptr) {
        destroyTree(p->llink);
        destroyTree(p->rlink);
        delete p;
        p = nullptr;
    }
}

void BST::destroyTree() {
    destroyTree(root);
}

BST::~BST() {
    destroyTree(root);
}

Packet.h:

代码语言:javascript
复制
#pragma once
#include 

using namespace std;

class Packet {
    public:
        Packet(int partId, string description, double price, int partCount) : 
            partId(partId), description(description), price(price), partCount(partCount) {}
        int getPartId() const {return partId;}
        string getDescription() const {return description;}
        double getPrice() const {return price;}
        int getPartCount() const {return partCount;}

    private:
        int partId;
        string description;
        double price;
        int partCount;
};
EN

回答 1

Code Review用户

发布于 2019-07-07 17:23:29

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/223650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档