C ++:将数据存储在另一个链表中的链表中

时间:2018-10-14 13:16:54

标签: c++ linked-list

我想问一下如何将Opt的链接列表存储在CustomerData的链接列表中?我现在使用的方法是分别创建两个链接列表。当我需要打印出我拥有的所有收据时,我需要使用if语句(optical_data.currentOpticalTrace == customer_data.currentCustomerTrace)才能打印出收据。但是,每个客户实际上并未将Optical_data保留在customer_data中。此外,每次客户循环时,光节点都需要从第一个节点运行到最后一个节点。如何使代码更高效并减少可重复性?

    #include<iostream>
    #include <string>
    #include <iomanip>
    using namespace std;


    struct Frame
    {
        int index;
        string itemCode;
        string name;
        int quantity;
        double price;

    };

    struct CustomerData
    {
        string customerName;
        int currentCustomerTrace;

    };


    struct CustomerDataNode
    {
        CustomerData customer_data;
        CustomerDataNode *next;
        CustomerDataNode *c_head, *c_tail;


    };

    struct Opt
    {
        string SitemCode;
        string Sname;
        double Sprice;
        int Squantity;
        int currentOpticalTrace;
    };

    struct OptNode
    {
        Opt optical_data;
        OptNode *next;
        OptNode *o_head, *o_tail;
    };

    class PurchaseSell
    {
        private:        
            int customerTrace;
            int opticalTrace;
            Frame frame;    
            CustomerDataNode cNode;
            OptNode oNode;
            CustomerData customer;
            Opt opt;

        public:
            PurchaseSell();
            void mainMenu_Sell();
            void addOrder();
            void customerCreateNode();
            void opticalCreateNode();
            void printReceipt();
            void printAllReceipt();
            Opt getOrder(Opt,string, string, double, int);
            void viewFrame();
    };



    PurchaseSell::PurchaseSell()
    {
        customerTrace = 0;
        opticalTrace = 0;
        cNode.c_head = NULL;
        cNode.c_tail = NULL;
        oNode.o_head = NULL;
        oNode.o_tail = NULL;

        frame.index=1;
        frame.itemCode="F0001";
        frame.name="FRAME 1";
        frame.price=50.00;
        frame.quantity=20;


    }

    void PurchaseSell::mainMenu_Sell()
    {

        int optS;
        bool inputSell=true;

        system("cls");
        cout<<"\t\t\t************   WELCOME TO CLEAR VISION OPTICAL SHOP    ************"<<endl;
        cout<<endl<<"\t\t\t\t\t\t Sell Menu"<<endl<<endl;
        cout<<"\t\t\t\t\t 1. Make New Order"<<endl;
        cout<<"\t\t\t\t\t 2. Print All Receipt"<<endl;
        cout<<"\t\t\t************   *************************************   ************"<<endl<<endl;

        cout<<"Please enter your option"<<endl;
        cin>>optS;

        switch(optS)
        {
            case 1: 
                addOrder();
                break;

            case 2:
                printAllReceipt();
                break;

            default:
                cout<<"Invalid input.";
        }
    }


    void PurchaseSell::customerCreateNode()
    {
        CustomerDataNode *ctemp = new CustomerDataNode;
        ctemp->customer_data = customer;
        ctemp->next = NULL;
        if(cNode.c_head==NULL)
        {
            cNode.c_head=ctemp;
            cNode.c_tail=ctemp;
            ctemp=NULL;
        }
        else
        {
            cNode.c_tail->next=ctemp;
            cNode.c_tail=ctemp;
        }
    }

    void PurchaseSell::opticalCreateNode()
    {
        OptNode *otemp = new OptNode;

        otemp->optical_data = opt;
        otemp->next = NULL;
        if(oNode.o_head==NULL)
        {
            oNode.o_head=otemp;
            oNode.o_tail=otemp;
            otemp=NULL;
        }
        else
        {
            oNode.o_tail->next=otemp;
            oNode.o_tail=otemp;
        }
    }

    Opt PurchaseSell::getOrder(Opt o, string itemCode, string name, double price, int Squantity)
    {
        o.SitemCode = itemCode ;
        o.Sname = name ;
        o.Sprice = price ;
        o.Squantity = Squantity ;

        return o;
    }

    void PurchaseSell::viewFrame()
    {
        cout<<"\t\t\t************       GLASS FRAME LIST    ************"<<endl<<endl;
        cout << "Index" << setw(20) << "Item Code" << setw( 20 )  << "Name" << setw( 20 ) << "Quantity" << setw( 20 ) << "Price(RM)" << setw( 20 ) << endl;


        cout << setw( 5 )<< 1 << setw( 20 ) << frame.itemCode << setw( 20 ) << frame.name<< setw( 20 ) << frame.quantity << setw( 20 ) << frame.price << setw( 20 ) <<endl<<endl;
    }

    void PurchaseSell::addOrder()
    {
            int choice;
            int quantityS;
            int quantityOptical;
            int pairs = 0;                  

            system("cls");
            cout<<"Please enter the customer name:  "<<endl;
            cin>> customer.customerName;
            opticalTrace = opticalTrace +1;

            viewFrame();

            do{
                cout<<"Please enter quantity to buy:\n" <<endl;
                cin>>quantityS;

                quantityOptical = frame.quantity-quantityS;
                frame.quantity=quantityOptical ;

                opt=getOrder(opt, frame.itemCode, frame.name, frame.price*quantityS, quantityS);
                opt.currentOpticalTrace=opticalTrace;
                opticalCreateNode();                        

                cout<<"\n\nPlease enter your option\n"; 
                cout<<"1) Continue to add on other optical type\n";
                cout<<"2) Complete order\n" ;
                cin>>choice;


                if(choice==1)
                {
                    pairs = pairs+1;

                    if(pairs==4)
                    {
                        cout<<"Customer has reach the order maximum 4 pairs of optical orders regardless the optical type."<<endl<<endl;
                        customerTrace= customerTrace+1;
                        customer.currentCustomerTrace=customerTrace;
                        customerCreateNode();
                        printReceipt();

                    }
                }
                else
                {   customerTrace= customerTrace+1;
                    customer.currentCustomerTrace=customerTrace;
                    customerCreateNode();
                    printReceipt();

                }


            }while(choice==1 && !(pairs >=4) );


    }

    void PurchaseSell::printReceipt()
    {
            system("cls");
            CustomerDataNode *ctemp = new CustomerDataNode;
            OptNode *otemp = new OptNode;
            ctemp = cNode.c_head;
            otemp = oNode.o_head;
            while(ctemp!=NULL)
            {
                if(ctemp->customer_data.currentCustomerTrace==customerTrace)
                {   
                    cout<<"CurrentCustomerTrace:" <<  ctemp->customer_data.currentCustomerTrace<<endl;
                    cout<<"Customer Name:" << ctemp->customer_data.customerName<<endl;

                    cout << "Item Code" << setw( 20 )  << "Name" << setw( 20 ) << "Quantity" << setw( 20 ) << "Amount(RM)" << endl;
                    while(otemp!=NULL)
                    {
                        if(otemp->optical_data.currentOpticalTrace == opticalTrace)
                        {
                            cout << otemp->optical_data.SitemCode << setw( 20 ) 
                            <<otemp->optical_data.Sname << setw( 20 ) << otemp->optical_data.Squantity << setw( 20 ) << otemp->optical_data.Sprice <<endl<<endl;
                        }
                        otemp = otemp->next;    
                    }   
                }
                ctemp = ctemp->next;
            }
            system("PAUSE");

    }

    void PurchaseSell::printAllReceipt()
    {
            CustomerDataNode *ctemp = new CustomerDataNode;
            OptNode *otemp = new OptNode;
            ctemp = cNode.c_head;
            otemp = oNode.o_head;
            while(ctemp!=NULL)
            {   
                    cout<<"CurrentCustomerTrace:" <<  ctemp->customer_data.currentCustomerTrace<<endl;
                    cout<<"Customer Name:" << ctemp->customer_data.customerName<<endl;

                    cout << "Item Code" << setw( 20 )  << "Name" << setw( 20 ) << "Quantity" << setw( 20 ) << "Amount(RM)" << endl;
                            otemp = oNode.o_head;

                    while(otemp!=NULL)
                    {
                        if(otemp->optical_data.currentOpticalTrace == ctemp->customer_data.currentCustomerTrace)
                        {
                            cout << otemp->optical_data.SitemCode << setw( 20 ) <<otemp->optical_data.Sname 
                                << setw( 20 ) << otemp->optical_data.Squantity << setw( 20 ) << otemp->optical_data.Sprice <<endl<<endl;
                        }
                        otemp = otemp->next;    
                    }   

                ctemp = ctemp->next;
            }
            system("PAUSE");
    }

    int main()
    {

        PurchaseSell p;
        while(true)
        {
            p.mainMenu_Sell();
        }
    }

0 个答案:

没有答案