现在的位置: 首页 > 综合 > 正文

股票撮合系统

2013年03月31日 ⁄ 综合 ⁄ 共 5621字 ⁄ 字号 评论关闭

 股票撮合系统(15分)
成绩: 15 / 折扣: 0.8
在股票交易中,股民可以通过各种手段将委托送到股票交易所。每个委托主要说明了股民身份、买卖的股票、价格和数量。交易的规则是价格优先、时间优先,即出的价格最高的人先买,出的价格最低的人先卖。两个委托只有价格合适时才能成交,未成交的委托按价格顺序放在撮合队列中。每个股票有两个撮合队列:买队列和卖队列。只有当买委托的价格高于等于卖委托的价格,两个委托才可以成交,成交价取两个委托价格的平均值,成交量取两个委托数量的最小值。委托可以是完全成交或部分成交,部分成交的委托保留在撮合队列中继续交易。试利用单链表作为存放委托的数据结构(撮合队列),编写一模拟股票交易的程序,该程序有以下几个功能:
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
(注意:当查询未成交委托时,如果没有委托,仍要输出“buy orders:”或“sell orders: ”)

 

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

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

struct order
{
        int orderid;
        int stock;
        float price;
        int quantity;
        char type;
        struct order *next;
};

void main()
{
        int j,k,type,id,quantity,count=1,temquantity,flag=0;
        float price=0.0,temprice=0.0,i=0.0;
        char bs,aaa;
        struct order *p,*q,*tem3,*front1=NULL,*rear1=NULL,*front2=NULL,*rear2=NULL;
        struct order *temp;

        front1=rear1=(struct order*)malloc(sizeof(struct order));
        front2=rear2=(struct order*)malloc(sizeof(struct order));
        front1->next=NULL;
        front2->next=NULL;
        while (1)
        {
                scanf("%d",&type);
                switch(type)
                {
                case 0:
                        exit(0);
                        break;
                case 1:
                        scanf("%d %f %d",&id,&price,&quantity);
                        getchar();
                        scanf("%c",&bs);
                        p=(struct order*)malloc(sizeof(struct order));
                        p->orderid=count++;
                        printf("orderid: %04d\n",p->orderid);
                        p->stock=id;
                        p->price=price;
                        p->quantity=quantity;
                        p->type=bs;
                        p->next=NULL;
                        if (bs == 'b')
                        {
                                if (front2!=rear2)
                                {
                                        q=front2->next;
                                        while (q!=NULL && p->quantity!=0)
                                        {
                                                if (p->stock==q->stock && p->price>=q->price && q->quantity!=0)
                                                {
                                                        temprice=(p->price+q->price)/2;
                                                        temquantity=min(p->quantity,q->quantity);
                                                        p->quantity-=temquantity;
                                                        q->quantity-=temquantity;
                                                        
                                                        printf("deal--price:%6.1f  quantity:%4d  buyorder:%04d  sellorder:%04d\n",temprice,temquantity,p->orderid,q->orderid);
                                                }
                                                q=q->next;
                                        }
                                        if (p->quantity!=0)
                                        {
                                                if (front1!=rear1)
                                                {
                                                        q=front1->next;
                                                        temp=front1;
                                                        while (q!=NULL)
                                                        {
                                                                if (p->price<=q->price)
                                                                {
                                                                        temp=q;
                                                                        q=q->next;
                                                                        if (q==NULL)
                                                                        {
                                                                                temp->next=p;
                                                                        }
                                                                        continue;
                                                                }
                                                                else
                                                                {
                                                                        p->next=q;
                                                                        temp->next=p;
                                                                        break;
                                                                }
                                                                if (q->next==NULL)
                                                                {
                                                                        q->next=p;
                                                                        p->next=NULL;
                                                                        rear1=p;
                                                                }
                                                        }
                                                }
                                                else
                                                {
                                                        front1->next=rear1=p;
                                                }
                                        }
                                        else
                                        {
                                                free(p);
                                        }

                                }
                                else
                                {
                                        if (front1!=rear1)
                                        {
                                                q=front1->next;
                                                temp=front1;
                                                while (q!=NULL)
                                                {
                                                        if (p->price<=q->price)
                                                        {
                                                                temp=q;
                                                                q=q->next;
                                                                if (q==NULL)
                                                                                {
                                                                                        temp->next=p;
                                                                                }
                                                                continue;
                                                        }
                                                        else
                                                        {
                                                                p->next=q;
                                                                temp->next=p;
                                                                break;
                                                        }
                                                        if (q->next==NULL)
                                                        {
                                                                q->next=p;
                                                                p->next=NULL;
                                                                rear1=p;
                                                        }
                                                }
                                        }
                                        else
                                        {
                                                front1->next=rear1=p;
                                        }
                                }
                        }
                                
                                if (bs == 's')
                                {
                                        if (front1!=rear1)
                                        {
                                                q=front1->next;
                                                while (q!=NULL && q->quantity!=0)
                                                {
                                                        if (p->stock==q->stock && p->price<=q->price && p->quantity!=0)
                                                        {
                                                                temprice=(p->price+q->price)/2;
                                                                temquantity=min(p->quantity,q->quantity);
                                                                p->quantity-=temquantity;
                                                                q->quantity-=temquantity;
                                                                
                                                        printf("deal--price:%6.1f  quantity:%4d  sellorder:%04d  buyorder:%04d\n",temprice,temquantity,p->orderid,q->orderid);
                                                        }
                                                        q=q->next;
                                                }
                                                if (p->quantity!=0)
                                                {
                                                        if (front2!=rear2)
                                                        {
                                                                q=front2->next;
                                                                temp=front2;
                                                                while (q!=NULL)
                                                                {
                                                                        if (p->price>=q->price)
                                                                        {
                                                                                temp=q;
                                                                                q=q->next;
                                                                                if (q==NULL)
                                                                                {
                                                                                        temp->next=p;
                                                                                }
                                                                                continue;
                                                                        }
                                                                        else
                                                                        {
                                                                                p->next=q;
                                                                                temp->next=p;
                                                                                break;
                                                                        }
                                                                        if (q->next==NULL)
                                                                        {
                                                                                q->next=p;
                                                                                p->next=NULL;
                                                                                rear2=p;
                                                                        }
                                                                }
                                                        }
                                                        else
                                                        {
                                                                front2->next=rear2=p;
                                                        }
                                                }
                                                else
                                                {
                                                        free(p);
                                                }
                                        }
                                        else
                                        {
                                                if (front2!=rear2)
                                                {
                                                        q=front2->next;
                                                        temp=front2;
                                                        while (q!=NULL)
                                                        {
                                                                if (p->price>=q->price)
                                                                {
                                                                        temp=q;
                                                                        q=q->next;
                                                                        if (q==NULL)
                                                                        {
                                                                                temp->next=p;
                                                                        }
                                                                        continue;
                                                                }
                                                                else
                                                                {
                                                                        p->next=q;
                                                                        temp->next=p;
                                                                        break;
                                                                }
                                                                if (q->next==NULL)
                                                                {
                                                                        q->next=p;
                                                                        p->next=NULL;
                                                                        rear2=p;
                                                                }
                                                        }
                                                }
                                                else
                                                        {
                                                                front2->next=rear2=p;
                                                        }
                                        

                                }




                        }
                        break;
                case 2:
                        scanf("%d",&id);
                        printf("buy orders:\n");
                        p=front1->next;
                        while (p!=NULL)
                        {
                                if (p->stock == id && p->quantity!=0)
                                {
                                        printf("orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n",p->orderid,p->stock,p->price,p->quantity,p->type);
                                }
                                p=p->next;
                        }
                        printf("sell orders:\n");
                        p=front2->next;
                        while (p!=NULL)
                        {
                                if (p->stock == id && p->quantity!=0)
                                {
                                        printf("orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n",p->orderid,p->stock,p->price,p->quantity,p->type);
                                }
                                p=p->next;
                        }
                        break;
                case 3:
                        scanf("%d",&id);
                        temp=front1;
                        p=front1->next;
                        while (p!=NULL)
                        {
                                if (p->orderid==id)
                                {
                                        temp->next=p->next;
                                        
                                        flag=1;
                                        printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n",p->orderid,p->stock,p->price,p->quantity,p->type);
                                        free(p);
                                        break;
                                }
                                temp=p;
                                p=p->next;
                        }
                        temp=front2;
                        p=front2->next;
                        while (p!=NULL && !flag)
                        {
                                if (p->orderid==id)
                                {
                                        temp->next=p->next;
                                        flag=1;
                                        printf("deleted order:orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n",p->orderid,p->stock,p->price,p->quantity,p->type);
                                        free(p);
                                        break;
                                }
                                temp=p;
                                p=p->next;
                        }
                        if (flag == 0)
                        {
                                printf("not found\n");
                        }
                        flag=0;
                        break;
                }
        }
}

 

抱歉!评论已关闭.