HDU 5083 Instruction(大模拟)

Instruction

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


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
 


题目大意:

多组输入,每组有两行,第一行输入0或1,代表操作选项,如果是0,将二进制翻译为指令,如果是1,将指令翻译为二进制码。

将指令翻译为二进制码的规则:指令分为三部分,第一部分是一个字符串,翻译为6位二进制码,如表3所示,

ADD对应000001,SUB对应000010,DIV对应000011,MUL对应00100,MOVE对应000101,SET对应000110

第二部分是Ra,Rb,如果1<=a<=31且1<=b<=31,那么可以将数a,数b翻译为对应的5位二进制,空位补0;否则指令是错误的。

(注意:当第一部分给出的字符串不在表3给出的6种之一时,指令是错误的;当第一部分给出的字符串是SET时,第二部分只有Ra。Rb不会给出,但是默认b=00000;给出的a,b的值不一定在[1,31]的范围内。)

将二进制码翻译为指令的规则:给出的二进制码的前6位所表示的数是代表字符串,接下来5位代表数a,最后5位代表数b。

(注意:当二进制码对应的指令是SET Ra时


解题思路:根据题目要求,模拟过程即可。



代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
struct node
{
    string s;
    string si;
    string i;
};
node a[35];
int judge(string s)
{
    if(s=="ADD")  return 1;
    if(s=="SUB")  return 2;
    if(s=="DIV")  return 3;
    if(s=="MUL")  return 4;
    if(s=="MOVE") return 5;
    if(s=="SET")  return 6;
    return 0;
}
int main()
{
    int i,j,k,t;
    string s1,s2,s3;
    a[0].s="000";
    a[1].s="ADD";a[2].s="SUB";a[3].s="DIV";a[4].s="MUL";a[5].s="MOVE";a[6].s="SET";
    a[0].si="000000";
    a[1].si="000001";a[2].si="000010";a[3].si="000011";a[4].si="000100";a[5].si="000101";a[6].si="000110";
     a[0].i="00000";
     a[1].i="00001"; a[2].i="00010"; a[3].i="00011"; a[4].i="00100"; a[5].i="00101"; a[6].i="00110"; a[7].i="00111"; a[8].i="01000";
     a[9].i="01001";a[10].i="01010";a[11].i="01011";a[12].i="01100";a[13].i="01101";a[14].i="01110";a[15].i="01111";a[16].i="10000";
    a[17].i="10001";a[18].i="10010";a[19].i="10011";a[20].i="10100";a[21].i="10101";a[22].i="10110";a[23].i="10111";a[24].i="11000";
    a[25].i="11001";a[26].i="11010";a[27].i="11011";a[28].i="11100";a[29].i="11101";a[30].i="11110";a[31].i="11111";
    while(~scanf("%d",&t))
    {
        if(t==1)
        {
            cin>>s1>>s2;
            if(s1=="SET")
            {
                string c1;
                for(i=0;i<s2.size();i++)
                {
                    if(s2[i]<='9'&&s2[i]>='0')
                        c1=c1+s2[i];
                }
                k=c1.size();
                int num=1,x=0;
                while(k--)
                {
                    x+=(c1[k]-'0')*num;
                    num*=10;
                }
                if(x<1||x>31)
                    printf("Error!\n");
                else
                {
                    cout<<"000110";
                    cout<<a[x].i<<"00000"<<endl;
                }
            }
            else
            {
                int x=judge(s1),y,z,num;
                string c2,c3;
                int flag=0;
                for(i=0;i<s2.size();i++)
                {
                    if(flag==0&&s2[i]>='0'&&s2[i]<='9')
                        c2=c2+s2[i];
                    if(s2[i]==',')
                        flag=1;
                    if(flag==1&&s2[i]>='0'&&s2[i]<='9')
                        c3=c3+s2[i];
                }
                k=c2.size();
                num=1,y=0;
                while(k--)
                {
                    y+=(c2[k]-'0')*num;
                    num*=10;
                }
                k=c3.size();
                num=1,z=0;
                while(k--)
                {
                    z+=(c3[k]-'0')*num;
                    num*=10;
                }
                //printf("x=%d y=%d z=%d\n",x,y,z);
                if(x==0||y<1||y>31||z<1||z>31)
                    printf("Error!\n");
                else
                    cout<<a[x].si<<a[y].i<<a[z].i<<endl;
            }
        }
        else
        {
            cin>>s3;
            int x=(s3[0]-'0')*32+(s3[1]-'0')*16+(s3[2]-'0')*8+(s3[3]-'0')*4+(s3[4]-'0')*2+(s3[5]-'0')*1;
            int y=(s3[6]-'0')*16+(s3[7]-'0')*8+(s3[8]-'0')*4+(s3[9]-'0')*2+(s3[10]-'0')*1;
            int z=(s3[11]-'0')*16+(s3[12]-'0')*8+(s3[13]-'0')*4+(s3[14]-'0')*2+(s3[15]-'0')*1;
            if(x<1||x>6||y<1||y>31||z<0||z>31)
                printf("Error!\n");
            else
            {
                if((z==0&&x!=6)||(x==6&&z!=0))
                    printf("Error!\n");
                else
                {
                    if(x==6&&z==0)
                        cout<<a[x].s<<" "<<"R"<<y<<endl;
                    else
                        cout<<a[x].s<<" "<<"R"<<y<<","<<"R"<<z<<endl;
                }
            }
        }
    }
    return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值