Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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++_C_Algorithm - Fatal编程技术网

C++ 如何反转真正“否”的数字?

C++ 如何反转真正“否”的数字?,c++,c,algorithm,C++,C,Algorithm,我知道用c反转数字表的数字,但我的问题是我们怎样才能反转真正的“否”的数字呢 例如,1200.23与32.0021相反 整数编码 #include <stdio.h> /* Iterative function to reverse digits of num(integer)*/ int reversDigits(int num) { int rev_num = 0; while(num > 0) { rev_num = rev_num*10 + nu

我知道用c反转数字表的数字,但我的问题是我们怎样才能反转真正的“否”的数字呢 例如,1200.23与32.0021相反

整数编码

#include <stdio.h>

/* Iterative function to reverse digits of num(integer)*/
int reversDigits(int num) 
{
  int rev_num = 0;
  while(num > 0)
  {
    rev_num = rev_num*10 + num%10;
    num = num/10;
  }
  return rev_num;
}

/*Driver program to test reversDigits*/
int main()
{
  int num = 4562;
  printf("Reverse of no. is %d", reversDigits(num));

  getchar();
  return 0;
}
试试这个代码-

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

float reversDigits(float num)
{
  char buffer[20],ch,*ptr;
  int i,j,len;
  snprintf(buffer,20,"%.3lf",num);
  len = strlen(buffer);
  for(i=0,j=len-1;i<j;i++,j--)
  {
        ch=buffer[i];
        buffer[i]=buffer[j];
        buffer[j]=ch;
  }
  buffer[len] = '\0';
  return atof(buffer);
}

int main()
{
  float num = 45000.062;
  printf("Reverse of no. is %.5lf\n", reversDigits(num));

  return 0;
}
我传递了一个后面有3位数字的数字,所以我在snprintf中使用了%.3lf,如果您只是简单地使用%lf的意思,在将其转换为字符串时,它将添加零和一些数字。为了避免我使用,否则你需要做更多的工作来删除它


注意:在将字符串转换为float或将float转换为字符串时,在十进制位置将添加一些额外的数字或零。程序员需要照顾它

显然,您的参数必须是浮点类型

您可以将双精度转换为字符串,然后反转字符串,然后再转换回双精度:

#include <string>
#include <sstream>

template<typename T>
std::string toString(T t)
{
    std::stringstream ss;
    ss << t;
    rteurn ss.str();
}

double reversDigits(double num)
{
    std::string s = std::to_string(num);
    std::reverse(s.begin(), s.end());


    double d;
    std::stringstream ss(s);
    ss >> d;
    return d;
}

在使用iostream库的地方,可以使用以下代码:

#include <iostream>
#include <math.h>
using namespace std;

int main() {

    double pass = 0;
    double n;
    double result=0;

    cout << "Please enter a number to reverse" << endl;
    cin >> n;

    while (trunc(n / pow(10.0, pass)) > 0) {

        result = fmod((n / pow(10.0, pass)), 10.0);
        cout << trunc(result);
        pass = pass + 1;

    }

    cout << endl;

    system("pause");

    return 0;

}
这将使用方法12345/10,该方法提供1234.5,然后将其截断为1234。上面的代码还使用了模方法“%”作为模的运算符。例如,如果执行12345%10,程序将显示12345/10的其余部分,在本例中为5

我在循环中使用此方法以获得所需的输出


请注意,我为一个类编写了这个程序,因此如果这不是您想要的,请忽略此帖子。

转换为字符串,反转字符串,必要时转换回浮点。这对我来说很危险。1200.23与1200.230001非常接近,但32.0021与100032.0021相差甚远。你打算用它做什么?我希望不要驾驶飞机。这个标题让它看起来像是真实的现实,其中大多数都有一个无限长的十进制扩展。以这种方式反转它们会使。。麻烦。@托尼克:我认为倒数数字没有实际用途。@普拉莫斯:我不确定你是否应该删除C++标签。我猜OP很高兴能用C或C++来回答问题,因为这是他如何标记问题的。通过删除此标记,您现在已使一个非常好的答案无效,请参见下文。to_stringm的哪个库出现此错误:“to_string”不是由于-Wfatal错误而终止的“std”编译的成员。@AshutoshSoni编写另一个toString函数并不困难,请参阅我的更新。@AshutoshSoni您需要包括。您如何编译它?对于d代码,它正在打印与编号相反的内容。is 260.000549请尝试编辑的代码!否则,请在main的printf中使用%.5lf!这是浮点数的大问题!编号的反面是260.00055,为精确值设计您自己的atof函数!