| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2035 人关注过本帖
标题:计算器的最高成就!
只看楼主 加入收藏
Domes
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-11-21
收藏
 问题点数:0 回复次数:14 
计算器的最高成就!
我编写了一个函数,用来计算带有多重括号,和基本函数的.从前大多数编写者都是采用单步运算
然而我这个函数,当你输入整个式子是,可以计算出结果!
请大家 多多指教!
本程序本来在上调试的,经过修改,如下,可以复制在代码中,直接调用

'=================版权所有 domes=================================='
 Public Function Arthmetic(ByVal s As String)
 On Error GoTo ee
        Dim m(500) As String '可随意改变数组的上限,这里可执行500位的算术
        Dim i, j As Integer '全局申明
        Dim add As String
        Dim count As Integer
        count = 0
        Dim v As Integer
        'begin
        '执行括号域分离
        '    再次调用本程序,即递归
        Const e = 2.01111
        Const pi = 3.1415926
        '==============================================================================
        Dim ch
        Do
            ch = InStr(s, ")") '查找")"以便识别括号内容
            If ch <> 0 Then
                For j = ch To 1 Step -1
                    If Mid(s, j, 1) = "(" Then '找到后在往前识别"("
                        Dim before As String
                        before = Mid(s, j + 1, ch - j - 1) '收集括号的内容
                        Dim after As String
                        after = "(" & before & ")"
                        Dim mm As String
                        mm = Arthmetic(before)
                        s = Replace(s, after, mm) '收集包括括号的内容
                        Exit For
                    End If
                Next
            Else
                If ch = 0 Then '当找不到)是退出循环,不会进入死循环
                    Exit Do
                End If
            End If
        Loop

        '===============过滤了符号下一处理是式子里没有括号=======================================

        '=========================将字符和运算符号分离==========================================

        For i = 1 To Len(s) '把符号和数字分开呈数组
            Dim check As String
            check = Mid(s, i, 1)
            If check = "*" Or check = "/" Or check = "-" Or check = "+" Or check = "$" Or check = "\" Or check = "&" Then '在四则运算中判断符号
                If Mid(add, 1, 1) = "M" Then
                    add = "-" & Mid(add, 2, Len(add) - 1)
                End If
                m(count) = add
                count = count + 1 '组织数组的项的完整性
                m(count) = check
                add = ""
                check = ""
                count = count + 1
            End If
            add = add + check
            If i = Len(s) Then
                If Mid(add, 1, 1) = "M" Then
                    add = "-" & Mid(add, 2, Len(add) - 1)
                End If
                m(count) = add '累积数字成数字
            End If

        Next
        '============================================================================
        '==================================最高级运算==================================
        For i = 0 To count
            Dim w As Integer
            w = InStr(1, m(i), "^") '^乘方运算
            If w Then
                  a1 = Val(Mid(m(i), 1, w - 1)) '高级用方括号填写参数,避免和小括号处理冲突
                 a2 = Val(Mid(m(i), w + 1, Len(m(i)) - w))
                m(i) = Val(a1) ^ Val(a2)
            End If
            'lg()对数运算
            w = InStr(1, m(i), "lg[")
            If w Then
                 a1 = Val(Mid(m(i), 4, Len(m(i)) - 4))
                m(i) = Log(Val(a1))  '下面是常用函数调用
            End If
            w = InStr(1, m(i), "sin[")
            If w Then
               
                a1 = Val(Mid(m(i), 5, Len(m(i)) - 5))
                m(i) = Sin(Val(a1))
            End If
            w = InStr(1, m(i), "cos[")
            If w Then
               
                a1 = Val(Mid(m(i), 5, Len(m(i)) - 5))
                m(i) = Cos(a1)
            End If
            w = InStr(1, m(i), "tan[")
            If w Then
               
                a1 = Val(Mid(m(i), 5, Len(m(i)) - 5))
                m(i) = Tan(a1)
            End If
            If m(i) = "pi" Then
                m(i) = pi
            End If
            If m(i) = "e" Then
                m(i) = e
            End If
            w = InStr(1, m(i), "log[")
            If w Then
               Dim gh
                gh = Split(Mid(m(i), 5, Len(m(i)) - 5), ",")
                m(i) = Log((Val(gh(0))) / Log(Val(gh(1))))
            End If
            w = InStr(1, m(i), "ln[")
            If w Then
               
                a1 = Mid(m(i), 4, Len(m(i)) - 4)
                m(i) = Log(Val(a1))
            End If
            w = InStr(1, m(i), "sqrt[")
            If w Then
               
                a1 = Mid(m(i), 6, Len(m(i)) - 6)
                m(i) = Sqr(Val(a1))
            End If
            w = InStr(m(i), "!")
            If w Then
               
                a1 = 1
                For j = 1 To Mid(m(i), 1, Len(m(i)) - 1)
                    a1 = a1 * j
                Next
                m(i) = a1
            End If
            If m(i) = "result" Then m(i) = ans
      
        Next
        '=================================低级运算 四则运算 基本运算===================================
        '===================================乘除运算==============================不算加减保留
        Do
            Dim fu As Integer '检查是否还有*或/的符号,以便再次调用方法
            fu = 0
            For i = 1 To count
                If m(i) = "*" Or m(i) = "/" Or m(i) = "$" Or m(i) = "\" Or m(i) = "&" Then
                    fu = i
                    Exit For
                End If
                If i = count Then
                    fu = 0 '没有符号是发出信号该下一句
                End If
            Next
            If fu <> 0 Then
                If m(fu) = "*" Then
                    m(fu - 1) = Val(m(fu - 1)) * Val(m(fu + 1)) '单目乘法

                End If
                If m(fu) = "/" Then
                    m(fu - 1) = Val(m(fu - 1)) / Val(m(fu + 1)) '单目除法

                End If
                If m(fu) = "$" Then
                    m(fu - 1) = Val(m(fu - 1)) Mod Val(m(fu + 1))
                End If
                If m(fu) = "\" Then
                    m(fu - 1) = Val(m(fu - 1)) \ Val(m(fu + 1))
                End If
                If m(fu) = "&" Then
                    m(fu - 1) = (m(fu - 1)) & (m(fu + 1))
                End If

                For i = fu To count - 2 '把数组空的部位给后面的数组填充
                    m(i) = m(i + 2)
                Next
                m(i) = ""
                m(i + 1) = ""
                count = count - 2 '删除多余的数组
            Else
                Exit Do '跳出循环
            End If
        Loop
        '最后把一条只有加减法的算式提供给下一子程序
        '=====================加减法运算================================子程序
        Dim n As Integer
        n = 1
        Do
            If m(n) = "+" Or m(n) = "-" Then
                If m(n) = "+" Then
                    m(n - 1) = Val(m(n - 1)) + Val(m(n + 1)) '执行加法
                End If
                If m(n) = "-" Then
                    m(n - 1) = Val(m(n - 1)) - Val(m(n + 1)) '执行减法
                End If
                For i = n To count - 2
                    m(i) = m(i + 2) '缩减数组长度,便于答案析出
                Next
                m(count) = ""
                m(count - 1) = "" '剪掉后面无用项
                count = count - 2
                If m(n) <> "+" And m(n) <> "-" Then '跳出循环
                    Exit Do '关闭循环
                End If
            Else
                Exit Do
            End If
        Loop
        If Mid(m(0), 1, 1) = "-" Then
            m(0) = "M" & Mid(m(0), 2, Len(m(0)) - 1)
        End If
         
        Arthmetic = m(0) '显示答案返回给调用的程序
        Exit Function
        
