注册 登录
编程论坛 VB6论坛

编了个计算器不会用”(“和”)“优先级不会啊。怎么弄啊。。

赤热的心 发布于 2013-12-25 18:31, 518 次点击
Dim shu1 As Single, shu2 As Single, suanfu As String
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Command1(Index).Caption
End Sub

Private Sub Command10_Click()

End Sub

Private Sub Command2_Click(Index As Integer)
 shu1 = Val(Text1.Text) '将shu1隐藏起来
 suanfu = Command2(Index).Caption
 Text1.Text = ""
End Sub
Private Sub Command4_Click()
Text1.Text = Text1.Text + "."
If (InStr(Text1.Text, ".") = 1) Then '第一位不能为小数
 Text1.Text = ""
 End If
If InStr(Text1.Text, ".") < Len(Text1.Text) Then
'防止出现两个小数点
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub Command5_Click() '开始加减乘除的运算
shu2 = Val(Text1.Text)
Select Case suanfu
Case "+"
Text1.Text = shu1 + shu2
Case "-"
Text1.Text = shu1 - shu2
Case "*"
Text1.Text = shu1 * shu2
Case "平方"
Text1.Text = shu1 * shu1
Case "立方"
Text1.Text = shu1 * shu1 * shu1
Case "^"
Text1.Text = (shu1) ^ (shu2)
Case "/"
Text1.Text = shu1 / shu2
Case "sin"
Text1.Text = Sin(shu2 * 3.14 / 180)
Case "cos"
Text1.Text = Cos(shu2 * 3.14 / 180)
Case "tan"
Text1.Text = Tan(shu2 * 3.14 / 180)

End Select
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 Command6_Click()
Text1.Text = "" '清除
End Sub
Private Sub Command7_Click()
End
End Sub

Private Sub Command8_Click()
a = Int(Rnd() * 255)
b = Int(Rnd() * 255)
c = Int(Rnd() * 255)
Form1.BackColor = RGB(a, b, c)
End Sub
6 回复
#2
vbvcr512013-12-25 21:57
和什么意思,是说and吗?
#3
赤热的心2013-12-25 21:59
回复 2楼 vbvcr51
是优先级的意思。我想编一个能加减乘除混合运算的。
#4
风吹过b2013-12-26 12:37
先搜索 () ,然后把这中间的部分当做一个完整的 表达式 进行运算,运算完成后,再替换到原来的式里去继续运算。

  搜索,先从左边找第一 ) ,然后再这个 ) 向左找第一个 ( 。找到后,这部分去去运算,换掉。
然后再继续搜索
直到没找到 ) 为止,然后再去运算。

慢慢想吧。
这个比较复杂,我以前写过一个,结果有BUG,后面没再去修改了。
#5
Artless2013-12-26 14:09
do
搜索最后一个()
运算
替换
loop
#6
赤热的心2013-12-26 17:23
回复 5楼 Artless
嗯,谢了,我试试。
#7
赤热的心2013-12-26 17:24
回复 4楼 风吹过b
嗯,谢了哈。我试一试。
1