Stack application: Digital Conversion

Source: Internet
Author: User

Number conversion:
Conversion of decimal number N and other decimal numbers is a basic problem in computer computing. There are many solutions. One simple method is based on the following principles.
N = (N Div d) * D + N mod D (where: div is the division operation, MOD is the division operation .)
For example, if the number of decimal digits 1348 is converted to octal: 2504, the calculation process is as follows:
N Div d N mod d
1348 168 4
168 21 0
21 2 5
2 0 2
Assume that you want to compile a program that meets the following requirements: For any non-negative decimal integer input, print the number of octal integers equal to the output, since the above calculation process generates the single digits of the eight-digit number in sequence from the low position to the high position, and the print output should generally be carried out from the high position to the low position, which is exactly the opposite of the calculation process. therefore, if you add the Octal numbers obtained in the calculation process to the stack, print the output in the output stack sequence as the Octal numbers corresponding to the input.

The algorithm is described as follows:

void conversion(){InitStack(s);scanf(" %d",N); while(N){ push(s,n%8) N=N/8;}while(!StackEmpty(s)){Pop(S,e);printf("%d",e);}}


This is the simplest example of using the advanced and later features of the stack. in this example, the sequence of stack operations is straight-line, that is, stack first, and then stack blindly. some people may ask: Isn't it easy to implement directly with arrays? By carefully analyzing the above algorithms, it is not difficult to see that the introduction of stacks simplifies programming and divides different levels of attention, narrowing down the scope of thinking. array not only masks the essence of the problem, but also disperses the effort to consider details such as the increase and decrease of array subscripts.
Complete code implementation:

# Include <iostream> # include <stack> using namespace STD; void change (int n, int d) {stack <int> S; int temp; int I = 0; if (n <D) {cout <n <Endl;} else {While (n) {temp = n % d; S. push (temp); n = N/d;} while (! S. empty () {temp = S. top (); cout <temp; S. pop () ;}cout <Endl ;}} void main () {int X, Y; cout <"Enter the number you want to convert" <Endl; cin> X; cout <"Enter the hexadecimal format you want to convert" <Endl; CIN> Y; change (x, y );}

Recursively convert decimal to octal:

/*  Name: conversion.cpp  Copyright: www.52coder.net      Author: HeHe.wang  Date: 16-06-12 13:03  Description: */ #include <iostream>using namespace std; void conversion(int N,int d){      int temp=N%d;      N=N/d;      if(N==0)      cout<<temp;      else      {       conversion(N,d);        cout<<N%d;      } }int main(){    conversion(7,2);    system("pause");}

From: I love programmers
Http://www.52coder.net/archives/1039.html copyright. This site in addition to the source, are the author of the original articles, can be freely cited, but please indicate the source.

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.