用vb编写科学计算器
发布网友
发布时间:2022-04-29 22:47
我来回答
共5个回答
热心网友
时间:2022-06-24 23:22
呵呵,windows下的那个好像不是科学型的吧,所以楼上大哥直接调用calc.exe就不符合要求了。
我也不懂vb,但是我想把我的思路说一下你看对你有没有用。(代码我写不了)
菜单用菜单编辑器(ctrl+e),不知道你的菜单里想要啥功能,最后一个“帮助”可能是想显示一个使用手册吧,是的话有很多方法,比如新建一个窗体,窗体上加一个文本框或者标签之类的和一个按钮,并把文本框的text或标签的caption改成你想写的帮助内容,在“帮助”菜单的单击事件下写form2.show。按钮的单击事件下写unlod me。
(form中写
Private Sub 帮助_Click()
Form2.Show
form2中写
Private Sub Command1_Click()
Unload Me
End Sub
)各个窗体和控件的属性看着这改改
下面该写计算部分了
首先你需要确定你的计算器能算几步(最多几个运算符),一般70步就行了吧,定义一个数组“运算符(70)”整型的就行了,让他记录每一个输入的运算符(比如把所有的运算符编号,“+”记录1,“-”记录2,“(”记成3 ,“)”记成4。。。也就几十种吧)(不要考虑内存问题了,现在的电脑再垃圾的代码也能运行)
计算时先查出所有括号,直接把优先级放到括号内,然后查“运算符(i)”,用循环语句,从优先级低的开始算(这样计算出来就会反过来从高到低),然后再检查括号(因为括号要两个符号,而且可以重复,所以要把检查括号跟检查其他运算符分开讨论)这样就成为单步计算的问题了,应该很简单吧?
具体的计算方法就是
定义 Sub 检查运算符( 字符串)
如果出现超过某限值的运算附代码,则直接运算之
‘ 比如出现sin之类的或者^之类的
检查现有的最低级别的运算符,记录其代号‘如:运算符(2)
检查运算符后的字符串,分别赋值为字符串a,字符串b
如果a(或b)中没有运算符则i(或f)为1.否则为0
‘ a、b、c、都是动态的
‘a(或b)就是a检查一下,b检查一下
‘ 嫌麻烦以后就这样写了
End Sub
再定义个 Sub 单步计算(字符串a,字符串b,运算符代号,i,f)
如果运算符代号超过某
则 运算符(Call检查字符串(字符串b))
如果运算符为 某
如果i(或f)=0 ‘ 不能这样写,理解意思即可
那么 Call 检查字符串(a)运算符 Call检查字符串b
否则,~~ ‘无运算符的就不用调用“检查字符串”了
‘ 嵌套一下
‘呵呵,就这样写了,理解意思即可
或者分别写出各个运算(加、减、乘、除)然后调用,sin,
统计计算部分就简单了,定义一个数组,分别记录数据,求平均就
for,,,a = 数组(i)+ a
求什么就for什么
进制转换部分写在单选钮的事件下(为了简单你可以要求转换时文本框中必须是“数字”),具体转换过程你如果知道怎么手算的话应该就能写出来
唉,写了这么多才发现我的思路漏洞百出,算了。。。
最后再说一句vb界面直接画就可以了,不用自己建立模块
热心网友
时间:2022-06-24 23:22
如果是C语言的话还能自己写。VB就只能上网搜了,还真别说,搜到一大堆!
http://www.google.cn/search?client=aff-cs-360se&forid=1&ie=utf-8&oe=UTF-8&q=%E8%AE%A1%E7%AE%97%E5%99%A8VB
热心网友
时间:2022-06-24 23:23
最简单的计算器
Option Explicit
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Const MAX_LEN = 200 '字符串最大长度
Private Sub Form_Load()
Dim WinPathLen As Long
Dim sTmp As String * MAX_LEN
WinPathLen = GetWindowsDirectory(sTmp, MAX_LEN)
Dim winPath As String
winPath = Left(sTmp, WinPathLen)
Shell winPath & "\system32\calc.exe", vbNormalFocus
End
End Sub
热心网友
时间:2022-06-24 23:23
Dim X As Double, Y As Double
Dim op As String
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
X = Val(Text1): Text1 = Sin(X * 3.1415926 / 180)
Case 1
X = Val(Text1): Text1 = 1 / Cos(X)
Case 2
X = Val(Text1): Text1 = Sin(X) * 180 / 3.1415926
Case 3
X = Val(Text1): Text1 = Atn(X) + 2 * Atn(1)
Case 5
X = Val(Text1): Text1 = Cos(X * 3.1415926 / 180)
Case 6
X = Val(Text1): Text1 = 1 / Sin(X)
Case 7
X = Val(Text1): Text1 = Cos(X) * 180 / 3.1415926
Case 8
X = Val(Text1)
If X <= 0 Then
Text1 = "负数或零的对数无意义"
Else
Text1 = Log(X)
End If
Case 10
X = Val(Text1): Text1 = Tan(X * 3.1415926 / 180)
Case 11
X = Val(Text1): Text1 = 1 / Tan(X)
Case 12
X = Val(Text1): Text1 = Atn(X) * 180 / 3.1415926
Case 13
X = Val(Text1)
If X <= 0 Then
Text1 = "负数或零的对数无意义"
Else
Text1 = Log(X) / Log(10)
End If
Case 14
X = Val(Text1)
If X <= 0 Then
X = Val(Text1): Text1 = Abs(X)
Else
X = Val(Text1): Text1 = Text1.Text
End If
End Select
End Sub
Private Sub Command2_Click(Index As Integer)
Select Case Index
Case 0 To 9
Text1.Text = Text1 & Command2(Index).Caption
Case 11
If InStr(Text1, ".") = 0 Then
Text1.Text = Text1 & Command2(Index).Caption
ElseIf Right(Text1, 1) = "." Then
Text1.Text = Text1 & ""
End If
Case 12
If InStr(Text1, "-") = 0 Then
Text1.Text = Text1 & Command2(Index).Caption
ElseIf Right(Text1, 1) = "-" Then
Text1.Text = Text1 & ""
End If
End Select
End Sub
Private Sub Command3_Click(Index As Integer)
Select Case Index
Case 0 To 6
X = Val(Text1)
If Index = 0 Then op = "+"
If Index = 1 Then op = "-"
If Index = 2 Then op = "*"
If Index = 3 Then op = "/"
If Index = 4 Then op = "^"
If Index = 5 Then op = "("
If Index = 6 Then op = ")"
Text1 = ""
End Select
End Sub
Private Sub Command4_Click()
If Len(Text1) <> 0 Then
Text1 = Left(Text1, Len(Text1) - 1)
End If
End Sub
Private Sub Command5_Click()
Text1 = ""
X = 0: Y = 0
End Sub
Private Sub Command6_Click()
Y = Val(Text1)
'Text1 = ""
If op = "^" Then Text1 = X ^ Y
If op = "+" Then Text1 = X + Y
If op = "-" Then Text1 = X - Y
If op = "*" Then Text1 = X * Y
If op = "/" Then
If Y <> 0 Then
Text1 = X / Y
Else
Text1 = "除数不能为零"
End If
End If
End Sub
Private Sub Form_Load()
End Sub
热心网友
时间:2022-06-24 23:24
只供参考:
Dim v As Boolean
Dim s As Integer
Dim X As Double
Dim Y As Double
Private Sub Command1_Click(Index As Integer)
If Form1.Tag = "s" Then
If Index = 10 Then
Text1.Text = "0."
Else
Text1.Text = Command1(Index).Caption
End If
Form1.Tag = ""
Else
Text1.Text = Text1.Text & Command1(Index).Caption
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Form1.Tag = "s"
If v Then
X = Val(Text1.Text)
v = Not v
Else
Y = Val(Text1.Text)
Select Case s
Case 0
Text1.Text = X + Y
Case 1
Text1.Text = X - Y
Case 2
Text1.Text = X * Y
Case 3
If Y <> 0 Then
Text1.Text = X / Y
Else
MsgBox ("不能以0为除数")
Text1.Text = X
v = False
End If
Case 4
Y = 0
v = False
End Select
X = Val(Text1.Text)
End If
s = Index
End Sub
Private Sub Command4_Click()
Text1.Text = ""
End Sub
Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
End Sub