将一个十进制数转换为另一种进制

3

我尝试了以下代码将一个十进制数转换为其他进制。如果目标进制中没有零(0),则代码可以正常工作。比如79和3,输出的结果是正确的:2221。 但当我将19和3进行转换时,结果应该是201而不是21,这表明出现了错误。

int x, y, a = 0, i, t, j;
cout << "enter two numbers" << endl;
cin >> x >> y; // x as the number in base-10 and x, as the destination base
a = x;
while (x >= y)
{
    t = 1;
    for (i = 0; x > y; i++)
    {
        x /= y;
    }
    cout << x;
    for (j = 0; j < i; j++)
    {
        t *= y;
    }
    a = a - (t*x);
    x = a;
}
cout << x<<endl;

尝试使用3 3,你会看到另一个错误。 - genisage
4个回答

3

使用递归函数比使用while循环更容易实现您想要的目标。

以下是可行的程序。

#include <iostream>

void printInBase(int x, int y)
{
   if ( x < y )
   {
      std::cout << x;
      return;
   }

   int rem = x%y;
   printInBase(x/y, y);
   std::cout << rem;
}

int main()
{
   int x, y;
   std::cout << "enter two numbers" << std::endl;
   std::cin >> x >> y; // x as the number in base-10 and x, as the destination base
   printInBase(x, y);
   std::cout << '\n';
}

1
int x, y, a = 0, i, t, j;
cout << "enter two numbers" << endl;
cin >> x >> y; // x as the number in base-10 and x, as the destination base
a = x;

t = 1;
while (x >= t*y)
{
    t = t * y;
}

while (t)
{
    cout << x/t << ' ';
    x -= t*(x/t);
    t /= y;
}

cout << '\n';

基本上,您没有跟踪要打印的数字,而且您的代码无法确定何时需要前导零。您可以通过打印类似于2*(3^2) + 1*(3^0)的内容或像我在上面的代码中所做的那样预先计算需要的位数来解决这个问题。

0

我的答案。

也适用于浮点数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void radix(char *strNum, int base);

int main ()
{
    radix("215.75", 8);
    return 0;
}

void swap(char *a, char *b)
{
    char temp = *a;
    *a = *b;
    *b = temp;
}

void radix(char *strNum, int base)
{
    char res[100]={0}, tmp[100]={0}, floPoint[100]={0}, *p = 0, *q = 0;
    int inum = 0, digitAfterDot = 3;
    float fnum = 0.0;
    if(strchr(strNum, '.'))
    {
        p = strNum + strcspn(strNum, ".");
        fnum = atof(p);
    } 
    inum = atoi(strNum);
    for(p = res; inum; inum /= base, p++)
        *p = (inum % base) + '0';
    *p = 0;
    if(fnum != 0.0)
    {
        p = floPoint;
        *p++ = '.';
        for(fnum *= base; digitAfterDot--; p++, fnum *= base)
        {
            sprintf(tmp, "%f", fnum);
            inum = atoi(tmp);
            fnum -= inum;
            *p = inum + '0';
        }
        *p = 0;
    }
    for(p = res, q = res+strlen(res)-1; p < q; p++, q--)
        swap(p, q);
    strcat(res, floPoint);
    puts(res);
}

我一直在尝试通过将“base”值更改为double来使上述代码工作。我不得不使用fmod()函数,像这样“*p = fmod(inum, base) + '0';”,这导致答案错误。有没有人对fmod函数有经验,可以解释这种异常情况? - 99Boboster99

0

虽然它可能有效,但这种方法在概念上存在一些问题:您实际上是将数字(算术运算的主题)与它们的文本表示(用于表示它们的数字序列)混淆。

int 类型-从外部角度看-没有“基数”(int 可能具有基于2的内部表示形式,这对于被算术单元电路处理的目的是有用的):它只是一个要添加,减去,乘以,除以和-ed,xor-ed等的东西。

当您执行 cout << a 时,<< 所做的是将 a 数字转换为可读的数字序列。默认情况下,它会以10进制数字表示为 ASCII 字符('0'...'9')来执行此操作。

您正在将一个数字转换为另一个数字,其十进制表示类似于您正在映射的基数。它可以在打印中工作,但没有算术可以使用它。因此,int 不是它们的正确表示。

你需要的是一个不同的文本转换器:它接受一个int和另一个指定基数的整数,并输出表示您数字的字符。
想象一下像这样的类。
class based
{
   int n, base;
public:
   based(int num, int base) :n(num), base(base) {}

   friend std::ostream& operator<<(std::ostream& out, const based& x);
};

用作

std::cout << bsed(79,3) << ' ' << based(19,3) << std::endl;

现在

std::ostream& operator<<(std::ostream& out, const based& x)
{
    static const size_t N = 8*sizeof(int)+2; //base 2 is the widest, and is 32 or 64 + 2
    char buff[N]; //keep space for sign + base2 + terminator
    size_t X = N-1; //current writing character position
    buff[X] = 0; //terminating char
    --X; //prepare next left character
    int n = x.n; //we will work on n
    bool neg = (n<0); //keep negative sign
    if(neg) n=-n; //and work always with posiotives
    while(n) //we will reduce n down to 0
    {
        int digit = n%x.base; //mod is the last digit
        n /= x.base; //next to the left
        buff[X] = (digit<10)? char(digit+'0'): char(digit-10+'A');
        --X; //next char 
    }
    if(neg) buff[X] = '-'; 
    else ++X; //no sign
    out << buff+X << '(' << x.base << ')'; //the text from the point we reach towards left
    return out; 
}

这将输出2221(3) 201(3)

还有一种更便携的方法可以执行char(digit+'0')等操作,但考虑到普通数字,这并不是所需的更多内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接