题目:
应用递归,将输入的二进制数转换为十进制。
#include <stdio.h> #include <stdlib.h> void binary_to_decimal(char *s, long dec) { if( *s == ‘\0‘ ) printf("Decimal: %ld\n",dec); else { if( *s != ‘0‘ && *s != ‘1‘ ) { printf("Illegal Input.\n"); exit(1); } dec = dec*2 + (*s -‘0‘); binary_to_decimal(s+1,dec); } } int main(void) { char s[101]; printf("Please input a binary number: "); scanf("%s",s); binary_to_decimal(s,0); return 0; }
原文地址:http://www.cnblogs.com/DayByDay/p/3853390.html