-
Notifications
You must be signed in to change notification settings - Fork 0
/
market_stock_match.cpp
155 lines (155 loc) · 6.46 KB
/
market_stock_match.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Created by defjia on 18-4-18.
//
#include <cstdlib>
#include <stdio.h>
using namespace std;
typedef struct node{
unsigned int stock_id, deal_id;
unsigned int amount;
double price;
node *next;
}NODE;
int main(){
unsigned int n, amount, stock_id, record = 0, deal_amount, tmp_stock_id, tmp_deal_id, flag;
char behavior;
double price;
NODE *buy = (node*)malloc(sizeof(node));
NODE *sell = (node*)malloc(sizeof(node));
buy->next = NULL; sell->next = NULL;
NODE *cur, *tmp;
if(scanf("%d", &n)!=1) exit(1);
while (n != 0){
switch (n) {
case 1: {
if (scanf("%04d %lf %d %c", &stock_id, &price, &amount, &behavior) != 4) exit(4);
printf("orderid: %04d\n", ++record);
flag = 0;
if (behavior == 'b') {
cur = sell;
while(cur->next && price >= cur->next->price){
if(cur->next->stock_id == stock_id){
if(amount >= cur->next->amount){
amount -= cur->next->amount;
deal_amount = cur->next->amount;
} else{
cur->next->amount -= amount;
deal_amount = amount;
amount = 0;
flag = 1;
}
printf("deal--price:%6.1lf quantity:%4d buyorder:%04d sellorder:%04d\n", (cur->next->price + price)/2, deal_amount, record, cur->next->deal_id);
if(flag == 1) break;
cur->next = cur->next->next;
if(amount == 0) break;
} else{
cur = cur->next;
}
}
if(amount){
tmp = (node*)malloc(sizeof(node));
tmp->amount = amount;
tmp->stock_id = stock_id;
tmp->price = price;
tmp->deal_id = record;
cur = buy;
while (cur->next && cur->next->price >= price){
cur = cur->next;
}
tmp->next = cur->next;
cur->next = tmp;
}
} else{
cur = buy;
while(cur->next && price <= cur->next->price){
if(cur->next->stock_id == stock_id){
if(amount >= cur->next->amount){
amount -= cur->next->amount;
deal_amount = cur->next->amount;
} else{
cur->next->amount -= amount;
deal_amount = amount;
amount = 0;
flag = 1;
}
printf("deal--price:%6.1lf quantity:%4d sellorder:%04d buyorder:%04d\n", (cur->next->price + price)/2, deal_amount, record, cur->next->deal_id);
if(flag == 1) break;
cur->next = cur->next->next;
if(amount == 0) break;
} else{
cur = cur->next;
}
}
if(amount){
tmp = (node*)malloc(sizeof(node));
tmp->amount = amount;
tmp->stock_id = stock_id;
tmp->price = price;
tmp->deal_id = record;
cur = sell;
while (cur->next && cur->next->price <= price){
cur = cur->next;
}
tmp->next = cur->next;
cur->next = tmp;
}
}
break;
}
case 2:{
if(scanf("%d", &tmp_stock_id) != 1) exit(1);
cur = buy;
printf("buy orders:\n");
while (cur->next){
if(cur->next->stock_id == tmp_stock_id){
printf("orderid: %04d, stockid:%04d, price: %6.1lf, quantity: %4d, b/s: b\n", cur->next->deal_id, cur->next->stock_id, cur->next->price, cur->next->amount);
}
cur = cur->next;
}
cur = sell;
printf("sell orders:\n");
while (cur->next){
if(cur->next->stock_id == tmp_stock_id){
printf("orderid: %04d, stockid:%04d, price: %6.1lf, quantity: %4d, b/s: s\n", cur->next->deal_id, cur->next->stock_id, cur->next->price, cur->next->amount);
}
cur = cur->next;
}
break;
}
case 3:{
if(scanf("%d", &tmp_deal_id) != 1) exit(1);
flag = 0;
cur = buy;
while (cur->next){
if(cur->next->deal_id == tmp_deal_id){
printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1lf, quantity: %4d, b/s: b\n", cur->next->deal_id, cur->next->stock_id, cur->next->price, cur->next->amount);
cur->next = cur->next->next;
flag = 1;
break;
}
cur = cur->next;
}
if(flag == 0){
cur = sell;
while (cur->next){
if(cur->next->deal_id == tmp_deal_id){
printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1lf, quantity: %4d, b/s: s\n", cur->next->deal_id, cur->next->stock_id, cur->next->price, cur->next->amount);
cur->next = cur->next->next;
flag = 1;
break;
}
cur = cur->next;
}
}
if(flag == 0){
printf("not found\n");
}
break;
}
default:
exit(1);
}
if(scanf("%d", &n)!=1) exit(1);
}
return 0;
}