大家帮帮忙,此函数的非递归实现该怎么做啊?
void printn(uint n, uint b){
static char *ntab = "0123456789ABCDEF";
uint a, m;
if (a = n / b){
printn(a, b);
}
m = n % b;
myputchar( ntab[m] );
}
[解决办法]
你这不是尾递归,非尾递归而要接触递归一般都伴随代码量大幅增加,所以楼主还是别了
[解决办法]
void printn(int n, int b){ static char *ntab = "0123456789ABCDEF"; int nrst = 1; static unsigned short usrst[20] = {0}; while (n != 0) { usrst[nrst++] = n % b; n = n / b; } while (--nrst) { //printf("%c",ntab[usrst[nrst]]); myputchar(ntab[usrst[nrst]]); }}
[解决办法]
晕,我测试的是9,所以到这和正着的一个样,所以没察觉。。。不好意思哈
void printn(uint n, uint b){ static char *ntab = "0123456789ABCDEF"; static char pcPut[128] = {0}; uint a, m=0,i=0; while(a = n / b){ pcPut[m] = n % b; n = a; m++; } pcPut[m] = n % b; for (i = m;i >= 0;i++) { myputchar(ntab[pcPut[i]]); }}