64,648
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int main() {
int d=12345;
char s[30];
printf("%d(10)==%s(7)\n",d,_itoa(d,s,7));
return 0;
}
#include <iostream>
using namespace std;
#define NUM 7
void Change(int num)
{
int outValue=-1;
if( ! num)
{
return;
}
else
{
int next=num/NUM;
outValue=num%NUM;
Change(next);
}
cout<<outValue;
}
int main(int argc , char *argv[])
{
Change(11);
cout<<endl;
return 0;
}
#include "stdafx.h"
#include<time.h>
#include<stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int decimal[3] = {12, 99, 34};
bool tenToseven(int IntSrc, char* pDst, int size)
{
int tmp = size -1;
int i = 0;
if (IntSrc > 0)
{
while (IntSrc != 0)
{
if (tmp<0)
{
return false;
}
pDst[tmp--] = (IntSrc%7)+'0';
IntSrc /=7;
}
}
else
{
return false;
}
// reverse the sequence
for (i=tmp+1; i<size; ++i)
{
pDst[i-tmp-1] = pDst[i];
}
return true;
}
int main()
{
int jj;
for (jj=0; jj<3; ++jj)
{
char* pCh = new char[10];
memset(pCh, 0, 10);
tenToseven(decimal[jj], pCh, 10);
printf("%s\t", pCh);
delete pCh;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[] = {12, 56, 19, 16, 56};
char szOutput[100];
int size=sizeof(array)/sizeof(int);
for ( int i=0; i<size; ++i )
{
itoa(array[i], szOutput, 7); //转7进制
puts(szOutput);
}
return 0;
}
/******************* from进制的大数转换为to进制 ************************/
#include <cstring>
#include <string>
string rev(string &s) /*字符串逆序*/
{
int i;
string temp="";
for(i=s.length()-1;i>=0;i--)
temp+=s.at(i);
return temp;
}
int getNum(char str)
{
int num;
if(isdigit(str))
num=str-'0';
else if(str>='A'&&str<='Z')
num=str-'A'+10;
else
num=str-'a'+36;
return num;
}
char getChar(int num)
{
char index[62]={
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
};
return index[num];
}
void Divide(string ÷nd,int to,string "ient,int *remainder,int from)/*分别表示被除数,除数,商、余数及被除数的进制*/
{
int i;
int flag=0; /*用来判断最后的商是否全为0*/
int curDividend=0; /*当前的被除数*/
quotient="";
*remainder=0;
for(i=0;i<dividend.length();i++)
{
curDividend=(*remainder)*from+getNum(dividend.at(i));
if(curDividend<to)
{
quotient+="0";
*remainder=curDividend;
}
else
{
quotient+=getChar(curDividend/to);
*remainder=curDividend%to;
}
}
/*对商进行处理,去除前导0*/
for(i=0;i<quotient.length();i++)
{
if(quotient.at(i)!='0')
{
flag=1;
quotient=quotient.erase(0,i); /*删除从0开始的i个字符,返回新的字符串*/
break;
}
}
if(flag==0) /*商全为0*/
quotient="0";
}
void Change(string &a,string &b,int from,int to) /*分别表示原进制大数、转换结果、原进制及目标进制,仅限在62进制以内*/
{
int remainder; /*余数*/
string quotient; /*商*/
b=""; /*初始化*/
Divide(a,to,quotient,&remainder,from);
while(quotient.compare("0")!=0)
{
b+=getChar(remainder);
a=quotient;
Divide(a,to,quotient,&remainder,from);
}
b+=getChar(remainder);
b=rev(b); /*逆序*/
}
/******************* from进制的大数转换为to进制 ************************/