股票撮合系统(C语言)

在股票交易中,股民可以通过各种手段将委托送到股票交易所。每个委托主要说明了股民身份、买卖的股票、价格和数量。交易的规则是价格优先、时间优先,即出的价格最高的人先买,出的价格最低的人先卖。两个委托只有价格合适时才能成交,未成交的委托按价格顺序放在撮合队列中。每个股票有两个撮合队列:买队列和卖队列。只有当买委托的价格高于等于卖委托的价格,两个委托才可以成交,成交价取两个委托价格的平均值,成交量取两个委托数量的最小值。委托可以是完全成交或部分成交,部分成交的委托保留在撮合队列中继续交易。试利用单链表作为存放委托的数据结构(撮合队列),编写一模拟股票交易的程序,该程序有以下几个功能:

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 9990 8 1000 s
1 0001 9 500 s
1 0001 8 200 s
1 0001 12 1000 s
1 0001 10 1000 b
2 0001
0

输出(1)

orderid: 0001
orderid: 0002
orderid: 0003
orderid: 0004
orderid: 0005
deal--price:   9.0  quantity: 200  buyorder:0005  sellorder:0003
deal--price:   9.5  quantity: 500  buyorder:0005  sellorder:0002
buy orders:
orderid: 0005, stockid:0001, price:   10.0, quantity:  300, b/s: b
sell orders:
orderid: 0004, stockid:0001, price:   12.0, quantity: 1000, b/s: s

输入(2)

1 2222 10 1000 b
1 2222 9 2000 b
1 2222 10 2000 b
2 2222
3 0003
1 2222 9 5000 s
0

输出(2)

orderid: 0001
orderid: 0002
orderid: 0003
buy orders:
orderid: 0001, stockid:2222, price:   10.0, quantity: 1000, b/s: b
orderid: 0003, stockid:2222, price:   10.0, quantity: 2000, b/s: b
orderid: 0002, stockid:2222, price:    9.0, quantity: 2000, b/s: b
sell orders:
deleted order:orderid: 0003, stockid:2222, price:   10.0, quantity: 2000, b/s: b
orderid: 0004
deal--price:   9.5  quantity:1000  sellorder:0004  buyorder:0001
deal--price:   9.0  quantity:2000  sellorder:0004  buyorder:0002

输入(3)

