簡體   English   中英

試圖了解調車場算法

[英]Trying to understand the Shunting Yard Algorithm

我正在嘗試做Shunting-yard算法,因此我開始對其進行研究。 這樣做時,我發現了一些我不太了解的有趣文檔:

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

我不明白為什么在while ,變量val被乘以10( val=(val*10) )。 有人可以幫助我理解為什么算法必須這樣做嗎?

因為否則,您只需要添加數字即可。 假設您要123 :得到1 ,乘以10得到10 ,再加上2得到12 ,乘以10得到120 ,然后再加上3得到123

如果您忽略了乘以10的乘法運算,則只會得到1 + 2 + 3 == 6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM