用vb编一个计算器,需要用到数组,看看下面的代码,欢迎来找碴!
2024-07-21 02:21:06
供稿:网友
这是我的代码,请帮我看看哪有问题:(我是要用数组作个计算器)
option explicit
dim shu1 as double, shu2 as double '先后输入的两个数
dim result as double '做了运算的结果
dim process, process0, process1, process2, process3, process4 as variant '保存数1与各个运算符的过程
dim judge as variant '判断按了那个运算符
private sub command1_click(index as integer) '0-9数字键
text1.text = text1.text & index
text1.setfocus
end sub
private sub command1_keypress(index as integer, keyascii as integer) '阻止键盘输入,不过不知为什么没有用
keyascii = 0
end sub
private sub command2_click(index as integer) ' "."键
text1.text = text1.text & "."
end sub
private sub command3_click() '退格键
if text1.text = "" then
exit sub
end if
text1.text = left(text1.text, len(text1.text) - 1)
end sub
private sub command4_click() '清除键
text1.text = ""
end sub
private sub command5_click(index as integer) '做运算,总觉得逻辑上哪有问题
shu1 = val(text1.text)
process = shu1
text1.text = ""
if judge = command5(0) then
process0 = shu1 + command(0)
elseif judge = command5(1) then
process1 = shu1 + command5(1)
elseif judge = command5(2) then
process2 = shu1 + command(2)
elseif judge = command5(3) then
process3 = shu1 + command5(3)
end if
shu2 = val(text1.text)
end sub
private sub command6_click() '等号
result = process + shu2
text1.text = result
if process0 then
result = shu1 + shu2
elseif process1 then
result = shu1 - shu2
elseif process2 then
result = shu1 * shu2
elseif process3 then
result = shu1 / shu2
end if
end sub
private sub text1_change()
if judge = false then
shu2 = val(text1.text)
end if
text1.setfocus
end sub