3つのアルゴリズムは、整数の各数字を取得する。


// getnumber.cpp : Defines the entry point for the console application.
//
// Author:shizhixin  
// Email:[email protected]  
// Blog:http://blog.csdn.net/shizhixin  
// Date:2012-05-06
/*
      ,             ,
        8 ,                      ,
    1131,  1 1 3 1
              ,      :
                 ,    
                 ,            
             
              ,          ,
           ,                。
                ,    !*/


#include "stdafx.h"
#include <math.h>
const int MAX_NUM_BIT = 5;

//      
bool assert_para(int n)
{
	if (n<=0 || n>pow(10,MAX_NUM_BIT))
	{
		printf("input number must be > 0 and < pow(10,MAX_NUM_BIT)!");
		return false;
	}
	return true;
}

/*       10        
              ,           ,     
    ,        
            ,       ,
         0   */
void func_getnumber1(int n)
{
    if (!assert_para(n))
    {
		return;
    }
	bool start = false;
	int m,k;
	for (int i=MAX_NUM_BIT; i>=0; i--)
	{
		m=n%int(pow(10,i));
		if (m!=n)
		{
			start = true;
			k = n/int(pow(10,i));
			printf("%d ",k);
			n=m;
		}
		else
		{
			if (start)
			{
				printf("0 ");
			}
		}
	}

}

/*                      ,
             0  */
void func_getnumber2(int n)
{
    if (!assert_para(n))
    {
		return;
    }
	int m,k[MAX_NUM_BIT];
	for(int i=1;i<=MAX_NUM_BIT;i++)
	{
		m = n%int(pow(10,i));
		k[i-1] = m/int(pow(10,i-1));
	}

	bool flag = false;
	for (i=MAX_NUM_BIT; i>0; i--)
	{
		if (k[i-1]!=0)
		{
			flag = true;
		}
		if (flag)
		{
			printf("%d ",k[i-1]);
		}
		
	}
}

/*                  ,      ,    ,
    ,           ,           */
void func_getnumber3(int n)
{
    if (!assert_para(n))
    {
		return;
    }
	int m;
	bool start = false;
	for (int i=MAX_NUM_BIT; i>=0; i--)
	{
		m = n/int(pow(10,i));
		if (m!=0 || start)
		{
			start = true;
			printf("%d ",m);
			n = n%int(pow(10,i));
		}
	}
}


int main(int argc, char* argv[])
{
	int num = 0;
	printf("use the func_getnumber1:  ");
	func_getnumber1(num);
	printf("
"); printf("use the func_getnumber2: "); func_getnumber2(num); printf("
"); printf("use the func_getnumber3: "); func_getnumber3(num); printf("
"); return 0; }