[Learn Little Bit-Data Structure-stack & queue] One of the stack applications: Numerical Conversion

Source: Internet
Author: User
Tags decimal to binary

/* Description of the Numerical Conversion problem: for the decimal value, it can be converted to another binary or octal value.

* Principle: N = (N Div d) * D + N mod D;

**/

// The conversion of the algorithm is converted from decimal to binary or octal and output. # Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <assert. h> // initial maximum stack capacity # define stack_max_size 100 // stack Capacity Increment # define increament 10 // status function # define error 0 # define OK 1 # define soverflow-1 # define Yes 1 # define no 0 typedef int status, selemtype, elemtype; typedef struct {selemtype * base; selemtype * Top; int stacksize; // currently allocated space, not the number of elements in the stack.} Sqstack; Status initstack (sqstack & S) {// initialize the stack space. The stack is empty. base = (selemtype *) malloc (stack_max_size * sizeof (elemtype); If (! S. base) {exit (soverflow);} s. top = S. base; S. stacksize = stack_max_size; Return OK;} // If the stack is not empty, use e to output the top element of the stack and Return OK. Otherwise, an error is returned. Status gettop (sqstack S, selemtype & E) {If (S. top = S. base) {return error;} e = * (S. top-1); Return OK;}/** push (S, e) medium pressure element e to stack S. Consider the following situations: * 1. the stack is full and space needs to be increased. If you cannot add more space, exit. * 2. the stack is not full. Press the element first, and then modify the stack top pointer. **/Status push (sqstack & S, selemtype e) {If (S. top-s. base)> = S. stacksize) {S. base = (elemtype *) realloc (S. base, (S. stacksize + increament) * sizeof (elemtype); If (! S. base) {exit (soverflow);} s. top = S. base + S. stacksize; S. stacksize + = increament;} // press the element first, and then modify the pointer. * S. Top = E; S. Top ++; Return OK;}/** pops up the top element of the stack. Note: * 1. the stack is not empty. Modify the pointer, then pop up the element and send it to e. * 2. If the stack is empty, an error is returned. */Status POP (sqstack & S, elemtype & E) {If (S. Top = S. Base) {Return Error ;}// first modify the pointer and then the element pops up. S. top --; E = * s. top; Return OK;} status isempty (sqstack s) {If (S. top = S. base) {return yes;} return no;} void conversion (int n, int base) {assert (base = 2 | base = 8); sqstack S; initstack (s); int t = n, Res; while (t) {push (S, T % base); t = T/base;} while (! Isempty (s) {Pop (S, Res); printf ("% d", Res);} printf ("\ n");} Main () {int rands; for (INT I = 0; I <100; I ++) {rands = rand () % 1000; printf ("int: % d --->", rands ); conversion (rands, 8);} system ("pause"); Return 0 ;}

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.