我正在尝试写一个程序,允许pi被萌发到300位数,但我似乎不知道如何在300位数的时候切断它。
到目前为止,代码已经永远运行了,而且我尝试过的任何其他方法都没有像在specfiec时中断一样工作,但是这并不是我所需要的。
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
class Gospers
{
cpp_int q, r, t, i, n;
public:
// use Gibbons spigot algorith based on the Gospers series
Gospers() : q{1}, r{0}, t{1}, i{1}
{
++*this; // move to the first digit
}
// the ++ prefix operator will move to the next digit
Gospers& operator++()
{
n = (q*(27*i-12)+5*r) / (5*t);
while(n != (q*(675*i-216)+125*r)/(125*t))
{
r = 3*(3*i+1)*(3*i+2)*((5*i-2)*q+r);
q = i*(2*i-1)*q;
t = 3*(3*i+1)*(3*i+2)*t;
i++;
n = (q*(27*i-12)+5*r) / (5*t);
}
q = 10*q;
r = 10*r-10*n*t;
return *this;
}
// the dereference operator will give the current digit
int operator*()
{
return (int)n;
}
};
int main()
{
Gospers g;
std::cout << *g << "."; // print the first digit and the decimal point
for(300;) // run forever
{
std::cout << *++g; // increment to the next digit and print
}
}发布于 2022-11-25 17:40:25
您正在声明您生成了300位数字,但是这个for-循环中断了:
for(300;)它不是有效的C++代码,因为换环的结构如下:
for ( declaration ; expression ; increment)虽然所有三个段都是可选的,但是有效的语法至少需要两个分号(;)。
要实现重复序列300次的for循环,您需要一个for循环,如下所示:
for (int i = 0; i < 300; ++i)https://stackoverflow.com/questions/74576092
复制相似问题