hdu5083——Instruction

Instruction

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


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)109destination operator code(5 bits)54source operator code(5 bits)0Form 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 code000001000010000011000100000101000110Form 3


Destination operator code and source operator code is the register code of the register which is related to.
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 0s. 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 should transfer an instruction into a 16-bit binary code;
‘0’ means you should 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.
1a,b31
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
  
  
1 ADD R1,R2 0 0000010000100010 0 1111111111111111
 

Sample Output
  
  
0000010000100010 ADD R1,R2 Error!
 

Source
 

Recommend
heyang   |   We have carefully selected several similar problems for you:   5085  5084  5082  5081  5080
SourceBestCoder Round #15 Recommendheyang | We have carefully selected several similar problems for you: 5085 5084 5082 5081 5080


模拟题,注意几个坑点就行

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<iostream>
#include<algorithm>

using namespace std;

char r1[22];
char r2[22];
char ss[22];
char sss[22];

int main()
{
    int c;
    map<string, string> qu, qu2;
    qu.clear();
    qu2.clear();
    qu["000001"] = "ADD";
    qu["000010"] = "SUB";
    qu["000011"] = "DIV";
    qu["000100"] = "MUL";
    qu["000101"] = "MOVE";
    qu["000110"] = "SET";
    qu2["ADD"] = "000001";
    qu2["SUB"] = "000010";
    qu2["DIV"] = "000011";
    qu2["MUL"] = "000100";
    qu2["MOVE"] = "000101";
    qu2["SET"] = "000110";
    while (~scanf("%d", &c))
    {
        if (c == 1)
        {
            scanf("%s%s", ss, r1);
            if ( strcmp(ss, "SET") )
            {
                printf("%s", qu2[ss].c_str());
                int t1 = 0, len = strlen(r1);
                int t2 = 0, j;
                for (int i = 1; i < len; i++)
                {
                    if (r1[i] == ',')
                    {
                        j = i;
                        break;
                    }
                    t1 = t1 * 10 + (r1[i] - '0');
                }
                len = strlen(r1);
                for (int i = j + 2; i < len; i++)
                {
                    t2 = t2 * 10 + (r1[i] - '0'); 
                }
        //        printf("%d %d\n", t1, t2);
                for (int i = 4; i >= 0; i--)
                {
                    if(t1 & (1 << i))
                    {
                        printf("1");
                    }
                    else
                    {
                        printf("0");
                    }
                }
                for (int i = 4; i >= 0; i--)
                {
                    if(t2 & (1 << i))
                    {
                        printf("1");
                    }
                    else
                    {
                        printf("0");
                    }
                }
                printf("\n");
            }
            else
            {
                printf("%s", qu2["SET"].c_str());
                int t1 = 0, len = strlen(r1);
                for (int i = 1; i < len; i++)
                {
                    t1 = t1 * 10 + (r1[i] - '0'); 
                }
                for (int i = 4; i >= 0; i--)
                {
                    if(t1 & (1 << i))
                    {
                        printf("1");
                    }
                    else
                    {
                        printf("0");
                    }
                }
                printf("00000\n");
            }
        }
        else
        {
            scanf("%s", sss);
            int len = strlen(sss);
            if(len != 16)
            {
                printf("Error!\n");
                continue;
            }
            char v[8], v1[8], v2[8];
            int t = 0, t1 = 0, t2 = 0;
            for (int i = 0; i < 6; i++)
            {
                v[i] = sss[i];
                 t = t * 2 + v[i] - '0';
            }
            v[6] = '\0';
            int c = 0;
        //    printf("%d\n", t);
            if(t < 1 || t > 6)
            {
                printf("Error!\n");
                continue;
            }
            for (int i = 6; i <= 10; i++)
            {
                v1[i - 6] = sss[i];
                t1 = t1 * 2 + v1[i - 6] - '0';
            }
            v1[5] = '\0';
        //    printf("%d\n", t1);
            for (int i = 11; i < 16; i++)
            {
                v2[i - 11] = sss[i];
                c = c * 2 + v2[i - 11] - '0';
            }
            v2[5] = '\0';
        //    printf("%d\n", c);
            if(t1 == 0)
            {
                printf("Error!\n");
                continue;
            }
            if(c < 0  || c > 31 || t1 < 0 || t1 > 31 || (t == 6 && c != 0) )
            {
                printf("Error!\n");
                continue;
            }
            if (t >= 1 && t <= 5 && c == 0)
            {
                printf("Error!\n");
                continue;
            }
            if(c == 0)
            {
                printf("%s R%d\n", qu[v].c_str(), t1);
                continue;
            }
            printf("%s R%d,R%d\n", qu[v].c_str(), t1, c);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值