void classfyOp0(string str)
{
string Op[]={"+","-","*","!","=","<",">","&&","+="};
int len = sizeof(Op)/sizeof(Op[0]);
string oldstr = str;
string nochange = str;
string newstr;
int storeRes[20];
int site = 0;
int OpSite = 0;
int oldfindRes = 0;
while(str.length())
{
// int findRes = str.find(Op[Opsite]);
// int Oplen = Op[Opsite].length();
int findRes = str.find("*");
if(findRes!=-1)
{
int newfindRes = findRes+oldfindRes;
storeRes[site] = newfindRes;
cout<<"Res *"<<newfindRes<<endl;
site++;
oldfindRes = findRes+oldstr.length()-str.length()+1;
newstr = str.substr(findRes+1,oldstr.length());
str = newstr;
int l = str.length();
cout<<"str "<<str<<"\t"<<l<<endl;
}
}
}
每次找到后,就把从(找到的下一个字符)到(结尾)中间这一段字符串截取下来,继续寻找。如果找到,要计算它在整个大字符串的位置,存储下来。
例如,ab*c*de*f——目前已经识别了字符串中第二个*在第二次串(c*de*f)的位置,加上第一次被截去的长度(ab*的长度),这时等于oldfindRes。第三次截取字符串(de*f),然后回到while循环,此时的newfindRes也就是最后的结果,为第三个*在第三次串找到的位置加第三次串前面所有的长度(oldfindRes)