User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 374,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,576 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser:
Dec 3rd, 2007
Views: 4,530
Visal has contributed code for computing a formula. I have updated his code
to reflect vb.net coding style. I have also refactored the code, so it is easier
to understand.

I have not checked the code.

The vb.net project files are as attached.

Jerry Dusing (email: jerrydusing@yahoo.com)
visualbasic Syntax | 5 stars
  1. ''' <summary>
  2. ''' To use this class do the following assignment
  3. ''' Value = StringCalculator.Calculate("Formula String")
  4. ''' </summary>
  5. ''' <remarks></remarks>
  6. Public Class StringCalculator
  7.  
  8. ' Updated to VB.net 2005 by Jerry W. A. Dusing
  9. ' Email : jerrydusing@yahoo.com
  10.  
  11. ' Refactored from code initially created by Visal
  12. ' Email : invisal@gmail.com'
  13. ' Vote for Visal in PSCODE through the link:
  14. ' http://www.pscode.com/vb/scripts/showcode.asp?txtCodeId=59600&lngWId=1
  15.  
  16. #Region "Public methods"
  17.  
  18. ''' <summary>
  19. ''' Computes the value of a given formula.
  20. '''
  21. ''' </summary>
  22. ''' <param name="pFormula">string formula</param>
  23. ''' <returns>Double</returns>
  24. ''' <remarks>
  25. ''' Supports the functions: cos, sin, log, abs, and mod.
  26. ''' For example : [10*2+(2+3)]/(5 mod 4)
  27. ''' </remarks>
  28. Public Shared Function Calculate(ByVal pFormula As String) As Double
  29. Try
  30. If FoundParenthesis(pFormula) Then
  31.  
  32. Return Calculate(ExpressionInParenthesis(pFormula))
  33.  
  34. ElseIf InStr(1, pFormula, "abs", vbTextCompare) > 0 Then
  35.  
  36. Return Absolute(pFormula)
  37.  
  38. ElseIf InStr(1, pFormula, "cos", vbTextCompare) > 0 Then
  39.  
  40. Return Cosine(pFormula)
  41.  
  42. ElseIf InStr(1, pFormula, "sin", vbTextCompare) > 0 Then
  43.  
  44. Return Sine(pFormula)
  45.  
  46. ElseIf InStr(1, pFormula, "log", vbTextCompare) > 0 Then
  47.  
  48. Return Logarithm(pFormula)
  49.  
  50. ElseIf InStr(1, pFormula, "mod") > 1 Then
  51.  
  52. Return Modulo(pFormula)
  53.  
  54. ElseIf InStr(1, pFormula, "+") > 1 Then
  55.  
  56. Return Plus(pFormula)
  57.  
  58. ElseIf InStr(1, pFormula, "-") > 1 Then
  59.  
  60. Return Minus(pFormula)
  61.  
  62. ElseIf InStr(1, pFormula, "*") > 1 Then
  63.  
  64. Return Multiply(pFormula)
  65.  
  66. ElseIf InStr(1, pFormula, "/") > 1 Then
  67.  
  68. Return Divide(pFormula)
  69.  
  70. ElseIf InStr(1, pFormula, "^") > 1 Then
  71.  
  72. Return Exponential(pFormula)
  73.  
  74. Else
  75.  
  76. If IsNumeric(pFormula) = True Then
  77. Return CDbl(pFormula)
  78. Else
  79. Throw New Exception("Unrecognized formula expresion.")
  80. End If
  81. End If
  82.  
  83. Catch ex As Exception
  84. Throw New Exception(ex.Message)
  85. End Try
  86.  
  87. End Function
  88.  
  89. #End Region
  90.  
  91. #Region "Private methods"
  92.  
  93. Private Shared Function Absolute( _
  94. ByVal pExp As String _
  95. ) As Double
  96. Dim index As Integer = InStr(1, pExp, "abs", vbTextCompare)
  97. Dim exp As String = ""
  98. For i As Integer = index + 3 To Len(pExp)
  99. Dim cmid As String = Mid(pExp, i, 1)
  100. If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
  101. exp = exp & cmid
  102. Else
  103. Exit For
  104. End If
  105. Next
  106. If exp <> "" Then
  107. ' replace the adsolute number with the answer
  108. exp = Replace(pExp, "abs" & exp, Math.Abs(CDbl(exp)).ToString())
  109. Return Calculate(exp)
  110. Else
  111. Mid(pExp, index, 3) = "000"
  112. Return Calculate(pExp)
  113. End If
  114. End Function
  115.  
  116. Private Shared Function Cosine( _
  117. ByVal pExp As String _
  118. ) As Double
  119. Dim index As Integer = InStr(1, pExp, "cos", vbTextCompare)
  120. ' clear all the text in exp2
  121. Dim exp As String = ""
  122. ' get cosines angle
  123. For i As Integer = index + 3 To Len(pExp)
  124. Dim cmid As String = Mid(pExp, i, 1)
  125. If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
  126. exp = exp & cmid
  127. Else
  128. Exit For
  129. End If
  130. Next
  131. If exp <> "" Then
  132. ' replace cosines with the answer
  133. exp = Replace(pExp, "cos" & exp, Math.Cos(CDbl(exp)).ToString())
  134. Return Calculate(exp)
  135. Else
  136. Mid(pExp, index, 3) = "000"
  137. Return Calculate(pExp)
  138. End If
  139. End Function
  140.  
  141. Private Shared Function Divide( _
  142. ByVal pExp As String _
  143. ) As Double
  144. Dim index As Integer = InStr(1, pExp, "/")
  145. Dim divisor As Double = Calculate(Right(pExp, Len(pExp) - index))
  146. If divisor = 0.0! Then
  147. Throw New Exception("Division by zero!")
  148. Else
  149. Return Calculate(Left(pExp, index - 1)) / divisor
  150. End If
  151. End Function
  152.  
  153. Private Shared Function Exponential( _
  154. ByVal pExp As String _
  155. ) As Double
  156. Dim index As Integer = InStr(1, pExp, "^")
  157. Return Calculate(Left(pExp, index - 1)) ^ Calculate(Right(pExp, Len(pExp) - index))
  158. End Function
  159.  
  160. Private Shared Function ExpressionInParenthesis( _
  161. ByVal pExp As String _
  162. ) As String
  163. Dim sIndex, eIndex As Integer
  164. 'searching for ")"
  165. MatchClosingParenthesis(pExp, sIndex, eIndex)
  166.  
  167. If eIndex <= sIndex + 1 Then ' check if () is empty or not
  168. Return Replace(pExp, "()", "0")
  169. Else
  170. ' get expression between "(" and ")"
  171. Dim RetVal As String = Mid(pExp, sIndex + 1, eIndex - (sIndex + 1))
  172. Return Replace(pExp, "(" & RetVal & ")", Calculate(RetVal).ToString())
  173. End If
  174. End Function
  175.  
  176. Private Shared Function FoundParenthesis( _
  177. ByRef pExp As String _
  178. ) As Boolean
  179. pExp = Replace(pExp, "\", "/") ' \ and / are same division
  180. pExp = Replace(pExp, "[", "(") ' "[" and "(" are the same also "]" and ")"
  181. pExp = Replace(pExp, "]", ")")
  182. Return InStr(1, pExp, "(") > 0
  183. End Function
  184.  
  185. Private Shared Function Logarithm( _
  186. ByVal pExp As String) As Double
  187. Dim index As Integer = InStr(1, pExp, "log", vbTextCompare)
  188. ' clear all the text in exp2
  189. Dim exp As String = ""
  190. ' get number
  191. For i As Integer = index + 3 To Len(pExp)
  192. Dim cmid As String = Mid(pExp, i, 1)
  193. If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
  194. exp = exp & cmid
  195. Else
  196. Exit For
  197. End If
  198. Next i
  199. If exp <> "" Then
  200. ' replace logarithmic with the answer
  201. exp = Replace(pExp, "log" & exp, Math.Log(CDbl(exp)).ToString())
  202. Return Calculate(exp)
  203. Else
  204. Mid(pExp, index, 3) = "000"
  205. Return Calculate(pExp)
  206. End If
  207. End Function
  208.  
  209. Private Shared Sub MatchClosingParenthesis( _
  210. ByRef pExp As String, _
  211. ByRef pStart As Integer, _
  212. ByRef pEnd As Integer _
  213. )
  214. Dim count As Integer
  215. For pEnd = (pStart + 1) To Len(pExp)
  216. Select Case Mid(pExp, pEnd, 1)
  217. Case "("
  218. count += 1
  219. Case ")"
  220. If count = 0 Then Exit For
  221. count -= 1
  222. End Select
  223. Next pEnd
  224.  
  225. If count > 0 Then ' if no ")" create ")" automatically
  226. pExp = pExp & Replace(Space(count + 1), " ", ")")
  227. pEnd = pEnd + count
  228. End If
  229. End Sub
  230.  
  231. Private Shared Function Minus( _
  232. ByVal pExp As String _
  233. ) As Double
  234. Dim index As Integer = InStr(1, pExp, "-")
  235. Return Calculate(Left(pExp, index - 1)) - Calculate(Right(pExp, Len(pExp) - index))
  236. End Function
  237.  
  238. Private Shared Function Modulo( _
  239. ByVal pExp As String _
  240. ) As Double
  241. Dim index As Integer = InStr(1, pExp, "mod")
  242. Return Calculate(Left(pExp, index - 1)) Mod Calculate(Right(pExp, Len(pExp) - (index + 2)))
  243. End Function
  244.  
  245. Private Shared Function Multiply( _
  246. ByVal pExp As String _
  247. ) As Double
  248. Dim index As Integer = InStr(1, pExp, "*")
  249. Return Calculate(Left(pExp, index - 1)) * Calculate(Right(pExp, Len(pExp) - index))
  250. End Function
  251.  
  252. Private Shared Function Plus( _
  253. ByVal pExp As String _
  254. ) As Double
  255. Dim index As Integer = InStr(1, pExp, "+")
  256. Return Calculate(Left(pExp, index - 1)) + Calculate(Right(pExp, Len(pExp) - index))
  257. End Function
  258.  
  259. Private Shared Function Sine( _
  260. ByVal pExp As String _
  261. ) As Double
  262. Dim index As Integer = InStr(1, pExp, "sin", vbTextCompare)
  263. ' clear all the text in exp2
  264. Dim exp As String = ""
  265. ' get sines angle
  266. For i As Integer = index + 3 To Len(pExp)
  267. Dim cmid As String = Mid(pExp, i, 1)
  268. If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
  269. exp = exp & cmid
  270. Else
  271. Exit For
  272. End If
  273. Next i
  274. If exp <> "" Then
  275. ' replace Sines with the answer
  276. exp = Replace(pExp, "sin" & exp, Math.Sin(CDbl(exp)).ToString())
  277. Return Calculate(exp)
  278. Else
  279. Mid(pExp, index, 3) = "000"
  280. Return Calculate(pExp)
  281. End If
  282. End Function
  283.  
  284. #End Region
  285.  
  286. End Class
Comments (Newest First)
paramesh_dreams | Newbie Poster | Mar 11th, 2008
hi can any one help for me i created the database through access named db1, and my coding is in vb 6 i hav completed the add and after that i go for delete option

i m not a expert in vb my question is i wanna del the record which consist of selected name,
before that there is a combo box if i run the project all the name should display in that combo box and i wanna select one name from that box and it should be deleted
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC