From the two blog, we can know that the stack has a last-in-first-out feature, and the output of the binary conversion is just the opposite of the computational process, to meet the stack this last-in-first-out feature, so you can use the stack to quickly implement the conversion, the following is a stack to implement the C function of the conversion
void conversion (sqstack *pstack,unsigned int N, const unsigned int d) {if (Pstack = = NULL)////When the incoming parameter is a pointer, you must empty exit ( -1); int mod ;//Save mod = n%dwhile (n! = 0) {mod = n%d;stack_push (pstack,&mod);//mod into stack N = n/d;} int top = 0;//Displays the stack top element printf ("10 binary%d to%d binary:", n,d), while (!stack_is_empty (Pstack)) {Stack_pop (pstack,&top); printf ("%d", top);} return;}
2. Results and conclusions
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C-Language conversion (stack implementation)