fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <list>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7. struct OrderNode
  8. {
  9. int order_id;
  10. char type; // B: 买,S:卖
  11. int num; //股票数量
  12. int price; //价格
  13. OrderNode(int order_id, char type, int num, int price) : order_id(order_id), type(type), num(num), price(price) {}
  14. };
  15. using OrderList = list<OrderNode>;
  16. using PriceMap = map<int, OrderList>;
  17. using OrderMap = map<int, OrderNode>;
  18.  
  19. struct Context
  20. {
  21. PriceMap buy_map;
  22. PriceMap sell_map;
  23. OrderMap order_map;
  24. void print() const
  25. {
  26. cout << "buy list:" << endl;
  27. for (auto it : buy_map)
  28. {
  29. cout << it.first << ": ";
  30. for (auto i : it.second)
  31. {
  32. cout << i.num << ", ";
  33. }
  34. cout << endl;
  35. }
  36. cout << "sell list:" << endl;
  37. for (auto it : sell_map)
  38. {
  39. cout << it.first << ": ";
  40. for (auto i : it.second)
  41. {
  42. cout << i.num << ", ";
  43. }
  44. cout << endl;
  45. }
  46. }
  47. };
  48.  
  49. class ExeOrder
  50. {
  51. public:
  52. // ExeOrder() {};
  53. virtual bool Exe(int order_id, char type, int num, int price) = 0;
  54. };
  55.  
  56. class AddOrder : public ExeOrder
  57. {
  58. public:
  59. AddOrder(Context *ctx) : ctx_(ctx) {}
  60. bool Exe(int order_id, char type, int num, int price)
  61. {
  62. PriceMap *price_map;
  63. if (deal(type, num, price))
  64. {
  65. cout << "deal done :"
  66. << "A," << order_id << "," << type << "," << num << "," << price << endl;
  67. cout <<"1050 ::"<< &ctx_->sell_map[1050].front().num<<endl;
  68. return true;
  69. }
  70. if (type == 'B')
  71. {
  72. price_map = &ctx_->buy_map;
  73. }
  74. else
  75. {
  76. price_map = &ctx_->sell_map;
  77. }
  78. OrderNode item(order_id, type, num, price);
  79. if (price_map->find(price) == price_map->end())
  80. {
  81. price_map->insert(make_pair(price, OrderList()));
  82. }
  83. price_map->at(price).push_back(item);
  84. ctx_->order_map.insert(make_pair(order_id, item));
  85. return true;
  86. }
  87. bool deal(char type, int &num, int price)
  88. {
  89. PriceMap *price_map;
  90. if (type == 'B')
  91. {
  92. price_map = &ctx_->sell_map;
  93. }
  94. else
  95. {
  96. price_map = &ctx_->buy_map;
  97. }
  98. if (price_map->find(price) == price_map->end())
  99. {
  100. return false;
  101. }
  102. OrderList* order_list = &price_map->at(price);
  103. for (auto it : *order_list)
  104. {
  105. if (it.num > num)
  106. {
  107. it.num = it.num - num;
  108. cout << "deal done :"
  109. << "aaa," << it.order_id << "," << type << "," << num << "," << price << endl;
  110. num = 0;
  111. break;
  112. }
  113. if (it.num == num)
  114. {
  115. cout << "deal done :"
  116. << "bbb," << it.order_id << "," << type << "," << num << "," << price << endl;
  117. ctx_->order_map.erase(it.order_id);
  118. order_list->pop_front();
  119. if (order_list->size() == 0)
  120. {
  121. price_map->erase(price);
  122. }
  123. num = 0;
  124. break;
  125. }
  126. num -= it.num;
  127. ctx_->order_map.erase(it.order_id);
  128. order_list->pop_front();
  129. }
  130. if (num)
  131. {
  132. return false;
  133. }
  134. cout <<"testbbb"<<order_list->size()<<endl;
  135. return true;
  136. }
  137. Context *ctx_;
  138. };
  139.  
  140. class DelOrder : public ExeOrder
  141. {
  142. public:
  143. DelOrder(Context *ctx) : ctx_(ctx) {}
  144. bool Exe(int order_id, char type, int num, int price)
  145. {
  146. PriceMap *price_map;
  147. if (type == 'B')
  148. {
  149. price_map = &ctx_->buy_map;
  150. }
  151. else
  152. {
  153. price_map = &ctx_->sell_map;
  154. }
  155. if (ctx_->order_map.find(order_id) == ctx_->order_map.end())
  156. {
  157. cout << "delete cmd ("
  158. << "X," << order_id << "," << type << "," << num << "," << price << ") failed" << endl;
  159. return false;
  160. }
  161. auto it = ctx_->order_map.at(order_id);
  162. if (it.num == num && it.price == price && it.type == type)
  163. {
  164. price_map->at(price).remove_if([num, price, type](OrderNode it){
  165. if (it.num == num && it.price == price && it.type == type)
  166. {
  167. return true;
  168. }
  169. return false;
  170. });
  171. ctx_->order_map.erase(order_id);
  172. cout << "delete cmd ("
  173. << "X," << order_id << "," << type << "," << num << "," << price << ") success" << endl;
  174. return true;
  175. }
  176. cout << "delete cmd ("
  177. << "X," << order_id << "," << type << "," << num << "," << price << ") failed" << endl;
  178. return false;
  179. }
  180. Context *ctx_;
  181. };
  182. using ExeMap = std::map<char, ExeOrder *>;
  183. void registerCommand(ExeMap& m_exe_map_, Context* m_ctx_)
  184. {
  185. m_exe_map_.insert(make_pair('A', new AddOrder(m_ctx_)));
  186. m_exe_map_.insert(make_pair('X', new DelOrder(m_ctx_)));
  187. }
  188. class ExeManage
  189. {
  190. public:
  191. ExeManage()
  192. {
  193. registerCommand(m_exe_map_, &m_ctx_);
  194. }
  195. ~ExeManage()
  196. {
  197. for (auto it : m_exe_map_)
  198. {
  199. delete it.second;
  200. }
  201. m_exe_map_.clear();
  202. }
  203. void do_it(const string &cmd)
  204. {
  205. char cmd_type = '\0';
  206. int order_id = 0;
  207. char type = '\0';
  208. int num = 0;
  209. int price = 0;
  210. sscanf(cmd.c_str(), "%c,%d,%c,%d,%d", &cmd_type, &order_id, &type, &num, &price);
  211. printf("%c,%d,%c,%d,%d\n", cmd_type, order_id, type, num, price);
  212. if (m_exe_map_.find(cmd_type) == m_exe_map_.end())
  213. {
  214. cout << "command error11, " << cmd << endl;
  215. return;
  216. }
  217. m_exe_map_[cmd_type]->Exe(order_id, type, num, price);
  218. cout<<"test"<<m_ctx_.sell_map.size()<<endl;
  219. }
  220. void print()
  221. {
  222. m_ctx_.print();
  223. }
  224.  
  225. private:
  226. Context m_ctx_;
  227. ExeMap m_exe_map_;
  228. };
  229. int main()
  230. {
  231. string s[] = {"A,100000,S,1,1075","A,100001,B,9,1000","A,100002,B,30,975","A,100003,S,10,1050","A,100004,B,10,950","A,100005,S,2,1025",
  232. "A,100006,B,1,1000","X,100004,B,10,950","A,100007,S,5,1025","A,100008,B,3,1050","X,100008,B,3,1050","X,100005,S,2,1025"};
  233. vector<string> input(s, s + 12);
  234. ExeManage exe;
  235. for (auto it : input)
  236. {
  237. exe.do_it(it);
  238. }
  239. exe.print();
  240. }
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
A,100000,S,1,1075
test1
A,100001,B,9,1000
test1
A,100002,B,30,975
test1
A,100003,S,10,1050
test2
A,100004,B,10,950
test2
A,100005,S,2,1025
test3
A,100006,B,1,1000
test3
X,100004,B,10,950
delete cmd (X,100004,B,10,950) success
test3
A,100007,S,5,1025
test3
A,100008,B,3,1050
deal done :aaa,100003,B,3,1050
testbbb1
deal done :A,100008,B,0,1050
1050 ::0x62e698
test3
X,100008,B,3,1050
delete cmd (X,100008,B,3,1050) failed
test3
X,100005,S,2,1025
delete cmd (X,100005,S,2,1025) success
test3
buy list:
950: 
975: 30, 
1000: 9, 1, 
sell list:
1025: 5, 
1050: 10, 
1075: 1,