Moze li nekoj da mi reshi edna zadacha od codefu.mk za 300 boda vo molam hinto mi e jas simnav edna. eve ja
StackMachine
A simple Stack Machine has the following instruction set:
1. PUSH "NUMBER" - Add new element at the top of the stack, with value equals to "NUMBER".
The word "PUSH" is separated from the "NUMBER" part with exactly one SPACE.
"NUMBER" is an integer, with at least one digit, and at most 15 digits.
2. POP - Remove the element that is at the top of the stack.
3. ADD - Addition
4. SUB - Subtraction
5. MUL - Multiplication
6. SWAP - Swaps the two top most elements from the stack.
7. PRINT - Print the element that is at the top of the stack, enclosed in square brackets. If there are no elements in the stack, [EMPTY] is printed.
The three arithmetic instructions (ADD, SUB and MUL) operate on the two top most elements in the stack.
For each arithmetic instructions:
- first the two top most elements are removed from the stack,
- the arithmetic operation is performed and
- the result is pushed as a new top of the stack.
For SUB, the first operand is the element at the top of the stack.
When there are not enough elements in the stack to perform the operation,
the state of the Stack remains unchanged and the instruction prints the following error message: [ERROR].
This is the case for the: ADD, MUL, SUB, SWAP and POP instructions.
Each element of the stack can hold a long number (both positive and negative).
There will be no overflow (guaranteed by the test cases).
Input parameters:
instructions - array of String, the instructions that are to be executed.
Constraints:
instructions will have between 1 and 100 elements, inclusive.
each element of instructions will be a valid instruction, as described above.
the calculation results will never exceed a long value.
Return value:
String, the output of the execution of the instructions.
Class Name:
StackMachine
Method signature:
public String getOutput(String[] instructions)
Test Case 1:
getOutput({"PUSH 3", "PRINT"}) = "[3]"
Test Case 2:
getOutput({"PUSH 2", "PUSH 3", "ADD", "PRINT"}) = "[5]"
Test Case 3:
getOutput({"PUSH 2", "PUSH 3", "MUL", "PRINT"}) = "[6]"
Test Case 4:
getOutput({"PUSH 2", "PUSH 3", "SUB", "PRINT"}) = "[1]"
Test Case 5:
getOutput({"PUSH 3", "PUSH 2", "SUB", "PRINT"}) = "[-1]"
Test Case 6:
getOutput({"PUSH 111", "PUSH 222", "ADD"}) = ""
Test Case 7:
getOutput({"PUSH 200", "ADD", "MUL", "PRINT"}) = "[ERROR][ERROR][200]"
Test Case 8:
getOutput({"PUSH 99999", "SUB", "SWAP", "PRINT"}) = "[ERROR][ERROR][99999]"
Test Case 9:
getOutput({"PUSH 456", "SWAP", "PRINT", "POP", "POP", "PRINT"}) = "[ERROR][456][ERROR][EMPTY]"
Test Case 10:
getOutput({"POP", "PRINT", "PUSH 100", "ADD", "PRINT"}) = "[ERROR][EMPTY][ERROR][100]"