评估者表达式:如何知道输入是否错误

时间:2016-10-21 07:32:43

标签: java stack evaluator

我正在解决下一个技术问题(Q1):http://blog.sdeskills.com/qotd-2016-oct-17-resistance-is-futile/

这几乎已经完成,只有一项任务正在等待中。评估输入是否平衡。检查括号是否有序,已完成,但不是为了评估标记。

  

在给定的子网中不能混合串联/并联   连接,所以(500 + 200 | 300)是不允许的。

这是我目前的代码:https://repl.it/EC3i/2有关如何将前一个表达式评估为错误的想法吗?

1 个答案:

答案 0 :(得分:1)

试试这个。此代码检查运算符sereis以及平衡括号。

@Test
public void testIsBalanced() {
    assertTrue(isBalanced("(2)"));
    assertTrue(isBalanced("(2+3+3)"));
    assertTrue(isBalanced("2+3+3"));
    assertTrue(isBalanced("2+(4|5|5)+3"));
    assertTrue(isBalanced("2+(4|(2+3+4)|5)+3"));
    assertTrue(isBalanced("(2)+3()"));
    assertFalse(isBalanced("(2"));
    assertFalse(isBalanced("(2))"));
    assertFalse(isBalanced("((2)"));
    assertFalse(isBalanced("2|3+3"));
    assertFalse(isBalanced("2+(4|5+5)+3"));
    assertFalse(isBalanced("2+3|3"));
}

和JUnit测试代码。

Started InternetExplorerDriver server (32-bit)
2.53.1.0
Listening on port 16183
Only local connections are allowed
Oct 21, 2016 10:14:12 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Oct 21, 2016 10:14:12 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to straight W3C remote end connection
Oct 21, 2016 10:14:12 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to original OSS JSON Wire Protocol.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}], required capabilities = null
Build info: version: 'unknown', revision: '3169782', time: '2016-09-29 10:24:50 -0700'
System info: host: 'BWT12654001', ip: '10.52.132.157', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_101'
Driver info: driver.version: InternetExplorerDriver
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:80)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:141)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:242)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:228)
    at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:180)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:172)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:144)
    at mypackage.TestIEBrowser.main(TestIEBrowser.java:33)