[ACM] HDU 5083 Instruction (simulation)

Source: Internet
Author: User

[ACM] HDU 5083 Instruction (simulation)
Instruction

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 347 Accepted Submission (s): 101


Problem Description

Nowadays, Jim Green has produced a kind of computer called JG. in his computer, the instruction is represented by binary code. however when we code in this computer, we use some mnemonic symbols. for example, ADD R1, R2 means to add the number in register R1 and R2, then store the result to R1. But this instruction cannot be execute directly by computer, before this instruction is executed, it must be changed to binary code which can be executed by computer. each instruction corresponds to a 16-bit binary code. the higher 6 bits indicates the operation code, the middle 5 bits indicates the destination operator, and the lower 5 bits indicates the source operator. you can see Form 1 for more details.
15 operation code (6 bits) 109 destination operator code (5 bits) 54 source operator code (5 bits) 0 Form 1

In JG system there are 6 instructions which are listed in Form 2.
InstructionADD Ra, RbSUB Ra, RbDIV Ra, RbMUL Ra, RbMOVE Ra, RbSET RafunctionAdd the number in register Ra and Rb, then store the result to Ra. subtract the number in register Ra to Rb, then store the result to Ra. divide the number in register Ra by Rb, then store the result to Ra. mulplicate the number in register Ra and Rb, then store the result to Ra. move the number in register Rb to Ra. set 0 to Ra. form 2

Operation code is generated according to Form 3.
OperationADDSUBDIVMULMOVESETOperation code00000000010000011000100000101000110form 3

Destination operator code and source operator code is the register code of the register which is related.
There are 31 registers in total. Their names are R1, R2, R3 ..., R30, R31. The register code of Ri is the last 5 bits of the number of I in the binary system. for eaxample the register code of R1 is 00001, the register code of R2 is 00010, the register code of R7 is 00111, the register code of R10 is 01010, the register code of R31 is 11111.
So we can transfer an instruction into a 16-bit binary code easyly. for example, if we want to transfer the instruction ADD R1, R2, we know the operation is ADD whose operation code is 000001, destination operator code is 00001 which is the register code of R1, and source operator code is 00010 which is the register code of R2. So we joint them to get the 16-bit binary code which is 0000010000100010.
However for the instruction SET Ra, there is no source register, so we fill the lower 5 bits with five 0 s. For example, the 16-bit binary code of SET R10 is 0001100101000000
You are expected to write a program to transfer an instruction into a 16-bit binary code or vice-versa.


Input

Multi test cases (about 50000), every case contains two lines.
First line contains a type sign, '0' or '1 '.
'1' means you shoshould transfer an instruction into a 16-bit binary code;
'0' means you shoshould transfer a 16-bit binary code into an instruction.
For the second line.
If the type sign is '1', an instruction will appear in the standard form which will be given in technical specification;
Otherwise, a 16-bit binary code will appear instead.
Please process to the end of file.

[Technical Specification]
The standard form of instructions is
ADD Ra, Rb
SUB Ra, Rb
DIV Ra, Rb
MUL Ra, Rb
MOVE Ra, Rb
SET Ra
Which are also listed in the Form 2.
1 ≤ a, B ≤ 31
There is exactly one space after operation, and exactly one comma between Ra and Rb other than the instruction SET Ra. No other character will appear in the instruction.


Output

For type '0', if the 16-bit binary code cannot be transferred into a instruction according to the description output "Error !" (Without quote), otherwise transfer the 16-bit binary code into instruction and output the instruction in the standard form in a single line.
For type '1', transfer the instruction into 16-bit binary code and output it in a single line.


Sample Input

1ADD R1,R20000001000010001001111111111111111


Sample Output

0000010000100010ADD R1,R2Error!


Source

BestCoder Round #15

Solution:

Read and understand the simulation questions. When the command is Set, pay special attention to it separately.

Code:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Include
         
           # Include
          
            Using namespace std; map
           
             Z; // map operator to binary instruction
            
              Z2; // The binary number corresponds to the decimal number void prepare () {z ["000001"] = "ADD"; z ["000010"] = "SUB "; z ["000011"] = "DIV"; z ["000100"] = "MUL"; z ["000101"] = "MOVE "; z ["000110"] = "SET"; z2 ["00001"] = 1; z2 ["00010"] = 2; z2 ["00011"] = 3; z2 ["00100"] = 4; z2 ["00101"] = 5; z2 ["00110"] = 6; z2 ["00111"] = 7; z2 ["01000"] = 8; z2 ["01001"] = 9; z2 ["01010"] = 10; z2 ["01011"] = 11; z2 ["01100"] = 12; z2 ["01101"] = 13; z2 ["01110"] = 14; z2 ["01111"] = 15; z2 ["10000"] = 16; z2 ["10001"] = 17; z2 ["10010"] = 18; z2 ["10011"] = 19; z2 ["10100"] = 20; z2 ["10101"] = 21; z2 ["10110"] = 22; z2 ["10111"] = 23; z2 ["11000"] = 24; z2 ["11001"] = 25; z2 ["11010"] = 26; z2 ["11011"] = 27; z2 ["11100"] = 28; z2 ["11101"] = 29; z2 ["11110"] = 30; z2 ["11111"] = 31 ;} string find (string op) // BINARY command corresponding to the search operator {map
             
               : Iterator I; for (I = z. begin (); I! = Z. end (); I ++) {if (I-> second = op) return (I-> first); if (I-> first = op) return (I-> second);} return "Error! ";} String find2 (int op) // find the binary {map
              
                : Iterator I; for (I = z2.begin (); I! = Z2.end (); I ++) {if (I-> second = op) return (I-> first);} return "Error! ";} Int find22 (string op) // search for the decimal {map corresponding to the binary number
               
                 : Iterator I; for (I = z2.begin (); I! = Z2.end (); I ++) {if (I-> first = op) return (I-> second) ;}return-1 ;}string op, source; // enter string zhi; // enter string wa = "Error! "; Int sigh; int main () {prepare (); while (scanf (" % d ", & sigh )! = EOF) {if (sigh = 1) {cin> op> source; if (op = "SET ") // judge {cout <"000110"; int a = 0; // extract the decimal number for (int I = 1; I
                
                  0 & deng = 0) a1 = a1 * 10 + source [p]-'0'; if (deng = 1) break;} p = p + 2; for (; p
                 
                   Zhi; string zhiling = zhi. substr (0, 6); string temp = find (zhiling); if (temp = wa) {cout <
                  <
                   


<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.