1 0789 79 671 s
1 0799 27 535 b
1 0809 75 631 b
1 0822 56 495 s
1 0831 4 591 b
1 0841 53 688 b
1 0854 33 552 s
1 0864 81 648 b
1 0547 30 512 b
1 0557 78 608 s
1 0570 59 472 b
1 0580 7 569 b
1 0590 55 433 s
1 0600 36 529 b
1 0613 84 393 s
1 0622 33 489 s
1 0632 13 585 b
1 0642 61 450 s
1 0655 10 546 s
1 0665 90 410 b
1 0675 39 506 s
1 0688 87 370 s
1 0698 67 467 b
1 0707 16 331 s
1 0717 64 427 s
1 0730 45 291 b
1 0740 93 387 s
1 0750 41 483 s
1 0760 22 348 b
1 0773 70 444 s
1 0782 19 308 s
1 0792 99 404 b
1 0802 48 268 s
1 0815 96 364 b
1 0825 76 229 b
1 0835 25 325 s
1 0845 73 189 b
1 0858 54 285 b
1 0867 2 381 s
1 0551 50 245 b
1 0564 31 342 b
1 0574 79 206 s
1 0583 28 302 b
1 0593 8 166 b
1 0606 56 262 s
1 0616 5 126 b
1 0626 85 223 b
1 0636 34 87 s
1 0649 82 183 b
1 0658 62 279 s
1 0668 11 143 s
1 0678 59 239 b
1 0691 40 104 s
1 0701 88 200 s
1 0711 36 64 b
1 0724 17 160 s
1 0734 65 24 s
1 0743 14 121 b
1 0753 94 985 s
1 0766 42 81 s
1 0776 91 177 b
1 0786 71 41 s
1 0796 20 137 s
1 0809 68 2 b
1 0818 49 98 s
1 0828 97 962 b
1 0838 45 58 b
1 0851 26 922 s
1 0861 74 18 b
1 0871 23 883 b
1 0557 71 979 s
1 0567 51 75 b
1 0577 0 939 b
1 0587 48 35 s
1 0600 29 899 b
1 0609 77 996 b
1 0619 25 860 s
1 0629 6 956 b
1 0642 54 820 b
1 0652 3 916 s
1 0662 83 780 b
1 0671 31 877 b
1 0685 80 973 s
1 0694 60 837 b
1 0704 9 933 s
1 0717 57 797 s
1 0727 37 893 b
1 0737 86 758 s
1 0747 34 854 s
1 0760 15 718 b
1 0769 63 814 s
1 0779 11 678 s
1 0789 92 775 b
1 0802 40 871 s
1 0812 89 735 s
1 0822 69 831 b
1 0831 18 695 s
1 0845 66 791 s
1 0854 46 656 b
1 0864 95 752 s
3 0043
3 0024
2 0782
3 0020
3 0001
2 0707
3 0098
2 0802
2 0632
3 0075
2 0727
3 0004
3 0052
2 0652
3 0081
2 0642
2 0580
3 0058
2 0567
2 0831
3 0035
2 0822
3 0064
3 0013
2 0747
3 0041
2 0841
2 0671
3 0019
2 0766
3 0015
3 0096
2 0691
3 0093
3 0041
2 0616
3 0070
2 0606
2 0871
3 0047
2 0858
3 0076
3 0024
2 0786
3 0053
2 0551
2 0711
3 0030
2 0805
2 0636
3 0007
2 0730
3 0004
3 0085
2 0655
3 0082
2 0750
2 0580
3 0059
2 0675
3 0088
3 0036
2 0600
3 0065
2 0590
2 0854
3 0042
2 0841
2 0779
3 0019
2 0769
3 0048
3 0096
2 0694
3 0025
2 0789
2 0619
3 0002
2 0714
3 0099
3 0080
2 0639
3 0076
3 0057
2 0564
3 0054
2 0658
2 0818
3 0031
2 0583
3 0060
3 0008
2 0838
3 0037
2 0825
2 0658
3 0014
2 0753
2 0583
3 0091
0

输出(3)

