Advertisement
Guest User

pseudocode

a guest
Mar 27th, 2011
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1.  
  2.  
  3. Exercise 1
  4.  
  5.  
  6.  
  7. Algorithm spur(t, n)
  8.  
  9. Pre: t :: Train
  10.  
  11. n :: Integer
  12.  
  13. Returns: a train
  14.  
  15. Post: the returned train has the first n cars removed
  16.  
  17.  
  18.  
  19. refToCar rDataOut
  20.  
  21. for i from 0 to n-1 do
  22.  
  23. Dequeue(t, rDataOut)
  24.  
  25. done
  26.  
  27. return t
  28.  
  29.  
  30.  
  31.  
  32.  
  33. Exercise 2
  34.  
  35.  
  36.  
  37. Algorithm SpurSome(f, n)
  38.  
  39. Pre: f :: String // naming the file to read
  40.  
  41. n :: Integer
  42.  
  43. Returns: a train
  44.  
  45. Post: the train read from the file named by ‘f’,
  46.  
  47. and with n cars removed from the front
  48.  
  49.  
  50.  
  51. NEED TO READ THE TRAIN FILE TO
  52. COUNT THE NUMBER OF LINES (NO. OF CARS) AND
  53. SET THIS NUMBER AS nCars
  54.  
  55.  
  56. Train* train<-makeTrain(nCars)
  57.  
  58. readTrain(train, f)
  59.  
  60. spur(train, n)
  61.  
  62. return train
  63.  
  64.  
  65.  
  66.  
  67.  
  68. Exercise 3
  69.  
  70.  
  71.  
  72.  
  73. Algorithm shunt(t)
  74.  
  75. Pre: t :: Train
  76.  
  77. Returns: a train
  78.  
  79. Post: the returned train is the original train, but in reverse order
  80.  
  81.  
  82.  
  83. Train* newtrn<-makeTrain(t->nCars) //note that the -> symbol indicates a node nCars of the t Train structure and Train* is the same as refToTrain
  84.  
  85. refToCar rDataOut //Car is an element and refToCar just means that it is a reference to Car structure
  86.  
  87. for i from 0 to nCars-1 do
  88.  
  89. PopFromStack(t,rDataOut)
  90.  
  91. PushOntoStack(newtrn,*rDataOut)
  92.  
  93. done
  94.  
  95. return *newtrain
  96.  
  97.  
  98.  
  99.  
  100.  
  101. Exercise 4
  102.  
  103.  
  104.  
  105. Algorithm reverse(f)
  106.  
  107. Pre: f :: String // naming a file
  108.  
  109. Returns: a train
  110.  
  111. Post: the returned train is the one read in, but in reverse order
  112.  
  113.  
  114.  
  115. refToCar rDataOut
  116.  
  117. NEED TO READ THE TRAIN FILE TO
  118. COUNT THE NUMBER OF LINES (NO. OF CARS) AND
  119. SET THIS NUMBER AS nCars
  120.  
  121.  
  122. Train* train<-makeTrain(nCars) // Train* could have been replaced by refToTrain in this pseudocode standard
  123.  
  124. readTrain(train, f)
  125.  
  126. shunt(train)
  127.  
  128. return train
  129.  
  130.  
  131.  
  132. Exercise 5
  133.  
  134.  
  135.  
  136.  
  137. Algorithm reverseSome(f, n)
  138.  
  139. Pre: f :: String // naming a file
  140.  
  141. n :: Integer
  142.  
  143. Returns: a train
  144.  
  145. Post: the returned train is the reverse of one read in,
  146.  
  147. with n cars removed before the reverse
  148.  
  149.  
  150.  
  151. NEED TO READ THE TRAIN FILE TO
  152. COUNT THE NUMBER OF LINES (NO. OF CARS) AND
  153. SET THIS NUMBER AS nCars
  154.  
  155.  
  156. Train* train<-makeTrain(nCars) // Train* is the same as refToTrain
  157.  
  158. train<-spurSome(f,n)
  159.  
  160. shunt(train)
  161.  
  162. return train
  163.  
  164.  
  165.  
  166. Exercise 6
  167.  
  168.  
  169.  
  170. Algorithm main()
  171.  
  172. Pre: a file containing commands in the form of
  173.  
  174. spur fname n
  175.  
  176. shunt fname n
  177.  
  178. concerned train files
  179.  
  180. Post: commands are executed and the resulting trains are added one after the other
  181.  
  182. in a sequence
  183.  
  184. Return: boolean showing success or failure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement