我正在尝试使用 C++ 将字符串分解为“符号”以进行进一步的工作。我已经很久没有用 C++ 写过任何东西了,所以如果这段代码本身有什么问题,请原谅我。
下面函数的目的symbolize()
是将一个字符串,例如“5+5”,分解成一个vector
字符串,例如{"5","+","5"}
。它不工作。如果您认为代码太乱,请提出一种简化它的方法。
到目前为止,这是我的代码:
#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
#include <sstream>
using namespace std;
vector<string> symbolize(string);
int main(int argc, const char * argv[])
{
string input;
cin >> input;
vector<string> symbols;
symbols = symbolize(input);
for(int i=0;i<symbols.size();i++){
cout<<symbols.at(i) << endl;
}
return 0;
}
vector<string> symbolize(string input){
int position = 0;
char c;
stringstream s;
vector<string> symbols;
enum symbolType {TEXT,OPERATOR}symbolType,charType;
while(position < input.size()){
c = input.at(position);
if(isalnum(c))symbolType = TEXT;
else symbolType = OPERATOR;
charType = symbolType;
while(symbolType == charType){
s << c;
position++;
if(position>=input.length())break;
c = input.at(position);
if(isalnum(c)) charType = TEXT;
else charType = OPERATOR;
}
symbols.push_back(s.str());
s.clear();
}
return symbols;
}
谢谢参观。
编辑:顺便说一句,我应该提到该函数返回第一个“令牌”,例如“5+5”->“5”
编辑2:我错了。我刚刚尝试了“5+5”,它返回了{"5","5+","5+5"}
。但是,它只返回空格前的第一个。对困惑感到抱歉!
编辑3:谢谢大家!对于那些将来可能会遇到此页面的人,这里是说完所有事情后的代码:
#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
#include <sstream>
using namespace std;
vector<string> symbolize(string);
int main(int argc, const char * argv[])
{
string input;
getline(cin,input);
vector<string> symbols;
symbols = symbolize(input);
for(int i=0;i<symbols.size();i++){
cout<<symbols.at(i) << endl;
}
return 0;
}
vector<string> symbolize(string input){
int position = 0;
char c;
//stringstream s;
vector<string> symbols;
enum symbolType {TEXT,OPERATOR}symbolType,charType;
while(position < input.size()){
stringstream s;
c = input.at(position);
if(isalnum(c))symbolType = TEXT;
else symbolType = OPERATOR;
charType = symbolType;
while(symbolType == charType){
s << c;
position++;
if(position>=input.length())break;
c = input.at(position);
if (isspace(c)||c=='\n'){position++; break;}
if(isalnum(c)) charType = TEXT;
else charType = OPERATOR;
}
symbols.push_back(s.str());
}
return symbols;
}