orderid: 0001
orderid: 0002
orderid: 0003
orderid: 0004
orderid: 0005
orderid: 0006
orderid: 0007
orderid: 0008
orderid: 0009
orderid: 0010
orderid: 0011
orderid: 0012
orderid: 0013
orderid: 0014
orderid: 0015
orderid: 0016
orderid: 0017
orderid: 0018
orderid: 0019
orderid: 0020
orderid: 0021
orderid: 0022
orderid: 0023
orderid: 0024
orderid: 0025
orderid: 0026
orderid: 0027
orderid: 0028
orderid: 0029
orderid: 0030
orderid: 0031
orderid: 0032
orderid: 0033
orderid: 0034
orderid: 0035
orderid: 0036
orderid: 0037
orderid: 0038
orderid: 0039
orderid: 0040
orderid: 0041
orderid: 0042
orderid: 0043
orderid: 0044
orderid: 0045
orderid: 0046
orderid: 0047
orderid: 0048
orderid: 0049
orderid: 0050
orderid: 0051
orderid: 0052
orderid: 0053
orderid: 0054
orderid: 0055
orderid: 0056
orderid: 0057
orderid: 0058
orderid: 0059
orderid: 0060
orderid: 0061
orderid: 0062
orderid: 0063
orderid: 0064
orderid: 0065
orderid: 0066
orderid: 0067
orderid: 0068
orderid: 0069
orderid: 0070
orderid: 0071
orderid: 0072
orderid: 0073
orderid: 0074
orderid: 0075
orderid: 0076
orderid: 0077
orderid: 0078
orderid: 0079
orderid: 0080
orderid: 0081
orderid: 0082
orderid: 0083
orderid: 0084
orderid: 0085
orderid: 0086
orderid: 0087
orderid: 0088
orderid: 0089
orderid: 0090
orderid: 0091
orderid: 0092
orderid: 0093
deal--price:  85.5  quantity: 671  buyorder:0093  sellorder:0001
orderid: 0094
orderid: 0095
orderid: 0096
deal--price:  62.5  quantity: 495  buyorder:0096  sellorder:0004
orderid: 0097
orderid: 0098
deal--price:  69.5  quantity: 189  sellorder:0098  buyorder:0037
orderid: 0099
deal--price:  39.5  quantity: 552  buyorder:0099  sellorder:0007
orderid: 0100
deleted order:orderid: 0043, stockid:0583, price:   28.0, quantity:  302, b/s: b
deleted order:orderid: 0024, stockid:0707, price:   16.0, quantity:  331, b/s: s
buy orders:
sell orders:
orderid: 0031, stockid:0782, price:   19.0, quantity:  308, b/s: s
deleted order:orderid: 0020, stockid:0665, price:   90.0, quantity:  410, b/s: b
not found
buy orders:
sell orders:
deleted order:orderid: 0098, stockid:0845, price:   66.0, quantity:  602, b/s: s
buy orders:
sell orders:
orderid: 0094, stockid:0802, price:   40.0, quantity:  871, b/s: s
orderid: 0033, stockid:0802, price:   48.0, quantity:  268, b/s: s
buy orders:
orderid: 0017, stockid:0632, price:   13.0, quantity:  585, b/s: b
sell orders:
deleted order:orderid: 0075, stockid:0600, price:   29.0, quantity:  899, b/s: b
buy orders:
orderid: 0087, stockid:0727, price:   37.0, quantity:  893, b/s: b
sell orders:
not found
deleted order:orderid: 0052, stockid:0678, price:   59.0, quantity:  239, b/s: b
buy orders:
sell orders:
orderid: 0080, stockid:0652, price:    3.0, quantity:  916, b/s: s
deleted order:orderid: 0081, stockid:0662, price:   83.0, quantity:  780, b/s: b
buy orders:
orderid: 0079, stockid:0642, price:   54.0, quantity:  820, b/s: b
sell orders:
orderid: 0018, stockid:0642, price:   61.0, quantity:  450, b/s: s
buy orders:
orderid: 0012, stockid:0580, price:    7.0, quantity:  569, b/s: b
sell orders:
deleted order:orderid: 0058, stockid:0743, price:   14.0, quantity:  121, b/s: b
buy orders:
orderid: 0072, stockid:0567, price:   51.0, quantity:   75, b/s: b
sell orders:
buy orders:
orderid: 0005, stockid:0831, price:    4.0, quantity:  591, b/s: b
sell orders:
orderid: 0097, stockid:0831, price:   18.0, quantity:  695, b/s: s
deleted order:orderid: 0035, stockid:0825, price:   76.0, quantity:  229, b/s: b
buy orders:
orderid: 0096, stockid:0822, price:   69.0, quantity:  336, b/s: b
sell orders:
deleted order:orderid: 0064, stockid:0809, price:   68.0, quantity:    2, b/s: b
deleted order:orderid: 0013, stockid:0590, price:   55.0, quantity:  433, b/s: s
buy orders:
sell orders:
orderid: 0089, stockid:0747, price:   34.0, quantity:  854, b/s: s
deleted order:orderid: 0041, stockid:0564, price:   31.0, quantity:  342, b/s: b
buy orders:
orderid: 0006, stockid:0841, price:   53.0, quantity:  688, b/s: b
sell orders:
buy orders:
orderid: 0082, stockid:0671, price:   31.0, quantity:  877, b/s: b
sell orders:
deleted order:orderid: 0019, stockid:0655, price:   10.0, quantity:  546, b/s: s
buy orders:
sell orders:
orderid: 0060, stockid:0766, price:   42.0, quantity:   81, b/s: s
deleted order:orderid: 0015, stockid:0613, price:   84.0, quantity:  393, b/s: s
deleted order:orderid: 0096, stockid:0822, price:   69.0, quantity:  336, b/s: b
buy orders:
sell orders:
orderid: 0053, stockid:0691, price:   40.0, quantity:  104, b/s: s
deleted order:orderid: 0093, stockid:0789, price:   92.0, quantity:  104, b/s: b
not found
buy orders:
orderid: 0046, stockid:0616, price:    5.0, quantity:  126, b/s: b
sell orders:
deleted order:orderid: 0070, stockid:0871, price:   23.0, quantity:  883, b/s: b
buy orders:
sell orders:
orderid: 0045, stockid:0606, price:   56.0, quantity:  262, b/s: s
buy orders:
sell orders:
deleted order:orderid: 0047, stockid:0626, price:   85.0, quantity:  223, b/s: b
buy orders:
orderid: 0038, stockid:0858, price:   54.0, quantity:  285, b/s: b
sell orders:
deleted order:orderid: 0076, stockid:0609, price:   77.0, quantity:  996, b/s: b
not found
buy orders:
sell orders:
orderid: 0062, stockid:0786, price:   71.0, quantity:   41, b/s: s
deleted order:orderid: 0053, stockid:0691, price:   40.0, quantity:  104, b/s: s
buy orders:
orderid: 0040, stockid:0551, price:   50.0, quantity:  245, b/s: b
sell orders:
buy orders:
orderid: 0055, stockid:0711, price:   36.0, quantity:   64, b/s: b
sell orders:
deleted order:orderid: 0030, stockid:0773, price:   70.0, quantity:  444, b/s: s
buy orders:
sell orders:
buy orders:
sell orders:
orderid: 0048, stockid:0636, price:   34.0, quantity:   87, b/s: s
not found
buy orders:
orderid: 0026, stockid:0730, price:   45.0, quantity:  291, b/s: b
sell orders:
not found
deleted order:orderid: 0085, stockid:0704, price:    9.0, quantity:  933, b/s: s
buy orders:
sell orders:
deleted order:orderid: 0082, stockid:0671, price:   31.0, quantity:  877, b/s: b
buy orders:
sell orders:
orderid: 0028, stockid:0750, price:   41.0, quantity:  483, b/s: s
buy orders:
orderid: 0012, stockid:0580, price:    7.0, quantity:  569, b/s: b
sell orders:
deleted order:orderid: 0059, stockid:0753, price:   94.0, quantity:  985, b/s: s
buy orders:
sell orders:
orderid: 0021, stockid:0675, price:   39.0, quantity:  506, b/s: s
deleted order:orderid: 0088, stockid:0737, price:   86.0, quantity:  758, b/s: s
deleted order:orderid: 0036, stockid:0835, price:   25.0, quantity:  325, b/s: s
buy orders:
orderid: 0014, stockid:0600, price:   36.0, quantity:  529, b/s: b
sell orders:
deleted order:orderid: 0065, stockid:0818, price:   49.0, quantity:   98, b/s: s
buy orders:
sell orders:
buy orders:
orderid: 0099, stockid:0854, price:   46.0, quantity:  104, b/s: b
sell orders:
deleted order:orderid: 0042, stockid:0574, price:   79.0, quantity:  206, b/s: s
buy orders:
orderid: 0006, stockid:0841, price:   53.0, quantity:  688, b/s: b
sell orders:
buy orders:
sell orders:
orderid: 0092, stockid:0779, price:   11.0, quantity:  678, b/s: s
not found
buy orders:
sell orders:
orderid: 0091, stockid:0769, price:   63.0, quantity:  814, b/s: s
deleted order:orderid: 0048, stockid:0636, price:   34.0, quantity:   87, b/s: s
not found
buy orders:
orderid: 0084, stockid:0694, price:   60.0, quantity:  837, b/s: b
sell orders:
deleted order:orderid: 0025, stockid:0717, price:   64.0, quantity:  427, b/s: s
buy orders:
sell orders:
buy orders:
sell orders:
orderid: 0077, stockid:0619, price:   25.0, quantity:  860, b/s: s
deleted order:orderid: 0002, stockid:0799, price:   27.0, quantity:  535, b/s: b
buy orders:
sell orders:
deleted order:orderid: 0099, stockid:0854, price:   46.0, quantity:  104, b/s: b
deleted order:orderid: 0080, stockid:0652, price:    3.0, quantity:  916, b/s: s
buy orders:
sell orders:
not found
deleted order:orderid: 0057, stockid:0734, price:   65.0, quantity:   24, b/s: s
buy orders:
sell orders:
deleted order:orderid: 0054, stockid:0701, price:   88.0, quantity:  200, b/s: s
buy orders:
sell orders:
orderid: 0050, stockid:0658, price:   62.0, quantity:  279, b/s: s
buy orders:
sell orders:
deleted order:orderid: 0031, stockid:0782, price:   19.0, quantity:  308, b/s: s
buy orders:
sell orders:
deleted order:orderid: 0060, stockid:0766, price:   42.0, quantity:   81, b/s: s
deleted order:orderid: 0008, stockid:0864, price:   81.0, quantity:  648, b/s: b
buy orders:
orderid: 0067, stockid:0838, price:   45.0, quantity:   58, b/s: b
sell orders:
not found
buy orders:
sell orders:
buy orders:
sell orders:
orderid: 0050, stockid:0658, price:   62.0, quantity:  279, b/s: s
deleted order:orderid: 0014, stockid:0600, price:   36.0, quantity:  529, b/s: b
buy orders:
sell orders:
buy orders:
sell orders:
deleted order:orderid: 0091, stockid:0769, price:   63.0, quantity:  814, b/s: s

