In computer science, we can use a fixed format to represent an arithmetic.
Now, I give you a fixed format with 5 fields, op, rd, rs ,rt and imm.
The op means what operator will be done and there are 5 kinds of op, PLUS, MINUS, SWAP, SET and ANSWER.
The rd means where the operate result will be put.
The rs means where is the operand 1 and the rt mean where is the operand 2.
The imm means the constant.
PLUS and MINUS operator’s operand are variables.
The result of MINUS is always positive number.
The constant of SET is always positive number.
For example, the format (PLUS C A B -) means C is A plus B. Because we do not need constant, we give imm a -.
Format (SET B - - 200) means B is been set to 200.
Format (ANSWER - - - -) means print result.
Now, there are 5 integer array and each length is 2000000 and each integer represents a digit ( 0 ~ 9).
In addition, there are some arithmetic you need to complete.
#include#include #include #define OP_ANSWER 0 #define OP_PLUS 1 #define OP_MINUS 2 #define OP_SWAP 3 #define OP_SET 4 #define LEN 2000000 int *A, *B, *C, *D, *E; int op_translation(char *s){ if(!strcmp(s, "PLUS")) return OP_PLUS; else if(!strcmp(s, "MINUS")) return OP_MINUS; else if(!strcmp(s, "SWAP")) return OP_SWAP; else if(!strcmp(s, "SET")) return OP_SET; else if(!strcmp(s, "ANSWER")) return OP_ANSWER; else return -1; } int** operand_translation(char *rx){ switch(*rx){ case 'A': return &A; case 'B': return &B; case 'C': return &C; case 'D': return &D; case 'E': return &E; default: return NULL; } } void alu_plus(int *rd, int *rs, int *rt){ /*** Big number operation of plus: rd = rs + rt ***/ /// add your code here } void alu_minus(int *rd, int *rs, int *rt){ /*** Big number operation of minus: rd = rs - rt ***/ /// add your code here } void alu_set(int *rd, char *imm){ /*** Big number operation of set: rd = imm ***/ /// add your code here } void alu_swap(int **rd, int **rs){ /*** Big number operation of swap: rd <=> rs ***/ /// add your code here } void alu_answer_print(char *s){ int i, flag, *a=*operand_translation(s); flag = 0; printf("%s: ",s); for(i=LEN-1; i>=0; i--){ if(flag || *(a+i)>0){ printf("%d", *(a+i)); flag=1; } } if(!flag) printf("0\n"); else printf("\n"); } void alu_answer(){ alu_answer_print("A"); alu_answer_print("B"); alu_answer_print("C"); alu_answer_print("D"); alu_answer_print("E"); } void initial(){ A = (int*)malloc(sizeof(int)*LEN); B = (int*)malloc(sizeof(int)*LEN); C = (int*)malloc(sizeof(int)*LEN); D = (int*)malloc(sizeof(int)*LEN); E = (int*)malloc(sizeof(int)*LEN); memset(A, 0, sizeof(int)*LEN); memset(B, 0, sizeof(int)*LEN); memset(C, 0, sizeof(int)*LEN); memset(D, 0, sizeof(int)*LEN); memset(E, 0, sizeof(int)*LEN); } void destroy(){ free(A); free(B); free(C); free(D); free(E); } int main(){ char op[8]; /// operator char rd[4]; /// destination char rs[4]; /// source 1 char rt[4]; /// source 2 char imm[LEN]; /// immediate int **prd; /// pointer rd int **prs; /// pointer rs int **prt; /// pointer rt initial(); while(scanf("%s %s %s %s %s", op, rd, rs, rt, imm)==5){ prd = operand_translation(rd); prs = operand_translation(rs); prt = operand_translation(rt); switch(op_translation(op)){ case OP_MINUS : alu_minus (*prd, *prs, *prt); break; case OP_PLUS : alu_plus (*prd, *prs, *prt); break; case OP_SET : alu_set (*prd, imm ); break; case OP_SWAP : alu_swap ( prd, prs ); break; case OP_ANSWER : alu_answer(); break; } } destroy(); return 0; }
Many groups of arithmetic command with above format and rules.
5 variable.