-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata 12.cpp
More file actions
200 lines (190 loc) · 4.24 KB
/
data 12.cpp
File metadata and controls
200 lines (190 loc) · 4.24 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stock {
int stockid;
double price;
int quantity;
char type;
int orderid;
struct stock *next;
int size;
} stock;
int Sell(stock *q, stock *s) {
while (q->next != NULL) {
q = q->next;
if (q->quantity == 0) {
continue;
}
if (q->stockid == s->stockid) {
if ((int)(s->price * 2 + 0.1) >= (int)(q->price * 2 + 0.1)) {
int min = s->quantity < q->quantity ? s->quantity : q->quantity;
printf("deal--price:%6.1lf quantity:%4d buyorder:%04d sellorder:%04d\n", (s->price + q->price) / 2.0, min, s->orderid, q->orderid);
s->quantity -= min;
q->quantity -= min;
if (s->quantity == 0) {
return 0;
}
}
}
}
return 1;
}
int Buy(stock *q, stock *s) {
while (q->next != NULL) {
q = q->next;
if (q->quantity == 0) {
continue;
}
if (q->stockid == s->stockid) {
if ((int)(s->price * 2 + 0.1) <= (int)(q->price * 2 + 0.1)) {
int min = s->quantity < q->quantity ? s->quantity : q->quantity;
printf("deal--price:%6.1lf quantity:%4d sellorder:%04d buyorder:%04d\n", (s->price + q->price) / 2.0, min, s->orderid, q->orderid);
s->quantity -= min;
q->quantity -= min;
if (s->quantity == 0) {
return 0;
}
}
}
}
return 1;
}
int pushbuy(stock *q, stock *s) {
int flag = 0;
if (q->next == NULL) {
flag = 2;
q->next = s;
s->next = NULL;
}
else {
stock *tmp = (stock*)malloc(sizeof(stock));
while (q->next != NULL) {
tmp = q;
q = q->next;
if (s->stockid == q->stockid) {
if (s->price > q->price) {
s->next = q;
tmp->next = s;
flag = 1;
return flag;
}
}
}
}
if (flag == 0) {
q->next = s;
s->next = NULL;
}
return flag;
}
int pushsell(stock *q, stock *s) {
int flag = 0;
if (q->next == NULL) {
flag = 2;
q->next = s;
s->next = NULL;
}
else {
stock *tmp = (stock*)malloc(sizeof(stock));
while (q->next != NULL) {
tmp = q;
q = q->next;
if (q->stockid == s->stockid) {
if (s->price < q->price) {
s->next = q;
tmp->next = s;
flag = 1;
return flag;
}
}
}
}
if (flag == 0) {
q->next = s;
s->next = NULL;
}
return flag;
}
int searchbuy(stock *q, int s) {
int index = 0;
while (q->next != NULL) {
q = q->next;
if (q->stockid == s && q->quantity >0) {
index = 1;
printf("orderid: %04d, stockid:%04d, price: %6.1lf, quantity: %4d, b/s: %c\n", q->orderid, q->stockid, q->price, q->quantity, q->type);
}
}
return index;
}
int searchsell(stock *q, int s) {
int index = 0;
while (q->next != NULL) {
q = q->next;
if (q->stockid == s && q->quantity>0) {
index = 1;
printf("orderid: %04d, stockid:%04d, price: %6.1lf, quantity: %4d, b/s: %c\n", q->orderid, q->stockid, q->price, q->quantity, q->type);
}
}
return index;
}
int DEL(stock *q, int s) {
while (q->next != NULL) {
q = q->next;
if (q->orderid == s && q->quantity > 0) {
printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1lf, quantity: %4d, b/s: %c\n", q->orderid,q->stockid ,q->price, q->quantity, q->type);
q->quantity = 0;
return 0;
}
}
return -1;
}
int main(void) {
int count = 0;
stock *buy = (stock*)malloc(sizeof(stock));
stock *sell = (stock*)malloc(sizeof(stock));
sell->next = NULL;
sell->size = 0;
sell->quantity = 0;
buy->next = NULL;
buy->size = 0;
buy->quantity = 0;
while (1) {
int number;
scanf("%d", &number);
if (number == 1) {
int flag = 0;
stock *s = (stock*)malloc(sizeof(stock));
s->next = NULL;
scanf("%d %lf %d %c", &s->stockid, &s->price, &s->quantity, &s->type);
count++;
s->orderid = count;
printf("orderid: %04d\n", count);
//if (s->quantity == 0)continue;
if (s->type == 'b') flag = Sell(sell, s);
else if (s->type == 's') flag = Buy(buy, s);
if (flag == 1) {
if (s->type == 'b') flag = pushbuy(buy, s);
else if (s->type == 's') flag = pushsell(sell, s);
}
}
else if (number == 2) {
int order;
scanf("%d", &order);
printf("buy orders:\n");
searchbuy(buy, order);
printf("sell orders:\n");
searchsell(sell, order);
}
else if (number == 3) {
int x, f1 = 0, f2 = 0;
scanf("%d", &x);
f1 = DEL(buy, x);
f2 = DEL(sell, x);
if (f1 == -1 && f2 == -1) {
printf("not found\n");
}
}
else if (number == 0) break;
}
}