Data structure (Yan Weimin) sequential stack-number Conversion

Source: Internet
Author: User

The Code is as follows:

Note:

1. p48 page algorithm 3.1

2. Add this code segment to "data structure (Yan Weimin) sequence stack ".

If any non-negative decimal integer is input, print the number of octal equal to its value.

Void conversion ()/* algorithm 3.1 */
{Sqstack S;
Unsigned N;/* non-negative integer */
Selemtype E;
Initstack (& S);/* initialize stack */
Printf ("N (> = 0) = ");
Scanf ("% u", & N);/* enter a non-negative decimal integer N */
While (N)/* WHEN n is not equal to 0 */
{
Push (& S, N % 8);/* The number of the N in the inbound stack divided by the remainder of 8 (the low position in the octal System )*/
N = N/8;
}
While (! Stackempty (s)/* When the stack is not empty */
{
Pop (& S, & E);/* the top element of the stack is displayed and assigned to E */
Printf ("% d", e);/* Output E */
}
Printf ("/N ");
}

For any non-negative decimal integer input, print the hexadecimal number equivalent to the output value.

Void conversion ()
{Sqstack S;
Unsigned N;/* non-negative integer */
Selemtype E;
Initstack (& S);/* initialize stack */
Printf ("N (> = 0) = ");
Scanf ("% u", & N);/* enter a non-negative decimal integer N */
While (N)/* WHEN n is not equal to 0 */
{
Push (& S, N % 16);/* the remainder of inbound stack n divided by 16 (low position in hexadecimal mode )*/
N = N/16;
}
While (! Stackempty (s)/* When the stack is not empty */
{
Pop (& S, & E);/* the top element of the stack is displayed and assigned to E */
If (E <= 9)
Printf ("% d", e );
Else
Printf ("% C", e + 55 );
}
Printf ("/N ");
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.