数学趣题——应用递归实现数制转换
递归法将二进制转换为十进制
源码:
1: #include <stdio.h>
2: #include <math.h>
3:
4: void biTOde(int n,int *sum,int *m)
5: {
6: char c;
7: scanf("%c",&c);
8: if(c!='#'){
9: *m = *m +1;
10: biTOde(n+1,&(*sum),&(*m));
11: }
12: if(c == '1') *sum = *sum + pow(2,(*m)-n-1);
13: }
14:
15: int main()
16: {
17: int sum = 0,m=0;
18: printf("Please input a binary number ending for '#'\n");
19: biTOde(0,&sum,&m) ;
20: printf("The Decimal Decimal digit is\n");
21: printf("%d ",sum);
22: return 0;
23: }
24: