闭包实现计算器:Calculator
2023-12-26 13:48:10
python
def calculator():
result = 0
def add(x):
nonlocal result
result += x
return result
def minus(x):
nonlocal result
result -= x
return result
def times(x):
nonlocal result
result *= x
return result
def divide(x):
nonlocal result
result = int(result / x)
return result
keys = ('add', 'minus', 'times', 'divide')
functions = (add, minus, times, divide)
tuple_list = list(zip(keys, functions))
return dict(tuple_list)
c1 = calculator()
print(c1['add'](10)) // 10
print(c1['times'](15)) //150
print(c1['divide'](3)) //50
print(c1['minus'](2)) // 48
c2 = calculator()
print(c2['add'](3)) // 3
java
public class Closure4Java {
private static Map<String, Function<Integer, Integer>> createCalculator() {
// 使用包含可变状态的对象模拟闭包
class MutableResult {
private int result = 0;
}
MutableResult mutableResult = new MutableResult();
// 定义四个操作函数,并将其添加到映射中
Map<String, Function<Integer, Integer>> operations = new HashMap<>();
operations.put("add", x -> {
mutableResult.result += x;
return mutableResult.result;
});
operations.put("minus", x -> {
mutableResult.result -= x;
return mutableResult.result;
});
operations.put("times", x -> {
mutableResult.result *= x;
return mutableResult.result;
});
operations.put("divide", x -> {
mutableResult.result /= x;
return mutableResult.result;
});
return operations;
}
public static void main(String[] args) {
Map<String, Function<Integer, Integer>> c1 = createCalculator();
System.out.println(c1.get("add").apply(3)); // 3
System.out.println(c1.get("times").apply(7)); //21
System.out.println(c1.get("divide").apply(2)); //10
Map<String, Function<Integer, Integer>> c2 = createCalculator();
System.out.println(c2.get("add").apply(4)); //4
}
}
groovy-script
def calculator() {
def result = 0
def add = { x ->
result += x
result
}
def minus = { x ->
result -= x
result
}
def times = { x ->
result *= x
result
}
def divide = { x ->
result /= x
result
}
// 创建包含闭包的映射
def operations = [
'add': add,
'minus': minus,
'times': times,
'divide': divide
]
return operations
}
// 使用闭包计算器
def c1 = calculator()
println c1['add'](5) // 输出: 5
println c1['minus'](3) // 输出: 2
println c1['times'](4) // 输出: 8
println c1['divide'](2) // 输出: 4
def c2 = calculator()
println c2['add'](3) // 输出: 3
groovy-class
class Closure4Groovy2 {
static class Calculator {
private int result = 0
def add = { x ->
result += x
result
}
def minus = { x ->
result -= x
result
}
def times = { x ->
result *= x
result
}
def divide = { x ->
result /= x
result
}
def getOperations() {
[
'add': add,
'minus': minus,
'times': times,
'divide': divide
]
}
}
static void main(String[] args) {
// 使用闭包计算器
def c1 = new Calculator()
def operations = c1.getOperations()
println operations['add'](5) // 输出: 5
println operations['minus'](3) // 输出: 2
println operations['times'](4) // 输出: 8
println operations['divide'](2) // 输出: 4
}
}
文章来源:https://blog.csdn.net/it_freshman/article/details/135213818
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!