代码

#include <stdio.h>
#include <stdlib.h>

int flag;

typedef struct Node {
    int orderid;
    int stockid;
    float price;
    int quantity;
    char type;
    struct Node* next;
} NODE;

NODE* head1, *p, *head2; // head1 buy; head2 sell;

int Min(int a, int b) {
    return a <= b ? a : b;
}

void InitBuyQueue() {
    head1 = (NODE*)malloc(sizeof(NODE));
    head1->orderid = -1;
    head1->stockid = -1;
    head1->price = -1.0;
    head1->quantity = -1;
    head1->type = 'x';
    head1->next = NULL;
}

void InitSellQueue() {
    head2 = (NODE*)malloc(sizeof(NODE));
    head2->orderid = -1;
    head2->stockid = -1;
    head2->price = -1.0;
    head2->quantity = -1;
    head2->type = 'x';
    head2->next = NULL;
}

void SaveBuy(NODE* p) {
    NODE* q = head1, *q_pre;
    int flag1 = 0;
    while (q->next != NULL) {
        q_pre = q;
        q = q->next;
        if (p->stockid == q->stockid) {
            flag1 = 1;
            while (q != NULL && (q->price > p->price || q->price == p->price) && q->stockid == p->stockid) {
                q_pre = q;
                q = q->next;
            }
            q_pre->next = p;
            p->next = q;
            break;
        }
    }
    if (flag1 == 0) {
        p->next = head1->next;
        head1->next = p;
    }
}

