我刚刚通过以下问题为研究生 C++ 开发人员进行了测试。它并不顺利,因为我无法确定完成任务的明确方式。时间限制也没有帮助。我对经验丰富的开发人员如何解决以下问题感兴趣 - 在伪代码或示例代码中:
Evaluate
Write a function in C or C++ that evaluates the result of a simple expression.
The function should ignore whitespace, but stop at the first non valid character.
Valid tokens are listed in the table below:
0-9 - Only integers are allowed in expressions
() - Nested expressions should be evaluated first.
+, -, *, / - Basic operators are addition, subtraction, multiplication and division.
The expression should be parsed from left to right. It is not necessary to consider operator precedence in your solution (e.g. 1 + 3 * 4 = 16). If there is an error in the expression, the function should return false.
Suggested prototype for function:
Example:
bool evaluate(const char *expression, int &result)
{
...
}
**Input**
1+3
(1 + (12 * 2)
**Result**
4
N/A
**Return code**
true
false (missing bracket)
此外,这是我未能成功完成的第二个 C++。有 1 年的实习经验和 1 年的 C++ 学术经验,但我还没有为其中一些测试做好准备。是否有任何推荐的资源可以让我尝试解决诸如此类的问题以获得更多的“测试”经验?