Linear table _ Using stacks for binary conversion to octal/decimal/16 binary

Source: Internet
Author: User
Tags pow printf

1. Definitions and Concepts

Stack

Stacks are special linear tables that allow insertions and deletions on the same side. One end of the allowed insert and delete operations is called the top of the stack (top), the other end is the bottom of the stack (bottom), the stack bottom is fixed, and the stack top floats, and the number of elements in the stack is zero, called the empty stack. Insertion is generally called a stack (PUSH), and deletion is called a fallback (POP). A stack is also called a LIFO table.


viod *malloc (size_t size)

Allocates a memory space that specifies size bytes to the system request. The return type is the void* type. Void* represents a pointer to an indeterminate type. c,c++ Specifies that the void* type can be cast to any other type of pointer by type conversion. If the assignment succeeds, returns a pointer to the allocated memory (the initial value in this store is indeterminate), otherwise the null pointer is returned NULL. Use the free () function to release memory blocks when memory is no longer in use. The pointer returned by the function must be properly aligned so that it can be used with any data object.

ReAlloc prototype is extern void *realloc (void *mem_address, unsigned int newsize);

First determine whether the current pointer has enough contiguous space, if there is, expand the address pointed to by Mem_address, and return mem_address, if the space is not enough, first in accordance with the size specified by newsize to allocate space, the original data from beginning to end copied to the newly allocated memory area, Then release the original mem_address memory area (Note: The original pointer is automatically freed, do not need to use free), and return the new allocated memory area of the first address. That is, the address of the memory block is redistributed.

2. Code implementation

/*********************************************//* Use stack to convert binary to eight/10/16 * */* by Jo 17/05/01 * */********************************************/#include <stdio.h> #include <stdlib.h> #include <mat      H.h> #define STACK_INIT_SIZE 20//Stack initialization size #define STACKINCREMENT 10//stack expands each time the size typedef char ELEMTYPE;       Stores binary data in character form typedef struct {elemtype *base;        Stack low pointer elemtype *top;        stack top pointer int stackSize;

stack size}sqstack; function function: Initialize stack (allocate memory space for stack)//Parameter *s: Stack object address void Initstack (Sqstack *s) {s->base = (elemtype*) malloc (stack_init_size * Sizeo
	F (elemtype));
	if (!s->base) exit (0);
	S->top = s->base;
S->stacksize = stack_init_size; }//function: Data into stack//parameter *s: Stack object address//parameter E: Pending stack data void Push (Sqstack *s, Elemtype e) {if (S->top-s->base >= s->s tacksize)//stack current capacity is greater than storage capacity, expansion stack size {s->base = (elemtype*) realloc (S, (stack_init_size + stackincrement) * sizeof (ELEMTYPE)
		);
	if (!s->base) exit (0); } * (S-&Gt;top) = e;
s->top++; }//function: Data stack//parameter *s: Stack object address//Parameter *e: Object address to be stored out stack data void Pop (Sqstack *s, Elemtype *e) {if (s->base = s->top) Retu
	Rn       *e = *--(s->top);
The stack always points to the top of the stack, so the top pointer minus 1 and then the value}//function function: Get stack length//parameter s: Stack data object//(function to modify the arguments, use the pointer->; if only the query, etc.) does not modify the arguments, use the object itself.) int Stacklen (Sqstack s)

{return (s.top-s.base);}

function function: empty stack//parameter *s: Stack object address void Clearstack (Sqstack *s) {s->top = s->base;}
	function function: Destroy stack//parameter *s: Stack object address void Destorystack (Sqstack *s) {int i, Len;
	Len = s->stacksize;
	for (i = 0, i < len; i++) {free (s->base);//Data release from the bottom of the stack s->base++;
	} s->top = S->base = NULL;
s->stacksize = 0;
	}//Function function: 16 conversion//parameter C: The character to be converted to hexadecimal representation Char Tohexchar (char c) {switch (c) {case 10:return ' A ';
	Case 11:return ' B ';
	Case 12:return ' C ';
	Case 13:return ' D ';
	Case 14:return ' E ';
	Case 15:return ' F ';  Default:return c+48;
	The character ' 0-9 ' is converted to the number 0-9}} int main () {//1. binary converted to decimal//Elemtype C;
	Sqstack s; int Len, I, sum= 0;
	Initstack (&s); printf ("Enter binary number to end with #.")
	\ n ");  scanf ("%c", &c);
		Read one character at a time from the keyboard buffer while (c! = ' # ') {Push (&s, C);
	scanf ("%c", &c);  } getchar ();
	Remove ' \ n ' from the Cache len = Stacklen (s);
	printf ("Binary stack current capacity:%d \ n", Len);
		for (i = 0; i < len; i++) {Pop (&s, &c);
	sum = sum + (c-48) *pow (2, I);
	} printf ("Binary converted to decimal:%d \ n", sum);
	*///2. Binary conversion to octal/* elemtype C;
	Sqstack s, soct;

	int Len, lenoct, I, temp = 0;
	Initstack (&s);

	Initstack (&AMP;SOCT); printf ("Enter binary number to end with #.")
	\ n ");  scanf ("%c", &c);
		Read one character at a time from the keyboard buffer while (c! = ' # ') {Push (&s, C);
	scanf ("%c", &c);  } getchar ();
	Remove ' \ n ' from the Cache len = Stacklen (s);

	printf ("Binary stack current capacity:%d \ n", Len);
	int count = 0;
		for (i = 0; i < len; i++) {Pop (&s, &c);
		Temp = temp + (c-48) *pow (2, count);
		count++; if (count = = 3 | |
		(S.top = = s.base))  {Push (&soct, temp);
			Low placed on bottom of stack temp = 0;
		Count = 0;
	}} lenoct = Stacklen (SOCT); printf (the current capacity of the octal stack is:%d \ n ", lenoct);
	printf ("Binary converted to octal:");
		for (i = 0; i < lenoct; i++) {Pop (&soct, &c);
	printf ("%d", c);
	} *///3. Binary conversion to hexadecimal///* elemtype C;
	Sqstack s, ShEx;

	int Len, lenhex, I, temp = 0;
	Initstack (&s);

	Initstack (&shex); printf ("Enter binary number to end with #.")
	\ n ");  scanf ("%c", &c);
		Read one character at a time from the keyboard buffer while (c! = ' # ') {Push (&s, C);
	scanf ("%c", &c);  } getchar ();
	Remove ' \ n ' from the Cache len = Stacklen (s);

	printf ("Binary stack current capacity:%d \ n", Len);
	int count = 0;
		for (i = 0; i < len; i++) {Pop (&s, &c);
		Temp = temp + (c-48) *pow (2, count);
		count++; if (count = = 4 | |   (S.top = = s.base))  Important differences with octal {Push (&shex, temp);
			Low placed on bottom of stack temp = 0;
		Count = 0;
	}} Lenhex = Stacklen (ShEx);

	printf ("Hexadecimal stack current capacity:%d \ n", Lenhex);
	printf ("Binary conversion to 16 binary:");
	Char Tempchar;
		for (i = 0; i < Lenhex; i++) {Pop (&shex, &c);
		Tempchar = Tohexchar (c);
	printf ("%c", Tempchar);
}//*/return 0; }


3. Results



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.