Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. Private Function _replacePower(Str As String) As String
  2. Const PowerString As String = "Pow({0},{1})"
  3.  
  4. Dim i, j As Integer
  5. Dim c As String
  6. Dim before, after, all As String
  7.  
  8. Dim other_p As Integer 'Keep track of other opening/closing parenthesis, to avoid breaking for a nested one.
  9.  
  10. other_p = -1
  11. i = 1
  12. While i <= Len(Str)
  13. c = Mid(Str, i, 1)
  14. If c = "^" Then
  15. If Mid(Str, i - 1, 1) = ")" Then
  16. j = i - 1
  17. Do While Mid(Str, j, 1) <> "(" Or other_p > 0
  18. If Mid(Str, j, 1) = ")" Then other_p = other_p + 1
  19. If Mid(Str, j, 1) = "(" Then other_p = other_p - 1
  20. j = j - 1
  21. Loop
  22. before = Mid(Str, j, i - j)
  23. Else
  24. j = i - 1
  25. 'The expression to be raised is everything between the power and + - * / , ( <- Opening parenthesis is not ok if there is no closing one, and this case is handled above.
  26. Do While (Mid(Str, j, 1) <> "+" And Mid(Str, j, 1) <> "-" And Mid(Str, j, 1) <> "*" And Mid(Str, j, 1) <> "/" And Mid(Str, j, 1) <> "," And Mid(Str, j, 1) <> "(")
  27. j = j - 1
  28. If j = 0 Then Exit Do
  29. Loop
  30. before = Mid(Str, j + 1, i - j - 1)
  31. End If
  32.  
  33. other_p = -1
  34.  
  35. If Mid(Str, i + 1, 1) = "(" Or other_p > 0 Then
  36. j = i + 1
  37. Do While Mid(Str, j, 1) <> ")"
  38. If Mid(Str, j, 1) = ")" Then other_p = other_p - 1
  39. If Mid(Str, j, 1) = "(" Then other_p = other_p + 1
  40. j = j + 1
  41. Loop
  42. after = Mid(Str, i + 1, j - i)
  43. Else
  44. j = i + 1
  45. Do While (Mid(Str, j, 1) <> "+" And Mid(Str, j, 1) <> "-" And Mid(Str, j, 1) <> "*" And Mid(Str, j, 1) <> "/" And Mid(Str, j, 1) <> "," And Mid(Str, j, 1) <> ")")
  46. j = j + 1
  47. If j = Len(Str) + 1 Then Exit Do
  48. Loop
  49. after = Mid(Str, i + 1, j - i - 1)
  50. End If
  51.  
  52. all = before & "^" & after
  53. Str = Replace(Str, all, String.Format(PowerString, before, after))
  54. i = 1
  55. End If
  56. i = i + 1
  57. End While
  58. Return Str
  59. End Function
Add Comment
Please, Sign In to add comment