C language hexadecimal conversion (stack implementation)
From the last two blogs, we can know that the stack has the characteristics of backward first-in-first-out, while the printing and output of the hexadecimal conversion are just the opposite of the computing process, meeting the requirements of the stack, so we can use the stack to quickly implement the hexadecimal conversion. below is the c function that implements the hexadecimal conversion using the stack.
Void conversion (SqStack * pstack, unsigned int N, const unsigned int d) {if (pstack = NULL) // when the input parameter is a pointer, exit (-1); int mod; // save mod = N % dwhile (N! = 0) {mod = N % d; stack_push (pstack, & mod); // import mod to stack N = N/d;} int top = 0; // display the top-of-stack element printf (convert % d in decimal order to % d and convert it to:, N, d); while (! Stack_is_empty (pstack) {stack_pop (pstack, & top); printf (% d, top);} return ;}
2. Results and Conclusions