📄 stringtokenizer.cpp
字号:
/*
TP2 LP INTERPRETADOR TINY
ALUNO : ANDRE LUIS DURAO ABDO
MATRICULA : 283999
DATA DA ENTREGA : 10/05/2007
*/
//StringTokenizer.cpp
#include "StringTokenizer.h"
StringTokenizer::StringTokenizer(const string & str, const string & delim){
pos = 0;
string::size_type lastPos=str.find_first_not_of(delim,0);
string::size_type pos=str.find_first_not_of(delim,lastPos);
//string::size_type posDelim=str.find_first_of(delim,0);
while(string::npos!=pos || string::npos != lastPos){
//substring de c++ substring(inicio,comprimento)
string tmp1 = str.substr(lastPos,pos-lastPos);
tokens.push_back(tmp1);
//cout << "pos = "<<pos<<" lastPos = "<<lastPos<<" -> ["<<tmp1<<"]\n";
//tokens.push_back(str.substr(lastPos,pos-lastPos));
lastPos=str.find_first_not_of(delim,pos);
/*enquanto as proximas posicoes de pos forem delimitadores
continua inserindo delimitadores no vetor*/
if(string::npos!=pos && string::npos != lastPos)
for (int i=pos;i<lastPos;i++){
string tmp2 = str.substr(i,1);
tokens.push_back(tmp2);
}
/*Verifica se antes do final da string existem ocorrencias de
delimitadores, cc lastpos pode ir para o final da string e nao
encontra tokens como no caso x++ => [x] [+] [+]*/
if(string::npos!=pos && string::npos == lastPos)
for (int i=pos;i<str.length();i++){
string tmp2 = str.substr(i,1);
tokens.push_back(tmp2);
}
//cout << "pos = "<<pos<<" lastPos = "<<lastPos<<" -> {"<<tmp1<<"}\n";
pos=str.find_first_of(delim,lastPos);
}//while
}
//implementar hasmoretoken
bool StringTokenizer::hasMoreTokens(){
//caso pos esteja ate o ultimo elemento
return ( pos < (tokens.size()-1) );
}
//implementar nexttoken
string StringTokenizer::nextToken(){
if ( hasMoreTokens() ){
pos++;
return tokens.at(pos);
}
}
//alterado
void StringTokenizer::previousToken(){
if ( pos>0 ){
pos--;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -