Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exercise 1
- Algorithm spur(t, n)
- Pre: t :: Train
- n :: Integer
- Returns: a train
- Post: the returned train has the first n cars removed
- refToCar rDataOut
- for i from 0 to n-1 do
- Dequeue(t, rDataOut)
- done
- return t
- Exercise 2
- Algorithm SpurSome(f, n)
- Pre: f :: String // naming the file to read
- n :: Integer
- Returns: a train
- Post: the train read from the file named by f,
- and with n cars removed from the front
- NEED TO READ THE TRAIN FILE TO
- COUNT THE NUMBER OF LINES (NO. OF CARS) AND
- SET THIS NUMBER AS nCars
- Train* train<-makeTrain(nCars)
- readTrain(train, f)
- spur(train, n)
- return train
- Exercise 3
- Algorithm shunt(t)
- Pre: t :: Train
- Returns: a train
- Post: the returned train is the original train, but in reverse order
- 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
- refToCar rDataOut //Car is an element and refToCar just means that it is a reference to Car structure
- for i from 0 to nCars-1 do
- PopFromStack(t,rDataOut)
- PushOntoStack(newtrn,*rDataOut)
- done
- return *newtrain
- Exercise 4
- Algorithm reverse(f)
- Pre: f :: String // naming a file
- Returns: a train
- Post: the returned train is the one read in, but in reverse order
- refToCar rDataOut
- NEED TO READ THE TRAIN FILE TO
- COUNT THE NUMBER OF LINES (NO. OF CARS) AND
- SET THIS NUMBER AS nCars
- Train* train<-makeTrain(nCars) // Train* could have been replaced by refToTrain in this pseudocode standard
- readTrain(train, f)
- shunt(train)
- return train
- Exercise 5
- Algorithm reverseSome(f, n)
- Pre: f :: String // naming a file
- n :: Integer
- Returns: a train
- Post: the returned train is the reverse of one read in,
- with n cars removed before the reverse
- NEED TO READ THE TRAIN FILE TO
- COUNT THE NUMBER OF LINES (NO. OF CARS) AND
- SET THIS NUMBER AS nCars
- Train* train<-makeTrain(nCars) // Train* is the same as refToTrain
- train<-spurSome(f,n)
- shunt(train)
- return train
- Exercise 6
- Algorithm main()
- Pre: a file containing commands in the form of
- spur fname n
- shunt fname n
- concerned train files
- Post: commands are executed and the resulting trains are added one after the other
- in a sequence
- Return: boolean showing success or failure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement