/* Program: number-conversion'.c (Report comments/bugs to chikh@yuntech.edu.tw) Function: 鍵盤讀入一個十進位正整數n及基底b(<=16的整數),隨後呼叫convert(int n, int b) 遞迴函數把n以b為基底表示的數字顯示出來;程式能持續轉換新輸入的數字,直到讀取到的n 為負數方結束執行 Note: 可以網址 https://bit.ly/3LVUgTg 所載軟體比對本程式產出結果是否正確 */ #include #include void convert(int n, int base) { int digit; if (n < base) { if (n < 10) printf("%d",n); else printf("%c",'A'+(n-10)); return; } convert(n/base,base); digit = n%base; if (digit < 10) printf("%d",digit); else printf("%c",'A'+(digit-10)); } int main() { int n = 0, base; for (;;) { printf("\n輸入待轉換的十進位正整數 => "); scanf("%d",&n); if (n < 0) break; printf("\n輸入轉換之基底(<=16) => "); scanf("%d",&base); printf("(%d)_10 = (",n); convert(n,base); printf(")_%d\n",base); } printf("\n程式將結束..."); return 0; }