Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. //: A UIKit based Playground for presenting user interface
  2.  
  3. import UIKit
  4. import PlaygroundSupport
  5. import Foundation
  6.  
  7. func reversedPolPost(_ input: String){
  8. var resultArray = [Character]()
  9. var sequencePriority = false
  10. var gap = 0 // gap for opertion prior
  11. var existOutOfSequencePriorityLowOperation = false
  12. var array = Array(input)
  13. for i in 0..<array.count {
  14. if array[i] == "+" || array[i] == "-" {
  15. if sequencePriority{
  16. resultArray.append(array[i])
  17. let temp = resultArray[resultArray.count-1]
  18. resultArray[resultArray.count-1] = resultArray[resultArray.count-2]
  19. resultArray[resultArray.count-2] = temp
  20. } else {
  21. resultArray.insert(array[i], at: 0)
  22. existOutOfSequencePriorityLowOperation = true
  23. gap += 1
  24. }
  25. }
  26. if array[i] == "*" || array[i] == "/" {
  27. if sequencePriority{
  28. resultArray.append(array[i])
  29. let temp = resultArray[resultArray.count-1]
  30. resultArray[resultArray.count-1] = resultArray[resultArray.count-2]
  31. resultArray[resultArray.count-2] = temp
  32. } else {
  33. existOutOfSequencePriorityLowOperation ? resultArray.insert(array[i], at: gap) : resultArray.insert(array[i], at: 0)
  34. }
  35. }
  36. if array[i] == "(" {
  37. sequencePriority = true
  38. }
  39. if array[i] == ")" {
  40. sequencePriority = false
  41. }
  42. if array[i].isNumber || array[i] == "."{ //number
  43. var gap = i
  44. while i < array.count { //(array[i].isNumber || array[i] == ".") &&
  45. if let _ = Int(String(array[gap])) {
  46. resultArray.append(array[gap])
  47. array[gap] = "n"
  48. gap += 1
  49. } else {
  50. if (array[gap] == "+" || array[gap] == "-" || array[gap] == "/" || array[gap] == "*") {
  51. resultArray.insert(array[gap], at: gap-(gap-i))
  52. array[gap] = "n"
  53. break
  54. }
  55. if (array[gap] == ".") {
  56. resultArray.append(array[gap])
  57. array[gap] = "n"
  58. gap += 1
  59. }
  60. }
  61. }
  62. resultArray.append(" ")
  63. }
  64. }
  65. print(String(resultArray))
  66. }
  67.  
  68. reversedPolPost("5+(874+1)*86+9")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement