注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Write a Matching Engine
you're going to write an exchange order matching engine.
The input is from stdin, each line file has several columns separated by one space.
The first column specifies the operation to be done. Our supported operations are:
BUY
SELL
CANCEL
MODIFY
PRINT
If the first column is BUY or SELL, then this line has five columns in total:
The second column is the order type, it's either IOC or GFD.
The third column is the price you want to buy or sell, it's an integer.
The fourth column shows the quantity of that buy or sell, it's an integer. Both the price and quantity are positive numbers.
The fifth column is the order id, it can be arbitrary words.
If the first column is CANCEL, then this line has two columns in total:
The second column is the order id, it means that order needs to be deleted, it can't be traded anymore.
If that order id doesn't exist, just do nothing.
If the first column is MODIFY, then this line has five columns in total:
The second column is the order id, it means that order needs to be modified.
The third column is either BUY or SELL.
The fourth column is the new price of that order.
The fifth column is the new quantity of that order.
If that order id doesn't exist, just do nothing.
(Note: that we can't modify an IOC order type, as we'll mention later.)
MODIFY is a powerful operation, e.ger1 1000 10 order3 900 10
We guarantee that:
order id is unique.
Example 1:
BUY GFD 1000 10 order1
PRINT
The output of above would be:
SELL:
BUY:
1000 10
Example 2:
BUY GFD 1000 10 order1
BUY GFD 1000 20 order2
PRINT
The output of above would be:
SELL:
BUY:
1000 30
Example 3:
BUY GFD 1000 10 order1
BUY GFD 1001 20 order2
PRINT
The output of above would be:
SELL:
BUY:
1001 20
1000 10
Example 4:
BUY GFD 1000 10 order1
SELL GFD 900 20 order2
PRINT
The output of above would be:
TRADE order1 1000 10 order2 900 10
SELL:
900 10
BUY:
Any input with price <= 0 or quantity <= 0 or empty order id is invalid. Should be ignored by the application. |