十进制转化为七进制问题

亚旺 2011-05-21 11:07:34
请问怎样把一个数组中的数转化为七进制呢,数组里面的数都是十进制的,希望高手能够给一些代码参考
...全文
950 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wesweeky 2011-05-23
  • 打赏
  • 举报
回复
5# 牛逼
xmu_才盛 2011-05-23
  • 打赏
  • 举报
回复
楼上的思路是对的, 但是 lz要得是转换, 不是打印。。

参考8楼的方法,
i = 0; char tmp[N];
1. 若A > 0 , A模7, 余数为a, 存入tmp[i]
2. A /= 7, 重复1, i++;

然后 tmp 中数据 反过来 就是7进制
toadzw 2011-05-23
  • 打赏
  • 举报
回复
友情帮UP
vincentcoll 2011-05-23
  • 打赏
  • 举报
回复
选转化为二进制,再转化为十进制嘛...这样无论计算还是编程都可能简单些
赵4老师 2011-05-23
  • 打赏
  • 举报
回复
#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;
}
Net_Flasher 2011-05-23
  • 打赏
  • 举报
回复

#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;
}
  • 打赏
  • 举报
回复
比如你 七进制的类 叫做 A;
那么你可以转换 通过 A a;
a.converse(char *str, int n);将对整数n进行七进制转换的每一位放在你的 str里面
亚旺 2011-05-21
  • 打赏
  • 举报
回复
但是怎样放到另外一个数组中呢,可以给些代码吗
  • 打赏
  • 举报
回复
跟二进制,十六进制,八进制转换原理一样,请参考 网络
giant1st 2011-05-21
  • 打赏
  • 举报
回复
VC 2005中测试结果是好的。

#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;
}
KID_coder 2011-05-21
  • 打赏
  • 举报
回复

#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;
}
LucEaspe 2011-05-21
  • 打赏
  • 举报
回复
/******************* 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进制 ************************/
qq120848369 2011-05-21
  • 打赏
  • 举报
回复
7进制对应0...6就是了.

%7得1位,/7,再循环一直到0.

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