【CUGBACM15级BC第15场 B】hdu 5082 Instruction

70 篇文章 0 订阅

Instruction

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



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!
 

题目的输入是有多组的,每一组有两行,第一行是命令,第二行是一串字符串!

所以这道题有两个操作:

当输入 1 的时候表示:将第二行所输入的命令翻译为16位2进制;

当输入 0 的时候表示:将第二行所输入的16位2进制翻译为命令;

 

先说下怎么将命令翻译为2进制:eg:ADD RX,RY--> ADD R1,R4=>0000010000100100

总共有6个操作符,第i个操作符所代表的数字为i(1=<i<=6)

然后有两个数字X和Y;(1=<X,Y<=31)

将i,X,Y翻译为相应的2进制然后输出即可!当然要注意的是:因为总共16位,所以注意补0 ,以此来达到16位!

SET RX  :这里只有两个数字!可以把这条语句想成是move RX R0(只是这样想,不能按照语句这样翻译!)那么SET R4翻译出来应该是:0001100010000000

 

然后再说下怎么将2进制翻译为命令:根据题意将这16位2进制切割成3部分,然后翻译每一部分,如果翻译出来的值的范围超出题意所要求的,或是不符合规则,就输出“Error!”。当然同样也要注意SET这个操作符!


/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;

map<string, string> MP;
map<string, string>mp;
char aa[1005];
int len;
bool in(int x)
{
    return x >= 1 && x <= 31;
}
void OT(int a)
{
    char ans[10];
    ans[5] = '\0';
    for (int i = 4; i >= 0; i--)
    {
        ans[i] = a % 2 + '0';
        a /= 2;
    }
    printf("%s", ans);
}
void work_1(int n)
{
    string a;
    int i;
    for (i = 0; aa[i] != ' ' && i < len; i++)
    {
        a += aa[i];
    }
    if (n != 1 || aa[i] != ' ' || MP.count(a) == 0)
    {
        puts("Error!");
        return;
    }


    int b = 0, c = 0;
    for (i = i + 2; i < len && aa[i] <= '9' && aa[i] >= '0'; i++)
    {
        b = b * 10 + aa[i] - '0';
    }
    if (a == "SET")
    {
        if (in(b) == 0 || i != len)
        {
            puts("Error!");
            return;
        }
        printf("000110");
        OT(b);
        OT(0);
        cout << endl;
        return;
    }
    if (aa[i] != ',' || aa[i + 1] != 'R')
    {
        puts("Error!");
        return;
    }
    for (i = i + 2; i < len && aa[i] <= '9' && aa[i] >= '0'; i++)
    {
        c = c * 10 + aa[i] - '0';
    }
    if (in(b) == 0 || in(c) == 0 || i != len)
    {
        puts("Error!");
        return;
    }
    cout << MP[a];
    OT(b);
    OT(c);
    cout << endl;
    return;
}
void work_2()
{
    string a;
    for (int i = 0; i < 6; i++)
    {
        a += aa[i];
    }
    if (len != 16 || mp.count(a) == 0)
    {
        puts("Error!");
        return;
    }


    int b = 0, c = 0;
    for (int i = 6; i < 11; i++)
    {
        b = b * 2 + aa[i] - '0';
    }
    for (int i = 11; i < 16; i++)
    {
        c = c * 2 + aa[i] - '0';
    }
    if (in(b) == 0)
    {
        puts("Error!");
        return;
    }
    if (a == "000110")
    {
        if (c != 0)
        {
            puts("Error!");
        }
        else
        {
            cout << mp[a] << " R" << b << endl;
        }
        return;
    }
    if (in(c) == 0)
    {
        puts("Error!");
        return;
    }
    cout << mp[a] << " R" << b << ",R" << c << endl;
}
void initmp()
{
    mp["000001"] = "ADD";
    mp["000010"] = "SUB";
    mp["000011"] = "DIV";
    mp["000100"] = "MUL";
    mp["000101"] = "MOVE";
    mp["000110"] = "SET";

    MP["ADD"] = "000001";
    MP["SUB"] = "000010";
    MP["DIV"] = "000011";
    MP["MUL"] = "000100";
    MP["MOVE"] = "000101";
    MP["SET"] = "000110";
}
int main()
{
    initmp();
    int n;
    while (cin >> n)
    {
        getchar();
        gets(aa);
        len = strlen(aa);
        if (n)
        {
            work_1(n);
        }
        else
        {
            work_2();
        }
    }
    return 0;
}
/************************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值