为什么"operand = (operand*10) + (expression[i] - '0');"
Why "operand = (operand*10) + (expression[i] - '0');"
为什么下面的代码会operand * 10
? 为什么它不只做operand = (expression[i] - '0');
而不是operand = (operand*10) + (expression[i] - '0');
else if(IsNumericDigit(expression[i]))
{
int operand = 0;
while(i<expression.length() && IsNumericDigit(expression[i]))
{
operand = (operand*10) + (expression[i] - '0');
// why is he doing operand *10?
// example : if have a string 2 3 * 3 4 +.
// It is pushing 23 on stack rather than 2 and 3
i++;
}
i--;
S.push(operand);
}
因为它被告知。假设你有 2 3 4 5 i 你的表达式数组。然后,您的代码operand = (operand*10) + (expression[i] - '0');
将执行以下操作:
operand = (0*10) + (2-0)
operand = (2*10) + (3-0)
operand = (23*10) + (4-0)
operand = (234*10) + (5-0)
因此,最后您的操作数将包含数字 2345。这就像你在纸上写下数字一样。首先你写下 2,然后在它之后写 3。现在你有 23,与 20 + 3 相同。因此乘以 10。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 为什么使用 "this" 指针调用派生成员函数?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么比较运算符如此快速
- 为什么 Serial.println(<char[]>);返回随机字符?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么不;名字在地图上是按顺序排列的吗
- 我的字符计数代码计算错误.为什么
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 为什么"operand = (operand*10) + (expression[i] - '0');"
- 为什么这句话"The expression can be used only as the left-hand operand of a member function call"在 [expr.re
- 为什么编译器显示错误"no match for 'operator[]' (operand types are 'carti' and 'int')"
- 将链接哈希表初始化为 NULL。获取"lvalue required as left operand of assignment"错误。为什么?这是我的代码: