zoukankan      html  css  js  c++  java
  • 暴力 C++

    一个面试题。主要复习C++语法与STL和list sort 二元谓语函数编写。

    #include<stdio.h>
    #include<string.h>
    #include<vector>
    #include<algorithm>
    #include<iostream>
    #include<list>
    
    using namespace std;
    
    /*
        authhor: ramanu_jan
    */
    
    /**
    *   the Order class
    */
    class Order{
        public:
             Order(){}
             Order(string order_type, int price, int quantity, string order_id){
                this->order_type = order_type;  this->price = price;
                this->quantity = quantity;      this->order_id = order_id;
             }
             int get_price(){return price;}
             int get_quantity(){return quantity;}
             string get_order_id(){return order_id;}
             string get_order_type(){return order_type;}
             void set_price(int price){this->price = price;}
             void set_quantity(int quantity){this->quantity = quantity;}
             void set_order_id(string order_id){this->order_id = order_id;}
        private:
            string order_id;
            string order_type;
            int price;
            int quantity;
    };
    
    /**
    *   use a buy list and a sell list to save orders for potential trades and PRINT operation.
    *   some string constants
    */
    list<Order> l_buy, l_sell;
    const string BUY("BUY"), SELL("SELL"), CANCEL("CANCEL"),
                 IOC("IOC"), GFD("GFD"), MODIFY("MODIFY"), PRINT("PRINT");
    
    // buy operation
    void buy(Order order){
        list<Order>::iterator itr;
        //check if IOC orders exists, and cancel it when exist.
        for(itr = l_buy.begin(); itr != l_buy.end(); itr++)
            if(itr->get_order_type() == IOC) l_buy.erase(itr);
        //check the trade condition if exists, conducts the trade when exists.
        for(itr = l_sell.begin(); itr != l_sell.end(); itr++){
            if(order.get_price() >= itr->get_price()){      // trade occurs
                int traded_quantity = min(itr->get_quantity(), order.get_quantity());
                cout << "TRADE " << itr->get_order_id() << " " << itr->get_price() << " " << traded_quantity << " "
                                 << order.get_order_id() << " " << order.get_price() << " " << traded_quantity << endl;
                if(order.get_quantity() >= itr->get_quantity()){
                    order.set_quantity(order.get_quantity() - itr->get_quantity());
                    l_sell.erase(itr);
                }
                else{
                    itr->set_quantity(itr->get_quantity() - order.get_quantity());
                    order.set_quantity(0);  break;
                }
            }
        }
        //check if there is IOC type orders in sell queue, cancel them when there's.
        for(itr = l_sell.begin(); itr !=l_sell.end(); itr++)
            if(itr->get_order_type() == IOC) l_sell.erase(itr);
    
        if(order.get_quantity() > 0) l_buy.push_back(order);
    }
    
    // sell operation
    void sell(Order order){
        list<Order>::iterator itr;
        //check if IOC orders exists, and cancel it when exist.
        for(itr = l_sell.begin(); itr != l_sell.end(); itr++)
            if(itr->get_order_type() == IOC) l_sell.erase(itr);
        //check the trade condition if exists, conducts the trade when exists.
        for(itr = l_buy.begin(); itr !=l_buy.end(); itr++){
            if(itr->get_price() >= order.get_price()){
                int traded_quantity = min(itr->get_quantity(), order.get_quantity());
                cout << "TRADE " << itr->get_order_id() << " " << itr->get_price() << " " << traded_quantity << " "
                                 << order.get_order_id() << " " << order.get_price() << " " << traded_quantity << endl;
                if(order.get_quantity() >= itr->get_quantity()){
                    order.set_quantity(order.get_quantity() - itr->get_quantity());
                    l_buy.erase(itr);
                }
                else{
                    itr->set_quantity(itr->get_quantity() - order.get_quantity());
                    order.set_quantity(0);  break;
                }
            }
        }
        //check if there is IOC type orders in buy queue, cancel them when there's.
        for(itr = l_buy.begin(); itr != l_buy.end(); itr++)
            if(itr->get_order_type() == IOC) l_buy.erase(itr);
        if(order.get_quantity() > 0) l_sell.push_back(order);
    }
    
    // sell operation: check the input order id if exists in list sell and buy, cancel that when exists.
    void cancel(string order_id){
        list<Order>::iterator itr;
        for(itr = l_buy.begin(); itr != l_buy.end(); itr++)
            if(order_id == itr->get_order_id()){
                l_buy.erase(itr);   break;
            }
        for(itr = l_sell.begin(); itr != l_sell.end(); itr++)
            if(order_id == itr->get_order_id()){
                l_sell.erase(itr);  break;
            }
    }
    
    //modify operation
    void modify(string order_id, string todo, int price, int quantity){
        list<Order>::iterator itr;
        //check if IOC orders exists, cancel them if exists.
        for(itr = l_sell.begin(); itr != l_sell.end(); itr++) if(itr->get_order_type() == IOC)
            l_sell.erase(itr);
        for(itr = l_buy.begin(); itr != l_buy.end(); itr++) if(itr->get_order_type() == IOC)
            l_buy.erase(itr);
    
        //check sell orders
        for(itr = l_sell.begin(); itr != l_sell.end(); itr++) if(itr->get_order_id() == order_id){
            //when nothing modified, we just do nothing
            //in other cases, we shall check if there is a trade.
            if(todo == SELL && price == itr->get_price() && quantity == itr->get_quantity()) return;
            Order order(itr->get_order_type(), price, quantity, order_id);
            l_sell.erase(itr);
            if(todo == SELL) sell(order);
            else buy(order);
            return;
        }
    
        //check buy orders
        for(itr = l_buy.begin(); itr != l_sell.end(); itr++) if(itr->get_order_id() == order_id){
           if(todo == BUY && price == itr->get_price() && quantity == itr->get_quantity()) return;
            Order order(itr->get_order_type(), price, quantity, order_id);
            l_buy.erase(itr);
            if(todo == BUY) buy(order);
            else sell(order);
            return;
        }
    }
    
    bool cmp(Order x, Order y){
        return x.get_price() > y.get_price();
    }
    
    /**
    *   use backup lists to sort and integrate orders by price and different lists they are in, then print them out.
    */
    void print(){
        list<Order> lbuy(l_buy), lsell(l_sell);
        lbuy.sort(cmp);        lsell.sort(cmp);
        if(lbuy.size() > 1){
            list<Order>::iterator pre = lbuy.begin(), cur = pre;    cur++;
            while(cur != lbuy.end()){
                if(cur->get_price() == pre->get_price()){
                    cur->set_quantity(cur->get_quantity() + pre->get_quantity());
                    lbuy.erase(pre);
                    pre = cur;
                }
                cur++;
            }
        }
    
        if(lsell.size() > 1){
            list<Order>::iterator pre = lsell.begin(), cur = pre;   cur++;
            while(cur != lsell.end()){
                if(cur->get_price() == pre->get_price()){
                    cur->set_quantity(cur->get_quantity() + pre->get_quantity());
                    lsell.erase(pre);
                    pre = cur;
                }
                cur++;
            }
        }
    
        list<Order>::iterator itr;
        cout << "SELL:" << endl;
        for(itr = lsell.begin(); itr != lsell.end(); itr++)
            cout << itr->get_price() << " " << itr->get_quantity() << endl;
        cout << "BUY:" << endl;
        for(itr = lbuy.begin(); itr != lbuy.end(); itr++)
            cout << itr->get_price() << " " << itr->get_quantity() << endl;
    }
    
    int main(){
        l_buy.clear();    l_sell.clear();
        string str;
        while(cin >> str){
            if(str == BUY){
                string order_type, order_id;  int price, quantity;
                cin >> order_type >> price >> quantity >> order_id;
                Order order(order_type, price, quantity, order_id);
                buy(order);
            }
            else if(str == SELL){
                string order_type, order_id;  int price, quantity;
                cin >> order_type >> price >> quantity >> order_id;
                Order order(order_type, price, quantity, order_id);
                sell(order);
            }
            else if(str == CANCEL){
                string order_id;
                cin >> order_id;
                cancel(order_id);
            }
            else if(str == MODIFY){
                string order_id, todo;  int quantity, price;
                cin >> order_id >> todo >> price >> quantity;
                modify(order_id,todo, price, quantity);
            }
            else print();
        }
        return 0;
    }
    

      

  • 相关阅读:
    Android-Universal-Image-Loader学习笔记(两)--LruDiscCache
    linux 多个源文件在编译时会产生一个目标文件
    springMVC 获取本地项目路径 及后整理上传文件的方法
    Cf 444C DZY Loves Colors(段树)
    什么是EF, 和 Entity Framework Demo简单构建一个良好的发展环境
    Mac下一个/usr/include失踪
    ArcGIS 10 破解安装(win7 64位)
    Android ProgressBar 反向进度条/进度条从右到左走
    Java的位运算符具体解释实例——与(&amp;)、非(~)、或(|)、异或(^)
    poj 3273 Monthly Expense (二分)
  • 原文地址:https://www.cnblogs.com/ramanujan/p/5854885.html
Copyright © 2011-2022 走看看