C++ 斐波那契c++;gmp发生器

C++ 斐波那契c++;gmp发生器,c++,fibonacci,gmp,C++,Fibonacci,Gmp,您好,我正在尝试对我编写的现有斐波那契生成器实施gmp。我一直在阅读gmp文件,但仍有很多我不明白的地方。原始斐波那契生成器如下所示: #include <iostream> using namespace std; class Fib { int n; long unsigned int first, second; public: Fib() { first = 0; second = 1; cout << "Enter the

您好,我正在尝试对我编写的现有斐波那契生成器实施gmp。我一直在阅读gmp文件,但仍有很多我不明白的地方。原始斐波那契生成器如下所示:

#include <iostream>
using namespace std;

class Fib {
  int n; 
  long unsigned int first, second;
public:
  Fib() {
    first = 0;
    second = 1;
    cout << "Enter the number of terms of Fibonacci series you want" << endl;
    cin >> n;
    cout << "First " << n << " terms of Fibonacci series are:" << endl;
  }

  int solve() {
    int i; 
    long unsigned int next;
    for(i = 0 ; i < n + 1 ; i++) {
      if(i <= 1) {
        next = i;
      }
      else {
        next = first + second;
        first = second;
        second = next;
      }
    }
    return next;
  }
};

int main() {
  Fib fib;
  cout << fib.solve() << endl;
  return 0; 
}
#包括
使用名称空间std;
类Fib{
int n;
长无符号整数第一,第二;
公众:
Fib(){
第一个=0;
秒=1;
cout n;

无法解决,谢谢Marc Glisse为我指明了正确的方向

我只是删除了return函数,并允许该函数只返回输出

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

class Fib {
  int n; 
public:
  Fib() {
    cout << "Enter the number of terms of Fibonacci series you want" << endl;
    cin >> n;
    cout << "The " << n << "'st Fibonacci number is:" << endl;
  }

  void solve() {
    int i;
    mpz_class first, second, next;
    first = 0;
    second = 1;
    for(i = 0 ; i < n + 1 ; i++) {
      if(i <= 1) {
        next = i;
      }
      else {
        next = first + second;
        first = second;
        second = next;
      }
    }
    cout << next << endl;
  }
};

int main() {
  Fib fib;
  fib.solve();
  return 0; 
}

请使用C++接口。你只需在原始代码中包含和替换<代码>未签名的长< /代码>代码> MPZULASE 。顺便说一下,你真的需要学习如何阅读文档。MPZY-IIT需要一个参数,你给它2是什么意思?几乎得到了。ubuntu@ubuntu:~/projects/c++/fibonacci_cpp$g++-lgmp fib.cpp-o fib fib.cpp:在成员函数'int fib::solve()'中:fib.cpp:30:12:错误:无法转换'mpz_类{aka_ugmp_expr}'转换为'int'作为下一步的返回;^ubuntu@ubuntu:~/projects/c++/fibonacci_cpp$好吧,您返回的不是int而是一个mpz_类,所以请修复函数的签名…我查看了其他人的示例,认为您应该在第二个参数中设置init的值t教程。这可能是我的问题的一部分。抱歉,我看不懂文档。嗯,这是坏的。你的编译器没有告诉你你缺少一个返回语句吗?我将int签名更改为void,因为它只是发送一个输出流。谷歌墙选项现在
fib.cpp: In member function ‘int Fib::solve()’:
fib.cpp:30:12: error: cannot convert ‘mpz_class {aka __gmp_expr<__mpz_struct [1],     __mpz_struct [1]>}’ to ‘int’ in return
     return next;
            ^
#include <iostream>
#include <gmpxx.h>
using namespace std;

class Fib {
  int n; 
public:
  Fib() {
    cout << "Enter the number of terms of Fibonacci series you want" << endl;
    cin >> n;
    cout << "The " << n << "'st Fibonacci number is:" << endl;
  }

  void solve() {
    int i;
    mpz_class first, second, next;
    first = 0;
    second = 1;
    for(i = 0 ; i < n + 1 ; i++) {
      if(i <= 1) {
        next = i;
      }
      else {
        next = first + second;
        first = second;
        second = next;
      }
    }
    cout << next << endl;
  }
};

int main() {
  Fib fib;
  fib.solve();
  return 0; 
}
Enter the number of terms of Fibonacci series you want
3301
First 3301 terms of Fibonacci series are:
    330153163507162264637094778670152653434758914922281728912670042596222213549775330156165336158736310556035302724174567603559968964146698655928480718496410717009709564103992213321320869628734803460669663152332798570186240768164370808688660485835985642189726235311578136722218902035069558368032277843436948382319806290480685283349217035498351102885889468646619750569482644246863804467015344937199892515242806415403581786532923017170033416624774209919795051514102027827396052441847160310846646083321110222356075543424672128051593137886359425865994528848747739182600228659941846983982384323813903695048726976986370288741982958687841091743740983161275336114608885705665822704734020694899622487801
ubuntu@ubuntu:~/projects/c++/fibonacci_cpp$