DSP

编一个程序,完成64位数据(无符号)的加法、减法运算。不允许直接使用64位的数据类型。(未完成)

2019-07-13 17:51发布

    #include #include #define ADD 1 #define SUB 2 typedef struct Num_64 { long low; long high; }Num_64;   Num_64 Add_64(Num_64 a, Num_64 b); Num_64 Sub_64(Num_64 a, Num_64 b); Num_64 ConChToInt(char *p,int len);   Num_64 GetInput() { Num_64 input; char arr[20]; scanf("%s",arr); input = ConChToInt(arr, strlen(arr)); printf("0X%x%x/n",input.high,input.low); return input; } void main() { int choice; Num_64 a; Num_64 b; Num_64 result; //printf("请选择你要进行的运算:/n1.加法/t2.减法/n"); //scanf("%d",&choice); //while(1 != choice || 2 != choice) //{ // printf("您的输入有误,请重新输入:/n"); // scanf("%d",&choice); //} //switch(choice) //{ //case ADD: // ; // break; //case SUB: // ; // break; //default: // break; //} //printf("请输入两个64位数(十六进制)"); a = GetInput(); /*a.high = 0x01111111; a.low = 0xcccc1111; b.high = 0xcccc1111; b.low = 0xcccc1111; result = Add_64(a, b);*/ //printf("a + b =%x%x/n",result.high,result.low); }   Num_64 Add_64(Num_64 a, Num_64 b) { Num_64 result; if(a.low + b.low > 0xFFFFFFFF) { result.low = a.low + b.low - 0xFFFFFFFF +1; result.high = a.high + b.high + 1; if(result.high > 0xFFFF) { printf("相加溢出!/n"); exit(1); } else  return result;   } else { result.low = a.low + b.low; result.high = a.high + b.high; if(result.high > 0xFFFFFFFF) { printf("相加溢出!/n"); exit(1); } else  return result; } } Num_64 Sub_64(Num_64 a, Num_64 b) { Num_64 result; if(a.high < b.high || a.high == b.high && a.low < b.low) { printf("减数小于被减数!/n"); exit(1); } else { if(a.low < b.low) { result.low = 0xFFFFFFFF - b.low + 1 + a.low; result.high = a.high - b.high - 1; } else { result.low = a.low - b.low; result.high = a.high - b.high; } } return result; } Num_64 ConChToInt(char *p,int len) { Num_64 input = {0,0}; int i; //int val; int power = 1; for(i = 1; i<= 8; ++i) { if(p[len - i] <= '9' && p[len - i] >= '0') input.low += (p[len - i] - '0') * power; else if(p[len - i] <= 'f' && p[len - 1] >= 'a') input.low += (p[len - i] - 'a') * power; else if(p[len - i] <= 'F' && p[len - 1] >= 'A') input.low += (p[len - i] - 'A') * power; else { printf("您的输入有误,请重新输入:/n"); input = GetInput(); return input; } power *=8; } power = 1; while(0 != (len - i + 1)) { input.high += p[len - i] * power; power *=8; ++i; } return input; }