Data_Structure/Vorlesungen/DS/Beispiele/number-conversion.c
2025-01-20 21:25:33 +08:00

40 lines
829 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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