成绩 | 15 | 开启时间 | 2017年09月27日 星期三 00:00 |
折扣 | 0.8 | 折扣时间 | 2017年10月20日 星期五 23:55 |
允许迟交 | 否 | 关闭时间 | 2018年01月8日 星期一 23:55 |
在股票交易中,股民可以通过各种手段将委托送到股票交易所。每个委托主要说明了股民身份、买卖的股票、价格和数量。交易的规则是价格优先、时间优先,即出的价格最高的人先买,出的价格最低的人先卖。两个委托只有价格合适时才能成交,未成交的委托按价格顺序放在撮合队列中。每个股票有两个撮合队列:买队列和卖队列。只有当买委托的价格高于等于卖委托的价格,两个委托才可以成交,成交价取两个委托价格的平均值,成交量取两个委托数量的最小值。委托可以是完全成交或部分成交,部分成交的委托保留在撮合队列中继续交易。试利用单链表作为存放委托的数据结构(撮合队列),编写一模拟股票交易的程序,该程序有以下几个功能:
1. 委托申请:
输入:每个委托包括四个数据项,股票编码( 4 位数字)、价格(浮点数)、数量(整数)、买 / 卖( B/S )
输出: a. 程序为每个委托产生一个唯一的序号( %04d ),该序号从 1 开始; b. 每笔成交包括:成交价格( %6.1f )、成交量( %4d )、新的委托序号( %04d )、匹配的委托序号( %04d )。
2. 查询未成交的委托:
输入:股票编码
输出:按撮合队列中委托的顺序,分别输出该股票未成交的委托,每个输出的委托包括:委托序号( %04d )、 股票编码 ( %04d ) 、 价格( %6.1f )、数量( %4d )、 B/S (买 / 卖 )
3. 委托撤消:
输入:要撤消的委托号。
输出:若成功,显示该委托信息,其中委托包括数据项:委托序号、股票编码、价格、数量、 B/S (买 / 卖 ) ;否则显示“ not found ”失败信息。
委托输入格式 : 1 股票编码 价格 数量 买卖
查询输入格式 : 2 股票编码
委托撤销 : .3 委托号
退出: 0
例: (下面的黑斜体为输入, 输入输出格式参见测试用例)
1 0038 20 1000 b
orderid: 0001
1 0278 18 2000 s
orderid: 0002
1 0003 8 5000 b
orderid: 0003
1 0003 12 1000 b
orderid: 0004
1 0003 10 500 b
orderid: 0005
1 0003 11 9000 b
orderid: 0006
1 0003 18 1000 s
orderid: 0007
2 0003
buy orders:
orderid: 0004, stockid:0003, price: 12.0, quantity: 1000, b/s: b
orderid: 0006, stockid:0003, price: 11.0, quantity: 9000, b/s: b
orderid: 0005, stockid:0003, price: 10.0, quantity: 500, b/s: b
orderid: 0003, stockid:0003, price: 8.0, quantity: 5000, b/s: b
sell orders:
orderid: 0007, stockid:0003, price: 18.0, quantity: 1000, b/s: s
3 0006
deleted order:orderid: 0006, stockid:0003, price: 11.0, quantity: 9000, b/s: b
3 0197
not found
2 0003
buy orders:
orderid: 0004, stockid:0003, price: 12.0, quantity: 1000, b/s: b
orderid: 0005, stockid:0003, price: 10.0, quantity: 500, b/s: b
orderid: 0003, stockid:0003, price: 8.0, quantity: 5000, b/s: b
sell orders:
orderid: 0007, stockid:0003, price: 18.0, quantity: 1000, b/s: s
1 0003 9 1200 s
orderid: 0008
deal--price: 10.5 quantity:1000 sellorder:0008 buyorder:0004
deal--price: 9.5 quantity: 200 sellorder:0008 buyorder:0005
2 0003
buy orders:
orderid: 0005, stockid:0003, price: 10.0, quantity: 300, b/s: b
orderid: 0003, stockid:0003, price: 8.0, quantity: 5000, b/s: b
sell orders:
orderid: 0007, stockid:0003, price: 18.0, quantity: 1000, b/s: s
注意:
1、当查询未成交委托时,如果没有委托,仍要输出“buy orders:”或“sell orders: ”。
2、输入的委托price>=0。
3、输入的委托quantity>=0,当接受一个新的委托时,首先根据委托的股票编码和价格与现有队列中的委托进行撮合,若撮合后有剩余的数量没有得到满足(即quantity>0),则将新委托插入到相应队列中。
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
|
以文本方式显示
|
1秒 | 64M | 0 |
测试用例 2 | 以文本方式显示
|
以文本方式显示
|
1秒 | 64M | 0 |
#include
#include
#define LEN sizeof(struct node)
typedef struct node
{
int stackcode;
int orderid;
float price;
int quantity;
char type;
struct node *next;
}Stack, *stack;
stack buy, sell;
stack b, s;
stack b1, s1;
stack b2, s2;
stack b3, s3;
stack q, p; stack q1, p1;
int stock_code, num; float money; char stock_type;
int order = 1;
int num_buy, num_ofsell;
void fun1()
{
b = buy; s = sell; b1 = b->next; s1 = s->next;
scanf("%d %f %d %c", &stock_code, &money, &num, &stock_type);
printf("orderid: %04d\n", order++);
if (stock_type == 'b')
{
while (s1 != NULL)
{
if (money >= s1->price)
{
if (stock_code == s1->stackcode)
{
if (num == s1->quantity)
{
printf("deal--price:%6.1f quantity:%4d buyorder:%04d sellorder:%04d\n", (money + s1->price) / 2, num, order - 1, s1->orderid);
num = 0;
s->next = s1->next;
free(s1); s1 = s->next; break;
}
else if (num < s1->quantity)
{
printf("deal--price:%6.1f quantity:%4d buyorder:%04d sellorder:%04d\n", (money + s1->price) / 2, num, order - 1, s1->orderid);
s1->quantity -= num;
num = 0;
break;
}
else if (num>s1->quantity)
{
printf("deal--price:%6.1f quantity:%4d buyorder:%04d sellorder:%04d\n", (money + s1->price) / 2, s1->quantity, order - 1, s1->orderid);
num -= s1->quantity;
s->next = s1->next;
free(s1);
s1 = s->next;
continue;
}
}
else
{
s = s->next; s1 = s->next; continue;
}
}
else
break;
}
if (num != 0)
{
q = buy; p = q->next;
while (p)
{
if (money > p->price)
{
stack pnew = (stack)malloc(LEN); pnew->orderid = order - 1; pnew->price = money; pnew->quantity = num; pnew->stackcode = stock_code; pnew->type = stock_type;
q->next = pnew; pnew->next = p;
break;
}
else
{
q = q->next, p = q->next;
}
}
if (p == NULL)
{
stack pnew = (stack)malloc(LEN); pnew->orderid = order - 1; pnew->price = money; pnew->quantity = num; pnew->stackcode = stock_code; pnew->type = stock_type;
q->next = pnew; pnew->next = NULL;
}
}
}
if (stock_type == 's')
{
while (b1 != NULL)
{
if (money <= b1->price)
{
if (stock_code == b1->stackcode)
{
if (num == b1->quantity)
{
printf("deal--price:%6.1f quantity:%4d sellorder:%04d buyorder:%04d\n", (money + b1->price) / 2, num, order - 1, b1->orderid);
num = 0;
b->next = b1->next;
free(b1); b1 = b->next; break;
}
else if (num < b1->quantity)
{
printf("deal--price:%6.1f quantity:%4d sellorder:%04d buyorder:%04d\n", (money + b1->price) / 2, num, order - 1, b1->orderid);
b1->quantity -= num;
num = 0;
break;
}
else if (num>b1->quantity)
{
printf("deal--price:%6.1f quantity:%4d sellorder:%04d buyorder:%04d\n", (money + b1->price) / 2, b1->quantity, order - 1, b1->orderid);
num -= b1->quantity;
b->next = b1->next;
free(b1); b1 = b->next;
continue;
}
}
else
{
b = b->next; b1 = b->next; continue;
}
}
else
break;
}
if (num != 0)
{
q = sell; p = q->next;
while (p)
{
if (money < p->price)
{
stack pnew = (stack)malloc(LEN); pnew->orderid = order - 1; pnew->price = money; pnew->quantity = num; pnew->stackcode = stock_code; pnew->type = stock_type;
q->next = pnew; pnew->next = p;
break;
}
else
{
q = q->next, p = q->next;
}
}
if (p == NULL)
{
stack pnew = (stack)malloc(LEN); pnew->orderid = order - 1; pnew->price = money; pnew->quantity = num; pnew->stackcode = stock_code; pnew->type = stock_type;
q->next = pnew; pnew->next = NULL;
}
}
}
}
void fun2()
{
scanf("%d", &stock_code);
p = buy; q = p->next; p1 = sell; q1 = p1->next;
printf("buy orders:\n");
while (q)
{
if (q->stackcode == stock_code)
printf("orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n",q->orderid,q->stackcode,q->price,q->quantity,q->type);
p = p->next; q = p->next;
}
printf("sell orders:\n");
while (q1)
{
if (q1->stackcode == stock_code)
printf("orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n", q1->orderid, q1->stackcode, q1->price, q1->quantity, q1->type);
p1 = p1->next; q1 = p1->next;
}
}
void fun3()
{
int id;
int flag1 = 0, flag2 = 0;
scanf("%d", &id);
p = buy; q = p->next; p1 = sell; q1 = p1->next;
while (q&&!flag1)
{
if (q->orderid == id)
{
printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n",q->orderid,q->stackcode,q->price,q->quantity,q->type);
p->next = q->next; free(q); flag1 = 1; break;
}
else
{
p = p->next; q = p->next;
}
}
while (q1&&!flag2&&!flag1)
{
if (q1->orderid == id)
{
printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n", q1->orderid, q1->stackcode, q1->price, q1->quantity, q1->type);
p1->next = q1->next; free(q1); flag2 = 1; break;
}
else
{
p1 = p1->next; q1 = p1->next;
}
}
if (!flag1&&!flag2)
{
printf("not found\n");
}
}
int main()
{
//freopen("3.txt", "r", stdin);
buy = (stack)malloc(LEN); buy->next = NULL;
sell = (stack)malloc(LEN); sell->next = NULL;
int opcode;
while (1)
{
scanf("%d", &opcode);
if (opcode == 0)
break;
switch (opcode)
{
case 1:fun1(); break;
case 2:fun2(); break;
case 3:fun3(); break;
}
}
return 0;
}