| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4291 人关注过本帖
标题:计算器原代码(原创)
只看楼主 加入收藏
xinfresh
Rank: 4
等 级:贵宾
威 望:13
帖 子:594
专家分:0
注 册:2006-1-13
收藏
 问题点数:0 回复次数:18 
计算器原代码(原创)

没有科学计算功能,不过有自动处理先乘除后加减的功能。也是为了实现这个功能,可能代码有点多,希望大家多给意见,谢了

Option Explicit
Dim a As Double, b As Double, c As Double
'a是最终结果,b是优先级结果,c是录入变量
Dim idx As Integer
Dim PreMode As String, JustMode As String, CurMode As String

Private Sub ComAC_Click() '清空按钮
a = 0
b = 0
c = 0
Text1.Text = "0"
idx = 1
PreMode = ""
JustMode = ""
CurMode = ""
End Sub

Private Sub ComAdd_Click() '加法
On Error GoTo er
c = Val(Text1.Text)
CurMode = "+"
If PreMode = "" Then
If JustMode = "" Then
a = c
ElseIf JustMode = "+" Then
a = a + c
ElseIf JustMode = "-" Then
a = a - c
ElseIf JustMode = "*" Then
a = a * c
ElseIf JustMode = "/" Then
a = a / c
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
a = a + b * c
ElseIf JustMode = "/" Then
a = a + b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
a = a - b * c
ElseIf JustMode = "/" Then
a = a - b / c
End If
End If

Text1.Text = CStr(a)
PreMode = ""
JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error", vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub

Private Sub ComBak_Click() '退格
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
If Len(Text1.Text) = 0 Then
Text1.Text = "0"
End If
End Sub

Private Sub ComDiv_Click() '除法
On Error GoTo er
c = Val(Text1.Text)
CurMode = "/"
If PreMode = "" Then
If JustMode = "" Then
a = c
Text1.Text = CStr(a)
ElseIf JustMode = "+" Or JustMode = "-" Then
b = c
PreMode = JustMode
ElseIf JustMode = "*" Then
a = a * c
Text1.Text = CStr(a)
ElseIf JustMode = "/" Then
a = a / c
Text1.Text = CStr(a)
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
End If

JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub

