zoukankan      html  css  js  c++  java
  • 对字符串提取以及运算操作处理

         enum.h文件,枚举类型文件

     1 #ifndef ENUM_H
     2 #define ENUM_H
     3 
     4 #include <QDataStream>
     5 #include <QtCore/qmath.h>
     6 
     7 enum REG_INS_TYPE {REG_INS,POS_REG_INS,POS_REG_AXIS_INS,AR_PAL_REG_INS,STR_REG};
     8 enum VALUE_TYPE
     9 {
    10     REG_AR,REG_CONST,REG_R,REG_PR,REG_GI,
    11     REG_GO,REG_AI,REG_AO,REG_DI,REG_DO,REG_RI,
    12     REG_RO,REG_SI,REG_SO,REG_UI,REG_UO,REG_TIMER,
    13     REG_TIMER_OVERFLOW,REG_P,REG_Lpos,REG_Jpos,
    14     REG_UFRAM,REG_UTOOL,REG_PL,REG_ADD,REG_DEC,
    15     REG_MUL,REG_DIVIVIDE,REG_MOD,REG_DIV,
    16     REG_LEFT_BRK,REG_RIGHT_BRK,REG_PL_CONST,REG_PR_IJ
    17 };
    18 
    19 enum ADDRESSING_TYPE {DIRECT,INDIRECT};
    20 
    21 #endif // ENUM_H
    
    

    字符串的解析

    parseStack.h
     1 #ifndef PARSESTACK_H
     2 #define PARSESTACK_H
     3 
     4 //#include "datavector.h"
     5 #include <QVector>
     6 #include <QStack>
     7 #include <QDebug>
     8 
     9 #include "register.h"
    10 
    11 class ParseStack
    12 {
    13 public:
    14     ParseStack();
    15     
    16     QVector<RegConditionBlock> listData;
    17 
    18 //    QVector<DataVector> listData;
    19     
    20     void initData();
    21     double parseData();
    22     void calc();
    23 
    24     void startReg(Register reg);
    25     void parsePlData();
    26     
    27     QList <int> rList;
    28     QList <int> prList;
    29     QList <int> plList;
    30     QList <int> srList;
    31 
    32     QVector <int> DI;
    33 
    34     QVector <int> arList;
    35     QVector <int> utooList;
    36 
    37     QVector <QVector <int> > prIjList;
    38 
    39     QVector <QVector<int> > realPList;
    40 
    41     QVector <int> cVector;
    42     
    43     QStack <int> chStack;
    44     QStack <double> numStack;
    45 
    46 //    QStack <QVector <int> > numStack;
    47     
    48 };
    49 
    50 #endif // PARSESTACK_H

    parseStack.cpp

      1 #include "parsestack.h"
      2 
      3 ParseStack::ParseStack()
      4 {
      5     for(int i = 0; i < 5;i++)
      6     {
      7         rList.append(i);
      8         prList.append(i+2);
      9         plList.append(i+3);
     10         srList.append(i+4);
     11         DI.append(i+5);
     12         arList.append(i);
     13         utooList.append(i+1);
     14     }
     15 
     16     realPList.resize(5);
     17     prIjList.resize(5);
     18     for(int i = 0; i < 5;i++)
     19     {
     20         realPList[i].resize(3);
     21         prIjList[i].resize(3);
     22     }
     23 
     24     for(int i = 0; i < 5;i++)
     25     {
     26         for(int j = 0; j < 3;j++)
     27         {
     28             realPList[i][j] = (i + j);
     29             prIjList[i][j] = (i + j);
     30         }
     31     }
     32 
     33     cVector.resize(3);
     34 }
     35 
     36 //void ParseStack::initData()
     37 //{
     38 ////    DataVector dataVec;
     39 ////    dataVec.regType = REG_R;
     40 ////    dataVec.i = 0;
     41 ////    listData.append(dataVec);
     42     
     43 ////    dataVec.regType = REG_DIV;
     44 ////    listData.append(dataVec);
     45     
     46 ////    dataVec.regType = REG_LEFT_BRK;
     47 ////    listData.append(dataVec);
     48     
     49 ////    dataVec.regType = REG_LEFT_BRK;
     50 ////    listData.append(dataVec);
     51     
     52 ////    dataVec.regType = REG_R;
     53 ////    dataVec.i = 1;
     54 ////    listData.append(dataVec);
     55 
     56 ////    dataVec.regType = REG_ADD;
     57 ////    listData.append(dataVec);
     58     
     59 ////    dataVec.regType = REG_PR;
     60 ////    dataVec.i = 1;
     61 ////    listData.append(dataVec);
     62     
     63 ////    dataVec.regType = REG_DIV;
     64 ////    listData.append(dataVec);
     65     
     66 ////    dataVec.regType = REG_PL;
     67 ////    dataVec.i = 1;
     68 ////    listData.append(dataVec);
     69     
     70 ////    dataVec.regType = REG_RIGHT_BRK;
     71 ////    listData.append(dataVec);
     72     
     73 ////    dataVec.regType = REG_MUL;
     74 ////    listData.append(dataVec);
     75     
     76 ////    dataVec.regType = REG_LEFT_BRK;
     77 ////    listData.append(dataVec);
     78     
     79 ////    dataVec.regType = REG_SR;
     80 ////    dataVec.i = 1;
     81 ////    listData.append(dataVec);
     82     
     83 ////    dataVec.regType = REG_DEC;
     84 ////    listData.append(dataVec);
     85     
     86 ////    dataVec.regType = REG_R;
     87 ////    dataVec.i = 2;
     88 ////    listData.append(dataVec);
     89     
     90 ////    dataVec.regType = REG_RIGHT_BRK;
     91 ////    listData.append(dataVec);
     92     
     93 ////    dataVec.regType = REG_DEC;
     94 ////    listData.append(dataVec);
     95     
     96 ////    dataVec.regType = REG_SR;
     97 ////    dataVec.i = 2;
     98 ////    listData.append(dataVec);
     99     
    100 ////    dataVec.regType = REG_RIGHT_BRK;
    101 ////    listData.append(dataVec);
    102     
    103 ////    dataVec.regType = REG_MOD;
    104 ////    listData.append(dataVec);
    105     
    106 ////    dataVec.regType = REG_CONST;
    107 ////    dataVec.i = 4;
    108 ////    listData.append(dataVec);
    109     
    110 ////    dataVec.regType = REG_ADD;
    111 ////    listData.append(dataVec);
    112     
    113 ////    dataVec.regType = REG_PR;
    114 ////    dataVec.i = 2;
    115 ////    listData.append(dataVec);
    116 //    DataVector dataVec;
    117 
    118 //    dataVec.regType = REG_LEFT_BRK;
    119 //    listData.append(dataVec);
    120 
    121 //    dataVec.regType = REG_R;
    122 //    dataVec.i = 3;
    123 //    listData.append(dataVec);
    124 
    125 //    dataVec.regType = REG_ADD;
    126 //    listData.append(dataVec);
    127 
    128 //    dataVec.regType = REG_AR;
    129 //    dataVec.i = 2;
    130 //    listData.append(dataVec);
    131 
    132 //    dataVec.regType = REG_RIGHT_BRK;
    133 //    listData.append(dataVec);
    134 
    135 //    dataVec.regType = REG_MUL;
    136 //    listData.append(dataVec);
    137 
    138 //    dataVec.regType = REG_PR;
    139 //    dataVec.i = 3;
    140 //    listData.append(dataVec);
    141 
    142 //    dataVec.regType = REG_DIV;
    143 //    listData.append(dataVec);
    144 
    145 //    dataVec.regType = REG_LEFT_BRK;
    146 //    listData.append(dataVec);
    147 
    148 //    dataVec.regType = REG_R;
    149 //    dataVec.i = 1;
    150 //    listData.append(dataVec);
    151 
    152 //    dataVec.regType = REG_ADD;
    153 //    listData.append(dataVec);
    154 
    155 //    dataVec.regType = REG_PR;
    156 //    dataVec.i = 1;
    157 //    listData.append(dataVec);
    158 
    159 //    dataVec.regType = REG_RIGHT_BRK;
    160 //    listData.append(dataVec);
    161 
    162 //    dataVec.regType = REG_DEC;
    163 //    listData.append(dataVec);
    164 
    165 //    dataVec.regType = REG_CONST;
    166 //    dataVec.i = 10;
    167 //    listData.append(dataVec);
    168 
    169 //    dataVec.regType = REG_DIV;
    170 //    listData.append(dataVec);
    171 
    172 //    dataVec.regType = REG_LEFT_BRK;
    173 //    listData.append(dataVec);
    174 
    175 //    dataVec.regType = REG_PR;
    176 //    dataVec.i = 0;
    177 //    listData.append(dataVec);
    178 
    179 //    dataVec.regType = REG_MUL;
    180 //    listData.append(dataVec);
    181 
    182 //    dataVec.regType = REG_LEFT_BRK;
    183 //    listData.append(dataVec);
    184 
    185 //    dataVec.regType = REG_PL;
    186 //    dataVec.i = 3;
    187 //    listData.append(dataVec);
    188 
    189 //    dataVec.regType = REG_DIV;
    190 //    listData.append(dataVec);
    191 
    192 //    dataVec.regType = REG_PR;
    193 //    dataVec.i = 0;
    194 //    listData.append(dataVec);
    195 
    196 //    dataVec.regType = REG_DEC;
    197 //    listData.append(dataVec);
    198 
    199 //    dataVec.regType = REG_R;
    200 //    dataVec.i = 0;
    201 //    listData.append(dataVec);
    202 
    203 //    dataVec.regType = REG_RIGHT_BRK;
    204 //    listData.append(dataVec);
    205 
    206 //    dataVec.regType = REG_MOD;
    207 //    listData.append(dataVec);
    208 
    209 //    dataVec.regType = REG_R;
    210 //    dataVec.i = 2;
    211 //    listData.append(dataVec);
    212 
    213 //    dataVec.regType = REG_ADD;
    214 //    listData.append(dataVec);
    215 
    216 //    dataVec.regType = REG_PR;
    217 //    dataVec.i = 1;
    218 //    listData.append(dataVec);
    219 
    220 //    dataVec.regType = REG_RIGHT_BRK;
    221 //    listData.append(dataVec);
    222 
    223 //}
    224 
    225 double ParseStack::parseData()
    226 {
    227     qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"****listDataSize****"<<listData.size()<<
    228               "***ch**"<<chStack.size()<<endl;
    229     while(listData.size() > 0)
    230     {
    231         //获取当前表达式头部的第一个字符
    232         int dataType = listData.at(0).valueType;
    233         double num = 0;
    234         bool existNum = false;
    235 
    236         qDebug() << "**dataType***"<<dataType<<endl;
    237         if(dataType == REG_CONST ||
    238                 dataType == REG_PR ||
    239                 dataType == REG_PR_IJ ||
    240                 dataType == REG_R ||
    241                 dataType == REG_AR ||
    242                 dataType ==  REG_GI ||
    243                 dataType ==  REG_GO ||
    244                 dataType ==  REG_AI ||
    245                 dataType ==  REG_AO ||
    246                 dataType ==  REG_DI ||
    247                 dataType ==  REG_DO ||
    248                 dataType ==  REG_RI ||
    249                 dataType ==  REG_RO ||
    250                 dataType ==  REG_SI ||
    251                 dataType ==  REG_SO ||
    252                 dataType ==  REG_UI ||
    253                 dataType ==  REG_UO ||
    254                 dataType ==  REG_TIMER ||
    255                 dataType ==  REG_TIMER_OVERFLOW ||
    256                 dataType ==  REG_P ||
    257                 dataType ==  REG_Lpos ||
    258                 dataType ==  REG_Jpos ||
    259                 dataType ==  REG_UFRAM ||
    260                 dataType ==  REG_UTOOL )
    261         {
    262             existNum = true;
    263             if(existNum)
    264             {
    265                 if(dataType == REG_CONST)
    266                 {
    267                     num = listData.at(0).val;
    268                     qDebug() <<__LINE__ <<"***num**"<<num<<endl;
    269                 }
    270                 else if(dataType == REG_PR)
    271                 {
    272                     if(INDIRECT == listData[0].rightVarType)
    273                     {
    274                         num = prList[rList[listData[0].i]];
    275                     }
    276                     else if(DIRECT == listData[0].rightVarType)
    277                     {
    278                         num = prList.at(listData.at(0).i);
    279                     }
    280                     qDebug() <<__LINE__ <<"***num**"<<num<<endl;
    281                 }
    282                 else if(dataType == REG_PR_IJ)
    283                 {
    284                     num = prIjList[listData[0].i][listData[0].j];
    285                     qDebug() << __FUNCTION__ <<"*****"<<__LINE__<<"**I**"<<listData[0].i<<
    286                                 "**j***"<<listData[0].j<<"****num**"<<num<<endl;
    287                 }
    288                 else if(dataType == REG_R)
    289                 {
    290                     if(INDIRECT == listData[0].rightVarType)
    291                     {
    292                         num = rList[rList[listData[0].i]];
    293                     }
    294                     else if(DIRECT == listData[0].rightVarType)
    295                     {
    296                         num = rList.at(listData.at(0).i);
    297                     }
    298                     qDebug() <<__LINE__ <<"***num**"<<num<<endl;
    299                 }
    300                 else if(dataType == REG_AR)
    301                 {
    302                     if(INDIRECT == listData[0].rightVarType)
    303                     {
    304                         num = arList[rList[listData[0].i]];
    305                     }
    306                     else if(DIRECT == listData[0].rightVarType)
    307                     {
    308                         num = arList.at(listData.at(0).i);
    309                     }
    310                     qDebug() <<__LINE__ <<"***num**"<<num<<endl;
    311                 }
    312                 else if(dataType ==  REG_GI)
    313                 {
    314 
    315                 }
    316                 else if(dataType ==  REG_GO)
    317                 {
    318 
    319                 }
    320                 else if(dataType ==  REG_AI)
    321                 {
    322 
    323                 }
    324                 else if(dataType ==  REG_AO)
    325                 {
    326 
    327                 }
    328                 else if(dataType ==  REG_DI)
    329                 {
    330                     if(INDIRECT == listData[0].rightVarType)
    331                     {
    332                         num = DI[rList[listData[0].i]];
    333                     }
    334                     else if(DIRECT == listData[0].rightVarType)
    335                     {
    336                         num = DI[listData.at(0).i];
    337                     }
    338                     qDebug() <<__LINE__ <<"***num**"<<num<<endl;
    339                 }
    340                 else if(dataType ==  REG_DO)
    341                 {
    342 
    343                 }
    344                 else if(dataType ==  REG_RI)
    345                 {
    346 
    347                 }
    348                 else if(dataType ==  REG_RO)
    349                 {
    350 
    351                 }
    352                 else if(dataType ==  REG_SI)
    353                 {
    354 
    355                 }
    356                 else if(dataType ==  REG_SO)
    357                 {
    358 
    359                 }
    360                 else if(dataType ==  REG_UI)
    361                 {
    362 
    363                 }
    364                 else if(dataType ==  REG_UO)
    365                 {
    366 
    367                 }
    368                 else if(dataType ==  REG_TIMER)
    369                 {
    370 
    371                 }
    372                 else if(dataType ==  REG_TIMER_OVERFLOW)
    373                 {
    374 
    375                 }
    376                 else if(dataType ==  REG_P)
    377                 {
    378 
    379                 }
    380                 else if(dataType ==  REG_Lpos)
    381                 {
    382 
    383                 }
    384                 else if(dataType ==  REG_Jpos)
    385                 {
    386 
    387                 }
    388                 else if(dataType ==  REG_UFRAM)
    389                 {
    390 
    391                 }
    392                 else if(dataType ==  REG_UTOOL)
    393                 {
    394                     if(INDIRECT == listData[0].rightVarType)
    395                     {
    396                         num = utooList[rList[listData[0].i]];
    397                     }
    398                     else if(DIRECT == listData[0].rightVarType)
    399                     {
    400                         num = utooList.at(listData.at(0).i);
    401                     }
    402                     qDebug() <<__LINE__ <<"***num**"<<num<<endl;
    403                 }
    404                 listData.removeFirst();
    405                 numStack.push(num);
    406                 if(listData.size() == 0)
    407                 {
    408                     break;
    409                 }
    410             }
    411         }
    412         else if(dataType == REG_ADD ||
    413                 dataType == REG_DEC ||
    414                 dataType == REG_DIV ||
    415                 dataType == REG_LEFT_BRK ||
    416                 dataType == REG_MOD ||
    417                 dataType == REG_MUL ||
    418                 dataType == REG_DIVIVIDE ||
    419                 dataType == REG_RIGHT_BRK)
    420         {
    421             listData.removeFirst();
    422             if(chStack.isEmpty() ||
    423 //               chStack.top() == REG_LEFT_BRK ||
    424                     dataType == REG_LEFT_BRK)
    425             {
    426                 chStack.push(dataType);
    427                 //listData.removeFirst();
    428                 continue;
    429             }
    430             switch (dataType)
    431             {
    432                 case REG_RIGHT_BRK:
    433                 {
    434                     while((numStack.size() >= 2) &&
    435                           !chStack.isEmpty()&&
    436                           (chStack.top() != REG_LEFT_BRK))
    437                     {
    438                         qDebug() << __LINE__<<"***numStack****"<<numStack.size()<<endl;
    439                         calc();
    440                     }
    441                     if(!chStack.isEmpty() &&
    442                         (chStack.top() == REG_LEFT_BRK))
    443                     {
    444                         qDebug() <<"***right****"<<chStack.size()<<"%%%%chStack%%%"<<endl;
    445                         chStack.pop();
    446                         continue;
    447                     }
    448                     else
    449                     {
    450                         qDebug()<<"The number of barckets are not equals!!"<<endl;
    451                     }
    452                     break;
    453                 }
    454                 case REG_DIV:
    455                 case REG_MOD:
    456                 case REG_MUL:
    457                 case REG_DIVIVIDE:
    458                 {
    459                     /**若符号栈栈顶元素为+、-、( 或者符号栈为空,
    460                      * 则意味着符号栈栈顶符号比ch优先级底,
    461                      * 所以,将ch压栈。否则,将符号栈栈顶元素弹出来,
    462                      * 然后开始计算。*/
    463                     while ((numStack.size() >= 2) &&
    464                            !(chStack.isEmpty() ||
    465                              (chStack.top() == REG_LEFT_BRK) ||
    466                              (chStack.top() == REG_ADD) ||
    467                              (chStack.top() == REG_DEC)))
    468                     {
    469                         qDebug() << __LINE__<<"***numStack****"<<numStack.size()<<endl;
    470                         calc();
    471                     }
    472                     /**若符号栈顶元素优先级比ch的低*/
    473                     if((chStack.isEmpty()) ||
    474                        (chStack.top() == REG_LEFT_BRK) ||
    475                             (chStack.top() == REG_ADD)||
    476                             (chStack.top() == REG_DEC))
    477                     {
    478                         chStack.push(dataType);
    479                         //listData.removeFirst();
    480                         continue;
    481                     }
    482 
    483                 }
    484                 case REG_ADD:
    485                 case REG_DEC:
    486                 {
    487                     while((numStack.size() >= 2) &&
    488                           !chStack.isEmpty() &&
    489                            (chStack.top() != REG_LEFT_BRK))
    490                     {
    491                         qDebug() << __LINE__<<"***numStack****"<<numStack.size()<<endl;
    492                         calc();
    493                     }
    494                     qDebug() << "***chStack**"<<chStack.size()<<endl;
    495                     if(chStack.isEmpty() || (chStack.top() == REG_LEFT_BRK))
    496                     {
    497                         chStack.push(dataType);
    498                         //listData.removeFirst();
    499                         continue;
    500                     }
    501                     else
    502                     {
    503                         qDebug()<<"this string is not illeagal"<<endl;
    504                     }
    505                 }
    506                 default:
    507                 {
    508                     qDebug()<<"argument error!!!"<<endl;
    509                 }
    510             }
    511              //listData.removeFirst();
    512              if(listData.size() == 0)
    513              {
    514                  break;
    515              }
    516         }
    517     }//while结束
    518 
    519      // 若符号栈不为空,则不断的从符号栈和数字栈中弹出元素,进行计算
    520     while(!chStack.isEmpty())
    521     {
    522         qDebug() << __LINE__<<"***numStack****"<<numStack.size()<<endl;
    523         calc();
    524     }
    525     qDebug() << "***numStackSize***"<<numStack.size()<<endl;
    526     return (numStack.size() == 1)?numStack.pop():0;
    527 }
    528 
    529 void ParseStack::calc()
    530 {
    531 
    532     qDebug() << "**num#######stack###size####"<<numStack.size()<<endl;
    533 
    534     double b = numStack.pop();
    535     double a = numStack.pop();
    536 
    537     int operatorType = chStack.pop();
    538 
    539     qDebug() <<__FUNCTION__<< "******"<<__LINE__<<"***a**"<<a<<
    540                "***b**"<<b<<"**oper**"<<operatorType<<endl;
    541 
    542     double result = 0;
    543 
    544     switch (operatorType)
    545     {
    546         case REG_ADD:
    547         {
    548             result = a + b;
    549             break;
    550         }
    551         case REG_DEC:
    552         {
    553             result = a -b;
    554             break;
    555         }
    556         case REG_MUL:
    557         {
    558             result = a * b;
    559             break;
    560         }
    561         case REG_DIV:
    562         {
    563             if( b == 0)
    564             {
    565                 qDebug() << "divisor can not be zero!!!"<<endl;
    566             }
    567             else
    568             {
    569                 result = int(a / b);
    570             }
    571             break;
    572         }
    573         case REG_DIVIVIDE:
    574         {
    575             if( b == 0)
    576             {
    577                 qDebug() << "divisor can not be zero!!!"<<endl;
    578             }
    579             else
    580             {
    581                 result = a / b;
    582             }
    583             break;
    584         }
    585         case REG_MOD:
    586         {
    587             if( b == 0)
    588             {
    589                 qDebug() << "divisor can not be zero!!!"<<endl;
    590             }
    591             else
    592             {
    593                 result = (int)a%(int)b;
    594             }
    595             break;
    596         }
    597     }
    598 
    599     qDebug() << "***result#**"<<result<<endl;
    600     numStack.push(result);
    601 }
    602 
    603 //void ParseStack::calc()
    604 //{
    605 //    QVector <int> aVector;
    606 //    QVector <int> bVector;
    607 
    608 
    609 //    aVector.resize(3);
    610 //    bVector.resize(3);
    611 
    612 
    613 //    bVector = numStack.pop();
    614 //    aVector = numStack.pop();
    615 
    616 //    qDebug() << "**a**"<<aVector<<"***b***"<<bVector<<endl;
    617 
    618 //    int operatorType = chStack.pop();
    619 
    620 
    621 
    622 //    switch (operatorType)
    623 //    {
    624 //        case REG_ADD:
    625 //        {
    626 //            for(int i = 0; i < 3;i++)
    627 //            {
    628 //                cVector[i] = aVector[i] + bVector[i];
    629 //            }
    630 //            break;
    631 //        }
    632 //        case REG_DEC:
    633 //        {
    634 //            for(int i = 0; i < 3;i++)
    635 //            {
    636 //                cVector[i] = aVector[i] - bVector[i];
    637 //            }
    638 //            break;
    639 //        }
    640 //    }
    641 //    numStack.push(cVector);
    642 //}
    643 
    644 
    645 void ParseStack::startReg(Register reg)
    646 {
    647     for(int i = 0; i < reg.regcondition.size();++i)
    648     {
    649         listData.append(reg.regcondition[i]);
    650     }
    651     qDebug() << __FUNCTION__<<"******"<<__LINE__<< "***reg**Type**"<<reg.type<<endl;
    652     switch(reg.type)
    653     {
    654         case AR_PAL_REG_INS:
    655         {
    656             switch (reg.addressType)
    657             {
    658                 case INDIRECT:
    659                 {
    660                     qDebug() << "****INDIRECT***"<<endl;
    661 //                    parsePlData();
    662                     break;
    663                 }
    664                 case DIRECT:
    665                 {
    666                     qDebug() << "****DIRECT***"<<endl;
    667 //                    parsePlData();
    668                     break;
    669                 }
    670             }
    671             break;
    672         }
    673         case REG_INS:
    674         {
    675             switch(reg.addressType)
    676             {
    677                 case INDIRECT:
    678                 {
    679                     qDebug() << "****INDIRECT***"<<endl;
    680                     double result = parseData();
    681                     qDebug() << __FUNCTION__ <<"******"<<__LINE__<<"***RESULT**"<<result<<endl;
    682                     break;
    683                 }
    684                 case DIRECT:
    685                 {
    686                     qDebug() << "****DIRECT***"<<endl;
    687                     double result = parseData();
    688                     qDebug() << __FUNCTION__ <<"******"<<__LINE__<<"***RESULT**"<<result<<endl;
    689                     break;
    690                 }
    691             }
    692             break;
    693         }
    694     }
    695 }
    696 /*
    697 void ParseStack::parsePlData()
    698 {
    699     QVector <int> plTempList;
    700     plTempList.resize(3);
    701     while(listData.size() > 0)
    702     {
    703         //获取当前表达式头部的第一个字符
    704         int dataType = listData.at(0).valueType;
    705         double num = 0;
    706         bool existNum = false;
    707 
    708         if(dataType == REG_PL_CONST ||
    709                 dataType == REG_PL)
    710         {
    711             existNum = true;
    712             if(existNum)
    713             {
    714                 if(dataType == REG_PL_CONST)
    715                 {
    716                     if(INDIRECT == listData[0].pl_iVarType)
    717                     {
    718                         plTempList[0] = rList[listData[0].i];
    719                     }
    720                     else if(DIRECT == listData[0].pl_iVarType)
    721                     {
    722                         plTempList[0] = listData[0].i;
    723                     }
    724                     if(INDIRECT == listData[0].pl_jVarType)
    725                     {
    726                         plTempList[1] = rList[listData[0].j];
    727                     }
    728                     else if(DIRECT == listData[0].pl_jVarType)
    729                     {
    730                         plTempList[1] = listData[0].j;
    731                     }
    732                     if(INDIRECT == listData[0].pl_kVarType)
    733                     {
    734                         plTempList[2] = rList[listData[0].k];
    735                     }
    736                     else if(DIRECT == listData[0].pl_kVarType)
    737                     {
    738                         plTempList[2] = listData[0].k;
    739                     }
    740                 }
    741                 else if(dataType == REG_PL)
    742                 {
    743                     if(INDIRECT == listData.at(0).rightVarType)
    744                     {
    745                         qDebug()<<"***r[i]***"<<listData[0].plVal<<endl;
    746                         plTempList = realPList[rList[listData[0].plVal]];
    747                         qDebug() << "***plTempList***"<<plTempList<<endl;
    748                     }
    749                     else if(DIRECT == listData.at(0).rightVarType)
    750                     {
    751                        plTempList = realPList[listData[0].plVal];
    752                     }
    753                 }
    754 
    755                 listData.removeFirst();
    756 //                numStack.push(num);
    757 
    758                 numStack.push(plTempList);
    759                 if(listData.size() == 0)
    760                 {
    761                     break;
    762                 }
    763             }
    764         }
    765         else if(dataType == REG_ADD ||
    766                 dataType == REG_DEC ||
    767                 dataType == REG_LEFT_BRK ||
    768                 dataType == REG_RIGHT_BRK)
    769         {
    770             listData.removeFirst();
    771             if(chStack.isEmpty() ||
    772                chStack.top() == REG_LEFT_BRK ||
    773                     dataType == REG_LEFT_BRK)
    774             {
    775                 chStack.push(dataType);
    776                 //listData.removeFirst();
    777                 continue;
    778             }
    779             switch (dataType)
    780             {
    781                 case REG_RIGHT_BRK:
    782                 {
    783                     while(numStack.size() >= 2 &&
    784                           !chStack.isEmpty()&&
    785                           chStack.top() != REG_LEFT_BRK)
    786                     {
    787                         calc();
    788                     }
    789                     if(!chStack.isEmpty() &&
    790                         chStack.top() == REG_LEFT_BRK)
    791                     {
    792                         chStack.pop();
    793                         continue;
    794                     }
    795                     else
    796                     {
    797                         qDebug()<<"The number of barckets are not equals!!"<<endl;
    798                     }
    799                     break;
    800                 }
    801                 case REG_ADD:
    802                 case REG_DEC:
    803                 {
    804                     while(numStack.size() >= 2 &&
    805                           !chStack.isEmpty() &&
    806                            chStack.top() != REG_LEFT_BRK)
    807                     {
    808                         calc();
    809                     }
    810                     if(chStack.isEmpty() || chStack.top() == REG_LEFT_BRK)
    811                     {
    812                         chStack.push(dataType);
    813                         //listData.removeFirst();
    814                         continue;
    815                     }
    816                     else
    817                     {
    818                         qDebug()<<"this string is not illeagal"<<endl;
    819                     }
    820                 }
    821                 default:
    822                 {
    823                     qDebug()<<"argument error!!!"<<endl;
    824                 }
    825             }
    826              //listData.removeFirst();
    827              if(listData.size() == 0)
    828              {
    829                  break;
    830              }
    831         }
    832     }//while结束
    833 
    834      // 若符号栈不为空,则不断的从符号栈和数字栈中弹出元素,进行计算
    835     while(!chStack.isEmpty())
    836     {
    837         calc();
    838     }
    839 
    840     qDebug() <<"***result***"<<cVector<<endl;
    841 
    842 //    return numStack.size() == 1?numStack.pop():0;
    843 }
    844 */

    解析函数

      1 #include "register.h"
      2 
      3 
      4 
      5 
      6 Register::Register()
      7 {
      8 }
      9 
     10 int Register::strToInt(QString in)
     11 {
     12     QString tmp;
     13     for(int j = 0; j < in.length(); j++)
     14     {
     15         if(in[j] >= '0' && in[j] <= '9')
     16             tmp.append(in[j]);
     17     }
     18     bool ok;
     19     int tar=tmp.toInt(&ok,10); // ok=rue
     20     return tar;
     21 }
     22 
     23 double Register::strToDouble (QString in)
     24 {
     25     QString tmp;
     26     for(int j = 0; j < in.length(); j++)
     27     {
     28         if((in[j] >= '0' && in[j] <= '9')||in[j]=='.')
     29             tmp.append(in[j]);
     30     }
     31     bool ok;
     32     double tar=tmp.toDouble(&ok); // ok=rue
     33     return tar;
     34 }
     35 
     36 QString Register::strFilt(QString in)
     37 {
     38     QString tmp;
     39     for(int j = 0; j < in.length(); j++)
     40     {
     41         if((in[j] >= 'a' && in[j] <= 'z') || (in[j] >= 'A' && in[j] <= 'Z')||in[j]=='['||in[j]==']'||in[j] =='%'||in[j] == '/')
     42             tmp.append(in[j]);
     43     }
     44     return tmp;
     45 }
     46 
     47 
     48 int Register::patRegister(QString programStr)
     49 {
     50     //  qDebug()<<"patRegister*****************
    
    
    
    
    
    
    
    
    
    *****************
    
    
    
    ******************"<<endl;
     51     int ownerStart = programStr.indexOf("//");
     52     programStr = programStr.left(ownerStart);
     53 //    this[0].type = REG;
     54     QString pattern1 = ("([PSRL]{1,2})\[([R]?\[?\d+\]?)\s*,?\s*(\d*)\s*:?\s*(\w*)\]\s*=(.*)");
     55     QRegExp rx1(pattern1);
     56     programStr.indexOf(rx1);
     57 
     58     qDebug() << "***cap**1**"<<rx1.cap(1)<<endl;
     59     /*********存储等号左边的内容**********/
     60     if(rx1.cap(1) == "R")
     61     {
     62         this[0].type = REG_INS;
     63     }
     64     else if(rx1.cap(1) == "PR")
     65     {
     66         if(rx1.cap(3) == "")
     67         {
     68             this[0].type = POS_REG_INS;
     69         }
     70         else
     71         {
     72             this[0].type = POS_REG_AXIS_INS;
     73         }
     74     }
     75     else if(rx1.cap(1) == "PL")
     76     {
     77         this[0].type = AR_PAL_REG_INS;
     78     }
     79     else if(rx1.cap(1) == "SR")
     80     {
     81         this[0].type = STR_REG;
     82     }
     83 
     84     qDebug() << "***cap**2**"<<rx1.cap(2)<<endl;
     85     qDebug() << "***cap**3**"<<rx1.cap(3)<<endl;
     86     qDebug() << "***cap**4**"<<rx1.cap(4)<<endl;
     87 
     88     int ri = strToInt(rx1.cap(2));
     89     int rj = strToInt(rx1.cap(3));
     90     this[0].i = ri;
     91     this[0].j = rj;
     92     this[0].expression = rx1.cap(4);
     93     if(strFilt(rx1.cap(2)) == "R[]")
     94     {
     95         this[0].addressType = INDIRECT;
     96     }
     97     else
     98     {
     99         this[0].addressType = DIRECT;
    100     }
    101     /*********存储等号后边的内容***********/
    102     /****先取出等号后边的内容,再放在QMap中****/
    103     QMap<int, QString> map;
    104     QString strpro = rx1.cap(5);
    105     strpro.replace(QString(" "),QString(""));//  去掉空格
    106     QString  strchar ;
    107     QStringList  strList;
    108     int xi = 0;
    109     int p1;
    110     int p2;
    111 //    qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***strpro**"<<strpro<<endl;
    112     /*************获取TIMER[] , UFRAM[], UTOOL[]*********/
    113     strchar  = "TIMER;UFRAM;UTOOL";
    114     strList = strchar.split(";");
    115     xi = 0;
    116     while(xi < strList.length())
    117     {
    118         p1 = strpro.indexOf(strList[xi]);
    119 //        qDebug() << __FUNCTION__<<"**1111111**"<<__LINE__<<"***p1**"<<p1<<endl;
    120         if(p1 != -1)
    121         {
    122             p2 = p1;
    123             while(1)
    124             {
    125 //                qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***mid***p2**1**"<<strpro.mid(p2,1)<<endl;
    126                 if(strpro.mid(p2,1) == "]")
    127                 break;
    128                 p2++;
    129             }
    130             QString strget = strpro.mid(p1,p2-p1+1);
    131 //            qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**p2***"<<p2<<"**strget**"<<strget<<endl;
    132             map.insert(p1,strget);      //插入到QMAP中
    133             strpro.replace(p1,5,"@@@@@");
    134             int R = strget.indexOf("R");
    135             if(R != -1)
    136             {
    137                 strpro.replace(p1+R,1,"@");
    138 //                qDebug() << __FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl;
    139             }
    140             qDebug() << __FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl;
    141         }
    142         else if(p1 == -1)
    143         {
    144             xi++;
    145         }
    146     }
    147 //     qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***map**"<<map.size()<<endl;
    148     /************获取 DIV MOD******************/
    149     strchar  = "MOD;DIV";
    150     strList = strchar.split(";");
    151     xi = 0;
    152     while(xi < strList.length())
    153     {
    154         p1 = strpro.indexOf(strList[xi]);
    155 //        qDebug() << __FUNCTION__<<"**22222222**"<<__LINE__<<"***p1**"<<p1<<endl;
    156         if(p1 != -1)
    157         {
    158             QString strget = strpro.mid(p1,3);
    159 //            qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**strget**"<<strget<<endl;
    160             map.insert(p1,strget);      //插入到QMAP中
    161             strpro.replace(p1,3,"###");
    162             qDebug() << __FUNCTION__<<"***##***"<<__LINE__<<"***strpro**"<<strpro<<endl;
    163         }
    164         else if(p1 == -1)
    165         {
    166             xi++;
    167         }
    168     }
    169     /*******PR GI GO AI AO DI DO RI RO SI SO UI UO SR AR PL R[ P[,要放在获取完TIMER[] 和DIV之后*********/
    170     strchar  = "PR:GI:GO:AI:AO:DI:DO:RI:RO:SI:SO:UI:UO:SR:AR:PL:R[:P[";
    171     strList = strchar.split(":");
    172     xi = 0;
    173     while(xi < strList.length())
    174     {
    175         p1 = strpro.indexOf(strList[xi]);
    176 //        qDebug() << __FUNCTION__<<"**3333333**"<<__LINE__<<"***p1**"<<p1<<endl;
    177         if(p1 != -1)
    178         {
    179             p2 = p1;
    180             while(1)
    181             {
    182 //                qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***mid***p2**1**"<<strpro.mid(p2,1)<<endl;
    183                 if(strpro.mid(p2,1) == "]")
    184                 break;
    185                 p2++;
    186             }
    187             QString strget = strpro.mid(p1,p2-p1+1);
    188 //            qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**p2***"<<p2<<"**strget**"<<strget<<endl;
    189             map.insert(p1,strget);      //插入到QMAP中
    190             strpro.replace(p1,2,"@@");
    191 //            qDebug() << __FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl;
    192             strget.replace(0,2,"@@");   //因为PR[R[]]里面有两个R;
    193             qDebug() << __FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**p2***"<<p2<<"**strget**"<<strget<<endl;
    194             int R = strget.indexOf("R");     //判断字符串里面是否有R[],如果有应该替换掉,避免误认为是外面的R[];
    195             if(R != -1)
    196             {
    197                 strpro.replace(p1+R,1,"@");
    198 //                qDebug() << __FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl;
    199             }
    200         }
    201         else if(p1 == -1)
    202         {
    203             xi++;
    204         }
    205     }
    206     /*********************获取“+-* / ( )****************/
    207     strchar = "+ - * / ( )";
    208     strList = strchar.split(" ");
    209     xi  = 0;
    210     int op = 0;
    211     while(xi < strList.length() )
    212     {
    213         op = strpro.indexOf(strList[xi]);
    214 //        qDebug() << __FUNCTION__<<"**4444444**"<<__LINE__<<"***op**"<<op<<endl;
    215         if(op != -1)
    216         {
    217             QString oper = strpro.mid(op,1);
    218 //            qDebug() << __FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***oper****"<<oper<<endl;
    219             map.insert( op,oper);     //插入到QMAP
    220             strpro.replace(op,1,"#");
    221             qDebug() << __FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<endl;
    222         }
    223         else if(op == -1)
    224         {
    225             xi++;
    226         }
    227     }
    228     /***********获取 Lpos Jpos ,要 放在获取完+-*/()之后**************/
    229     strchar  = "Lpos;Jpos";
    230     strList = strchar.split(";");
    231     xi = 0;
    232     while(xi < strList.length())
    233     {
    234         p1 = strpro.indexOf(strList[xi]);
    235 //        qDebug() << __FUNCTION__<<"**555555**"<<__LINE__<<"***p1**"<<p1<<endl;
    236         if(p1 != -1)
    237         {
    238             QString strget = strpro.mid(p1,4);
    239 //            qDebug() << __FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***strget****"<<strget<<endl;
    240             map.insert(p1,strget);      //插入到QMAP中
    241             strpro.replace(p1,4,"@@@@");
    242             qDebug() << __FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<endl;
    243         }
    244         else if(p1 == -1)
    245         {
    246             xi++;
    247         }
    248     }
    249     QMap <int,int> plMap;
    250     plMap.clear();
    251 
    252     QVector <int> leftList;
    253     QVector <int> rightList;
    254     leftList.clear();
    255     rightList.clear();
    256 
    257     int plCount = 0;
    258     int pcCount = 0;
    259     /*********获取【i,j,k】**********/
    260     for(int i = 0; i < strpro.length();i++)
    261     {
    262         if('[' == strpro[i])
    263         {
    264             leftList.append(i);
    265         }
    266         else if(']' == strpro[i])
    267         {
    268             rightList.append(i);
    269         }
    270     }
    271     /***************获取相匹配的括号的位置*****************/
    272     for(int i = leftList[pcCount]; i < strpro.length();i++)
    273     {
    274         if('[' == strpro[i])
    275         {
    276             plCount++;
    277         }
    278         else if(']' == strpro[i])
    279         {
    280             plCount--;
    281         }
    282 
    283         if(0 == plCount)
    284         {
    285            plMap.insert(leftList[pcCount],i);
    286            if(pcCount < leftList.size() - 1)
    287            {
    288                i = leftList[++pcCount] - 1;
    289            }
    290         }
    291     }
    292 
    293     qDebug() << __LINE__<<"***STRPRO***"<<strpro<<endl;
    294     QString tempStr;
    295     int front_i,back_i;
    296     for(int i = 0; i < plMap.size();i++)
    297     {
    298         front_i = plMap.keys().at(i);
    299         back_i = plMap.values().at(i);
    300         tempStr = strpro.mid(front_i,back_i - front_i + 1);
    301         qDebug() << __LINE__<<"****i**"<<i<<"**front_i***"<<front_i<<"***back_i***"<<back_i<<"***tempStr**"<<tempStr<<endl;
    302         int count = tempStr.count(",");
    303         if(2 == count)
    304         {
    305 //            qDebug() << "****i**"<<i<<"**front_i***"<<front_i<<"***back_i***"<<back_i<<"***tempStr**"<<tempStr<<endl;
    306             QString strget = tempStr;
    307             map.insert(front_i,strget);
    308             QString str;
    309             str.fill('@',back_i - front_i + 1);
    310             strpro.replace(front_i,back_i - front_i + 1,str);
    311         }
    312     }
    313 
    314     /**************获取常数项***************/
    315     while(1)
    316     {
    317         p1 = strpro.indexOf("#");
    318         qDebug() << __FUNCTION__<<"**66666**"<<__LINE__<<"***p1**"<<p1<<endl;
    319         if(p1 == -1)
    320         {
    321             break;
    322         }
    323         qDebug() << __FUNCTION__<<"**66666**"<<__LINE__<<"***p1+1**"<<strpro.mid(p1+1,1)<<endl;
    324         if(strpro.mid(p1+1,1)>="0" && strpro.mid(p1+1,1)<="9")
    325         {
    326             p2 = p1;
    327             while(1)
    328             {
    329                 p2++;
    330                 qDebug() << __FUNCTION__<<"**66666**"<<__LINE__<<"***p2**"<<strpro.mid(p2,1)<<endl;
    331                 if(strpro.mid(p2,1) == "#" || strpro.mid(p2,1) == "")
    332                 {
    333                     break;
    334                 }
    335             }
    336             QString strget = strpro.mid(p1+1,p2-p1-1);
    337             qDebug() << __FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***strget****"<<strget<<endl;
    338             map.insert(p1+1,strget);      //插入到QMAP中
    339         }
    340         strpro.replace(p1,1,"$");
    341         qDebug() << __FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<endl;
    342     }
    343     //第一个字符串为常数项的情况
    344     if(!strpro.contains("$"))//全是数字,没有字母
    345     {
    346         QString strget = strpro;
    347         map.insert(0,strget);
    348     }
    349     else
    350     {
    351         if(strpro.mid(0,1) >= "0" && strpro.mid(0,1)<="9")
    352         {
    353             p1 = 0;
    354             while(1)
    355             {
    356                 p1++;
    357                 if(strpro.mid(p1,1) == "$")
    358                 {
    359                     break;
    360                 }
    361             }
    362             QString strget = strpro.mid(0,p1);
    363             qDebug() << __FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***strget****"<<strget<<endl;
    364             map.insert(0,strget);      //插入到QMAP中
    365         }
    366     }
    367     for(int i = 0; i < map.size();i++)
    368     {
    369         qDebug() << "***i=***"<<i<<
    370                     "***key***"<<map.keys().at(i)<<
    371                     "***value***"<<map.values().at(i)<<endl;
    372     }
    373 
    374     /********************将获得的每个字符串按顺序存储**********************/
    375     int pi ;
    376     int pj ;
    377     int sum = 0;
    378     float val;
    379     QString strfile;
    380     QString plStr;
    381     QMap<int, QString>::const_iterator mi;
    382     for( mi=map.constBegin(); mi!=map.constEnd(); ++mi)
    383     {
    384         sum++;
    385         this[0].regcondition.resize(sum);
    386         if(this[0].type == AR_PAL_REG_INS)
    387         {
    388             qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    389             strList = mi.value().split("[");
    390             if("PL" == strList[0])
    391             {
    392                qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    393                this[0].regcondition[sum - 1].valueType = REG_PL;
    394                if(strList.size()>1)
    395                {
    396                     if(strList[1] == "R")
    397                     {
    398                          qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    399                         this[0].regcondition[sum-1].rightVarType = INDIRECT;
    400                         qDebug() << "**strList[1]***"<<strList[2]<<endl;
    401                         this[0].regcondition[sum - 1].plVal = strToInt(strList[2]);
    402                     }
    403                     else
    404                     {
    405                         qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    406                         this[0].regcondition[sum-1].rightVarType = DIRECT;
    407                         this[0].regcondition[sum-1].plVal = strToInt(strList[1]);
    408                     }
    409                }
    410 
    411             }
    412             else if("+" == strList[0])
    413             {
    414                 qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    415                this[0].regcondition[sum-1].valueType = REG_ADD;
    416             }
    417             else if("-" == strList[0])
    418             {
    419                 qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    420                this[0].regcondition[sum-1].valueType = REG_DEC;
    421             }
    422             else if(strList[0] == "(")
    423             {
    424                 qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    425                 this[0].regcondition[sum-1].valueType = REG_LEFT_BRK;
    426             }
    427             else if(strList[0] == ")")
    428             {
    429                 qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    430                 this[0].regcondition[sum-1].valueType = REG_RIGHT_BRK;
    431             }
    432             else
    433             {
    434                 qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    435                 this[0].regcondition[sum - 1].valueType = REG_PL_CONST;
    436                 plStr = mi.value();
    437                 if(2 == plStr.count(","))
    438                 {
    439                     plStr = plStr.remove(" ");
    440                     plStr = plStr.remove("[");
    441                     plStr = plStr.remove("]");
    442                     qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    443                     strList = plStr.split(",");
    444                     for(int j = 0; j < strList.size();j++)
    445                     {
    446                         if("*" == strList[j])
    447                         {
    448                             strList[j] = "-100000";
    449                         }
    450                         else if("2-0" == strList[j])
    451                         {
    452                             strList[j] = "20000";
    453                         }
    454                         else if("2-1" == strList[j])
    455                         {
    456                             strList[j] = "-20000";
    457                         }
    458                          qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    459                         QString str_i,str_j,str_k;
    460 
    461                         str_i = strList[0];
    462                         str_j = strList[1];
    463                         str_k = strList[2];
    464                          qDebug() << __FUNCTION__<<"******"<<__LINE__<<endl;
    465                         if(str_i.contains("R"))
    466                         {
    467                            this[0].regcondition[sum - 1].pl_iVarType = INDIRECT;
    468                            this[0].regcondition[sum - 1].i = strToInt(str_i);
    469                         }
    470                         else
    471                         {
    472                             this[0].regcondition[sum - 1].pl_iVarType = DIRECT;
    473                             this[0].regcondition[sum - 1].i = strToInt(str_i);
    474                         }
    475                         if(str_j.contains("R"))
    476                         {
    477                             this[0].regcondition[sum - 1].pl_jVarType = INDIRECT;
    478                             this[0].regcondition[sum - 1].j = strToInt(str_j);
    479                         }
    480                         else
    481                         {
    482                             this[0].regcondition[sum - 1].pl_jVarType = DIRECT;
    483                             this[0].regcondition[sum - 1].j = strToInt(str_j);
    484                         }
    485                         if(str_k.contains("R"))
    486                         {
    487                             this[0].regcondition[sum - 1].pl_kVarType = INDIRECT;
    488                             this[0].regcondition[sum - 1].k = strToInt(str_k);
    489                         }
    490                         else
    491                         {
    492                             this[0].regcondition[sum - 1].pl_kVarType = DIRECT;
    493                             this[0].regcondition[sum - 1].k = strToInt(str_k);
    494                         }
    495                     }
    496                 }
    497             }
    498         }
    499         else
    500         {
    501             strList = mi.value().split("[") ;                          //如 :R[R[2]] -> R   R 2]];
    502             qDebug() << __LINE__ <<"****strList*******"<<strList[0]<<endl;
    503             if(strList[0] == "AR")
    504             {
    505                 this[0].regcondition[sum-1].valueType = REG_AR;
    506             }
    507             else if(strList[0] == "R")
    508             {
    509                 this[0].regcondition[sum-1].valueType = REG_R;
    510             }
    511             else if(strList[0] == "P")
    512             {
    513                 this[0].regcondition[sum-1].valueType = REG_P;
    514             }
    515             else if(strList[0] == "PR")
    516             {
    517                 this[0].regcondition[sum-1].valueType = REG_PR;
    518             }
    519             else if(strList[0] == "GI")
    520             {
    521                 this[0].regcondition[sum-1].valueType = REG_GI;
    522             }
    523             else if(strList[0] == "GO")
    524             {
    525                 this[0].regcondition[sum-1].valueType = REG_GO;
    526             }
    527             else if(strList[0] == "AI")
    528             {
    529                 this[0].regcondition[sum-1].valueType = REG_AI;
    530             }
    531             else if(strList[0] == "AO")
    532             {
    533                 this[0].regcondition[sum-1].valueType = REG_AO;
    534             }
    535             else if(strList[0] == "DI")
    536             {
    537                 this[0].regcondition[sum-1].valueType = REG_DI;
    538             }
    539             else if(strList[0] == "DO")
    540             {
    541                 this[0].regcondition[sum-1].valueType = REG_DO;
    542             }
    543             else if(strList[0] == "RI")
    544             {
    545                 this[0].regcondition[sum-1].valueType = REG_RI;
    546             }
    547             else if(strList[0] == "RO")
    548             {
    549                 this[0].regcondition[sum-1].valueType = REG_RO;
    550             }
    551             else if(strList[0] == "SI")
    552             {
    553                 this[0].regcondition[sum-1].valueType = REG_SI;
    554             }
    555             else if(strList[0] == "SO")
    556             {
    557                 this[0].regcondition[sum-1].valueType = REG_SO;
    558             }
    559             else if(strList[0] == "UI")
    560             {
    561                 this[0].regcondition[sum-1].valueType = REG_UI;
    562             }
    563             else if(strList[0] == "UO")
    564             {
    565                 this[0].regcondition[sum-1].valueType = REG_UO;
    566             }
    567             else if(strList[0] == "TIMER")
    568             {
    569                 this[0].regcondition[sum-1].valueType = REG_TIMER;
    570             }
    571             else if(strList[0] == "TIMER_OVERFLOW")
    572             {
    573                 this[0].regcondition[sum-1].valueType = REG_TIMER_OVERFLOW;
    574             }
    575             else if(strList[0] == "Lpos")
    576             {
    577                 this[0].regcondition[sum-1].valueType = REG_Lpos;
    578             }
    579             else if(strList[0] == "Jpos")
    580             {
    581                 this[0].regcondition[sum-1].valueType = REG_Jpos;
    582             }
    583             else if(strList[0] == "UFRAM")
    584             {
    585                 this[0].regcondition[sum-1].valueType = REG_UFRAM;
    586             }
    587             else if(strList[0] == "UTOOL")
    588             {
    589                 this[0].regcondition[sum-1].valueType = REG_UTOOL;
    590             }
    591 //            else if(strList[0] == "PL")
    592 //            {
    593 //                this[0].regcondition[sum-1].valueType = REG_PL;
    594 //            }
    595             else if(strList[0] == "MOD")
    596             {
    597                 this[0].regcondition[sum-1].valueType = REG_MOD;
    598             }
    599             else if(strList[0] == "DIV")
    600             {
    601                 this[0].regcondition[sum-1].valueType = REG_DIV;
    602             }
    603             else if(strList[0] == "+")
    604             {
    605                 this[0].regcondition[sum-1].valueType = REG_ADD;
    606             }
    607             else if(strList[0] == "-")
    608             {
    609                 this[0].regcondition[sum-1].valueType = REG_DEC;
    610             }
    611             else if(strList[0] == "*")
    612             {
    613                 this[0].regcondition[sum-1].valueType = REG_MUL;
    614             }
    615             else if(strList[0] == "/")
    616             {
    617                 this[0].regcondition[sum-1].valueType = REG_DIVIVIDE;
    618             }
    619             else if(strList[0] == "(")
    620             {
    621                 this[0].regcondition[sum-1].valueType = REG_LEFT_BRK;
    622             }
    623             else if(strList[0] == ")")
    624             {
    625                 this[0].regcondition[sum-1].valueType = REG_RIGHT_BRK;
    626             }
    627             else
    628             {
    629                 this[0].regcondition[sum-1].valueType = REG_CONST;
    630                 val = strToDouble(strList[0]);
    631                 this[0].regcondition[sum-1].val = val;
    632             }
    633             /*************判断地址类型************/
    634             if(strList.size()>1)
    635             {
    636                 if(strList[1] == "R")
    637                 {
    638                     this[0].regcondition[sum-1].rightVarType = INDIRECT;
    639                 }
    640                 else
    641                 {
    642                     this[0].regcondition[sum-1].rightVarType = DIRECT;
    643                 }
    644             }
    645             /***************存储 i,j***************/
    646             if( this[0].regcondition[sum-1].valueType != REG_CONST)
    647             {
    648                 strList = mi.value().split(",") ;           //如 R[2,4] ->R[2  ;     4];
    649                 pi = strToInt(strList[0]);
    650                 this[0].regcondition[sum-1].i = pi;
    651                 if(strList.size() > 1)
    652                 {
    653                     this[0].regcondition[sum-1].valueType = REG_PR_IJ;
    654                     pj = strToInt(strList[1]);
    655                     this[0].regcondition[sum-1].j = pj;
    656                 }
    657             }
    658         }
    659     }
    660     return 1;
    661 }

    以上为对字符串的提取解析以及运算操作

    
    
    
    
    
     
  • 相关阅读:
    服务器文档下载zip格式
    关于精度,模运算和高精的问题//19/07/14
    Luogu P2010 回文日期 // 暴力
    树形DP水题集合 6/18
    普通背包水题集合 2019/6/17
    因为时间少
    重学树状数组6/14(P3368 【模板】树状数组 2)
    Luogu P1291 [SHOI2002]百事世界杯之旅 // 易错的期望
    Luogu P4316 绿豆蛙的归宿//期望
    树剖
  • 原文地址:https://www.cnblogs.com/wanzaiyimeng/p/5672484.html
Copyright © 2011-2022 走看看