码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5083 Instruction (稍比较复杂的模拟题)

时间:2014-11-05 22:51:37      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   div   on   

题意:

二进制指令转汇编指令,汇编指令转二进制指令。

 

思路:

额,条理分好,想全,思维不能乱。

 

代码:

int findyu(char yu[50],char c){
    int l=strlen(yu);
    rep(i,0,l-1) if(c==yu[i]) return i;
}
int calc(char t[50],int x,int k){
    int res=0;
    rep(i,x,x+k-1) res*=10, res+=(t[i]-0);
    return res;
}
int calc2(char t[50]){
    int l=strlen(t);
    int res=0;
    rep(i,0,l-1) res*=2, res+=(t[i]-0);
    return res;
}
void print(int x){
    int t1[10];
    int c=0;
    rep(i,0,4){
        t1[++c]=(x&1);
        x>>=1;
    }
    rep2(i,c,1) printf("%d",t1[i]);
}
int main(){
    //freopen("test.in","r", stdin);

    int kind;
    map<string,string> mp1;
    mp1["ADD"]="000001";
    mp1["SUB"]="000010";
    mp1["DIV"]="000011";
    mp1["MUL"]="000100";
    mp1["MOVE"]="000101";
    mp1["SET"]="000110";
    map<string,string> mp2;
    mp2["000001"]="ADD";
    mp2["000010"]="SUB";
    mp2["000011"]="DIV";
    mp2["000100"]="MUL";
    mp2["000101"]="MOVE";
    mp2["000110"]="SET";
    while(scanf("%d",&kind)!=EOF){
        if(kind==1){
            char ins[50],yu[50];
            int ra,rb=0;
            scanf("%s%s",ins,yu);
            if(strcmp(ins,"SET")==0)
                ra=calc(yu,1,strlen(yu)-1);
            else{
                int tc=findyu(yu,,);
                ra=calc(yu,1,tc-1);
                rb=calc(yu,tc+2,strlen(yu)-tc-2);
            }
            cout<<mp1[string(ins)];
            if(ins=="SET"){
                print(ra);
                printf("00000\n");
                continue;
            }
            print(ra);
            print(rb);
            cout<<endl;
            continue;
        }
        else{
            char str[50];
            scanf("%s",str);
            if(strlen(str)!=16){
                cout<<"Error!"<<endl;
                continue;
            }
            char ope[50],rra[50],rrb[50];
            rep(i,0,5) ope[i]=str[i]; ope[6]=0;
            rep(i,6,10) rra[i-6]=str[i]; rra[5]=0;
            rep(i,11,15) rrb[i-11]=str[i]; rrb[5]=0;
            int ra=calc2(rra);
            int rb=calc2(rrb);
            if(strcmp(ope,"000110")==0){
                if(rb!=0){
                    cout<<"Error!"<<endl;
                    continue;
                }
                if(ra<1 || ra>31){
                    cout<<"Error!"<<endl;
                    continue;
                }
                printf("SET R%d\n",ra);
                continue;
            }
            if(ra<1 || ra>31 || rb<1 || rb>31){
                cout<<"Error!"<<endl;
                continue;
            }
            if(mp2[string(ope)]==""){
                cout<<"Error!"<<endl;
                continue;
            }
            cout<<mp2[string(ope)];
            printf(" R%d,R%d\n",ra,rb);
            continue;
        }
    }

    //fclose(stdin);
}

 

hdu 5083 Instruction (稍比较复杂的模拟题)

标签:style   blog   io   color   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/fish7/p/4077363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!