void SaveSell(NODE* p) {
    NODE* q = head2, *q_pre;
    int flag1 = 0;
    while (q->next != NULL) {
        q_pre = q;
        q = q->next;
        if (p->stockid == q->stockid) {
            flag1 = 1;
            while (q != NULL && (q->price < p->price || q->price == p->price) && q->stockid == p->stockid) {
                q_pre = q;
                q = q->next;
            }
            q_pre->next = p;
            p->next = q;
            break;
        }
    }
    if (flag1 == 0) {
        p->next = head2->next;
        head2->next = p;
    }
}

void DeleteBuy(NODE* p) {
    NODE* x = head1;
    while (x->next->orderid != p->orderid)
        x = x->next;
    x->next = p->next;
}

void DeleteSell(NODE* p) {
    NODE* x = head2;
    while (x->next->orderid != p->orderid)
        x = x->next;
    x->next = p->next;
}

void Delete() {
    NODE* x;
    x = head1;
    while (x->next != NULL) {
        x = x->next;
        if (x->quantity == 0)
            DeleteBuy(x);
    }
    x = head2;
    while (x->next != NULL) {
        x = x->next;
        if (x->quantity == 0)
            DeleteSell(x);
    }
}

int main() {
    int choice, stockid, quantity, orderid = 1;
    float price;
    char bs;
    InitBuyQueue();
    InitSellQueue();
    while ((scanf("%d ", &choice)) && choice != 0) {
        if (choice == 0) {
            exit(0);
            break;
        } else if (choice == 1) {
            // Input
            scanf("%d %f %d %c", &stockid, &price, &quantity, &bs);
            getchar();
            p = (NODE*)malloc(sizeof(NODE));
            p->orderid = orderid++;
            p->price = price;
            p->stockid = stockid;
            p->quantity = quantity;
            p->type = bs;
            printf("orderid: %04d\n", p->orderid);
            if (bs == 'b') {
                NODE* q = head2;
                while (q->next != NULL) {
                    q = q->next;
                    if (q->stockid != p->stockid)
                        continue;
                    else {
                        if (q->price < p->price || q->price == p->price) {
                            int dealquantity = Min(p->quantity, q->quantity);
                            printf("deal--price:%6.1f  quantity:%4d  buyorder:%04d  sellorder:%04d\n", (p->price + q->price) / 2.0, dealquantity, p->orderid, q->orderid);
                            p->quantity -= dealquantity;
                            q->quantity -= dealquantity;
                            if (q->quantity == 0)
                                DeleteSell(q);
                            if (p->quantity == 0)
                                break;
                        } else
                            continue;
                    }
                }
                if (p->quantity == 0)
                    free(p);
                else {
                    SaveBuy(p);
                }
            } else {
                NODE* q = head1;
                while (q->next != NULL && p->quantity != 0) {
                    q = q->next;
                    if (q->stockid != p->stockid)
                        continue;
                    else {
                        if (q->price > p->price || q->price == p->price) {
                            int dealquantity = Min(p->quantity, q->quantity);
                            printf("deal--price:%6.1f  quantity:%4d  sellorder:%04d  buyorder:%04d\n", (p->price + q->price) / 2.0, dealquantity, p->orderid, q->orderid);
                            p->quantity -= dealquantity;
                            q->quantity -= dealquantity;
                            if (q->quantity == 0)
                                DeleteBuy(q);
                        } else
                            continue;
                    }
                }
                if (p->quantity == 0)
                    free(p);
                else {
                    SaveSell(p);
                }
            }
            Delete();
        } else if (choice == 2) {
            int stockid1;
            scanf("%d", &stockid1);
            getchar();
            printf("buy orders:\n");
            p = head1;
            while (p->next != NULL) {
                p = p->next;
                if (p->stockid == stockid1 && p->quantity != 0) {

                    printf("orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n", p->orderid, p->stockid, p->price, p->quantity, p->type);
                }
            }
            printf("sell orders:\n");
            p = head2;
            while (p->next != NULL) {
                p = p->next;
                if (p->stockid == stockid1 && p->quantity != 0) {

                    printf("orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n", p->orderid, p->stockid, p->price, p->quantity, p->type);
                }
            }

        } else if (choice == 3) {
            int orderid1;
            scanf("%d", &orderid1);
            getchar();
            flag = 0;
            p = head1;
            while (p->next != NULL) {
                p = p->next;
                if (p->orderid == orderid1) {
                    flag = 1;
                    printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n", p->orderid, p->stockid, p->price, p->quantity, p->type);
                    DeleteBuy(p);
                    break;
                }
            }
            p = head2;
            while (p->next != NULL && !flag) {
                p = p->next;
                if (p->orderid == orderid1) {
                    flag = 1;
                    printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n", p->orderid, p->stockid, p->price, p->quantity, p->type);
                    DeleteSell(p);
                    break;
                }
            }
            if (flag == 0) {
                printf("not found\n");
            }
            flag = 0;
        }
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值