简体   繁体   English

试图了解调车场算法

[英]Trying to understand the Shunting Yard Algorithm

I am trying to do the Shunting-yard algorithm so I started researching about it. 我正在尝试做Shunting-yard算法,因此我开始对其进行研究。 While doing so, I found some interesting documentation which I do not really understand: 这样做时,我发现了一些我不太了解的有趣文档:

    // Current token is a number, push 
    // it to stack for numbers. 
    else if(isdigit(tokens[i])){ 
        int val = 0; 

        // There may be more than one 
        // digits in number. 
        while(i < tokens.length() && 
                    isdigit(tokens[i])) 
        { 
            val = (val*10) + (tokens[i]-'0'); 
            i++; 
        } 

        values.push(val); 
    } 

I do not understand why inside the while , the variable val is being multiplied by 10 ( val=(val*10) ). 我不明白为什么在while ,变量val被乘以10( val=(val*10) )。 Can someone help me understand why the algorithm has to do this? 有人可以帮助我理解为什么算法必须这样做吗?

Because otherwise you'd just add the digits. 因为否则,您只需要添加数字即可。 Say for instance you want 123 : you get 1 , multiply with 10 to get 10 , add 2 to get 12 , multiply with 10 to get 120 , then add 3 to get 123 . 假设您要123 :得到1 ,乘以10得到10 ,再加上2得到12 ,乘以10得到120 ,然后再加上3得到123

If you omitted the multiplication by 10 , you'd just get 1 + 2 + 3 == 6 instead. 如果您忽略了乘以10的乘法运算,则只会得到1 + 2 + 3 == 6

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

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