模糊转换 - C ++

时间:2013-02-12 19:09:14

标签: c++ numbers type-conversion

我正在尝试使用C ++和Xcode编写一个函数来编译,它将测试a是否是回文。当参数是“C ++诞生”类型(例如int,long,double等)时,代码很有效,但我想将函数用于更大的值。所以我使用了BigInteger类型的参数。但是编译器在行上给出了错误

 BigInteger q = x - floor(x.toLong()/10)*10

Conversion from 'double' to 'const BigInteger' is ambiguous。这是完整的代码:

#include <iostream>
#include "BigInteger.hh"

using namespace std;
bool isPalindrom(BigInteger x){
long ch = ceil(log10(x.toUnsignedLong())), n[ch];
//    cout << floor(log10(x)) + 1 << endl;
for (int i = 0; i <= ch; i++){
    BigInteger q = x - floor(x.toLong()/10)*10;
    n[i] = q.toInt();
    //        cout << n[i] << endl;
    x /= 10;
}

for (long i = 0; i <= ceil(ch); i++){
    if (n[i] != n[ch - i]){
        return false;
    }
}
return true;
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

也许

  BigInteger q (static_cast<long>(x - floor(x.toLong()/10)*10));

可能会让编译器更快乐。查看BigInteger.hh内部的公共构造函数。请注意,floor提供了double,因此减法也提供了doubleBigInteger没有构造函数。

答案 1 :(得分:0)

如果您要一直转换为长片,使用BigInteger毫无意义。

您可以仅使用BigInteger次操作来编写该内容,其方式与使用原始整数完全相同:

bool isPalindrome(BigInteger x){
   std::vector<int> digits;
   while (x > 0)
   {
      digits.push_back((x % 10).toInt());
      x /= 10;
   }

   size_t sz = digits.size();
   for (size_t i = 0; i < sz; i++){
      if (digits[i] != digits[sz - i - 1]){
         return false;
      }
   }
   return true;
}