An example might be as follows. We want to build a part number, 4789. Part number 4789 is composed of 3 items of part number 1567, part number 4567 (which is a sub assembly) and part number 6791. Part number 4567 requires the assembly of two items of part number 7864 and one item of 7814. In order to build part number 4789 we could fetch part numbers 1567, 6791, 7864, 7814 then assemble part numbers 7864 and 7814 into 4567 then assemble 1567, 6791, and 4567 into 4789.
We have been asked to write the control software for a simple robot that will build assemblies. Fortunately for us, it is a very simple robot...it can only perform 4 operations:
* OPERATOR CODE 1 : fetch n items of a specific part number and put them individually on a stack.
* OPERATOR CODE 2 : assemble a part by retrieving (popping) the last n items off the stack and then placing the newly assembled item on the stack.
* OPERATOR CODE 3 : perform a final assembly of all items and subassemblies in the stack.
Detailed Description.
Our robot will receive simplified input from a user using scanf() (i.e. scanf("%d%d%d",&var1,&var2,&var2) with no prompting and only perform the necessary stack operations with the input data with no extra messages except for the final result. We will only support 3 of the opcodes. Opcode 1 fetches n items of a partnumber and places them individually on a stack. Opcode 2 pops the last n items on the stack to create a new part and pushes the new part back on the stack. Opcode 3 simply prints out the part number of the final product and then every item in the stack separated by 1 space. Our robot can only handle 12 items in the stack at a time and if at any time this is exceeded, a single string "FAILURE" is printed using printf() and the program exits. Make certain you exit gracefully (don't just call exit() if you detect a stack overflow in a sub-function). A non-trivial penalty is assigned for any use of an exit() in your program.
Special Note: All that a subassembly does is pop a certain number of items of the stack assemble them into another partnumber
then push the new part number onto the stack in place of all the items that were popped. There is no need to validate that the items popped actually relate to the part number. Your primary concerns are that the subassembly doesn't cause a stack underflow.
INPUT:
Each instruction will be entered by a user in the format below with each item on an input line separated by a single space. In the special case of the final assembly a NOITEMS of -1 will be used, since all items in the stack must be assembled.
<OPCODE> <PARTNUMBER> <NOITEMS>
An example of the input to create part number 4789 is shown below.
INPUT :
1 1567 3
1 7864 2
1 7814 1
2 4567 3
1 6791 1
3 4789 -1
OUTPUT:
In all cases, output will be a single line of text with no newlines or tabs. When the robot can successfully assemble the part, you will print out the final part number then the part number of each item on the stack starting from the top of the stack. An example for the input above is shown below.
4789 6791 4567 1567 1567 1567
Please note that there are 3 instances of 1567 and there are no instances or either 7864 or 7814. There are no instances of 7864 or 7814 because those were used to create sub-assembly 4567.


LinkBack URL
About LinkBacks




Reply With Quote



Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum