示例#1
0
void populateList (TradeList &BuyListAll, TradeList &SellListAll, string req){
    vector<string> tokenized_string = tokenize (req,'@');
    TradeList::iterator it;
        Scrip s;
        s.ID = tokenized_string[0];
        s.type = tokenized_string[1];
        s.name = tokenized_string[2]; 
        s.rate = ConvertStringToNumber(tokenized_string[3]);
        s.num = ConvertStringToNumber(tokenized_string[4]);
        vector<string> expiryTime = tokenize(tokenized_string[5],'$');
        s.expiry = expiryTime[1];
        s.date = expiryTime[0];
    
    if ( (s.type).compare("B") == 0 ){
        cout << "entering this part B" <<endl;
        //PrintScrip(s);
        it = BuyListAll.find(s.name);
        if ( it != BuyListAll.end() ) // key exists already and needs to be updated
            (it -> second).push_back(s);
        else{
            ScripList SL;
            SL.push_back(s);
            BuyListAll[s.name] = SL;
        }
            
            
            
        //BuyListAll[s.name] = s;
    }
    else{
        cout << "entering this part S" <<endl;
        //PrintScrip(s);
        it = SellListAll.find(s.name);
        if ( it != SellListAll.end() ) // key exists already and needs to be updated
            (it -> second).push_back(s);
        else{
            ScripList SL;
            SL.push_back(s);
            SellListAll[s.name] = SL;
        }
        //SellListAll[s.name] = s;
    }
        
}
示例#2
0
void doTransaction ( TradeList &BuyListAll, TradeList &SellListAll ){

    TradeList::iterator buy,sell;
    bool tradeflag = true;
    
    for ( sell = SellListAll.begin() ; sell != SellListAll.end() ; sell++ ){
        buy = BuyListAll.find(sell->first);

        if ( buy != BuyListAll.end() ){ // there is a buyer

            ScripList blist = buy->second;
            ScripList slist = sell->second;
            ScripList::iterator b1,s1;
            s1 = slist.begin();//check the first sale item

                for ( b1 = blist.begin(); b1 != blist.end() && tradeflag ; b1++ ){//check the buyer list entirely
                    if ( (*b1).rate > (*s1).rate ){

                        if ( (*b1).num >= (*s1).num ){//all sold
                            // *************print to file*************** //
                            PrintScrip(*s1); 
                            PrintScrip(*b1); 
                            (*b1).num -= (*s1).num;
                            (sell->second).erase(s1);
                            if ( (*b1).num == 0 )
                                (buy->second).erase(b1);
                            tradeflag = false;
                            }
                        if ( (*b1).num < (*s1).num ){//partially sold
                            // *************print to file*************** //
                            PrintScrip(*s1); 
                            PrintScrip(*b1); 
                            (*s1).num -= (*b1).num;
                            (buy->second).erase(b1);
                        }
                    }
                }
                
        }
    }
}