评估一个简单的字符串数学表达式 [英] Evaluate a simple string mathmatical expression

查看:125
本文介绍了评估一个简单的字符串数学表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚对一个研究生C ++开发人员进行测试,问题如下。它没有太好,因为我不能决定一个明确的方式完成任务。时间限制也没有帮助。我对有经验的开发人员如何解决伪代码或示例代码中的以下问题感兴趣:

 评估

在C或C ++中编写一个函数,用于计算简单表达式的结果。
该函数应该忽略空格,但停在第一个非有效字符。
有效的令牌列在下表中:

0-9 - 表达式中只允许使用整数

() - 应首先计算嵌套表达式。

+, - ,*,/ - 基本运算符是加法,减法,乘法和除法。

表达式应该从左到右进行解析。在解决方案中没有必要考虑运算符优先级(例如1 + 3 * 4 = 16)。如果表达式中存在错误,则函数应返回false。

建议的函数原型:

示例:

bool evaluate(const char * expression,int& result)
{
...
}

**输入**
1 + 3
(1 +(12 * 2)
$ b b **结果**
4
不适用

**返回代码**
true
false(缺少括号)

此外,这是我未能成功完成的第二个C ++。是否有任何推荐的资源,我可以努力解决这样的问题,以获得更多的测试体验?

解决方案

这是一个简单的扫描推送应用(扭曲是大括号)。


  1. 查找数字:

    • 如果您看到一个数字推到堆栈

    • 'push it to stack and goto 1

    • 否则会出现错误。


  2. 查找操作:

    • 如果您看到操作将其推入堆栈

    • 否则会出现错误


  3. 查找数字:

    • 如果您看到一个数字推到堆栈

    • 如果您看到一个'('推入堆栈和 goto 1

    • 否则会出现错误


      • 从堆栈中弹出最后三个项目(应为数字op编号)


        • 结果到堆栈。


      • 现在的复杂位:

        • 查看下一个字符是否
        • 如果没有更多输入 goto 7

          • 其他 goto 2


        • 项目在堆栈上有你的结果。

          • 否则会出现错误。


PopCode


  1. 从堆叠中弹出最后两个值。应为'(数字

    • 如果不是,则会出现错误


  2. 扔掉'('

  3. 如果堆栈顶部是op push值 goto 4(上面)

  4. 否则将值推入堆栈 goto 5(上面)

完成后应该有一个数字



示例:

  1 + 3 
规则1:push 1 stack ='1'
规则2:push + stack ='1 +'
规则3:push 3 stack ='1 + 3'
规则4: pop和do:stack ='4'
规则5:无堆栈='4'
规则6:goto 7 stack ='4'
规则7:stack ='4'

(1 +(12 * 2)
规则1:push(goto 1 stack ='('
Rule 1:push 1 stack ='(1'
Rule 2:push + stack ='(1 +'
规则3:push(goto 1 stack ='(1 +('
Rule 1:push 12 stack ='(1 +(12'
规则2:push * stack ='(1 +(12 *'
规则3:push 2 stack ='(1 +(12 * 2'
)规则4:Pop and do:stack = '1 +(24'
规则5:Do'PopCode'stack ='(1 +(24'
Pop 1:Pop 2 stack ='(1 +'
Pop 2: Holding 24 stack ='(1 +'
Pop 3:push 24 goto 4 stack ='(1 + 24'
规则4:Pop and do stack ='(25'
Rule 5 :Nothing stack ='(25'
规则6:goto 7 stacj ='(25'
规则7:多于1项错误

重新使用正确的公式
(1 +(12 * 2))
规则1:push(goto 1 stack ='('
Rule 1:push 1 stack ='(1'
Rule 2: push + stack ='(1 +'
Rule 3:push(goto 1 stack ='(1 +('
Rule 1:push 12 stack ='(1 +(12'
规则2:push * stack ='(1 +(12 *'
)规则3:push 2 stack ='(1 +(12 * 2'
)规则4:Pop and do:stack =' 1 +(24'
Rule 5:Do'PopCode'stack ='(1 +(24'
Pop 1:Pop 2 stack ='(1 +'
Pop 2:Holding 24 stack ='(1 +'
Pop 3:push 24 goto 4 stack ='(1 + 24'
Rule 4:Pop and do stack ='(25'
Rule 5:Do 'popCode'stack ='(25'
Pop 1:Pop 2 stack =''
Pop 2:holding 25 stack =''
Pop 3:Nothing。 stack =''
Pop 4:push 25 goto 5 stack ='25'
规则5:没有stack ='25'
规则6:goto 7 stack ='25'
规则7:结果= 25


I've just taken a test for a graduate C++ developer with the question below. It didn't go too well as I couldn't decide on a clear way of completing the task. Time limit didn't help either. I'm interested in how experienced developers would have tackled the follow problem - in pseudo or sample code:

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)

In addition, this is the 2nd C++ that I've failed to complete successfully. Have had 1 year intership experiece and 1 year academic experiece using C++, but I'm not prepared for some of these tests. Are there any recommended resources where I can attept to solve problems such as this one in order to gain more 'testing' experience?

解决方案

This is a simple scan push apply (the twist is the braces).

  1. Look for a number:
    • If you see a number push onto stack
    • if you see a '(' push it onto stack and goto 1
    • Otherwise an error.
  2. Look for an op:
    • If you see an op push it onto stack
    • Otherwise an error
  3. Look for a number:
    • If you see a number push onto stack
    • If you see a '(' push onto stack and goto 1
    • Otherwise an error
  4. pop last three items from the stack (should be number op number)
    • do the operation and push the result onto the stack.
  5. Now the complex bit:
    • Peek to see if the next character is a ')' if it is goto "PopCode" below.
  6. If no more input goto 7.
    • Otherewise goto 2
  7. If only one item on the stack you have your result.
    • Otherwise an error.

PopCode

  1. Pop last two values from the stack. Should be '( Number'
    • If it is not then an error
  2. Throw away the '('
  3. If the top of the stack is an op push value goto 4 (above)
  4. Otherwise push the value onto the stack goto 5 (above)

When finished there should be one number on the stack.

Example:

1+3
Rule 1: push 1             stack = '1'
Rule 2: push +             stack = '1 +'
Rule 3: push 3             stack = '1 + 3'
Rule 4: pop and do:        stack = '4'
Rule 5: Nothing            stack = '4'
Rule 6: goto 7             stack = '4'
Rule 7:                    stack = '4'

(1 + (12 * 2)
Rule 1: push ( goto 1      stack = '('
Rule 1: push 1             stack = '( 1'
Rule 2: push +             stack = '( 1 +'
Rule 3: push ( goto 1      stack = '( 1 + ('
Rule 1: push 12            stack = '( 1 + ( 12'
Rule 2: push *             stack = '( 1 + ( 12 *'
Rule 3: push 2             stack = '( 1 + ( 12 * 2'
Rule 4: Pop and do:        stack = '( 1 + ( 24'
Rule 5: Do 'PopCode'       stack = '( 1 + ( 24'
Pop  1: Pop 2              stack = '( 1 +'
Pop  2: Holding 24         stack = '( 1 +'
Pop  3: push 24 goto 4     stack = '( 1 + 24'
Rule 4: Pop and do         stack = '( 25'
Rule 5: Nothing            stack = '( 25'
Rule 6: goto 7             stacj = '( 25'
Rule 7: More than 1 item error

Re-Doing with correct formula
(1 + (12 * 2))
Rule 1: push ( goto 1      stack = '('
Rule 1: push 1             stack = '( 1'
Rule 2: push +             stack = '( 1 +'
Rule 3: push ( goto 1      stack = '( 1 + ('
Rule 1: push 12            stack = '( 1 + ( 12'
Rule 2: push *             stack = '( 1 + ( 12 *'
Rule 3: push 2             stack = '( 1 + ( 12 * 2'
Rule 4: Pop and do:        stack = '( 1 + ( 24'
Rule 5: Do 'PopCode'       stack = '( 1 + ( 24'
Pop  1: Pop 2              stack = '( 1 +'
Pop  2: Holding 24         stack = '( 1 +'
Pop  3: push 24 goto 4     stack = '( 1 + 24'
Rule 4: Pop and do         stack = '( 25'
Rule 5: Do 'PopCode'       stack = '( 25'
Pop  1: Pop 2              stack = ''
Pop  2: holding 25         stack = ''
Pop  3: Nothing.           stack = ''
Pop  4: push 25 goto 5     stack = '25'
Rule 5: Nothing            stack = '25'
Rule 6: goto 7             stack = '25'
Rule 7: Result = 25

这篇关于评估一个简单的字符串数学表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