Private Sub ComDot_Click() '小数点
If idx = 1 Then
If InStr(1, Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text + "."
End If
Else
Text1.Text = "0."
End If
idx = 1
End Sub

Private Sub ComEqu_Click() '等号
On Error GoTo er
c = Val(Text1.Text)
If PreMode = "" Then
If JustMode = "+" Then
a = a + c
ElseIf JustMode = "-" Then
a = a - c
ElseIf JustMode = "*" Then
a = a * c
ElseIf JustMode = "/" Then
a = a / c
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
a = a + b * c
ElseIf JustMode = "/" Then
a = a + b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
a = a - b * c
ElseIf JustMode = "/" Then
a = a - b / c
End If
End If

Text1.Text = CStr(a)
a = 0
b = 0
c = 0
PreMode = ""
JustMode = ""
CurMode = ""

idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub

Private Sub ComMul_Click() '乘号
On Error GoTo er
c = Val(Text1.Text)
CurMode = "*"
If PreMode = "" Then
If JustMode = "" Then
a = c
Text1.Text = CStr(a)
ElseIf JustMode = "+" Or JustMode = "-" Then
b = c
PreMode = JustMode
ElseIf JustMode = "*" Then
a = a * c
Text1.Text = CStr(a)
ElseIf JustMode = "/" Then
a = a / c
Text1.Text = CStr(a)
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
b = b * c
ElseIf JustMode = "/" Then
b = b / c
End If
End If


JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub

Private Sub ComNum_Click(Index As Integer) '数字键入
If idx = 1 Then
If Text1.Text = "0" Then
Text1.Text = CStr(Index)
Else
Text1.Text = Text1.Text + CStr(Index)
End If
Else
Text1.Text = CStr(Index)
End If
idx = 1
End Sub

Private Sub ComOff_Click()
Unload Me
End Sub

Private Sub ComPN_Click() '正负号
If Text1.Text = "0" Then
Text1.Text = "-"
Else
If InStr(1, Text1.Text, "-") = 0 Then
Text1.Text = "-" + Text1.Text
Else
Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
End If
End If
idx = 1
End Sub

Private Sub ComSub_Click() '减法
On Error GoTo er
c = Val(Text1.Text)
CurMode = "-"
If PreMode = "" Then
If JustMode = "" Then
a = c
ElseIf JustMode = "+" Then
a = a + c
ElseIf JustMode = "-" Then
a = a - c
ElseIf JustMode = "*" Then
a = a * c
ElseIf JustMode = "/" Then
a = a / c
End If
ElseIf PreMode = "+" Then
If JustMode = "*" Then
a = a + b * c
ElseIf JustMode = "/" Then
a = a + b / c
End If
ElseIf PreMode = "-" Then
If JustMode = "*" Then
a = a - b * c
ElseIf JustMode = "/" Then
a = a - b / c
End If
End If

Text1.Text = CStr(a)
PreMode = ""
JustMode = CurMode
idx = 2
Exit Sub
er:
MsgBox Err.Description, vbCritical, "Error"
Call ComAC_Click
Exit Sub
End Sub

Private Sub Form_Load()
'赋上初值
PreMode = ""
JustMode = ""
CurMode = ""
a = 0
b = 0
c = 0
idx = 1
End Sub
Private Sub Text1_Change()
If Val(Text1.Text) < 1 And Val(Text1.Text) > 0 And Left(Text1.Text, 1) <> "0" Then
Text1.Text = "0" & Text1.Text
End If
End Sub

fEol0o7q.rar (6.12 KB) 计算器原代码(原创)


[此贴子已经被作者于2006-1-13 19:57:52编辑过]

搜索更多相关主题的帖子: 计算器 Double String 代码 
2006-01-13 19:49
zzw1525
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-1-6
收藏
得分:0 

版主厉害,我是新手,请以后多多指教!

2006-01-14 23:03
rainy312
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-3-8
收藏
得分:0 

楼主厉害啊,我最近也在编写一个“完美”的计算器程序,一直都不成功,看了楼主的,真是让人佩服

不过有几个在我写程序的时候遇见的问题楼主也还没解决:

1,不小心先按了运算符,特别是乘除,在进行计算结果就会一直显示0

2,当数据过小时,比如1/99,结果会在整数部分前多了个0

3,当屏幕上显示的是小数,使用+/-键时,在点其他运算符后,显示的小数在整数部分的0会消失

这算是仅有的美中不足的吧。


2006-03-08 17:19
rainy312
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-3-8
收藏
得分:0 
不好意思,又发现了1个问题:
运算符的连按,就是在计算过程中,比如,本想按乘,却按了减,想改过来,又按了乘,原输入的数据已经变了,在按等号时也是这样,连按等号会清空结果。

2006-03-08 17:47
xinfresh
Rank: 4
等 级:贵宾
威 望:13
帖 子:594
专家分:0
注 册:2006-1-13
收藏
得分:0 
你厉害,是有这个问题,因为在我认为"+","-","="都是结束性操作,按了这些键就会把前面的内容算完.
这和我的算法有关....呵

E-mail:xinfresh@QQ:383094053校内:http:///getuser.do?id=234719042
2006-03-08 19:35
kfp_kaka
Rank: 4
等 级:贵宾
威 望:13
帖 子:353
专家分:0
注 册:2006-1-12
收藏
得分:0 
3楼的挺厉害的嘛,这么快就能找出BUG

爱一个人不一定要拥有她 拥有一个人就一定要好好的爱她
2006-03-09 11:35
majiaow
Rank: 1
等 级:新手上路
帖 子:172
专家分:0
注 册:2006-2-21
收藏
得分:0 
大家都好厲害啊

2006-03-10 13:29
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
能不能将运算和运算类型编成类来处理!其中运算类型按枚举来处理。同时每次以最后一次的运算符为准。并建议在相关运算函数加入错误处理。比如计算Tan时输入值不能为90度。

2007-01-18 22:00
qpwo
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-11-24
收藏
得分:0 
学习一下!

最新正版控件:Iocomp v3.08 ProEssentials v6 图形、报表...可以合作开发
2007-01-20 11:17
悟天
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-1-20
收藏
得分:0 

因该在 Private Sub 等号_Click() 事件代码里加入 if [运算符]="" then [结果变量]=val[运算符前边的数字]
呵呵,因为没细看你的代码,只是根据底下的回复,大概觉得这样可以解决按等号就清零的问题。
还有一点我很奇怪,这个程序好象不需要这么长的代码吧???晕的很


2007-01-20 15:32
快速回复:计算器原代码(原创)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016065 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved