兩個七進制數(shù)相加 七進制輸出

#include#include#include

//進制轉(zhuǎn)換

int hex2int(char *str, int *result){

? ? *result = 0;

? ? while(*str > 0)

{

// printf("\nstr %s\n",str);

// printf("*str %c\n",*str);

// printf("==========\n");

? ? ? ? if(*str >= '0' && *str <= '6')

{

//printf("*str %c\n",*str);

? ? ? ? ? ? *result = *result * 7 + *str++ - '0';

//printf("*result %d\n",*result);

? ? ? ? }

else if(*str >= 'a' && *str <= 'f')

{

? ? ? ? ? ? *result = *result * 16 + *str++ - 'a' + 10;

? ? ? ? }

else if(*str >= 'A' && *str <= 'F')

{

? ? ? ? ? ? *result = *result * 16 + *str++ - 'A' + 10;

? ? ? ? }

else

{

? ? ? ? ? ? return 1;

? ? ? ? }

? ? }

//printf("*result %d\n",*result);

? ? return 0;

}

int int2num(int num, char **result, char scale){

? ? char arr[32];

? ? char *temp = arr + 32;

? ? //char *table = "0123456789abcdef";

char *table = "0123456";

? ? *--temp = 0;

? ? while(num > scale)

{

? ? ? ? *--temp = table[num % scale];

? ? ? ? num = num / scale;

? ? }

? ? if(num)

{

? ? ? ? *--temp = table[num];

? ? }

else if(*temp == 0)

{

? ? ? ? *--temp = '0';

? ? }

// printf("\ntemp %s\n",temp);

? ? *result = strdup(temp);

}

char* ADD(char *x, char *y){

? ? int a, b;

? ? if(hex2int(x, &a)){

? ? ? ? printf("x isn't hex.\n");

? ? }

? ? if(hex2int(y, &b)){

? ? ? ? printf("y isn't hex.\n");

? ? }

? ? int2num(a + b , &x, 7);

? ? return x;

}

int main(){

? ? char x[32], y[32];

? ? printf("x: ");

? ? scanf("%s", x);

? ? printf("y: ");

? ? scanf("%s", y);

? ? char *t = ADD(x, y);

? ? printf("x + y = %s\n", t);

? ? free(t);//釋放內(nèi)存

? ? return 0;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 春雨來了,坐在窗前感受有雨的時光,記憶的閘門打開了…… 四歲時,跟奶奶到廣安幺爺爺家玩。那時,幺爺爺家沒...
    譯丹Sunshine閱讀 352評論 0 0

友情鏈接更多精彩內(nèi)容