在c++中编写一个程序实现进制转换。 60
由用户输入一个输入数字的进制,从0~20,然后输入数字,然后输入想转成的进制,得到结果,再输入一个想转成的进制,输入0或1结束。注意,例如十六进制中,abcd表示数字,则...
由用户输入一个输入数字的进制,从0~20,然后输入数字,然后输入想转成的进制,得到结果,再输入一个想转成的进制,输入0或1结束。注意,例如十六进制中,abcd表示数字,则ABCD也能表示同样的数字。
展开
5个回答
展开全部
直接输出x%就是16进制的了,你用MFC做很简单,需要详细程序可以联系我
追问
不是这样的,是从2进制到20进制都能互相转换
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main(int argc, char *argv[])
{
int oriBase,rstBase;
std::string oriNum;
static const char digits[] = "0123456789abcdef";
const char* sd;
char dig;
int x=0;
do{
std::cin>>oriBase;
if(oriBase==0||oriBase==1)
break;
std::cin>>oriNum;
std::cin>>rstBase;
if(rstBase==0||rstBase==1)
break;
//任意进制转十进制
for (const char*sc = oriNum.c_str(); (sd = (char *)memchr(&digits[0],
tolower(*sc), oriBase)) != 0; ++sc)
{
dig = (char)(sd - digits);
x = x * oriBase + dig;
}
//十进制转任意进制
std::vector<char> rst;
while(x>0){
rst.push_back(digits[x%rstBase]);
x/=rstBase;
}
//输出
std::copy(rst.rbegin(),rst.rend(),std::ostreambuf_iterator<char>(std::cout));
std::cout<<std::endl;
}while(1);
return 0;
}
#include <string>
#include <vector>
#include <sstream>
int main(int argc, char *argv[])
{
int oriBase,rstBase;
std::string oriNum;
static const char digits[] = "0123456789abcdef";
const char* sd;
char dig;
int x=0;
do{
std::cin>>oriBase;
if(oriBase==0||oriBase==1)
break;
std::cin>>oriNum;
std::cin>>rstBase;
if(rstBase==0||rstBase==1)
break;
//任意进制转十进制
for (const char*sc = oriNum.c_str(); (sd = (char *)memchr(&digits[0],
tolower(*sc), oriBase)) != 0; ++sc)
{
dig = (char)(sd - digits);
x = x * oriBase + dig;
}
//十进制转任意进制
std::vector<char> rst;
while(x>0){
rst.push_back(digits[x%rstBase]);
x/=rstBase;
}
//输出
std::copy(rst.rbegin(),rst.rend(),std::ostreambuf_iterator<char>(std::cout));
std::cout<<std::endl;
}while(1);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2017-05-21 · 知道合伙人软件行家
关注
展开全部
#include <stdio.h>
#include <string.h>
#include <STDLIB.H>
#include <math.H>
char* DtoAn(double Res, char *Des, int DesType, int Precision)
{
inti_integer;
doubled_decimal;
charc_integer[1000], c_decimal[1000], idx= 1;
strcpy(Des, "");
if(Res < 0)
{
Res= -Res;
strcpy(Des, "-");
}
i_integer= int(Res);
d_decimal = Res- i_integer;
itoa(i_integer, c_integer, DesType);
strupr(c_integer);
strcat(Des, c_integer);
c_decimal[0]= '.';
while(abs(d_decimal) <= 0.000000001 && idx <= Precision)//20表示精度
{
i_integer= int(d_decimal *= DesType);
if(i_integer >= 0 && i_integer <= 9)
c_decimal[idx++]= i_integer + 0x30;
else
c_decimal[idx++]= i_integer + 0x37;
d_decimal-= i_integer;
}
c_decimal[idx]= '\0';
return strcat(Des, c_decimal);
}
double AntoD(double &Des, char *Res, int ResType)
{
inti, n,
DotNum;
charnewRes[2000], SubDecimal[1000], SubInteger[1000];
doubleret= 0;
for(i= 0, n= 0; Res[i]!='\0'; i++)
if(Res[i]!=' ')
{
newRes[n++]= Res[i];
}
newRes[n]= '\0';
strupr(newRes);
if(strstr(newRes, "-")==NULL && strstr(newRes, ".")==NULL)
{
n= strlen(newRes);
for(Des=0, i= 1; i <= n; i++)
if(newRes[i-1] >= '0' && newRes[i-1] <= '9')
Des+= (newRes[i-1]-0x30)*pow(ResType, n-i);
else
Des+= (newRes[i-1]-0x37)*pow(ResType, n-i);
return Des;
}
if(strstr(newRes, "-")==NULL && strstr(newRes, ".")!=NULL)
{
DotNum= 0;
while(newRes[DotNum]!='.') DotNum++;
memcpy(SubInteger, newRes, DotNum);
strcpy(SubDecimal, &newRes[DotNum+1]);
SubInteger[DotNum]= '\0';
n= strlen(SubInteger);
for(Des=0, i= 1; i <= n; i++)
if(newRes[i-1] >= '0' && newRes[i-1] <= '9')
Des+= (SubInteger[i-1]-0x30)*pow(ResType, n-i);
else
Des+= (SubInteger[i-1]-0x37)*pow(ResType, n-i);
n= strlen(SubDecimal);
for(i= 1; i <= n; i++)
if(SubDecimal[i-1] >= '0' && SubDecimal[i-1] <= '9')
Des+= (SubDecimal[i-1]-0x30)*pow(ResType, -i);
else
Des+= (SubDecimal[i-1]-0x37)*pow(ResType, -i);
return Des;
}
if(strstr(newRes, "-")!=NULL && strstr(newRes, ".")==NULL)
{
n= strlen(newRes);
for(Des=0, i= 2; i <= n; i++)
if(newRes[i-1] >= '0' && newRes[i-1] <= '9')
Des-= (newRes[i-1]-0x30)*pow(ResType, n-i);
else
Des-= (newRes[i-1]-0x37)*pow(ResType, n-i);
return Des;
}
DotNum= 1;
while(newRes[DotNum]!='.') DotNum++;
memcpy(SubInteger, newRes, DotNum);
strcpy(SubDecimal, &newRes[DotNum+1]);
SubInteger[DotNum]= '\0';
n= strlen(SubInteger);
for(Des=0, i= 2; i <= n; i++)
if(SubInteger[i-1] >= '0' && SubInteger[i-1] <= '9')
Des-= (SubInteger[i-1]-0x30)*pow(ResType, n-i);
else
Des-= (SubInteger[i-1]-0x37)*pow(ResType, n-i);
n= strlen(SubDecimal);
for(i= 1; i <= n; i++)
if(SubInteger[i-1] >= '0' && SubInteger[i-1] <= '9')
Des-= (SubDecimal[i-1]-0x30)*pow(ResType, -i);
else
Des-= (SubDecimal[i-1]-0x37)*pow(ResType, -i);
return Des;
}
char* AntoAn(char *Des, int DesType, char *Res, int ResType, int Precision)
{
double d;
return DtoAn(AntoD(d, Res, ResType), Des, DesType, Precision);
}
void main()
{
char res[100];
char des[100];
int restype;
int destype;
printf("请输入要转换的数字:");gets(res);
printf("请输入该数的进制数:");scanf("%d", &restype);
printf("请输入转换的进制数:");scanf("%d", &destype);
printf("转换后的数字是%s\n", AntoAn(des, destype, res, restype, 7) );
}
#include <string.h>
#include <STDLIB.H>
#include <math.H>
char* DtoAn(double Res, char *Des, int DesType, int Precision)
{
inti_integer;
doubled_decimal;
charc_integer[1000], c_decimal[1000], idx= 1;
strcpy(Des, "");
if(Res < 0)
{
Res= -Res;
strcpy(Des, "-");
}
i_integer= int(Res);
d_decimal = Res- i_integer;
itoa(i_integer, c_integer, DesType);
strupr(c_integer);
strcat(Des, c_integer);
c_decimal[0]= '.';
while(abs(d_decimal) <= 0.000000001 && idx <= Precision)//20表示精度
{
i_integer= int(d_decimal *= DesType);
if(i_integer >= 0 && i_integer <= 9)
c_decimal[idx++]= i_integer + 0x30;
else
c_decimal[idx++]= i_integer + 0x37;
d_decimal-= i_integer;
}
c_decimal[idx]= '\0';
return strcat(Des, c_decimal);
}
double AntoD(double &Des, char *Res, int ResType)
{
inti, n,
DotNum;
charnewRes[2000], SubDecimal[1000], SubInteger[1000];
doubleret= 0;
for(i= 0, n= 0; Res[i]!='\0'; i++)
if(Res[i]!=' ')
{
newRes[n++]= Res[i];
}
newRes[n]= '\0';
strupr(newRes);
if(strstr(newRes, "-")==NULL && strstr(newRes, ".")==NULL)
{
n= strlen(newRes);
for(Des=0, i= 1; i <= n; i++)
if(newRes[i-1] >= '0' && newRes[i-1] <= '9')
Des+= (newRes[i-1]-0x30)*pow(ResType, n-i);
else
Des+= (newRes[i-1]-0x37)*pow(ResType, n-i);
return Des;
}
if(strstr(newRes, "-")==NULL && strstr(newRes, ".")!=NULL)
{
DotNum= 0;
while(newRes[DotNum]!='.') DotNum++;
memcpy(SubInteger, newRes, DotNum);
strcpy(SubDecimal, &newRes[DotNum+1]);
SubInteger[DotNum]= '\0';
n= strlen(SubInteger);
for(Des=0, i= 1; i <= n; i++)
if(newRes[i-1] >= '0' && newRes[i-1] <= '9')
Des+= (SubInteger[i-1]-0x30)*pow(ResType, n-i);
else
Des+= (SubInteger[i-1]-0x37)*pow(ResType, n-i);
n= strlen(SubDecimal);
for(i= 1; i <= n; i++)
if(SubDecimal[i-1] >= '0' && SubDecimal[i-1] <= '9')
Des+= (SubDecimal[i-1]-0x30)*pow(ResType, -i);
else
Des+= (SubDecimal[i-1]-0x37)*pow(ResType, -i);
return Des;
}
if(strstr(newRes, "-")!=NULL && strstr(newRes, ".")==NULL)
{
n= strlen(newRes);
for(Des=0, i= 2; i <= n; i++)
if(newRes[i-1] >= '0' && newRes[i-1] <= '9')
Des-= (newRes[i-1]-0x30)*pow(ResType, n-i);
else
Des-= (newRes[i-1]-0x37)*pow(ResType, n-i);
return Des;
}
DotNum= 1;
while(newRes[DotNum]!='.') DotNum++;
memcpy(SubInteger, newRes, DotNum);
strcpy(SubDecimal, &newRes[DotNum+1]);
SubInteger[DotNum]= '\0';
n= strlen(SubInteger);
for(Des=0, i= 2; i <= n; i++)
if(SubInteger[i-1] >= '0' && SubInteger[i-1] <= '9')
Des-= (SubInteger[i-1]-0x30)*pow(ResType, n-i);
else
Des-= (SubInteger[i-1]-0x37)*pow(ResType, n-i);
n= strlen(SubDecimal);
for(i= 1; i <= n; i++)
if(SubInteger[i-1] >= '0' && SubInteger[i-1] <= '9')
Des-= (SubDecimal[i-1]-0x30)*pow(ResType, -i);
else
Des-= (SubDecimal[i-1]-0x37)*pow(ResType, -i);
return Des;
}
char* AntoAn(char *Des, int DesType, char *Res, int ResType, int Precision)
{
double d;
return DtoAn(AntoD(d, Res, ResType), Des, DesType, Precision);
}
void main()
{
char res[100];
char des[100];
int restype;
int destype;
printf("请输入要转换的数字:");gets(res);
printf("请输入该数的进制数:");scanf("%d", &restype);
printf("请输入转换的进制数:");scanf("%d", &destype);
printf("转换后的数字是%s\n", AntoAn(des, destype, res, restype, 7) );
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个程序有点麻烦,我有一个挺长的程序,你要不?
更多追问追答
追问
发一下看看~谢谢啦
追答
下班了给你发哈,你留个邮箱。
程序是一个高精度自定义的数字类型。可以实现任意进制转换,任意长度的数字(正负数字,包括小数)的加、减、乘、除、求幂、求平方根等等。功能有点多的哈哈。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询