Stack application: Convert decimal to octal

Source: Internet
Author: User

PS: Application of 48-page data structure Stack

Decimal conversion octal

Write it by yourself. For your reference, you can further learn the data structure through the source code.

 

This book provides all the algorithm ideas

You have to think more about the code by yourself. You can't just read it. It's useless. You can't write your ideas as well.

To be a qualified programmer, write code. Reflect your capabilities through the amount of code

 

 

// Use the stack to implement the conversion between systems
# Include <stdio. h>
# Include <stdlib. h>
// # Include <malloc. h>
# Include <conio. h>
# Define stack_init_size 100
# Define stackincrement 10
Typedef struct {
Int * base;
Int * top;
Int stacksize;
} Sqstack;
Int initstack (sqstack & S) // construct an empty Stack
{
S. base = (int *) malloc (sizeof (INT) * (stack_init_size ));
If (! S. Base) Exit (-1 );
S. Top = S. base;
S. stacksize = stack_init_size;
Return 1;
}
Int push (sqstack & S, int e) // press the stack
{
If (S. Top-S. Base> = S. stacksize)
{
S. base = (int *) realloc (S. Base, sizeof (INT) * (stack_init_size + stackincrement ));
If (! S. Base) Exit (-1 );
S. Top = S. Base + S. stacksize;
S. stacksize + = stackincrement;
}
* S. Top ++ = E;
Return 1;
}
Int stackempty (sqstack s) // check whether the stack is empty
{
If (S. base = S. Top) return 1;
Else return 0;
}
Int POP (sqstack & S, Int & E) // output Stack
{
If (S. Top = S. Base) return 0;
E = * -- S. Top;
Return 1;
}
Void conversion () // convert decimal to octal
{
Sqstack S;
Int N;
Int E;
Initstack (s );
Printf ("input N value :");
Scanf ("% d", & N );
Printf ("converted value :");
While (N)
{
Push (S, N % 8 );
N = N/8;
}
While (! Stackempty (s ))
{
Pop (S, e );
Printf ("% d", e );
}
}
Void main ()
{
Conversion ();
Getch ();
}

 

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.