ee:
 MsgBox Err.Description
    End Function

'如果大家有什么建议,可发表!
搜索更多相关主题的帖子: 计算器 成就 String Dim 函数 
2007-12-05 10:24
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
写?..
还用on error? 用try..catch吧.
还用mid?..用substring吧.

我的msn: myfend@
2007-12-05 10:28
Domes
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-11-21
收藏
得分:0 
答复上面!
显示出来的是经过改写可以在vb6上运行的,
要知道,上某些函数不能在vb6上运行!
2007-12-05 10:41
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
那不直接在vb6里写不就好?..
干嘛这样嗐折腾自己?.

我的msn: myfend@
2007-12-05 10:43
那边有朵蘑菇云
Rank: 1
来 自:很……那啥的一个地方
等 级:新手上路
威 望:1
帖 子:390
专家分:0
注 册:2007-9-3
收藏
得分:0 
关注你们的讨论

身不残 志更不坚
2007-12-05 10:44
Domes
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-11-21
收藏
得分:0 
回复purana
要知道,在上编写可轻松过在vb6上.毕竟有格式化代码功能!
2007-12-05 11:02
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
代码格式化?..
那是程序员的习惯所致..格式最好还是不要太依赖于工具..

而且用.net就充分利用.net纯面向对象和.Net FrameWrok的优点..而再用vb6语法中的非面向对象的方法..

以上纯属是我个人观点..

我的msn: myfend@
2007-12-05 11:06
kerr
Rank: 1
等 级:新手上路
帖 子:189
专家分:0
注 册:2007-9-30
收藏
得分:0 
....我以前就写过多重括号的运算,没看LZ代码就知道还有很多没有处理。例如双重运算符和多重运算符。-- ++

天下风云出我,辈一入江湖岁月催.皇途霸业谈笑中,不盛人生
2007-12-05 12:48
刨子头
Rank: 1
等 级:新手上路
威 望:1
帖 子:319
专家分:0
注 册:2007-9-24
收藏
得分:0 
还是不错!
2007-12-05 20:54
xinfresh
Rank: 4
等 级:贵宾
威 望:13
帖 子:594
专家分:0
注 册:2006-1-13
收藏
得分:0 
我想支持一下purana的观点,不过听楼主的描述,在楼主手里是有点浪费了

E-mail:xinfresh@QQ:383094053校内:http:///getuser.do?id=234719042
2007-12-05 21:26
快速回复:计算器的最高成就!
数据加载中...
 
   



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

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