Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 数字的基本表示-输出顺序相反_C++_Reverse_Base - Fatal编程技术网

C++ 数字的基本表示-输出顺序相反

C++ 数字的基本表示-输出顺序相反,c++,reverse,base,C++,Reverse,Base,我试图在base中表示一个正整数(每个base都是 范围2到16)正常,但我得到的输出与要求的输出相反。例如,“2”的二进制表示应该是10,但我得到的是“01”。这是我的密码 #include<iostream.h> #include<conio.h> #include<math.h> int main(void) { int b, number, i, result; cout << "Enter the the number

我试图在base中表示一个正整数(每个base都是 范围2到16)正常,但我得到的输出与要求的输出相反。例如,“2”的二进制表示应该是10,但我得到的是“01”。这是我的密码

#include<iostream.h>
#include<conio.h>
#include<math.h>

int main(void)
{
    int b, number, i, result;
    cout << "Enter the the number "<< endl;
    cin >> number;

    if (number < 0)
    {
        cout << "Number is not valid, as it must be greater than or equal to 0 \n";
        system("PAUSE");
        return 0;
    }

    cout<<"Enter the base value"<< endl;
    cin >> b; 


    if(b < 2 || b > 16){
        cout << "Base is not valid.\n";
        system("PAUSE");
        return 0;    
    }

    cout << "The representation of the number with base " << b << " is" << endl;  

    while(number > 0)  //number in base 10
    {
        result = number%b; 
        number = number/b;
        i++;                    

        if (result == 10)
        {
            cout << "A";
        }
        if (result == 11)
        {
            cout<<"B";
        }
        else if (result == 12)
        {
            cout<< 'C';
        }
        else if( result == 13)
        {
            cout<<"D";
        }
        else if (result == 14)
        {
            cout << "E";
        }
        else if(result == 15)
        {
            cout << "F";
        }
        else {
            cout << result ;
        }

    }

    getch();
}
#包括
#包括
#包括
内部主(空)
{
int b,数字,i,结果;
库特数;
如果(数字<0)
{

cout您正在反向打印数字:
number%b
从数字中提取最后一个base-b数字,但它是您打印的第一个数字


要按正确的顺序打印数字,您必须以某种方式反转顺序,以便最后一个数字打印在最后。例如,您可以将数字添加到向量或将其写入字符串,然后将其反转。

在特定的基数中输出数字时,这是一个常见问题。通过将除法的模乘以基数你得到最后一个(最不重要的)数字。你应该为每个数字保存一个缓冲区,然后以相反的顺序输出缓冲区的内容。考虑下面的例子:

vector<char> buffer;
while(number > 0)  //number in base 10
{
  result = number%b; 
  number = number/b;
  i++;                    

  if (result >= 10)
    buffer.push_back((char)(result-10)+'A');   // alphabet is contiguous in ascii,
                                               // so you don't need a huge if

  else
    buffer.push_back((char)result+'0');
}
for(i = buffer.size()-1; i >=0; --i)
    cout << buffer[i];
向量缓冲区;
while(number>0)//基数为10的数字
{
结果=编号%b;
编号=编号/b;
i++;
如果(结果>=10)
buffer.push_back((char)(result-10)+'A');//字母表在ascii中是连续的,
//所以你不需要一个巨大的if
其他的
buffer.push_back((char)结果+0');
}
对于(i=buffer.size()-1;i>=0;--i)

cout要按正确的顺序获取数字,您可以“向后”操作。首先找到超过数字的基的最小幂,提取数字,然后继续使用较低的幂

// Find the smallest power
w= 1;
while (w * b <= n)
{
  w*= b;
}

// Extract the digits
while (w > 0)
{
   digit= n / w;
   n-= digit * w;
   w/= b;
}
//找到最小的幂
w=1;
while(w*b0)
{
数字=n/w;
n-=数字*w;
w/=b;
}

这不是很有效,因为每个数字需要两个除法。

结果%base
将始终获取需要打印的最后一个字符。因此,我们需要以某种方式反转输出

递归:
执行打印的递归函数可以反转输出。由于它是递归的,所以它将堆栈用作堆栈。因此,我们可以对输出进行
LIFO
排序。
只需将
while
循环替换为对以下内容的调用:

rec_print(int number, int base)
{
    rec_print(number/base, base);
    int result = number / base;
    if ( result <= 0) {return;}
    result += result<10?'0':'A'-10;
    cout << result;
    return;
}
首先计算出需要多少个数字,设置一个正确大小的向量来存储它,然后将每个数字从头到尾写入。 然后它抓取一个指向数据的指针以输出数据


问题不在于代码中的错误,而在于您进行转换的方式。除法并取剩余数将首先给出最后一位数字,这就是问题所在。无论如何,也许您应该首先清理代码。还有一个问题,如果我们想使用“字符串”,如何编写它?好的,那么我的数字o有一个小问题数字计算…正在进行。该函数运行良好,但我必须指出,数字/base+1与数字的数量并不接近!您要实现数字^(1/base)+1(^是幂号,而不是xor)一个提示:你可能要搜索数字中最重要的“1”位的序号,它可以被目标基中一个数字所覆盖的字节数除以。下面的例子:BASE=16—>每个数字覆盖的4位;数字=1234=0x4D2= 1001100100B-> -MS1B的序数为11,因此缓冲器的大小。11/4+1=3固定。
pow(数字,1/基数)
不正确,
log(数字)/log(基数)
完成了这项工作。空终止不应该是Nessery,因为当您调整向量大小时,内容会得到默认初始化,在
char
的情况下,它是
'\0'
。但是如果有人将其转换为
C
并使用
malloc
分配数组,那么它将是Nessery,所以我将它保留在这里。
// Find the smallest power
w= 1;
while (w * b <= n)
{
  w*= b;
}

// Extract the digits
while (w > 0)
{
   digit= n / w;
   n-= digit * w;
   w/= b;
}
rec_print(int number, int base)
{
    rec_print(number/base, base);
    int result = number / base;
    if ( result <= 0) {return;}
    result += result<10?'0':'A'-10;
    cout << result;
    return;
}
void print_number(int number, int base)
{
    int result;
    int number_of_digits = log(number)/log(base) +1;   //+1 because '/' truncates and we want to always round up.
    vector<char> output(number_of_digits+1); //vector<char> of the right size (+1 for null terminator)
    output[number_of_digits+1] = 0; //null terminate.
    while (number_of_digits--)
    {
        result = number % base;
        result += result<10?'0':'A'-10;
        output[number_of_digits] = result;
        number /= base;
    }
    std::cout << &output[0]<<endl;
}