0
我試圖將標記放入隊列中。但是,當我嘗試輸入一個具有多個數字的數字(即10,123)時,它會將它們讀取爲單獨的數字。有人能告訴我我做錯了什麼嗎?我試過管柱嵌和追加和他們都不工作問題將字符串添加到字符串
queue <string> getTokens(string token){
int a = (int) token.length();
string temp;
queue <string> numbers;
char t;
for (int i =0; i <a; i++){
t = token[i];
while (!isdigit(t)){
if (t=='+' || t=='-' || t=='/' || t=='*' || t=='^'){
string temp1;
temp1 += t;
numbers.push(temp1);
temp1.clear();
}
else if (t=='(' || t==')'){
string temp1;
temp1 += t;
numbers.push(temp1);
temp1.clear();
}
else if (!isalpha(token[i-1]) && t == 'e' && !isalpha(token[i+1])){
string e = "2.718";
numbers.push(e);
}
else if (!isalpha(token[i-1]) && t == 'p' && token[i+1]== 'i' && !isalpha(token[i+2])){
string pi = "3.14169";
numbers.push(pi);
}
break;
}
//if it is one single number
if (!isdigit(token[i-1]) && isdigit(t) && !isdigit(token[i+1])){
string tt;
tt += t;
numbers.push(tt);
}
//if there is more than one number
else if ((isdigit(t) && isdigit(token[i+1])) || (isdigit(token[i-1]) && isdigit(t))){ //if it is a number
string temp2;
string temp3="k";
string temp4;
//cout << t;
//int j=1;
if(isdigit(token[i])){
temp2 += t;
cout<<"temp2 : "<<temp2<<endl;
cout <<"temp3 :" << temp3<<endl;
//temp2.clear();
temp3 +=temp2;
}
temp4.append(temp3);
temp4 +=temp3;
//cout<<"hi"<<endl;
cout << "This is temp4: " << temp4 <<endl;
//cout << "this is temp3: " << temp3<< endl;
//temp2.clear();
//cout<<temp2 << "yo";
//temp3.assign(temp2);
//cout << "temp3 is : "<< temp3;
}
else
continue;
}
return numbers;
}
int main(){
string expression;
getline(cin,expression);
cout << expression;
queue <string> ts;
ts= getTokens(expression);
}
護理解釋一下這個代碼是意欲何爲?這是在某個地方的循環?什麼是T?什麼是令牌? – mascoj
我們需要更多信息來幫助您。 –
請閱讀[mcve]並嘗試提供一個。你在這裏顯示的代碼可以從字面上做任何事情,因爲我們不知道任何變量是什麼類型(除了'temp2')或它們是如何初始化的 – user463035818