1

你好,对不起我的基本英语。我正在尝试从 mpf_class 转换为字符串。我知道有一个函数(get_str()),但它只显示数字和它的指数分开。我想在一个字符串中获取整个表达式。我尝试使用 ostreamstring 并且它可以工作,但我想知道是否有另一种方法可以做到这一点。如果我说清楚了,请告诉我。

基本上我所做的是:

std::ostringstream show;
mpf_class result, Afact,Bfact,Cfact;
result=Afact*Bfact/Cfact;
show << result;
ui->lineEdit_5->setText(QString::fromStdString(show.str()));

正如你所看到的,我在一个 QT 项目中工作,我需要在 QLineEdit 中显示结果,并且使用 ostreamstring 它可以工作。我只是想知道是否有一个 gmp 函数可以做到这一点。谢谢

4

1 回答 1

0

不确定这是否可以帮助您,但您实际上可以打印一个mpf_class对象并在其上使用 I/O 操纵器作为典型float对象。

这是我的代码

#include <gmpxx.h>
#include <iostream>
#include <iomanip>

int main(void) {
    mpf_class a;
    a = 3141592653589793.2;

    std::cout << a << std::endl;
    // Outputs 3.14159e+15

    std::cout << std::uppercase << std::showpos << std::setprecision(3) << a << std::endl;
    // Outputs +3.14E+15

}

然后你可以使用一个std::ostringstream对象而不是std::cout.

参考:https ://gmplib.org/manual/C_002b_002b-Formatted-Output.html

于 2019-02-28T21:41:19.637 回答