UVA 448 OOPS!

题目:把机器语言(16进制)转换为汇编语言代码

分析:明确“Instruction Description”,“An operand is made up of two fields: a mode and a value”,“operand的个数”,16进制字符转2进制字符,前两位后14位再转10进制数字(具体看题目 或 输入)

说明:1.读入数据(有坑) 2.可以输入多个 “一组数据”(满足英文说明)(“ Each line in the text file will contain exactly 30 hexadecimal digits except for the last line which will contain from 1 to 30 hexadecimal digits.”)

ac代码:

//一开始,以为是全部读入所有行,再处理,但实际上不是
#include "cstring"
#include"cstdio"
#include"iostream"
char hexTObinary[16][5]=
{
    "0000","0001","0010","0011","0100","0101","0110","0111","1000","1001",
    "1010","1011","1100","1101","1110","1111"
};
char cmd_list[16][5] =
{
    "ADD", "SUB", "MUL", "DIV", "MOV",
    "BREQ", "BRLE", "BRLS", "BRGE", "BRGR", "BRNE", "BR",
    "AND", "OR", "XOR", "NOT",
};

int par_list[16] =//汇编指令的 操作数个数
{
    2, 2, 2, 2, 2,
    1, 1, 1, 1, 1, 1, 1,
    3, 3, 3, 1,
};
void hex_char_to_binary_char(char *temp,char *instance);
void print_mod(char *instance);
int main()
{
    char input[1000000]= {0};
    int start=0,end=0,c=0;unsigned int i;
    while(~scanf("%s",&input[c]))
    {
        start=end;
        c+=30;
        for(i=start; i<strlen(input); i++)
        {
            int operator_number;
            if(input[i]>='0'&&input[i]<='9')
                operator_number=input[i]-'0';
            else
                operator_number=input[i]-'A'+10;

            int operator_counter=par_list[operator_number];
            if((operator_counter*4)<=(strlen(input)-i-1))
            {
                //输出操作符
                printf("%s ",cmd_list[operator_number]);
                //几个操作数
                while(operator_counter--)
                {

                    char input_temp[5]= {0};
                    int value=0;
                    char hexTObinary_instance[17]= {0};
                    //截取4个字符
                    for(int j=0; j<4; j++)
                    {
                        input_temp[j]=input[++i];
                    }
                    //16->2 字符

                    hex_char_to_binary_char(input_temp,hexTObinary_instance);
                    //计算value
                    int pow=1;
                    for(int j=15; j>=2; j--)
                    {
                        value+=pow*(hexTObinary_instance[j]-'0');
                        pow*=2;
                    }

                    //输出mode
                    print_mod(hexTObinary_instance);
                    //输出value
                    printf("%d",value);
                    //逗号
                    if(operator_counter!=0)
                        printf(",");
                }
                printf("\n");
            }
            else {end=i;break;}
        }
        if(i==strlen(input)||strlen(input)%30!=0)//如果是重启,即一行未满或正好满了,等于让整个输入重新开始
        {
            start=0,end=0,c=0;
            memset(input,0,sizeof(input));  
        }

    }
    return 0;
}

void hex_char_to_binary_char(char *temp,char *instance)
{

    for(int i=0; i<4; i++)
    {
        if(temp[i]>='0'&&temp[i]<='9')
            strcat(instance,hexTObinary[temp[i]-'0']);
        else
            strcat(instance,hexTObinary[temp[i]-'A'+10]);
        instance=instance+4;
    }

}
void print_mod(char *instance)
{
    char *instance2=instance+1;
    if((*instance)=='0'&&(*instance2)=='0')
        printf("R");
    else if((*instance)=='0'&&(*instance2)=='1')
        printf("$");
    else if((*instance)=='1'&&(*instance2)=='0')
        printf("PC+");

    return;
}

re代码

#include "cstring"
#include"cstdio"
#include"iostream"
char hexTObinary[16][5]={
    "0000","0001","0010","0011","0100","0101","0110","0111","1000","1001",
    "1010","1011","1100","1101","1110","1111"
};
char cmd_list[16][5] = {
	"ADD", "SUB", "MUL", "DIV", "MOV",
	"BREQ", "BRLE", "BRLS", "BRGE", "BRGR", "BRNE", "BR",
	"AND", "OR", "XOR", "NOT",
};

int par_list[16] = {
	2, 2, 2, 2, 2,
	1, 1, 1, 1, 1, 1, 1,
	3, 3, 3, 1,
};
 void hex_char_to_binary_char(char *temp,char *instance);
void print_mod(char *instance);
int main()
{
    char input[10000]={0};int start=0;
   while(~scanf("%s",&input[start]))
   {
       start+=30;
   }
   for(unsigned int i=0;i<strlen(input);i++)
   {
       int operator_number;
       if(input[i]>='0'&&input[i]<='9') operator_number=input[i]-'0';
       else operator_number=input[i]-'A'+10;
       int operator_counter=par_list[operator_number];
       //输出操作符
       printf("%s ",cmd_list[operator_number]);
       //几个操作数
       while(operator_counter--)
       {

           char input_temp[5]={0};int value=0;char hexTObinary_instance[17]={0};
           //截取4个字符
           for(int j=0;j<4;j++)
           {
               input_temp[j]=input[++i];
           }
           //16->2 字符

           hex_char_to_binary_char(input_temp,hexTObinary_instance);
           //计算value
           int pow=1;
           for(int j=15;j>=2;j--)
           {
               value+=pow*(hexTObinary_instance[j]-'0');
               pow*=2;
           }

           //输出mode
           print_mod(hexTObinary_instance);
           //输出value
           printf("%d",value);
            //逗号
           if(operator_counter!=0) printf(",");
       }
       printf("\n");
   }

    return 0;
}

 void hex_char_to_binary_char(char *temp,char *instance)
{

    for(int i=0;i<4;i++)
    {
        if(temp[i]>='0'&&temp[i]<='9')
        strcat(instance,hexTObinary[temp[i]-'0']);
        else
            strcat(instance,hexTObinary[temp[i]-'A'+10]);
        instance=instance+4;
    }

}
void print_mod(char *instance)
{
    char *instance2=instance+1;
    if((*instance)=='0'&&(*instance2)=='0') printf("R");
    else if((*instance)=='0'&&(*instance2)=='1') printf("$");
    else if((*instance)=='1'&&(*instance2)=='0') printf("PC+");

    return;
}

re原因:全部读入,ctrl+z结束,但题目并非如此,导致re。

原题:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
算法写得不是很高效,可以参考另一博主的优秀代码:
https://blog.csdn.net/mobius_strip/article/details/76239894

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值