用VB做一个简单的加减乘除的计算器,代码是啥?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用VB做一个简单的加减乘除的计算器,代码是啥?相关的知识,希望对你有一定的参考价值。
代码简单点
不要太复杂的
谢谢了
我们现在首先要做的就是拉出Label,准备三个Label,把label1,label2,label3,这些改成第一个数、第二个数、结果。
我们现在要text也要拉出三个,把了text里面text1、text2、text3全部删掉。
我们现在要把command拉出四个,把里面command1-4都给成+-*/
现在我们现在要双击+,我们开始进行输入代码
在里面我们现在要:text3.text=val(text1.text)+val(text2.text)
我们要把这个(text3.text=val(text1.text)+val(text2.text))复制,把里面+号分别改成-*/
如图,就像这样就可以了,可以行径了,如果你在哪个代码里弄错了,你看一下还要哪些没有拉出来,或者你哪个代码输错了。
感谢观看。
参考技术A\'界面设置如图\' 代码如下\'已增加操作数正确性验证及 除数不能为0验证Private Sub Command1_Click() If IsNumeric(Text1.Text) And Text1.Text "" And IsNumeric(Text2.Text) And Text2.Text "" Then Select Case Combo1.ListIndex Case 0 Text3.Text = Val(Text1.Text) + Val(Text2.Text) Case 1 Text3.Text = Val(Text1.Text) - Val(Text2.Text) Case 2 Text3.Text = Val(Text1.Text) * Val(Text2.Text) Case 3 If Val(Text1.Text) 0 Then Text3.Text = Val(Text1.Text) / Val(Text2.Text) Else MsgBox "除数不能为0", , "错误" End If End Select Else MsgBox "请输入操作数", , "错误" End IfEnd SubPrivate Sub Form_Load() Combo1.AddItem "+" Combo1.AddItem "-" Combo1.AddItem "*" Combo1.AddItem "/" Combo1.ListIndex = 0 Text1.Text = "" Text2.Text = "" Text3.Text = "" Command1.Caption = "计算" End Sub本回答被提问者采纳 参考技术B Dim YSF% '定义运算符
Dim S1#, S2# '定义运算中的两个数值
Dim ZT% '定义状态
Dim FH As Boolean '定义符号
Dim XSD As Boolean '定义运算符
Dim CD1%, CD2% '定义与符号有关的两个字符串长度
Dim M1#, M2#, M3#, M4#, M5# '定义用于记忆功能的5个变量
'给运算符赋值,以便在计算结果。
If ZT = 1 Then
Select Case Index
Case 0
YSF = 1
Case 1
YSF = 2
Case 2
YSF = 3
Case 3
YSF = 4
End Select
If M = 1 Then
Label1 = M1
ElseIf M = 2 Then
Label1 = M2
ElseIf M = 3 Then
Label1 = M3
ElseIf M = 4 Then
Label1 = M4
End If
S1 = Val(Label1)
Label1 = ""
ZT = 2
'给XSD和FH从新赋值,以便下一个数能添加小数点
XSD = False
FH = False
End If
'给运算符赋值,以便在计算结果。
If ZT = 1 Then
Select Case Index
Case 0
YSF = 1
Case 1
YSF = 2
Case 2
YSF = 3
Case 3
YSF = 4
End Select
If M = 1 Then
Label1 = M1
ElseIf M = 2 Then
Label1 = M2
ElseIf M = 3 Then
Label1 = M3
ElseIf M = 4 Then
Label1 = M4
End If
S1 = Val(Label1)
Label1 = ""
ZT = 2
'给XSD和FH从新赋值,以便下一个数能添加小数点
XSD = False
FH = False
End If
'给运算符赋值,以便在计算结果。
If ZT = 1 Then
Select Case Index
Case 0
YSF = 1
Case 1
YSF = 2
Case 2
YSF = 3
Case 3
YSF = 4
End Select
If M = 1 Then
Label1 = M1
ElseIf M = 2 Then
Label1 = M2
ElseIf M = 3 Then
Label1 = M3
ElseIf M = 4 Then
Label1 = M4
End If
S1 = Val(Label1)
Label1 = ""
ZT = 2
'给XSD和FH从新赋值,以便下一个数能添加小数点
XSD = False
FH = False
End If
'给运算符赋值,以便在计算结果。
If ZT = 1 Then
Select Case Index
Case 0
YSF = 1
Case 1
YSF = 2
Case 2
YSF = 3
Case 3
YSF = 4
End Select
If M = 1 Then
Label1 = M1
ElseIf M = 2 Then
Label1 = M2
ElseIf M = 3 Then
Label1 = M3
ElseIf M = 4 Then
Label1 = M4
End If
S1 = Val(Label1)
Label1 = ""
ZT = 2
'给XSD和FH从新赋值,以便下一个数能添加小数点
XSD = False
FH = False
End If
顺序是+-*/ 参考技术C Dim op1 As Byte
Dim ops1&, ops2&
Dim res As Boolean
Private Sub Command1_Click(i As Integer)
If Not res Then
Label1.Caption = Label1.Caption & i
Else
Label1.Caption = i
res = False
End If
End Sub
Private Sub Command2_Click()
Label1.Caption = ""
End Sub
Private Sub Command3_Click()
ops2 = Label1.Caption
Select Case op1
Case 0
Label1.Caption = ops1 + ops2
Case 1
Label1.Caption = ops1 - ops2
Case 2
Label1.Caption = ops1 * ops2
Case 3
Label1.Caption = ops1 / ops2
End Select
res = True
End Sub
Private Sub Command4_Click(j As Integer)
ops1 = Label1.Caption
op1 = j
Label1.Caption = ""
End Sub
Private Sub Command5_Click()
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
res = False
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Now
End Sub 参考技术D 控件:
四个textbox
一个command键
private sub command1_click()
select case cstr(text2.text)
case "+"
text4.text=val(text1.text)+val(text3.text)
case "-"
text4.text=val(text1.text)-val(text3.text)
case "*"
text4.text=val(text1.text)*val(text3.text)
case "/"
if val(text3.text)=0 then
msgbox "分母不能为零"
text3.setfoucs
text3.text=""
endif
text4.text=val(text1.text)/val(text3.text)
end select
end sub
这够简单了吧!!!???
如何用python编写计算器
我想你的需求应该是一个图形界面的程序,而不是简单的在命令行上输入。那么,要做的第一件事就是选择一个图形界面套件。可以使用原生的TK,也可以用跨平台性能很好的wxPython,或者是整体结构很像MFC的PyWin32。至于pyGTK,pyQT,都是可选的,但是相对来说文档比较少,学习不便。
选定图形库之后,就可以看文档范例了。计算器总体是比较简单的。我记得WxPython的demo里直接就有一个简单计算器,您可以直接取来用。 参考技术A # true needed a captial T
while True:
# Brackets were mismatched
CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))
if CHOICE == "1":
print 'ADDING TWO NUMBERS:'
# Calling a function shouldn't have trailing :
add(c)
elif CHOICE == "2":
print 'SUBTRACTING TWO NUMBERS'
# Calling a function shouldn't have trailing :
sub(c)
elif CHOICE == "3":
print 'MULTIPLYING TWO NUMBERS'
# Calling a function shouldn't have trailing :
Mul(c)
elif CHOICE == "4":
print "DIVIDEING TWO NUMBERS"
# Calling a function shouldn't have trailing :
Div(c)
elif CHOICE == "0":
# can only return from a function use exit here instead
exit()
# else needs a trailing :
else:
# No capital P for print
print "The value Enter value from 1-4"
The code now has no syntax errors but still has many problems.
You pass c to your function, c is never initialized, what is c?
Your function doesn't take arguments def add(): (even though pass the mysterious cvalue).
Your function doesn't print or return the result it just computes.
You store CHOICE as an int are do comparisons with strings so the else case is always executed and there is no way to exit the loop (infinite looping).
Fixed code:
#!/usr/bin/python
def add():
print "Enter the two numbers to Add"
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A + B
def sub():
print "Enter the two numbers to Subtract"
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A - B
def mul():
print "Enter the two numbers to Multiply"
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A * B
def div():
print "Enter the two number to Divide"
A=float(raw_input("Enter A: "))
B=float(raw_input("Enter B: "))
return A / B
print "1: ADDITION"
print "2: SUBTRACTION"
print "3: MULTIPLICATION"
print "4: DIVITION"
print "0: QUIT"
while True:
CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION "))
if CHOICE == 1:
print 'ADDING TWO NUMBERS:'
print add()
elif CHOICE == 2:
print 'SUBTRACTING TWO NUMBERS'
print sub()
elif CHOICE == 3:
print 'MULTIPLYING TWO NUMBERS'
print mul()
elif CHOICE == 4:
print "DIVIDEING TWO NUMBERS"
print div()
elif CHOICE == 0:
exit()
else:
print "The value Enter value from 1-4" 参考技术B
代码
print('计算器')
problem = input('输入1个算式,按q退出')
while (problem != 'q'):
print('答案是',eval(problem))
problem = input('输入1个算式,按q退出')
注释 eval( ) 用于将括号里的算式返回到答案
以上是关于用VB做一个简单的加减乘除的计算器,代码是啥?的主要内容,如果未能解决你的问题,请参考以下文章