• c++实现简单股票买入和撤销功能(demo)


    c++实现简单股票买入和撤销功能


    需求:
    For example:

    A,16113575,B,18,585

    It decoded as:

    A = add new order
    16113575 = order id
    B = buy order
    18 = number of shares to buy
    585 = price

    Example of input file:

    A,100000,S,90,2
    A,100001,S,107,2
    A,100002,B,110,5
    A,100002,B,110,10

    输出:

    ASK
    110: 5 10

    107: 2
    90: 2
    BID

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    
    // 委托信息
    class orderMessage{
        public:
        orderMessage(){
            clear();
        }
    
        // 信息初始化
        void clear() {
            m_cAction   = ' ';
            m_noperionId= 0;
            m_cbsflag   = ' ';
            m_nbsqty    = 0;
            m_dbAmt     = 0;
        }
    
        // 打印订单信息
        void printAll() {
            cout<<m_cAction<<","<<m_noperionId<<","<<m_cbsflag<<","<<m_nbsqty<<","<<m_dbAmt<<endl;
        }
    
        public:
            // 数据结构:A,16113575,B,18,585
            char    m_cAction;      // 操作类型 A 添加 X 撤销
            int     m_noperionId;   // 委托编号
            char    m_cbsflag;      // 买卖标志 B 买入 S 卖出
            int     m_nbsqty;       // 买卖数量
            double  m_dbAmt;        // 买卖金额
    };
    
    // 检查int
    bool checkStringisInt(const string str){
        for (auto chr:str) 
        {
            if(!isdigit(chr)) return false;
        }
        return true;
    }
    
    // 检查double
    bool checkStringisDouble(const string str){
        for (auto chr:str) 
        {
            if(chr != '.' && !isdigit(chr)) return false;
        }
        return true;
    }
    
    // 正确读取数据成功
    bool getData(const string keep, orderMessage &objorderMessage) {
        int index = 0;
        string tmp = "";
        for (auto chr : keep) {
            if(chr == ',') {
                if(tmp == "" && index < 5) return false;
    
                // 获取参数
                if(index == 0)
                {
                    if(tmp.size() != 1 && strstr("AX", tmp.c_str())) return false;      // 添加/撤销
                    objorderMessage.m_cAction =  tmp[0];
                }else if(index == 1) 
                {
                    if(checkStringisInt(tmp) == false) return false;                    // 数字
                    objorderMessage.m_noperionId =  stoi(tmp);
                }else if(index == 2) 
                {
                    if(tmp.size() != 1 && strstr("BS", tmp.c_str())) return false;      // 买卖BS
                    objorderMessage.m_cbsflag =  tmp[0];
                }else if(index == 3) 
                {
                    if(checkStringisInt(tmp) == false) return false;                    // 数字
                    objorderMessage.m_nbsqty =  stoi(tmp);
                }else if(index == 4) 
                {
                    if(checkStringisDouble(tmp) == false) return false;                // 检查是否为double
                    objorderMessage.m_dbAmt =  stof(tmp);
                }   
    
                index++;
                tmp = "";
            }else{
                tmp += chr;
            }
        }
        if(index < 5) return false; // 列数不够,无效数据
        return true;
    }
    
    // 处理新增数据
    void dealAddData(map<double, map<int, orderMessage>> &mpBug, map<double, map<int, orderMessage>> &mpSale, orderMessage &objorderMessage, bool isBuy) {
        map<double, map<int, orderMessage>>::iterator it;
        map<int, orderMessage>::iterator it2;
        bool    isdelete = false;   // 是否删除元素
        int     nIndex = 0;          // 记录订单号
        // double  dbamt   = objorderMessage.m_dbAmt;
    
        if(isBuy) 
        {
            if(mpSale.size() > 0)                       // 判断卖map是否合适的卖单,可以处理
            {
                it = mpSale.begin();
                double dbamt   = it->first;
                if(dbamt <=  objorderMessage.m_dbAmt)   // 有低于买单的卖单
                {
                    bool check = false;
                    vector<double> vecDeleteAmt;
                    for(;it != mpSale.end(); it++) {
                        if(check) break;                                            // 买单全部成交完成
                        if(it->first > objorderMessage.m_dbAmt) break;              // 卖单高于买单价格 不能成交
                        vector<int> vecDeleteOrderid;
                        for(it2 = it->second.begin(); it2 != it->second.end(); it2++) {
                            if(it2->second.m_nbsqty > objorderMessage.m_nbsqty) {
                                it2->second.m_nbsqty -= objorderMessage.m_nbsqty;
                                objorderMessage.m_nbsqty = 0;
                                check = true;
                            }else{
                                objorderMessage.m_nbsqty -= it2->second.m_nbsqty;
                                vecDeleteOrderid.push_back(it2->second.m_noperionId);
                            }
                        }
                        for (auto elem: vecDeleteOrderid){
                            it->second.erase(elem);
                        }
                        if(it->second.size() <= 0) vecDeleteAmt.push_back(it->first);
                    }
    
                    for (auto elem: vecDeleteAmt){
                        mpSale.erase(elem);
                    }
                }
            }
    
            if(objorderMessage.m_nbsqty > 0) {
                mpBug[-objorderMessage.m_dbAmt][objorderMessage.m_noperionId] = objorderMessage;
            }
        }else
        {
            if(mpBug.size() > 0) {
                it = mpBug.begin();
                double dbamt   = -it->first;
                if(dbamt >=  objorderMessage.m_dbAmt)   // 有高于卖单的买单
                {
                    bool check = false;
                    vector<double> vecDeleteAmt;
                    for(;it != mpSale.end(); it++) {
                        if(check) break;                                            // 买单全部成交完成
                        if(-it->first < objorderMessage.m_dbAmt) break;              // 买单高于卖单价格 不能成交
                        vector<int> vecDeleteOrderid;
                        for(it2 = it->second.begin(); it2 != it->second.end(); it2++) {
                            if(it2->second.m_nbsqty > objorderMessage.m_nbsqty) {
                                it2->second.m_nbsqty -= objorderMessage.m_nbsqty;
                                objorderMessage.m_nbsqty = 0;
                                check = true;
                            }else{
                                objorderMessage.m_nbsqty -= it2->second.m_nbsqty;
                                vecDeleteOrderid.push_back(it2->second.m_noperionId);
                            }
                        }
                        for (auto elem: vecDeleteOrderid){
                            it->second.erase(elem);
                        }
                        if(it->second.size() <= 0) vecDeleteAmt.push_back(it->first);
                    }
    
                    for (auto elem: vecDeleteAmt){
                        mpBug.erase(elem);
                    }
                }
            }
    
            if(objorderMessage.m_nbsqty > 0) {
                mpSale[objorderMessage.m_dbAmt][objorderMessage.m_noperionId] = objorderMessage;
            }
        }
    }
    
    
    // 处理撤销数据
    void dealDeleteData(map<double, map<int, orderMessage>> &mpBug, orderMessage &objorderMessage, bool isBuy) {
        map<double, map<int, orderMessage>>::iterator it;
        map<int, orderMessage>::iterator it2;
        bool    isdelete = false;   // 是否删除元素
        int     nIndex = 0;          // 记录订单号
        double  dbamt   = objorderMessage.m_dbAmt;
    
        if(isBuy) dbamt = -dbamt;
    
        // 根据金额查询订单
        it = mpBug.find(dbamt);
        if(it != mpBug.end()) 
        {
            // 根据订单号 查询订单信息
            it2 = it->second.find(objorderMessage.m_noperionId);
            if(it2 != it->second.end()) {
                
                // 减掉撤销的数量
                it2->second.m_nbsqty -= objorderMessage.m_nbsqty;
                if(it2->second.m_nbsqty  <= 0) {
                    nIndex = it2->first; 
                    isdelete = true;
                }
            }
            if(isdelete == true) it->second.erase(nIndex);
        }
    
        if(it->second.size() <= 0) mpBug.erase(dbamt);
    }
    
    // 打印数据 
    void printMapOrderMessage(map<double, map<int, orderMessage>> &mp, bool isBuy) {
        if(isBuy == true) 
        {
            map<double, map<int, orderMessage>>::iterator it;
            map<int, orderMessage>::iterator it2;
            for (it = mp.begin(); it != mp.end(); it++) 
            {
                // if(it->second.size() <= 0) continue;
                double index = abs(it->first);
                map<int, orderMessage> mp2 = it->second;
                cout<<index<<": ";
                for (it2 = mp2.begin(); it2 != mp2.end(); it2++) 
                {
                    cout<<it2->second.m_nbsqty<<" ";
                }
                cout<<endl;
            }
        }else{
            map<double, map<int, orderMessage>>::reverse_iterator it;
            map<int, orderMessage>::iterator it2;
            for (it = mp.rbegin(); it != mp.rend(); it++) 
            {
                // if(it->second.size() <= 0) continue;
                double index = abs(it->first);
                map<int, orderMessage> mp2 = it->second;
                cout<<index<<": ";
                for (it2 = mp2.begin(); it2 != mp2.end(); it2++) 
                {
                    cout<<it2->second.m_nbsqty<<" ";
                }
                cout<<endl;
            }
        }
    }
    
    orderMessage objorderMessage;
    map<double, map<int, orderMessage>> mpBug;             // 买入订单 mpBug.first 为买入金额 mpBug.second.first 为委托编号,mpBug.second.second 为订单信息
    map<double, map<int, orderMessage>> mpSale;            // 卖出订单 与mpBug类似
    map<double, map<int, orderMessage>>::iterator it;
    map<int, orderMessage>::iterator it2;
    
    int main(){
        // 读取测试数据
        std::ifstream f{"C:/Users/Desktop/coding_test.txt", std::ios::binary};
        
        std::stringstream ss;
        ss << f.rdbuf();
        // string data = ss.str();
    
        // string data = "A,100000,S,1,1075\nA,100001,B,9,1000\nA,100002,B,30,975\nA,100003,S,10,1050\nA,100004,B,10,950\nA,100005,S,2,1025\nA,100006,B,1,1000\nX,100004,B,10,950\nA,100007,S,5,1025\nA,100008,B,3,1050\nX,100008,B,3,1050\nX,100005,S,2,1025\n";
        
        string data = "A,100000,S,1,1075\nA,100001,B,9,1000\nA,100002,B,30,975\nA,100003,S,10,975\n";
    
        string keep = "";
    
        cout<<"Example of input file:"<<endl;
        for (auto chr : data) {
            // 处理订单信息
            if(chr == '\n') 
            {
                keep.push_back(',');                // 增加',', 便于数据处理:A,100000,S,1,1075,
                objorderMessage.clear();
                if(getData(keep, objorderMessage))    // 取数据成功
                { 
                    objorderMessage.printAll();
                    bool isBug = true;                // 是否买入 true 买入 false 卖出
                    if(objorderMessage.m_cbsflag == 'S') isBug = false;
                    // <-------------------------------------代码核心数据处理部分------------------------------------>
                    if(objorderMessage.m_cAction == 'A')                    // 新增的订单
                    {
                        dealAddData(mpBug, mpSale, objorderMessage, isBug);
                    }
                    else if(objorderMessage.m_cAction == 'X')              // 撤销的订单
                    {   if(objorderMessage.m_cbsflag == 'B')
                        {
                            dealDeleteData(mpBug,  objorderMessage, isBug);
                        }else{
                            dealDeleteData(mpSale, objorderMessage, isBug);
                        }
                    }
                }
                keep = "";
            }else
            {
                keep += chr;
            }
        }
    
        // 结果打印
        cout<<endl<<endl;
        cout<<"my result:"<<endl;
        cout<<"================="<<endl;
        cout<<"ASK"<<endl;
        printMapOrderMessage(mpBug, true);        // 输出买入信息
        cout<<"------------"<<endl<<endl;
        printMapOrderMessage(mpSale, false);       // 输出卖出信息
        cout<<"BID"<<endl;
        cout<<"================="<<endl;
    
        system("pause");
        return 0;
    }
    
    • 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
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321

    运行数据

  • 相关阅读:
    Linux 文件系统Ramfs, rootfs and initramfs
    干货 | 外贸业务员必备的实用网站
    [python] Python日志记录库loguru使用指北
    数据结构与算法(Data Structures and Algorithm)——跟着Mark Allen Weiss用Java语言学习数据结构与算法
    LLMOps — 使用 BentoML 为 Llama-3 模型提供服务
    Find My技术|智能电动滑板车与Find My的完美组合
    基于YOLOV8检测和OCR识别的车牌识别
    轻量级模型YOLOv5-Lite基于自己的数据集【焊接质量检测】从零构建模型超详细教程
    腾讯待办关停怎么办?可将导出的ics文件添加到手机待办APP中
    Java中运算符一些注意事项
  • 原文地址:https://blog.csdn.net/hzx5200693/article/details/126145602