2

考虑以下代码

// BOGP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "gmp-static\gmp.h"
#include <stdlib.h>         /* For _MAX_PATH definition */
#include <stdio.h>
#include <malloc.h>


#define F(x) mpf_t x; mpf_init( x );

int main(int argc, char* argv[])
{
    F(foo);
    char * buff;
    mp_exp_t exp;

    mpf_init_set_str( foo, "123", 10 );
    buff = mpf_get_str(NULL, &exp, 10, 0, foo);
    puts( buff );
    puts("\n");
    free(buff); 

    mpf_init_set_str( foo, "-123", 10 );
    buff = mpf_get_str(NULL, &exp, 10, 0, foo);
    puts( buff );
    puts("\n");
    free(buff); 

    mpf_init_set_str( foo, "+123", 10 );
    buff = mpf_get_str(NULL, &exp, 10, 0, foo);
    puts( buff );
    puts("\n");
    free( buff );

}

在第一个实例中,在 mpf_get_str 调用之后,buff 包含“123”。在第二个中,buff 包含“-123”。但在第三个中,buff 包含一个空字符串(“”)。

这是使用 GMP 4.2.4。也许我需要再次查看手册,但我认为前导“+”会像前导“-”一样容易处理。

4

1 回答 1

3

据我所知, GMP 手册中的任何地方都没有提到您的问题。但是,您可以直接检查mpf_set_str的代码以查看它不处理“+”。

我不知道你在什么情况下需要这个,但如果你真的需要一个字符来表示正/负,你可以利用这些函数忽略前导空格的事实,从而使用" 123".

于 2009-01-19T08:18:11.080 回答