#include <stdio.h>
#include <stdlib.h>
int getIntLength(int i);
void getIntString(int i,char *p);
int getDigitsMi(int digist,int mi);
int getDigitsChu(int digist,int chu,int mi);
char getCharByInt(int digist);
int main()
{
int a=56780077;//长度,个数
char str2[10];
getIntString(a,str2);
printf("str2:%s\n",str2);
}
/**
*得到整型数组的长度
**/
int getIntLength(int i)
{
int count=1;
while (i>10) {
i=i/10;
count++;
}
return count;
}
/**
将整型数字转换成对应的字符串
*/
void getIntString(int digit,char *p)
{
int length=getIntLength(digit);
char str[length+1];
int result_yu;
int result_wei;
for (int i=0; i<length; i++) {
//str[i]=getDigitsMi(digit,10,i );
//result=result+
result_yu=digit%getDigitsMi(10,i);
result_wei=result_yu/getDigitsMi(10,i-1);
str[length-i-1]=getCharByInt(result_wei);
// *p(i)=getCharByInt(result_wei);
// printf("%d %d %d\n",i,result_yu,result_wei);
}
str[length]='\0';
if (p)
strcpy(p, str);
// printf("length:%d\n",length);
}
/**
* 求mi 运算
*/
int getDigitsMi(int digist,int mi)
{
if (mi==-1) {
return 1;
}
if(mi==0){
return digist;
}
int result=digist;
while (mi-->0) {
result=result*digist;
}
return result;
}
/**
* 求chu 运算
*/
int getDigitsChu(int digist,int chu,int mi)
{
if (chu==-1) {
return 1;
}
if(mi==0){
return digist;
}
int result=digist;
while (mi-->0) {
result=result/chu;
}
return result;
}
char getCharByInt(int digist)
{
char result;
switch (digist)
{
case 0:
result='0';
break;
case 1:
result='1';
break;
case 2:
result='2';
break;
case 3:
result='3';
break;
case 4:
result='4';
break;
case 5:
result='5';
break;
case 6:
result='6';
break;
case 7:
result='7';
break;
case 8:
result='8';
break;
case 9:
result='9';
break;
default:
result='0';
break;
}
return result;
}
|
|