请教一个反向计算和的问题(已解决)
本帖最后由 志艺风云 于 2018-3-28 18:46 编辑问题:结果如何反向计算单选项和多选项。任意组合的和不会相同。
1、如结果为9是否用到单选项2来计算,即只需得出用到哪个单选项?
2、或者如结果26,如何计算出单选项2和多选项8、16?
麻烦下载代码来看看,就明白我想问的问题了,即按钮2事件应该怎么设置
下面的代码已根据 afan版的帮助修改好的了。
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("计算", 355, 317, -1, -1)
$Group1 = GUICtrlCreateGroup("单选项", 10, 10, 130, 105)
$Radio1 = GUICtrlCreateRadio("1", 20, 35, 110, 17)
$Radio2 = GUICtrlCreateRadio("2", 20, 60, 110, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio3 = GUICtrlCreateRadio("4", 20, 85, 110, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("多选项", 10, 130, 130, 175)
$Checkbox1 = GUICtrlCreateCheckbox("8", 20, 155, 100, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Checkbox2 = GUICtrlCreateCheckbox("16", 20, 179, 100, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Checkbox3 = GUICtrlCreateCheckbox("32", 20, 203, 100, 17)
$Checkbox4 = GUICtrlCreateCheckbox("64", 20, 227, 100, 17)
$Checkbox5 = GUICtrlCreateCheckbox("128", 20, 251, 100, 17)
$Checkbox6 = GUICtrlCreateCheckbox("1024", 20, 275, 100, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label1 = GUICtrlCreateLabel("计算的结果:", 150, 17, 76, 17)
$Input1 = GUICtrlCreateInput("26", 225, 15, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUICtrlSetTip(-1, "输入一个整数来反计算")
$Button1 = GUICtrlCreateButton("选项合计", 150, 40, 90, 25)
$Button2 = GUICtrlCreateButton("反计算", 255, 40, 90, 25)
$Label2 = GUICtrlCreateLabel("单选项结果:", 150, 72, 76, 17)
$Input2 = GUICtrlCreateInput("2", 225, 70, 120, 21)
$Label3 = GUICtrlCreateLabel("多选项结果:", 150, 97, 76, 17)
$Input3 = GUICtrlCreateInput("8、16", 225, 95, 120, 21)
$Edit1 = GUICtrlCreateEdit("", 150, 135, 195, 110, BitOR($ES_READONLY, $ES_WANTRETURN))
GUICtrlSetData(-1, StringFormat("问题:结果如何反向计算单选项和\r\n多选项。任意组合的和不会相同。\r\n\r\n1、如结果为9是否用到单选项2来计\r\n算,即只需得出用到哪个单选项?\r\n\r\n2、或者如结果26,如何计算出单选\r\n项2和多选项8、16?"))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
Local $nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1
Local $sFlag = _SetFlag()
GUICtrlSetData($Input1, $sFlag)
Case $Button2
Local $nn = GUICtrlRead($Input1)
Local $sFlag0 = _Js($nn)
Local $result = StringInStr($sFlag0, "、")
GUICtrlSetData($Input2, StringLeft($sFlag0, $result - 1))
GUICtrlSetData($Input3, StringMid($sFlag0, $result + 1))
EndSwitch
WEnd
Func _SetFlag()
Local $iFlag = 0
;单选项
Select
Case GUICtrlRead($Radio1) = $GUI_CHECKED
$iFlag = $iFlag + 1
Case GUICtrlRead($Radio2) = $GUI_CHECKED
$iFlag = $iFlag + 2
Case GUICtrlRead($Radio3) = $GUI_CHECKED
$iFlag = $iFlag + 4
EndSelect
;多选项
If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $iFlag = $iFlag + 8
If GUICtrlRead($Checkbox2) = $GUI_CHECKED Then $iFlag = $iFlag + 16
If GUICtrlRead($Checkbox3) = $GUI_CHECKED Then $iFlag = $iFlag + 32
If GUICtrlRead($Checkbox4) = $GUI_CHECKED Then $iFlag = $iFlag + 64
If GUICtrlRead($Checkbox5) = $GUI_CHECKED Then $iFlag = $iFlag + 128
If GUICtrlRead($Checkbox6) = $GUI_CHECKED Then $iFlag = $iFlag + 1024
Return $iFlag
EndFunc ;==>_SetFlag
Func _Js($nn)
Local $sOut = ''
Local $aN =
For $ii = 0 To UBound($aN) - 1
If BitAND($nn, $aN[$ii]) Then $sOut &= $aN[$ii] & '、'
Next
Return StringTrimRight($sOut, 1)
EndFunc ;==>_Js
Local $aN =
Local $nn = 26
MsgBox(0, '', _Js($nn))
Func _Js($nn)
Local $sOut = ''
For $ii = 0 To UBound($aN) - 1
If BitAND($nn, $aN[$ii]) Then $sOut &= $aN[$ii] & ', '
Next
Return StringTrimRight($sOut, 2)
EndFunc ;==>_Js 感谢,一直搞不明白这些BitAND,BitNOT, BitOR, BitRotate, BitShift, BitXOR位运算。 回复 2# afan
再问下如果计算几个数字不不重复组合计算和呢?
例如:1、2、4、8
1+2=3
1+4=5
1+8=9
2+4=6
2+8=10
4+8=12
1+2+4=7
1+2+8=11
1+4+8=13
2+4+8=14
1+2+4+8=15
1、2、4、8 分别组合计算和结果:3、5、9、6、10、12、7、11、13、14、15,当然这个顺序随意。 本帖最后由 afan 于 2018-3-28 18:30 编辑
#include <Array.au3>
Local $aN =
For $ii = 2 To UBound($aN)
Local $aNCombo = _ArrayCombinations($aN, $ii, '+')
For $jj = 1 To $aNCombo
$aNCombo[$jj] = $aNCombo[$jj] & '=' & Execute($aNCombo[$jj])
Next
_ArrayDisplay($aNCombo)
Next 谢谢,+ 和Execute ,好 本帖最后由 志艺风云 于 2018-3-30 13:44 编辑
唔,这是个完善代码后的 分别组合计算和、反向计算 的工具了。
#include <array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form2_1 = GUICreate("分别组合计算和、反向计算", 356, 318, -1, -1)
$Group1 = GUICtrlCreateGroup("单选项", 10, 10, 130, 105)
$Radio1 = GUICtrlCreateRadio("1", 20, 35, 110, 17)
$Radio2 = GUICtrlCreateRadio("2", 20, 60, 110, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio3 = GUICtrlCreateRadio("4", 20, 85, 110, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("多选项", 10, 130, 130, 175)
$Checkbox1 = GUICtrlCreateCheckbox("8", 20, 155, 100, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Checkbox2 = GUICtrlCreateCheckbox("16", 20, 179, 100, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Checkbox3 = GUICtrlCreateCheckbox("32", 20, 203, 100, 17)
$Checkbox4 = GUICtrlCreateCheckbox("64", 20, 227, 100, 17)
$Checkbox5 = GUICtrlCreateCheckbox("128", 20, 251, 100, 17)
$Checkbox6 = GUICtrlCreateCheckbox("1024", 20, 275, 100, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label1 = GUICtrlCreateLabel("计算的结果:", 150, 17, 76, 17)
$Input1 = GUICtrlCreateInput("26", 225, 15, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUICtrlSetTip(-1, "输入一个整数来反计算")
$Button1 = GUICtrlCreateButton("选项合计", 150, 40, 90, 25)
$Button2 = GUICtrlCreateButton("反计算", 255, 40, 90, 25)
$Label2 = GUICtrlCreateLabel("单选项结果:", 150, 72, 76, 17)
$Input2 = GUICtrlCreateInput("2", 225, 70, 120, 21)
$Label3 = GUICtrlCreateLabel("多选项结果:", 150, 97, 76, 17)
$Input3 = GUICtrlCreateInput("8、16", 225, 95, 120, 21)
$Button4 = GUICtrlCreateButton("检测", 150, 125, 75, 25)
GUICtrlSetTip(-1, "检测结果是否用到单选项来计算")
$Label4 = GUICtrlCreateLabel("检测计算结果项是否用到单选项来计算。", 230, 125, 116, 27)
$Button3 = GUICtrlCreateButton("组合计算和", 150, 165, 75, 25)
$Label4 = GUICtrlCreateLabel("选择项分别组合计算和。", 230, 165, 116, 27)
$Edit1 = GUICtrlCreateEdit("", 150, 195, 195, 110, BitOR($GUI_SS_DEFAULT_EDIT, $ES_READONLY))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $Radio = 2
While 1
Local $nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Radio1
$Radio = 1
Case $Radio2
$Radio = 2
Case $Radio3
$Radio = 4
Case $Button1
Local $sFlag = _SetFlag()
GUICtrlSetData($Input1, $sFlag)
Case $Button2
Local $nn = GUICtrlRead($Input1)
Local $sFlag0 = _Js($nn)
Local $result = StringInStr($sFlag0, "、")
GUICtrlSetData($Input2, StringLeft($sFlag0, $result - 1))
GUICtrlSetData($Input3, StringMid($sFlag0, $result + 1))
Case $Button3
GUICtrlSetData($Edit1, _SUM())
Case $Button4
If BitAND(GUICtrlRead($Input1), $Radio) Then
MsgBox(4096, "提示", GUICtrlRead($Input1) & " 是用到 " & $Radio & " 计算")
Else
MsgBox(4096, "提示", GUICtrlRead($Input1) & " 否用到 " & $Radio & " 计算")
EndIf
EndSwitch
WEnd
Func _SetFlag()
Local $iFlag = 0
;单选项
Select
Case GUICtrlRead($Radio1) = $GUI_CHECKED
$iFlag = $iFlag + 1
Case GUICtrlRead($Radio2) = $GUI_CHECKED
$iFlag = $iFlag + 2
Case GUICtrlRead($Radio3) = $GUI_CHECKED
$iFlag = $iFlag + 4
EndSelect
;多选项
If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $iFlag = $iFlag + 8
If GUICtrlRead($Checkbox2) = $GUI_CHECKED Then $iFlag = $iFlag + 16
If GUICtrlRead($Checkbox3) = $GUI_CHECKED Then $iFlag = $iFlag + 32
If GUICtrlRead($Checkbox4) = $GUI_CHECKED Then $iFlag = $iFlag + 64
If GUICtrlRead($Checkbox5) = $GUI_CHECKED Then $iFlag = $iFlag + 128
If GUICtrlRead($Checkbox6) = $GUI_CHECKED Then $iFlag = $iFlag + 1024
Return $iFlag
EndFunc ;==>_SetFlag
Func _Js($nn)
Local $sOut = ''
Local $aN =
For $ii = 0 To UBound($aN) - 1
If BitAND($nn, $aN[$ii]) Then $sOut &= $aN[$ii] & '、'
Next
Return StringTrimRight($sOut, 1)
EndFunc ;==>_Js
Func _SUM()
Local $iFlag
;单选项
Select
Case GUICtrlRead($Radio1) = $GUI_CHECKED
$iFlag = $iFlag & "1,"
Case GUICtrlRead($Radio2) = $GUI_CHECKED
$iFlag = $iFlag & "2,"
Case GUICtrlRead($Radio3) = $GUI_CHECKED
$iFlag = $iFlag & "4,"
EndSelect
;多选项
If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $iFlag = $iFlag & "8,"
If GUICtrlRead($Checkbox2) = $GUI_CHECKED Then $iFlag = $iFlag & "16,"
If GUICtrlRead($Checkbox3) = $GUI_CHECKED Then $iFlag = $iFlag & "32,"
If GUICtrlRead($Checkbox4) = $GUI_CHECKED Then $iFlag = $iFlag & "64,"
If GUICtrlRead($Checkbox5) = $GUI_CHECKED Then $iFlag = $iFlag & "128,"
If GUICtrlRead($Checkbox6) = $GUI_CHECKED Then $iFlag = $iFlag & "1024,"
$iFlag = StringTrimRight($iFlag, 1)
$aN = StringSplit($iFlag, ",", 2)
Local $aArrayTarget
For $ii = 2 To UBound($aN)
Local $aNCombo = _ArrayCombinations($aN, $ii, '+')
For $jj = 1 To $aNCombo
$aNCombo[$jj] = $aNCombo[$jj] & '=' & Execute($aNCombo[$jj])
;$aNCombo[$jj] = Execute($aNCombo[$jj])
Next
_ArrayDelete($aNCombo, 0)
_ArrayConcatenate($aArrayTarget, $aNCombo)
Next
;_ArrayDisplay($aArrayTarget)
_ArrayDelete($aArrayTarget, 0)
Return _ArrayToString($aArrayTarget, @CRLF)
EndFunc ;==>_SUM
页:
[1]