[英]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.