简体   繁体   English

问题添加字符串到字符串

[英]issue Adding strings to string

I am trying to put tokens into a queue. 我正在尝试将令牌放入队列。 However, when I try to put in a number that has more than one digit (ie, 10, 123 ) it reads them as separate digits. 但是,当我尝试输入一个多于一个数字(即10、123)的数字时,它会将它们读取为单独的数字。 Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗? I've tried string insert and append and none of them seem to work 我试过了字符串插入和追加,但它们似乎都不起作用

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);
}

At every Iteration temp2 temp3 temp4 variable's value will get reset so it does not hold previous iteration's value. 在每个迭代temp2 temp3 temp4变量的值都将被重置,因此它不保存先前迭代的值。 I think this is where you might be facing problem. 我认为这是您可能面临的问题。 try to declare it outside for loop and clear it after you obtain desirable output, for next use 尝试在循环外声明它,并在获得所需的输出后清除它,以备下次使用

//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;

I hope this might help 我希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM