题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5083
题目意思:如果给出 instruction 就需要输出对应的 16-bit binary code,给出16-bit binary code 就需要输出对应的instruction。
由于不会截取的技巧,代码量非常可观 = =,所以说,一直很讨厌做模拟题!!!
留下这代码,纪念一个代码还是不够精简的自己!!!内存和时间还能接受,也比较容易理解,不过好多重复代码= =。以下这个可以代码可以忽略,改到晕= =。之后会补上简单容易理解版滴......
一...场.......噩..........梦!!!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 20 + 5; 8 const int N = 10 + 2; 9 char instruct[7][N] = {"0", "ADD", "SUB", "DIV", "MUL", "MOVE", "SET"}; 10 char num_instruct[7][N] = {"0", "000001", "000010", "000011", "000100", "000101", "000110"}; 11 12 char R[32][N] = {"0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", 13 "R9", "R10", "R11", "R12", "R13", "R14", "R15", "R16", 14 "R17", "R18", "R19", "R20", "R21", "R22", "R23", "R24", 15 "R25", "R26", "R27", "R28", "R29", "R30", "R31"}; 16 char num_R[32][N] = {"0", "00001", "00010", "00011", "00100", "00101", "00110", 17 "00111", "01000", "01001", "01010", "01011", "01100", "01101", 18 "01110", "01111", "10000", "10001", "10010", "10011", "10100", 19 "10101", "10110", "10111", "11000", "11001", "11010", "11011", 20 "11100", "11101", "11110", "11111"}; 21 22 char s1[N], s2[maxn]; 23 char s[maxn]; 24 25 int main() 26 { 27 #ifndef ONLINE_JUDGE 28 freopen("ljy.txt", "r", stdin); 29 #endif 30 31 int ask; 32 while (scanf("%d", &ask) != EOF) 33 { 34 char tmp1[N], tmp2[N]; 35 if (ask == 1) 36 { 37 int i; 38 scanf("%s%s", s1, s2); 39 for (i = 1; i < 7; i++) 40 { 41 if (!strcmp(s1, instruct[i])) 42 { 43 printf("%s", num_instruct[i]); 44 break; 45 } 46 } 47 if (!strcmp(s1, "SET")) // SET指令单独处理 48 { 49 for (i = 0; i < 32; i++) 50 { 51 if (!strcmp(s2, R[i])) 52 { 53 printf("%s00000\n", num_R[i]); 54 break; 55 } 56 } 57 } 58 else 59 { 60 int l1 = 0; 61 int len = strlen(s2); 62 for (i = 0; i < len; i++) 63 { 64 if (s2[i] == ‘,‘) 65 break; 66 tmp1[l1++] = s2[i]; 67 } 68 tmp1[l1] = ‘\0‘; // 截取Rdestination 69 int r = i+1; 70 for (i = 0; i < 32; i++) 71 { 72 if (!strcmp(tmp1, R[i])) 73 { 74 printf("%s", num_R[i]); 75 break; 76 } 77 } 78 int l2 = 0; 79 for (i = r; i < len; i++) 80 tmp2[l2++] = s2[i]; 81 tmp2[l2] = ‘\0‘; // 截取Rsource 82 for (int i = 1; i < 32; i++) 83 { 84 if (!strcmp(tmp2, R[i])) 85 { 86 printf("%s\n", num_R[i]); 87 break; 88 } 89 } 90 } 91 } 92 else 93 { 94 scanf("%s", s); 95 char ans[1][maxn]; 96 int len1 = strlen(s); 97 int l1 = 0; 98 for (int i = 0; i < 6; i++) 99 tmp1[l1++] = s[i]; 100 tmp1[l1] = ‘\0‘; 101 bool flag = false; 102 for (int i = 1; i < 7; i++) 103 { 104 if (!strcmp(tmp1, num_instruct[i])) 105 { 106 strcpy(ans[0], instruct[i]); // 有可能是SET指令,这要继续往后看判断 107 flag = true; 108 break; 109 } 110 } 111 if (!flag) // 找不到指令匹配 112 printf("Error!\n"); 113 else 114 { 115 int l1 = 0; 116 for (int i = 6; i < 11; i++) 117 tmp1[l1++] = s[i]; 118 tmp1[l1] = ‘\0‘; // Rdestination 119 120 int l2 = 0; 121 for (int i = 11; i < 16; i++) 122 tmp2[l2++] = s[i]; 123 tmp2[l2] = ‘\0‘; // Rsource 124 125 if (!strcmp(ans[0], "SET")) 126 { 127 if (!strcmp(tmp2, "00000") && strcmp(tmp1, "00000")) // 符合条件的形式:000110?????00000 128 { 129 for (int i = 1; i < 32; i++) 130 { 131 if (!strcmp(tmp1, num_R[i])) 132 { 133 printf("SET %s\n", R[i]); 134 break; 135 } 136 } 137 } 138 else 139 printf("Error!\n"); 140 } 141 142 else 143 { 144 if (!strcmp(tmp2, "00000") || !strcmp(tmp1, "00000")) // 不合法的Registers 145 printf("Error!\n"); 146 else 147 { 148 printf("%s ", ans[0]); // 除SET之外的其他指令 149 for (int i = 1; i < 32; i++) 150 { 151 if (!strcmp(tmp1, num_R[i])) 152 { 153 printf("%s,", R[i]); 154 break; 155 } 156 } 157 for (int i = 1; i < 32; i++) 158 { 159 if (!strcmp(tmp2, num_R[i])) 160 { 161 printf("%s\n", R[i]); 162 break; 163 } 164 } 165 } 166 } 167 } 168 } 169 } 170 return 0; 171 }
时间: 2024-10-23 14:35:27