数学期望计算器

2023/12/23
更新,添加清除按钮,百分比,快捷键操作,修改初始窗口大小。

2023/12/22
嫌整包打包Qt5太大,换Tkinter折腾了半天,调窗口大小和缩放,效果太差,不换了。

2023/12/21
(谁能想到搓个计算器的原因是出于打游戏呢

今天上CS2炼金,找了个许久之前用过的方子炼一炉黑龙Glock,出货率15%,果不其然的炸炉了,想了想当炼金术士这么久也没仔细算过期望值,都是大概估一眼就开了,于是决定搞搞这个,搓个计算器方便以后用。

关于数学期望:▷ 数学期望(或期望值) (statorials.org)

放到炼金这里 计算公式就是:
期望值=∑(产出值×产出概率)−成本

PyQt5的简单GUI,这种小打小闹就不往Github上再建个仓库了,直接贴blog。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QGridLayout
from PyQt5.QtCore import Qt

class CustomLineEdit(QLineEdit):
def keyPressEvent(self, event):
if event.key() in [Qt.Key_Plus, Qt.Key_Minus]:
# 将按键事件传递给主窗口处理
self.parent().keyPressEvent(event)
else:
# 对于其他按键事件,使用默认处理
super().keyPressEvent(event)

class ExpectedValueCalculator(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.value_entries = []
self.setGeometry(300, 300, 260, 220)
self.setWindowTitle('数学期望计算器')

# 创建布局
layout = QVBoxLayout()

# 成本输入
cost_layout = QHBoxLayout()
cost_layout.addStretch()
cost_layout.addWidget(QLabel('成本:'))
self.cost_entry = CustomLineEdit(self)
cost_layout.addWidget(self.cost_entry)
cost_layout.addStretch()
layout.addLayout(cost_layout)

# 几率和产出价值输入
self.grid_layout = QGridLayout()
self.grid_layout.addWidget(QLabel('几率%'), 0, 0)
self.grid_layout.addWidget(QLabel('产出价值'), 0, 1)
layout.addLayout(self.grid_layout)

# 添加和删除按钮布局
btn_layout = QHBoxLayout()
btn_layout.addStretch()
clear_btn = QPushButton('清除', self)
clear_btn.clicked.connect(self.clear_entries)
btn_layout.addWidget(clear_btn)
add_btn = QPushButton('+', self)
add_btn.clicked.connect(self.add_value_entry)
btn_layout.addWidget(add_btn)
remove_btn = QPushButton('-', self)
remove_btn.clicked.connect(self.remove_value_entry)
btn_layout.addWidget(remove_btn)
btn_layout.addStretch()
layout.addLayout(btn_layout)

# 计算按钮和结果显示
calc_layout = QHBoxLayout()
calc_layout.addStretch()
calc_btn = QPushButton('计算', self)
calc_btn.clicked.connect(self.calculate_expected_value)
calc_layout.addWidget(calc_btn)
calc_layout.addStretch()
layout.addLayout(calc_layout)

self.result_label = QLabel('数学期望: 0')
layout.addWidget(self.result_label)

self.setLayout(layout)
self.add_value_entry()
self.add_value_entry()
self.add_value_entry()

def keyPressEvent(self, event):
if event.key() == Qt.Key_Plus:
self.add_value_entry()
elif event.key() == Qt.Key_Minus:
self.remove_value_entry()
elif event.key() == Qt.Key_Return:
self.calculate_expected_value()
elif event.key() == Qt.Key_Escape:
self.clear_entries()

def add_value_entry(self):
probability_entry = CustomLineEdit(self)
value_entry = CustomLineEdit(self)
row = len(self.value_entries) + 2
self.grid_layout.addWidget(probability_entry, row, 0)
self.grid_layout.addWidget(value_entry, row, 1)
self.value_entries.append((probability_entry, value_entry))

def remove_value_entry(self):
if len(self.value_entries) > 1:
probability_entry, value_entry = self.value_entries.pop()
probability_entry.deleteLater()
value_entry.deleteLater()

def clear_entries(self):
self.cost_entry.clear()
for probability_entry, value_entry in self.value_entries:
probability_entry.clear()
value_entry.clear()
self.result_label.setText('数学期望: 0')

def calculate_expected_value(self):
try:
cost = float(self.cost_entry.text())
total_value = sum(float(entry[1].text()) * float(entry[0].text()) / 100 for entry in self.value_entries)
expected_value = total_value - cost
percentage_value = (expected_value / cost) * 100 if cost != 0 else 0
self.result_label.setText(f'数学期望: {expected_value:.2f} ({percentage_value:.2f}%)')
except ValueError as e:
self.result_label.setText(f'错误: {e}')

def main():
app = QApplication(sys.argv)
ex = ExpectedValueCalculator()
ex.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

数学期望计算器
http://example.com/2023/12/20/20231221-general-EVCalc/
作者
Caleb
发布于
2023年12月20日
许可协议