- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
<分区>
package PJ2;
import java.util.*;
public class SimpleLispExpressionEvaluator
{
// Current input Lisp expression
private String inputExpr;
// Main expression stack & current operation stack, see algorithm in evaluate()
private Stack<Object> exprStack;
private Stack<Double> currentOpStack;
// default constructor
// set inputExpr to ""
// create stack objects
public SimpleLispExpressionEvaluator()
{
// add statements
inputExpr = "";
exprStack = new Stack<Object>();
currentOpStack = new Stack<Double>();
}
// default constructor
// set inputExpr to inputExpression
// create stack objects
public SimpleLispExpressionEvaluator(String inputExpression)
{
// add statements
if(inputExpression == null)
{
throw new SimpleLispExpressionEvaluatorException();
}
inputExpr = inputExpression;
exprStack = new Stack<Object>();
currentOpStack = new Stack<Double>();
}
// set inputExpr to inputExpression
// clear stack objects
public void reset(String inputExpression)
{
// add statements
if(inputExpression == null)
{
throw new SimpleLispExpressionEvaluatorException();
}
inputExpr = inputExpression;
exprStack.clear();
currentOpStack.clear();
}
// This function evaluate current operator with its operands
// See complete algorithm in evaluate()
//
// Main Steps:
// Pop operands from exprStack and push them onto
// currentOpStack until you find an operator
// Apply the operator to the operands on currentOpStack
// Push the result into exprStack
//
private void evaluateCurrentOperation()
{
// add statements
String currOp;
boolean numeric = true;
do{
currOp = (String.valueOf(exprStack.pop()));
try{
Double number = Double.parseDouble(currOp);
currentOpStack.push(number);
}catch(NumberFormatException nfe){
numeric = false;
}
} while(numeric);
double result;
switch (currOp) {
case "*":
result = currentOpStack.pop();
while(!currentOpStack.isEmpty()){
result *= currentOpStack.pop();
}
break;
case "/":
result = currentOpStack.pop();
while(!currentOpStack.isEmpty()){
result /= currentOpStack.pop();
}
break;
case "+":
result = currentOpStack.pop();
while(!currentOpStack.isEmpty()){
result += currentOpStack.pop();
}
break;
case "-":
result = currentOpStack.pop();
while(!currentOpStack.isEmpty()){
result -= currentOpStack.pop();
}
break;
default:
result = currentOpStack.pop();
break;
}
exprStack.push(result);
}
/**
* This function evaluates current Lisp expression in inputExpr
* It return result of the expression
*
* The algorithm:
*
* Step 1 Scan the tokens in the string.
* Step 2 If you see an operand, push operand object onto the exprStack
* Step 3 If you see "(", next token should be an operator
* Step 4 If you see an operator, push operator object onto the exprStack
* Step 5 If you see ")", do steps 6,7,8 in evaluateCurrentOperation() :
* Step 6 Pop operands and push them onto currentOpStack
* until you find an operator
* Step 7 Apply the operator to the operands on currentOpStack
* Step 8 Push the result into exprStack
* Step 9 If you run out of tokens, the value on the top of exprStack is
* is the result of the expression.
*/
public double evaluate()
{
// only outline is given...
// you need to add statements/local variables
// you may delete or modify any statements in this method
// use scanner to tokenize inputExpr
Scanner inputExprScanner = new Scanner(inputExpr);
// Use zero or more white space as delimiter,
// which breaks the string into single character tokens
inputExprScanner = inputExprScanner.useDelimiter("\\s*");
// Step 1: Scan the tokens in the string.
while (inputExprScanner.hasNext())
{
// Step 2: If you see an operand, push operand object onto the exprStack
if (inputExprScanner.hasNextInt())
{
// This force scanner to grab all of the digits
// Otherwise, it will just get one char
String dataString = inputExprScanner.findInLine("\\d+");
// more ...
exprStack.push(Double.parseDouble(dataString));
}
else
{
// Get next token, only one char in string token
String aToken = inputExprScanner.next();
char item = aToken.charAt(0);
switch (item)
{
case '(':
break;
case ')':
evaluateCurrentOperation();
break;
case'*':
exprStack.push("*");
break;
case'+':
exprStack.push("+");
break;
case'/':
exprStack.push("/");
break;
case'-':
exprStack.push("-");
break;
// Step 3: If you see "(", next token shoube an operator
// Step 4: If you see an operator, push operator object onto the exprStack
// Step 5: If you see ")" do steps 6,7,8 in evaluateCurrentOperation() :
default: // error
throw new SimpleLispExpressionEvaluatorException(item + " is not a legal expression operator");
} // end switch
} // end else
} // end while
// Step 9: If you run out of tokens, the value on the top of exprStack is
// is the result of the expression.
//
// return result
return Double.parseDouble(String.valueOf(exprStack.pop()));
}
//=====================================================================
// This static method is used by main() only
private static void evaluateExprTest(String s, SimpleLispExpressionEvaluator expr)
{
Double result;
System.out.println("Expression " + s);
expr.reset(s);
result = expr.evaluate();
System.out.printf("Result %.2f\n", result);
System.out.println("-----------------------------");
}
// define few test cases, exception may happen
public static void main (String args[])
{
SimpleLispExpressionEvaluator expr= new SimpleLispExpressionEvaluator();
String test1 = "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))";
String test2 = "(+ (- 632) (* 21 3 4) (/ (+ 32) (* 1) (- 21 3 1)))";
String test3 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 1) (- 2 1 )))";
String test4 = "(+ (/2))";
String test5 = "(+ (/2 3 0))";
String test6 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 3) (- 2 1 ))))";
String test7 = "( + 100 )";
String test8 = "( + 100 5 5 5 )";
String test9 = "(*(/100 5)4 10)";
String test10 ="( + 100 )";
String test11 ="( + ( - 6 ) ( * 2 3 4 ) ( / ( + 3 ) ( * 1 ) ( - 2 3 1 ) ) )";
String test12 ="( + ( - 632 ) ( * 21 3 4 ) ( / ( + 32 ) ( * 1 ) ( - 21 3 1 ) ) )";
String test13 ="";
String test14 ="";
evaluateExprTest(test1, expr);
evaluateExprTest(test2, expr);
evaluateExprTest(test3, expr);
evaluateExprTest(test4, expr);
evaluateExprTest(test5, expr);
evaluateExprTest(test6, expr);
evaluateExprTest(test7, expr);
evaluateExprTest(test8, expr);
evaluateExprTest(test9, expr);
evaluateExprTest(test10, expr);
evaluateExprTest(test11, expr);
evaluateExprTest(test12, expr);
//evaluateExprTest(test13, expr);
//evaluateExprTest(test14, expr);
}
}
当我运行它时,仅测试 3、4、5、7、8、9、10 是否正常工作,基于:http://joeganley.com/code/jslisp.html
Expression (+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))
Result 28.50
-----------------------------
Expression (+ (- 632) (* 21 3 4) (/ (+ 32) (* 1) (- 21 3 1)))
Result 885.88
-----------------------------
Expression (+ (/ 2) (* 2) (/ (+ 1) (+ 1) (- 2 1 )))
Result 5.00
-----------------------------
Expression (+ (/2))
Result 2.00
-----------------------------
Expression (+ (/2 3 0))
Result Infinity
-----------------------------
Expression ( + 100 )
Result 100.00
-----------------------------
Expression ( + 100 5 5 5 )
Result 115.00
-----------------------------
Expression (*(/100 5)4 10)
Result 800.00
-----------------------------
Expression ( + 100 )
Result 100.00
-----------------------------
Expression ( + ( - 6 ) ( * 2 3 4 ) ( / ( + 3 ) ( * 1 ) ( - 2 3 1 ) ) )
Result 28.50
-----------------------------
Expression ( + ( - 632 ) ( * 21 3 4 ) ( / ( + 32 ) ( * 1 ) ( - 21 3 1 ) ) )
Result 885.88
-----------------------------
这些结果仅在我删除测试 6 时出现,因为它给出了一个异常并停止执行。知道我哪里出错了吗?
如果 6 未注释掉则出错:
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at java.util.Stack.pop(Unknown Source)
at PJ2.SimpleLispExpressionEvaluator.evaluateCurrentOperation(SimpleLispExpressionEvaluator.java:143)
at PJ2.SimpleLispExpressionEvaluator.evaluate(SimpleLispExpressionEvaluator.java:248)
at PJ2.SimpleLispExpressionEvaluator.evaluateExprTest(SimpleLispExpressionEvaluator.java:292)
at PJ2.SimpleLispExpressionEvaluator.main(SimpleLispExpressionEvaluator.java:321)
我正在用 C 写一个高级计算器。正如你所猜到的,它目前有很多函数,我使用一个开关来对每个函数名进行适当的操作。它是这样的: switch(hash_of(function_name_currently
在大约四天的时间里,我一直在收集托管应用程序的性能计数器。在此期间,发生了以下垃圾回收: 第 0 代:133,695 第一代:133,413 第 2 代:133,254 其中一些是使用GC.Colle
我构建了这个: [ Workshop_templates 表 ] id_template | Workshop_name 1 | Conflict resolution 2 | Building tr
我有一个 N 的列表元素,我想抽样 M ( N/2 . IE。当超过一半的值被采样时。但它非常适合 M N/2 时反转问题。 : 注意:这实际上是创建一个大小为 N 的屏蔽列表对于 M 是 Fals
伙计们,我是竞争性编程的新手,我遇到了一个小问题在提供输入的同时在问题中,顶点数从 1 到 n但是我编写程序时考虑到节点是从 0 开始的 但是当我通过从每个边的每个顶点减少 1 来输入测试用例时,我的
在一次旅行中,有多个停靠点,(一个停靠点 = 一个或多个订单加载或交付的地址),按特定顺序排列。 例如: Trip A Trip_order Action Place Ord
我有一个关于由微服务组成的应用程序架构的问题。 我的微服务很少,但在这个问题的上下文中有趣的是: 人力资源 - 这里存储了所有用户数据,如用户名、性别、用户体验等。 工作机会 - 这里存储了每个招聘广
假设我的工作空间(全局环境)中有许多对象,并且我想将大多数对象存储在列表中。这是一个简化的示例: # Put some objects in the workspace A <- 1 B <- 2 C
当我获得与本地时间相同的时间戳时,firebase 生成的服务器时间戳是否会自动转换为本地时间,或者我错过了什么? _firestore.collection("9213903123").docume
我需要帮助才能将未知整数分成给定数量的偶数部分——或者至少尽可能地均匀。各部分之和应为原值,但各部分应为整数,且应尽可能接近。 参数 num: Integer - 应该被分成相等部分的数字 parts
我的 Java 程序中有一个带有 JPanel 的 ScrollPane,它附加了大量文本。我需要 ScrollPane 在每次添加后滚动到最底部。我对以下代码的问题是它“几乎”滚动到底部但不是一直滚
我想检查两个稀疏数组是否(几乎)相等。而对于 numpy 数组,你可以这样做: import numpy as np a = np.ones(200) np.testing.assert_array_
我有以下一组几乎相同的 each 语句。我需要添加大约 20 个遵循类似模式的内容。我正在尝试找出如何获取小变量并将它们更新为单个语句(而不是 20 次相同但略有不同的内容)。 $.each(main
所以我想获取两个字典中(几乎)匹配的键的值并将它们连接起来。我尝试过: dict3 = {key:dict1[key].strip() for key in dict2.keys() if key.p
我的表看起来像这样: | id (int) | sentence (varchar) | 我想找到除了一个特定单词之外几乎相同的所有行。例如: | 230 | test | | 321 | test
起始情况:MS SQL 中有一个现有的数据库模式,它与 MySQL 中的现有模式完全相同(数据库优先 - 无法更改,因为已广泛安装)。但是,它们在用于相应列的数据类型方面可能略有不同。该数据库系统必须
对于复杂的元素,一个很好的做法是(几乎)总是在闭包中定义 Polymer 以保持所有只应在内部修改的变量和方法私有(private),而不是将它们附加到元素(例如 'this ')? 喜欢以下内容:
我正在解析 Java 中的 RestAssured 调用,该调用返回对象列表。如果我使用此代码,Idea 会生成未经检查的分配警告: List availableInventories = ListP
我真的被难住了。我所拥有的是一个样式化为矩形的 div,其中包含作为页面主要标题的文本。相关代码如下: HTML: SIN CSS: h1 { text-align:right
我需要将单选按钮及其旁边的文本包装在标签中,只是为了更加用户友好。 几天前我遇到了类似的问题,我有一个复选框,并且在我有一个 span 元素之后立即出现。我可以包装这两个元素。 我有这个 HTML:
我是一名优秀的程序员,十分优秀!