对字符串提取以及运算操作处理
2016-07-20 14:47
381 查看
enum.h文件,枚举类型文件
#ifndefENUM_H #defineENUM_H #include<QDataStream> #include<QtCore/qmath.h> enumREG_INS_TYPE{REG_INS,POS_REG_INS,POS_REG_AXIS_INS,AR_PAL_REG_INS,STR_REG}; enumVALUE_TYPE { REG_AR,REG_CONST,REG_R,REG_PR,REG_GI, REG_GO,REG_AI,REG_AO,REG_DI,REG_DO,REG_RI, REG_RO,REG_SI,REG_SO,REG_UI,REG_UO,REG_TIMER, REG_TIMER_OVERFLOW,REG_P,REG_Lpos,REG_Jpos, REG_UFRAM,REG_UTOOL,REG_PL,REG_ADD,REG_DEC, REG_MUL,REG_DIVIVIDE,REG_MOD,REG_DIV, REG_LEFT_BRK,REG_RIGHT_BRK,REG_PL_CONST,REG_PR_IJ }; enumADDRESSING_TYPE{DIRECT,INDIRECT}; #endif//ENUM_H
字符串的解析
parseStack.h
#ifndefPARSESTACK_H #definePARSESTACK_H //#include"datavector.h" #include<QVector> #include<QStack> #include<QDebug> #include"register.h" classParseStack { public: ParseStack(); QVector<RegConditionBlock>listData; //QVector<DataVector>listData; voidinitData(); doubleparseData(); voidcalc(); voidstartReg(Registerreg); voidparsePlData(); QList<int>rList; QList<int>prList; QList<int>plList; QList<int>srList; QVector<int>DI; QVector<int>arList; QVector<int>utooList; QVector<QVector<int>>prIjList; QVector<QVector<int>>realPList; QVector<int>cVector; QStack<int>chStack; QStack<double>numStack; //QStack<QVector<int>>numStack; }; #endif//PARSESTACK_H
parseStack.cpp
#include"parsestack.h" ParseStack::ParseStack() { for(inti=0;i<5;i++) { rList.append(i); prList.append(i+2); plList.append(i+3); srList.append(i+4); DI.append(i+5); arList.append(i); utooList.append(i+1); } realPList.resize(5); prIjList.resize(5); for(inti=0;i<5;i++) { realPList[i].resize(3); prIjList[i].resize(3); } for(inti=0;i<5;i++) { for(intj=0;j<3;j++) { realPList[i][j]=(i+j); prIjList[i][j]=(i+j); } } cVector.resize(3); } //voidParseStack::initData() //{ ////DataVectordataVec; ////dataVec.regType=REG_R; ////dataVec.i=0; ////listData.append(dataVec); ////dataVec.regType=REG_DIV; ////listData.append(dataVec); ////dataVec.regType=REG_LEFT_BRK; ////listData.append(dataVec); ////dataVec.regType=REG_LEFT_BRK; ////listData.append(dataVec); ////dataVec.regType=REG_R; ////dataVec.i=1; ////listData.append(dataVec); ////dataVec.regType=REG_ADD; ////listData.append(dataVec); ////dataVec.regType=REG_PR; ////dataVec.i=1; ////listData.append(dataVec); ////dataVec.regType=REG_DIV; ////listData.append(dataVec); ////dataVec.regType=REG_PL; ////dataVec.i=1; ////listData.append(dataVec); ////dataVec.regType=REG_RIGHT_BRK; ////listData.append(dataVec); ////dataVec.regType=REG_MUL; ////listData.append(dataVec); ////dataVec.regType=REG_LEFT_BRK; ////listData.append(dataVec); ////dataVec.regType=REG_SR; ////dataVec.i=1; ////listData.append(dataVec); ////dataVec.regType=REG_DEC; ////listData.append(dataVec); ////dataVec.regType=REG_R; ////dataVec.i=2; ////listData.append(dataVec); ////dataVec.regType=REG_RIGHT_BRK; ////listData.append(dataVec); ////dataVec.regType=REG_DEC; ////listData.append(dataVec); ////dataVec.regType=REG_SR; ////dataVec.i=2; ////listData.append(dataVec); ////dataVec.regType=REG_RIGHT_BRK; ////listData.append(dataVec); ////dataVec.regType=REG_MOD; ////listData.append(dataVec); ////dataVec.regType=REG_CONST; ////dataVec.i=4; ////listData.append(dataVec); ////dataVec.regType=REG_ADD; ////listData.append(dataVec); ////dataVec.regType=REG_PR; ////dataVec.i=2; ////listData.append(dataVec); //DataVectordataVec; //dataVec.regType=REG_LEFT_BRK; //listData.append(dataVec); //dataVec.regType=REG_R; //dataVec.i=3; //listData.append(dataVec); //dataVec.regType=REG_ADD; //listData.append(dataVec); //dataVec.regType=REG_AR; //dataVec.i=2; //listData.append(dataVec); //dataVec.regType=REG_RIGHT_BRK; //listData.append(dataVec); //dataVec.regType=REG_MUL; //listData.append(dataVec); //dataVec.regType=REG_PR; //dataVec.i=3; //listData.append(dataVec); //dataVec.regType=REG_DIV; //listData.append(dataVec); //dataVec.regType=REG_LEFT_BRK; //listData.append(dataVec); //dataVec.regType=REG_R; //dataVec.i=1; //listData.append(dataVec); //dataVec.regType=REG_ADD; //listData.append(dataVec); //dataVec.regType=REG_PR; //dataVec.i=1; //listData.append(dataVec); //dataVec.regType=REG_RIGHT_BRK; //listData.append(dataVec); //dataVec.regType=REG_DEC; //listData.append(dataVec); //dataVec.regType=REG_CONST; //dataVec.i=10; //listData.append(dataVec); //dataVec.regType=REG_DIV; //listData.append(dataVec); //dataVec.regType=REG_LEFT_BRK; //listData.append(dataVec); //dataVec.regType=REG_PR; //dataVec.i=0; //listData.append(dataVec); //dataVec.regType=REG_MUL; //listData.append(dataVec); //dataVec.regType=REG_LEFT_BRK; //listData.append(dataVec); //dataVec.regType=REG_PL; //dataVec.i=3; //listData.append(dataVec); //dataVec.regType=REG_DIV; //listData.append(dataVec); //dataVec.regType=REG_PR; //dataVec.i=0; //listData.append(dataVec); //dataVec.regType=REG_DEC; //listData.append(dataVec); //dataVec.regType=REG_R; //dataVec.i=0; //listData.append(dataVec); //dataVec.regType=REG_RIGHT_BRK; //listData.append(dataVec); //dataVec.regType=REG_MOD; //listData.append(dataVec); //dataVec.regType=REG_R; //dataVec.i=2; //listData.append(dataVec); //dataVec.regType=REG_ADD; //listData.append(dataVec); //dataVec.regType=REG_PR; //dataVec.i=1; //listData.append(dataVec); //dataVec.regType=REG_RIGHT_BRK; //listData.append(dataVec); //} doubleParseStack::parseData() { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"****listDataSize****"<<listData.size()<< "***ch**"<<chStack.size()<<endl; while(listData.size()>0) { //获取当前表达式头部的第一个字符 intdataType=listData.at(0).valueType; doublenum=0; boolexistNum=false; qDebug()<<"**dataType***"<<dataType<<endl; if(dataType==REG_CONST|| dataType==REG_PR|| dataType==REG_PR_IJ|| dataType==REG_R|| dataType==REG_AR|| dataType==REG_GI|| dataType==REG_GO|| dataType==REG_AI|| dataType==REG_AO|| dataType==REG_DI|| dataType==REG_DO|| dataType==REG_RI|| dataType==REG_RO|| dataType==REG_SI|| dataType==REG_SO|| dataType==REG_UI|| dataType==REG_UO|| dataType==REG_TIMER|| dataType==REG_TIMER_OVERFLOW|| dataType==REG_P|| dataType==REG_Lpos|| dataType==REG_Jpos|| dataType==REG_UFRAM|| dataType==REG_UTOOL) { existNum=true; if(existNum) { if(dataType==REG_CONST) { num=listData.at(0).val; qDebug()<<__LINE__<<"***num**"<<num<<endl; } elseif(dataType==REG_PR) { if(INDIRECT==listData[0].rightVarType) { num=prList[rList[listData[0].i]]; } elseif(DIRECT==listData[0].rightVarType) { num=prList.at(listData.at(0).i); } qDebug()<<__LINE__<<"***num**"<<num<<endl; } elseif(dataType==REG_PR_IJ) { num=prIjList[listData[0].i][listData[0].j]; qDebug()<<__FUNCTION__<<"*****"<<__LINE__<<"**I**"<<listData[0].i<< "**j***"<<listData[0].j<<"****num**"<<num<<endl; } elseif(dataType==REG_R) { if(INDIRECT==listData[0].rightVarType) { num=rList[rList[listData[0].i]]; } elseif(DIRECT==listData[0].rightVarType) { num=rList.at(listData.at(0).i); } qDebug()<<__LINE__<<"***num**"<<num<<endl; } elseif(dataType==REG_AR) { if(INDIRECT==listData[0].rightVarType) { num=arList[rList[listData[0].i]]; } elseif(DIRECT==listData[0].rightVarType) { num=arList.at(listData.at(0).i); } qDebug()<<__LINE__<<"***num**"<<num<<endl; } elseif(dataType==REG_GI) { } elseif(dataType==REG_GO) { } elseif(dataType==REG_AI) { } elseif(dataType==REG_AO) { } elseif(dataType==REG_DI) { if(INDIRECT==listData[0].rightVarType) { num=DI[rList[listData[0].i]]; } elseif(DIRECT==listData[0].rightVarType) { num=DI[listData.at(0).i]; } qDebug()<<__LINE__<<"***num**"<<num<<endl; } elseif(dataType==REG_DO) { } elseif(dataType==REG_RI) { } elseif(dataType==REG_RO) { } elseif(dataType==REG_SI) { } elseif(dataType==REG_SO) { } elseif(dataType==REG_UI) { } elseif(dataType==REG_UO) { } elseif(dataType==REG_TIMER) { } elseif(dataType==REG_TIMER_OVERFLOW) { } elseif(dataType==REG_P) { } elseif(dataType==REG_Lpos) { } elseif(dataType==REG_Jpos) { } elseif(dataType==REG_UFRAM) { } elseif(dataType==REG_UTOOL) { if(INDIRECT==listData[0].rightVarType) { num=utooList[rList[listData[0].i]]; } elseif(DIRECT==listData[0].rightVarType) { num=utooList.at(listData.at(0).i); } qDebug()<<__LINE__<<"***num**"<<num<<endl; } listData.removeFirst(); numStack.push(num); if(listData.size()==0) { break; } } } elseif(dataType==REG_ADD|| dataType==REG_DEC|| dataType==REG_DIV|| dataType==REG_LEFT_BRK|| dataType==REG_MOD|| dataType==REG_MUL|| dataType==REG_DIVIVIDE|| dataType==REG_RIGHT_BRK) { listData.removeFirst(); if(chStack.isEmpty()|| //chStack.top()==REG_LEFT_BRK|| dataType==REG_LEFT_BRK) { chStack.push(dataType); //listData.removeFirst(); continue; } switch(dataType) { caseREG_RIGHT_BRK: { while((numStack.size()>=2)&& !chStack.isEmpty()&& (chStack.top()!=REG_LEFT_BRK)) { qDebug()<<__LINE__<<"***numStack****"<<numStack.size()<<endl; calc(); } if(!chStack.isEmpty()&& (chStack.top()==REG_LEFT_BRK)) { qDebug()<<"***right****"<<chStack.size()<<"%%%%chStack%%%"<<endl; chStack.pop(); continue; } else { qDebug()<<"Thenumberofbarcketsarenotequals!!"<<endl; } break; } caseREG_DIV: caseREG_MOD: caseREG_MUL: caseREG_DIVIVIDE: { /**若符号栈栈顶元素为+、-、(或者符号栈为空, *则意味着符号栈栈顶符号比ch优先级底, *所以,将ch压栈。否则,将符号栈栈顶元素弹出来, *然后开始计算。*/ while((numStack.size()>=2)&& !(chStack.isEmpty()|| (chStack.top()==REG_LEFT_BRK)|| (chStack.top()==REG_ADD)|| (chStack.top()==REG_DEC))) { qDebug()<<__LINE__<<"***numStack****"<<numStack.size()<<endl; calc(); } /**若符号栈顶元素优先级比ch的低*/ if((chStack.isEmpty())|| (chStack.top()==REG_LEFT_BRK)|| (chStack.top()==REG_ADD)|| (chStack.top()==REG_DEC)) { chStack.push(dataType); //listData.removeFirst(); continue; } } caseREG_ADD: caseREG_DEC: { while((numStack.size()>=2)&& !chStack.isEmpty()&& (chStack.top()!=REG_LEFT_BRK)) { qDebug()<<__LINE__<<"***numStack****"<<numStack.size()<<endl; calc(); } qDebug()<<"***chStack**"<<chStack.size()<<endl; if(chStack.isEmpty()||(chStack.top()==REG_LEFT_BRK)) { chStack.push(dataType); //listData.removeFirst(); continue; } else { qDebug()<<"thisstringisnotilleagal"<<endl; } } default: { qDebug()<<"argumenterror!!!"<<endl; } } //listData.removeFirst(); if(listData.size()==0) { break; } } }//while结束 //若符号栈不为空,则不断的从符号栈和数字栈中弹出元素,进行计算 while(!chStack.isEmpty()) { qDebug()<<__LINE__<<"***numStack****"<<numStack.size()<<endl; calc(); } qDebug()<<"***numStackSize***"<<numStack.size()<<endl; return(numStack.size()==1)?numStack.pop():0; } voidParseStack::calc() { qDebug()<<"**num#######stack###size####"<<numStack.size()<<endl; doubleb=numStack.pop(); doublea=numStack.pop(); intoperatorType=chStack.pop(); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***a**"<<a<< "***b**"<<b<<"**oper**"<<operatorType<<endl; doubleresult=0; switch(operatorType) { caseREG_ADD: { result=a+b; break; } caseREG_DEC: { result=a-b; break; } caseREG_MUL: { result=a*b; break; } caseREG_DIV: { if(b==0) { qDebug()<<"divisorcannotbezero!!!"<<endl; } else { result=int(a/b); } break; } caseREG_DIVIVIDE: { if(b==0) { qDebug()<<"divisorcannotbezero!!!"<<endl; } else { result=a/b; } break; } caseREG_MOD: { if(b==0) { qDebug()<<"divisorcannotbezero!!!"<<endl; } else { result=(int)a%(int)b; } break; } } qDebug()<<"***result#**"<<result<<endl; numStack.push(result); } //voidParseStack::calc() //{ //QVector<int>aVector; //QVector<int>bVector; //aVector.resize(3); //bVector.resize(3); //bVector=numStack.pop(); //aVector=numStack.pop(); //qDebug()<<"**a**"<<aVector<<"***b***"<<bVector<<endl; //intoperatorType=chStack.pop(); //switch(operatorType) //{ //caseREG_ADD: //{ //for(inti=0;i<3;i++) //{ //cVector[i]=aVector[i]+bVector[i]; //} //break; //} //caseREG_DEC: //{ //for(inti=0;i<3;i++) //{ //cVector[i]=aVector[i]-bVector[i]; //} //break; //} //} //numStack.push(cVector); //} voidParseStack::startReg(Registerreg) { for(inti=0;i<reg.regcondition.size();++i) { listData.append(reg.regcondition[i]); } qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***reg**Type**"<<reg.type<<endl; switch(reg.type) { caseAR_PAL_REG_INS: { switch(reg.addressType) { caseINDIRECT: { qDebug()<<"****INDIRECT***"<<endl; //parsePlData(); break; } caseDIRECT: { qDebug()<<"****DIRECT***"<<endl; //parsePlData(); break; } } break; } caseREG_INS: { switch(reg.addressType) { caseINDIRECT: { qDebug()<<"****INDIRECT***"<<endl; doubleresult=parseData(); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***RESULT**"<<result<<endl; break; } caseDIRECT: { qDebug()<<"****DIRECT***"<<endl; doubleresult=parseData(); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***RESULT**"<<result<<endl; break; } } break; } } } /* voidParseStack::parsePlData() { QVector<int>plTempList; plTempList.resize(3); while(listData.size()>0) { //获取当前表达式头部的第一个字符 intdataType=listData.at(0).valueType; doublenum=0; boolexistNum=false; if(dataType==REG_PL_CONST|| dataType==REG_PL) { existNum=true; if(existNum) { if(dataType==REG_PL_CONST) { if(INDIRECT==listData[0].pl_iVarType) { plTempList[0]=rList[listData[0].i]; } elseif(DIRECT==listData[0].pl_iVarType) { plTempList[0]=listData[0].i; } if(INDIRECT==listData[0].pl_jVarType) { plTempList[1]=rList[listData[0].j]; } elseif(DIRECT==listData[0].pl_jVarType) { plTempList[1]=listData[0].j; } if(INDIRECT==listData[0].pl_kVarType) { plTempList[2]=rList[listData[0].k]; } elseif(DIRECT==listData[0].pl_kVarType) { plTempList[2]=listData[0].k; } } elseif(dataType==REG_PL) { if(INDIRECT==listData.at(0).rightVarType) { qDebug()<<"***r[i]***"<<listData[0].plVal<<endl; plTempList=realPList[rList[listData[0].plVal]]; qDebug()<<"***plTempList***"<<plTempList<<endl; } elseif(DIRECT==listData.at(0).rightVarType) { plTempList=realPList[listData[0].plVal]; } } listData.removeFirst(); //numStack.push(num); numStack.push(plTempList); if(listData.size()==0) { break; } } } elseif(dataType==REG_ADD|| dataType==REG_DEC|| dataType==REG_LEFT_BRK|| dataType==REG_RIGHT_BRK) { listData.removeFirst(); if(chStack.isEmpty()|| chStack.top()==REG_LEFT_BRK|| dataType==REG_LEFT_BRK) { chStack.push(dataType); //listData.removeFirst(); continue; } switch(dataType) { caseREG_RIGHT_BRK: { while(numStack.size()>=2&& !chStack.isEmpty()&& chStack.top()!=REG_LEFT_BRK) { calc(); } if(!chStack.isEmpty()&& chStack.top()==REG_LEFT_BRK) { chStack.pop(); continue; } else { qDebug()<<"Thenumberofbarcketsarenotequals!!"<<endl; } break; } caseREG_ADD: caseREG_DEC: { while(numStack.size()>=2&& !chStack.isEmpty()&& chStack.top()!=REG_LEFT_BRK) { calc(); } if(chStack.isEmpty()||chStack.top()==REG_LEFT_BRK) { chStack.push(dataType); //listData.removeFirst(); continue; } else { qDebug()<<"thisstringisnotilleagal"<<endl; } } default: { qDebug()<<"argumenterror!!!"<<endl; } } //listData.removeFirst(); if(listData.size()==0) { break; } } }//while结束 //若符号栈不为空,则不断的从符号栈和数字栈中弹出元素,进行计算 while(!chStack.isEmpty()) { calc(); } qDebug()<<"***result***"<<cVector<<endl; //returnnumStack.size()==1?numStack.pop():0; } */
解析函数
#include"register.h" Register::Register() { } intRegister::strToInt(QStringin) { QStringtmp; for(intj=0;j<in.length();j++) { if(in[j]>='0'&&in[j]<='9') tmp.append(in[j]); } boolok; inttar=tmp.toInt(&ok,10);//ok=rue returntar; } doubleRegister::strToDouble(QStringin) { QStringtmp; for(intj=0;j<in.length();j++) { if((in[j]>='0'&&in[j]<='9')||in[j]=='.') tmp.append(in[j]); } boolok; doubletar=tmp.toDouble(&ok);//ok=rue returntar; } QStringRegister::strFilt(QStringin) { QStringtmp; for(intj=0;j<in.length();j++) { if((in[j]>='a'&&in[j]<='z')||(in[j]>='A'&&in[j]<='Z')||in[j]=='['||in[j]==']'||in[j]=='%'||in[j]=='/') tmp.append(in[j]); } returntmp; } intRegister::patRegister(QStringprogramStr) { //qDebug()<<"patRegister*****************\n\n\n\n\n\n\n\n\n\n*****************\n\n\n\n******************"<<endl; intownerStart=programStr.indexOf("//"); programStr=programStr.left(ownerStart); //this[0].type=REG; QStringpattern1=("([PSRL]{1,2})\\[([R]?\\[?\\d+\\]?)\\s*,?\\s*(\\d*)\\s*:?\\s*(\\w*)\\]\\s*=(.*)"); QRegExprx1(pattern1); programStr.indexOf(rx1); qDebug()<<"***cap**1**"<<rx1.cap(1)<<endl; /*********存储等号左边的内容**********/ if(rx1.cap(1)=="R") { this[0].type=REG_INS; } elseif(rx1.cap(1)=="PR") { if(rx1.cap(3)=="") { this[0].type=POS_REG_INS; } else { this[0].type=POS_REG_AXIS_INS; } } elseif(rx1.cap(1)=="PL") { this[0].type=AR_PAL_REG_INS; } elseif(rx1.cap(1)=="SR") { this[0].type=STR_REG; } qDebug()<<"***cap**2**"<<rx1.cap(2)<<endl; qDebug()<<"***cap**3**"<<rx1.cap(3)<<endl; qDebug()<<"***cap**4**"<<rx1.cap(4)<<endl; intri=strToInt(rx1.cap(2)); intrj=strToInt(rx1.cap(3)); this[0].i=ri; this[0].j=rj; this[0].expression=rx1.cap(4); if(strFilt(rx1.cap(2))=="R[]") { this[0].addressType=INDIRECT; } else { this[0].addressType=DIRECT; } /*********存储等号后边的内容***********/ /****先取出等号后边的内容,再放在QMap中****/ QMap<int,QString>map; QStringstrpro=rx1.cap(5); strpro.replace(QString(""),QString(""));//去掉空格 QStringstrchar; QStringListstrList; intxi=0; intp1; intp2; //qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***strpro**"<<strpro<<endl; /*************获取TIMER[],UFRAM[],UTOOL[]*********/ strchar="TIMER;UFRAM;UTOOL"; strList=strchar.split(";"); xi=0; while(xi<strList.length()) { p1=strpro.indexOf(strList[xi]); //qDebug()<<__FUNCTION__<<"**1111111**"<<__LINE__<<"***p1**"<<p1<<endl; if(p1!=-1) { p2=p1; while(1) { //qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***mid***p2**1**"<<strpro.mid(p2,1)<<endl; if(strpro.mid(p2,1)=="]") break; p2++; } QStringstrget=strpro.mid(p1,p2-p1+1); //qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**p2***"<<p2<<"**strget**"<<strget<<endl; map.insert(p1,strget);//插入到QMAP中 strpro.replace(p1,5,"@@@@@"); intR=strget.indexOf("R"); if(R!=-1) { strpro.replace(p1+R,1,"@"); //qDebug()<<__FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl; } qDebug()<<__FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl; } elseif(p1==-1) { xi++; } } //qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***map**"<<map.size()<<endl; /************获取DIVMOD******************/ strchar="MOD;DIV"; strList=strchar.split(";"); xi=0; while(xi<strList.length()) { p1=strpro.indexOf(strList[xi]); //qDebug()<<__FUNCTION__<<"**22222222**"<<__LINE__<<"***p1**"<<p1<<endl; if(p1!=-1) { QStringstrget=strpro.mid(p1,3); //qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**strget**"<<strget<<endl; map.insert(p1,strget);//插入到QMAP中 strpro.replace(p1,3,"###"); qDebug()<<__FUNCTION__<<"***##***"<<__LINE__<<"***strpro**"<<strpro<<endl; } elseif(p1==-1) { xi++; } } /*******PRGIGOAIAODIDORIROSISOUIUOSRARPLR[P[,要放在获取完TIMER[]和DIV之后*********/ strchar="PR:GI:GO:AI:AO:DI:DO:RI:RO:SI:SO:UI:UO:SR:AR:PL:R[:P["; strList=strchar.split(":"); xi=0; while(xi<strList.length()) { p1=strpro.indexOf(strList[xi]); //qDebug()<<__FUNCTION__<<"**3333333**"<<__LINE__<<"***p1**"<<p1<<endl; if(p1!=-1) { p2=p1; while(1) { //qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***mid***p2**1**"<<strpro.mid(p2,1)<<endl; if(strpro.mid(p2,1)=="]") break; p2++; } QStringstrget=strpro.mid(p1,p2-p1+1); //qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**p2***"<<p2<<"**strget**"<<strget<<endl; map.insert(p1,strget);//插入到QMAP中 strpro.replace(p1,2,"@@"); //qDebug()<<__FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl; strget.replace(0,2,"@@");//因为PR[R[]]里面有两个R; qDebug()<<__FUNCTION__<<"****"<<__LINE__<<"***p1**"<<p1<<"**p2***"<<p2<<"**strget**"<<strget<<endl; intR=strget.indexOf("R");//判断字符串里面是否有R[],如果有应该替换掉,避免误认为是外面的R[]; if(R!=-1) { strpro.replace(p1+R,1,"@"); //qDebug()<<__FUNCTION__<<"***@@***"<<__LINE__<<"***strpro**"<<strpro<<endl; } } elseif(p1==-1) { xi++; } } /*********************获取“+-*/()****************/ strchar="+-*/()"; strList=strchar.split(""); xi=0; intop=0; while(xi<strList.length()) { op=strpro.indexOf(strList[xi]); //qDebug()<<__FUNCTION__<<"**4444444**"<<__LINE__<<"***op**"<<op<<endl; if(op!=-1) { QStringoper=strpro.mid(op,1); //qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***oper****"<<oper<<endl; map.insert(op,oper);//插入到QMAP strpro.replace(op,1,"#"); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<endl; } elseif(op==-1) { xi++; } } /***********获取LposJpos,要放在获取完+-*/()之后**************/ strchar="Lpos;Jpos"; strList=strchar.split(";"); xi=0; while(xi<strList.length()) { p1=strpro.indexOf(strList[xi]); //qDebug()<<__FUNCTION__<<"**555555**"<<__LINE__<<"***p1**"<<p1<<endl; if(p1!=-1) { QStringstrget=strpro.mid(p1,4); //qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***strget****"<<strget<<endl; map.insert(p1,strget);//插入到QMAP中 strpro.replace(p1,4,"@@@@"); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<endl; } elseif(p1==-1) { xi++; } } QMap<int,int>plMap; plMap.clear(); QVector<int>leftList; QVector<int>rightList; leftList.clear(); rightList.clear(); intplCount=0; intpcCount=0; /*********获取【i,j,k】**********/ for(inti=0;i<strpro.length();i++) { if('['==strpro[i]) { leftList.append(i); } elseif(']'==strpro[i]) { rightList.append(i); } } /***************获取相匹配的括号的位置*****************/ for(inti=leftList[pcCount];i<strpro.length();i++) { if('['==strpro[i]) { plCount++; } elseif(']'==strpro[i]) { plCount--; } if(0==plCount) { plMap.insert(leftList[pcCount],i); if(pcCount<leftList.size()-1) { i=leftList[++pcCount]-1; } } } qDebug()<<__LINE__<<"***STRPRO***"<<strpro<<endl; QStringtempStr; intfront_i,back_i; for(inti=0;i<plMap.size();i++) { front_i=plMap.keys().at(i); back_i=plMap.values().at(i); tempStr=strpro.mid(front_i,back_i-front_i+1); qDebug()<<__LINE__<<"****i**"<<i<<"**front_i***"<<front_i<<"***back_i***"<<back_i<<"***tempStr**"<<tempStr<<endl; intcount=tempStr.count(","); if(2==count) { //qDebug()<<"****i**"<<i<<"**front_i***"<<front_i<<"***back_i***"<<back_i<<"***tempStr**"<<tempStr<<endl; QStringstrget=tempStr; map.insert(front_i,strget); QStringstr; str.fill('@',back_i-front_i+1); strpro.replace(front_i,back_i-front_i+1,str); } } /**************获取常数项***************/ while(1) { p1=strpro.indexOf("#"); qDebug()<<__FUNCTION__<<"**66666**"<<__LINE__<<"***p1**"<<p1<<endl; if(p1==-1) { break; } qDebug()<<__FUNCTION__<<"**66666**"<<__LINE__<<"***p1+1**"<<strpro.mid(p1+1,1)<<endl; if(strpro.mid(p1+1,1)>="0"&&strpro.mid(p1+1,1)<="9") { p2=p1; while(1) { p2++; qDebug()<<__FUNCTION__<<"**66666**"<<__LINE__<<"***p2**"<<strpro.mid(p2,1)<<endl; if(strpro.mid(p2,1)=="#"||strpro.mid(p2,1)=="") { break; } } QStringstrget=strpro.mid(p1+1,p2-p1-1); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***strget****"<<strget<<endl; map.insert(p1+1,strget);//插入到QMAP中 } strpro.replace(p1,1,"$"); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<endl; } //第一个字符串为常数项的情况 if(!strpro.contains("$"))//全是数字,没有字母 { QStringstrget=strpro; map.insert(0,strget); } else { if(strpro.mid(0,1)>="0"&&strpro.mid(0,1)<="9") { p1=0; while(1) { p1++; if(strpro.mid(p1,1)=="$") { break; } } QStringstrget=strpro.mid(0,p1); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<"***strpro**"<<strpro<<"***strget****"<<strget<<endl; map.insert(0,strget);//插入到QMAP中 } } for(inti=0;i<map.size();i++) { qDebug()<<"***i=***"<<i<< "***key***"<<map.keys().at(i)<< "***value***"<<map.values().at(i)<<endl; } /********************将获得的每个字符串按顺序存储**********************/ intpi; intpj; intsum=0; floatval; QStringstrfile; QStringplStr; QMap<int,QString>::const_iteratormi; for(mi=map.constBegin();mi!=map.constEnd();++mi) { sum++; this[0].regcondition.resize(sum); if(this[0].type==AR_PAL_REG_INS) { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; strList=mi.value().split("["); if("PL"==strList[0]) { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].valueType=REG_PL; if(strList.size()>1) { if(strList[1]=="R") { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].rightVarType=INDIRECT; qDebug()<<"**strList[1]***"<<strList[2]<<endl; this[0].regcondition[sum-1].plVal=strToInt(strList[2]); } else { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].rightVarType=DIRECT; this[0].regcondition[sum-1].plVal=strToInt(strList[1]); } } } elseif("+"==strList[0]) { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].valueType=REG_ADD; } elseif("-"==strList[0]) { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].valueType=REG_DEC; } elseif(strList[0]=="(") { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].valueType=REG_LEFT_BRK; } elseif(strList[0]==")") { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].valueType=REG_RIGHT_BRK; } else { qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; this[0].regcondition[sum-1].valueType=REG_PL_CONST; plStr=mi.value(); if(2==plStr.count(",")) { plStr=plStr.remove(""); plStr=plStr.remove("["); plStr=plStr.remove("]"); qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; strList=plStr.split(","); for(intj=0;j<strList.size();j++) { if("*"==strList[j]) { strList[j]="-100000"; } elseif("2-0"==strList[j]) { strList[j]="20000"; } elseif("2-1"==strList[j]) { strList[j]="-20000"; } qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; QStringstr_i,str_j,str_k; str_i=strList[0]; str_j=strList[1]; str_k=strList[2]; qDebug()<<__FUNCTION__<<"******"<<__LINE__<<endl; if(str_i.contains("R")) { this[0].regcondition[sum-1].pl_iVarType=INDIRECT; this[0].regcondition[sum-1].i=strToInt(str_i); } else { this[0].regcondition[sum-1].pl_iVarType=DIRECT; this[0].regcondition[sum-1].i=strToInt(str_i); } if(str_j.contains("R")) { this[0].regcondition[sum-1].pl_jVarType=INDIRECT; this[0].regcondition[sum-1].j=strToInt(str_j); } else { this[0].regcondition[sum-1].pl_jVarType=DIRECT; this[0].regcondition[sum-1].j=strToInt(str_j); } if(str_k.contains("R")) { this[0].regcondition[sum-1].pl_kVarType=INDIRECT; this[0].regcondition[sum-1].k=strToInt(str_k); } else { this[0].regcondition[sum-1].pl_kVarType=DIRECT; this[0].regcondition[sum-1].k=strToInt(str_k); } } } } } else { strList=mi.value().split("[");//如:R[R[2]]->RR2]]; qDebug()<<__LINE__<<"****strList*******"<<strList[0]<<endl; if(strList[0]=="AR") { this[0].regcondition[sum-1].valueType=REG_AR; } elseif(strList[0]=="R") { this[0].regcondition[sum-1].valueType=REG_R; } elseif(strList[0]=="P") { this[0].regcondition[sum-1].valueType=REG_P; } elseif(strList[0]=="PR") { this[0].regcondition[sum-1].valueType=REG_PR; } elseif(strList[0]=="GI") { this[0].regcondition[sum-1].valueType=REG_GI; } elseif(strList[0]=="GO") { this[0].regcondition[sum-1].valueType=REG_GO; } elseif(strList[0]=="AI") { this[0].regcondition[sum-1].valueType=REG_AI; } elseif(strList[0]=="AO") { this[0].regcondition[sum-1].valueType=REG_AO; } elseif(strList[0]=="DI") { this[0].regcondition[sum-1].valueType=REG_DI; } elseif(strList[0]=="DO") { this[0].regcondition[sum-1].valueType=REG_DO; } elseif(strList[0]=="RI") { this[0].regcondition[sum-1].valueType=REG_RI; } elseif(strList[0]=="RO") { this[0].regcondition[sum-1].valueType=REG_RO; } elseif(strList[0]=="SI") { this[0].regcondition[sum-1].valueType=REG_SI; } elseif(strList[0]=="SO") { this[0].regcondition[sum-1].valueType=REG_SO; } elseif(strList[0]=="UI") { this[0].regcondition[sum-1].valueType=REG_UI; } elseif(strList[0]=="UO") { this[0].regcondition[sum-1].valueType=REG_UO; } elseif(strList[0]=="TIMER") { this[0].regcondition[sum-1].valueType=REG_TIMER; } elseif(strList[0]=="TIMER_OVERFLOW") { this[0].regcondition[sum-1].valueType=REG_TIMER_OVERFLOW; } elseif(strList[0]=="Lpos") { this[0].regcondition[sum-1].valueType=REG_Lpos; } elseif(strList[0]=="Jpos") { this[0].regcondition[sum-1].valueType=REG_Jpos; } elseif(strList[0]=="UFRAM") { this[0].regcondition[sum-1].valueType=REG_UFRAM; } elseif(strList[0]=="UTOOL") { this[0].regcondition[sum-1].valueType=REG_UTOOL; } //elseif(strList[0]=="PL") //{ //this[0].regcondition[sum-1].valueType=REG_PL; //} elseif(strList[0]=="MOD") { this[0].regcondition[sum-1].valueType=REG_MOD; } elseif(strList[0]=="DIV") { this[0].regcondition[sum-1].valueType=REG_DIV; } elseif(strList[0]=="+") { this[0].regcondition[sum-1].valueType=REG_ADD; } elseif(strList[0]=="-") { this[0].regcondition[sum-1].valueType=REG_DEC; } elseif(strList[0]=="*") { this[0].regcondition[sum-1].valueType=REG_MUL; } elseif(strList[0]=="/") { this[0].regcondition[sum-1].valueType=REG_DIVIVIDE; } elseif(strList[0]=="(") { this[0].regcondition[sum-1].valueType=REG_LEFT_BRK; } elseif(strList[0]==")") { this[0].regcondition[sum-1].valueType=REG_RIGHT_BRK; } else { this[0].regcondition[sum-1].valueType=REG_CONST; val=strToDouble(strList[0]); this[0].regcondition[sum-1].val=val; } /*************判断地址类型************/ if(strList.size()>1) { if(strList[1]=="R") { this[0].regcondition[sum-1].rightVarType=INDIRECT; } else { this[0].regcondition[sum-1].rightVarType=DIRECT; } } /***************存储i,j***************/ if(this[0].regcondition[sum-1].valueType!=REG_CONST) { strList=mi.value().split(",");//如R[2,4]->R[2;4]; pi=strToInt(strList[0]); this[0].regcondition[sum-1].i=pi; if(strList.size()>1) { this[0].regcondition[sum-1].valueType=REG_PR_IJ; pj=strToInt(strList[1]); this[0].regcondition[sum-1].j=pj; } } } } return1; }
以上为对字符串的提取解析以及运算操作
相关文章推荐
- MySQL 索引 总结
- 【Unity3d】碰撞检测
- openwrt 上cron的使用
- 李航《统计学习方法》第四章——用Python实现朴素贝叶斯分类器(MNIST数据集)
- Android开源:数据库ORM框架GreenDao学习(二)数据库查询篇
- NotImplementedError: stat.st_dev unsupported or native support failed to load
- 验证角谷的猜想
- Java常用排序算法/程序员必须掌握的8大排序算法
- [Java 安全]对称加密算法
- 计算天数差、获取当前时间、获取百分比
- Hibernate核心组件详解
- Codeforces-699B One Bomb
- CAS 4.0 Cluster / Tomcat redis Session manager / Token(TGT)存入Redis集群 / Nginx 负载
- pip 安装pandas报UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5错
- Text-Terminals on Linux
- 【SQLServer并发问题,先SELECT后UPDATE,避免并发脏读情况解决】
- 使用git pull文件时和本地文件冲突
- Combination Sum
- Tiling
- 静态路由和默认路由的理解