home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / componen / vsflex / demo / fregular.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-08-30  |  4.3 KB  |  144 lines

  1. VERSION 2.00
  2. Begin Form fRegular 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Evaluator"
  5.    ClientHeight    =   2805
  6.    ClientLeft      =   1875
  7.    ClientTop       =   1155
  8.    ClientWidth     =   4410
  9.    Height          =   3210
  10.    Left            =   1815
  11.    LinkTopic       =   "Form3"
  12.    ScaleHeight     =   2805
  13.    ScaleWidth      =   4410
  14.    Top             =   810
  15.    Width           =   4530
  16.    Begin TextBox Text2 
  17.       FontBold        =   0   'False
  18.       FontItalic      =   0   'False
  19.       FontName        =   "MS Sans Serif"
  20.       FontSize        =   9.75
  21.       FontStrikethru  =   0   'False
  22.       FontUnderline   =   0   'False
  23.       Height          =   465
  24.       Left            =   90
  25.       TabIndex        =   0
  26.       Top             =   1845
  27.       Width           =   3120
  28.    End
  29.    Begin CommandButton Command1 
  30.       BackColor       =   &H00C0C0C0&
  31.       Caption         =   "Formula"
  32.       FontBold        =   0   'False
  33.       FontItalic      =   0   'False
  34.       FontName        =   "MS Sans Serif"
  35.       FontSize        =   8.25
  36.       FontStrikethru  =   0   'False
  37.       FontUnderline   =   0   'False
  38.       Height          =   375
  39.       Left            =   3285
  40.       TabIndex        =   3
  41.       Top             =   90
  42.       Width           =   825
  43.    End
  44.    Begin TextBox Text1 
  45.       FontBold        =   0   'False
  46.       FontItalic      =   0   'False
  47.       FontName        =   "MS Sans Serif"
  48.       FontSize        =   9.75
  49.       FontStrikethru  =   0   'False
  50.       FontUnderline   =   0   'False
  51.       Height          =   465
  52.       Left            =   90
  53.       TabIndex        =   2
  54.       Text            =   "(5+3)*2"
  55.       Top             =   1305
  56.       Width           =   3120
  57.    End
  58.    Begin vsFlexString fs 
  59.       Left            =   3600
  60.       Pattern         =   "[^ ,    ]*"
  61.       Text            =   "VideoSoft FlexString"
  62.       Top             =   540
  63.    End
  64.    Begin Label Label1 
  65.       BackStyle       =   0  'Transparent
  66.       Caption         =   "Type expressions consisting of numbers, parenthesis, and basic operators (+, -, *, /, ^). Then click the button and see the result."
  67.       FontBold        =   0   'False
  68.       FontItalic      =   0   'False
  69.       FontName        =   "MS Sans Serif"
  70.       FontSize        =   9.75
  71.       FontStrikethru  =   0   'False
  72.       FontUnderline   =   0   'False
  73.       Height          =   1095
  74.       Left            =   90
  75.       TabIndex        =   1
  76.       Top             =   135
  77.       Width           =   3075
  78.    End
  79. Option Explicit
  80. Sub Command1_Click ()
  81.   caption = "Evaluator"
  82.   Text2 = Format(Evaluate(Text1), "0.00")
  83. End Sub
  84. Function Evaluate (ByVal s$)
  85.   Dim s1$, s2$, s3$
  86.   ' get ready to parse
  87.   fs = Trim(s)
  88.   ' parenthesis
  89.   fs.Pattern = "{.*}({.*}){.*}"
  90.   If fs.MatchCount > 0 Then
  91.     fs.TagIndex = 0: s1 = fs.TagString
  92.     fs.TagIndex = 1: s2 = fs.TagString
  93.     fs.TagIndex = 2: s3 = fs.TagString
  94.     Evaluate = Evaluate(s1 + Format(Evaluate(s2)) + s3)
  95.     Exit Function
  96.   End If
  97.   ' add
  98.   fs.Pattern = "{.*}{[+-]}{.*}"
  99.   If fs.MatchCount > 0 Then
  100.     fs.TagIndex = 0: s1 = fs.TagString
  101.     fs.TagIndex = 2: s2 = fs.TagString
  102.     fs.TagIndex = 1
  103.     Select Case fs.TagString
  104.       Case "+": Evaluate = Evaluate(s1) + Evaluate(s2)
  105.       Case "-": Evaluate = Evaluate(s1) - Evaluate(s2)
  106.     End Select
  107.     Exit Function
  108.   End If
  109.   ' multiply
  110.   fs.Pattern = "{.*}{[*/]}{.*}"
  111.   If fs.MatchCount > 0 Then
  112.     fs.TagIndex = 0: s1 = fs.TagString
  113.     fs.TagIndex = 2: s2 = fs.TagString
  114.     fs.TagIndex = 1
  115.     Select Case fs.TagString
  116.       Case "*": Evaluate = Evaluate(s1) * Evaluate(s2)
  117.       Case "/":
  118.         If Evaluate(s2) <> 0 Then
  119.           Evaluate = Evaluate(s1) / Evaluate(s2)
  120.         Else
  121.           caption = "error: Divide by 0"
  122.           Beep
  123.         End If
  124.     End Select
  125.     Exit Function
  126.   End If
  127.   ' power
  128.   fs.Pattern = "{.*}^{.*}"
  129.   If fs.MatchCount > 0 Then
  130.     fs.TagIndex = 0: s1 = fs.TagString
  131.     fs.TagIndex = 1: s2 = fs.TagString
  132.     Evaluate = Evaluate(s1) ^ Evaluate(s2)
  133.     Exit Function
  134.   End If
  135.   ' number
  136.   fs.Pattern = "[0-9]+\.?[0-9]?"
  137.   If fs.MatchLength = Len(fs) Then
  138.     Evaluate = Val(s)
  139.   Else
  140.     caption = "error: Syntax"
  141.     Beep
  142.   End If
  143. End Function
  144.