NIT4956/HDU5083 Instruction·模拟

c++ string版 也不知道能不能过 √

数字转string
to_string(x)

string转数字 = = 没找到 自己写

char*字符串转数字
atof(char* s)

截取部分string
substr(int pos,int len)截取从pos位置开始,长度为len的string
substr(int pos)截取从pos位置开始,一直到末尾

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,m,k;
string op[6]={
        "ADD","SUB","DIV","MUL","MOVE","SET"
};
string solve(int x,int len){
    string t="";
    for(int i=0;i<len;i++){
        t=(char)((x%2)+'0')+t;
        x/=2;
    }
    return t;
}
int cal(string t,int b){//涉及到二进制转十进制 十进制转二进制
    int res=0;
    for (int i = 0; i <t.length(); ++i) {
        res=res*b+t[i]-'0';
    }
    return res;
}

string s,t;
int main(){
    while(cin>>n){
        if(n==1){//操作语言转二进制
            cin>>s;
            for (int i = 0; i <6; ++i) {
                if(s==op[i]){
                    t=solve(i+1,6);
                    break;
                }
            }
            cin>>s;
            int pos=s.find(",");
            int a=cal(s.substr(1,pos-1),10);//Ra 提取字符串里面的数字
            t+=solve(a,5);//十进制转二进制

            if(pos!=-1){//说明是SET 操作 没有,
                int b=cal(s.substr(pos+2),10);//Rb
                t+=solve(b,5);
            }else t+="00000";

            cout<<t<<endl;
        }else{//二进制转操作语言
        
            cin>>s;
            int x=cal(s.substr(0,6),2);//二进制转十进制
            if(x>0 &&x<7){
                t=op[x-1];
                int y=cal(s.substr(6,5),2);//二进制转十进制
                int z=cal(s.substr(11,5),2);
                if(x==6){
                    if(z==0 && (y>0 && y<=31)){
                        t+=" R"+to_string(y);
                    }else goto no;
                }else{
                    if((z>0 && z<=31)&&(y>0 &&y<=31)){
                        t+=" R"+to_string(y)+",R"+to_string(z);
                    }else goto no;
                }
            }else goto no;
            cout<<t<<endl;
        }
        continue;
        no:puts("Error!");
    }
    return 0;
}
发布了34 篇原创文章 · 获赞 0 · 访问量 960

猜你喜欢

转载自blog.csdn.net/Yubing792289314/article/details/104177121
今日推荐