Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将C/C中的两个大数字相乘++;_C++_Iostream_Bignum - Fatal编程技术网

C++ 将C/C中的两个大数字相乘++;

C++ 将C/C中的两个大数字相乘++;,c++,iostream,bignum,C++,Iostream,Bignum,我正想办法解决这个问题。。。两个大数字,a和b由char[]或char*表示,目标是将它们相乘成第三个指针,char*c: void multiply( const char* a, const char* b ){ int len_a = strlen( a ); int len_b = strlen( b ); int* c = new int[ len_a + len_b]; memset( c, 0, sizeof(int) * ( len_a + len

我正想办法解决这个问题。。。两个大数字,
a
b
char[]
char*
表示,目标是将它们相乘成第三个指针,
char*c

void multiply( const char* a, const char* b ){
    int len_a = strlen( a );
    int len_b = strlen( b );
    int* c = new int[ len_a + len_b];
    memset( c, 0, sizeof(int) * ( len_a + len_b ));

    for( int i = len_a - 1; i >= 0; i-- ){
        for( int j = len_b - 1; j >= 0; j-- ){
            c[ i + j + 1 ] += ( b[ j ] - '0') * ( a[ i ] - '0' );
        }
    }

    for( int i = len_a + len_b; i >= 0; i-- ){
        if( c[ i ] >= 10 ){
            c[ i - 1 ] += c[ i ] / 10;
            c[ i ] %= 10;
        }
    }

    cout << a << " * " << b << " = " << c << endl;
    delete[] c;
}
我得到:

999 * 99999 = 0x100100080
为什么我得到的是内存地址而不是实际的数字?
谢谢

因为
c
是一个int指针,如果传递给cout的stream操作符,它将打印一个内存地址。要获取该值,您需要使用例如
*c
取消对指针的引用。您可能需要编写一个循环来打印整数的整个“字符串”。

因为
c
是一个int指针,而cout的stream操作符将在传递这样一个指针时打印内存地址。要获取该值,您需要使用例如
*c
取消对指针的引用。您可能需要编写一个循环来打印整数的整个“字符串”。

std::ostream
(std::cout的类型是)没有任何专门用于
int*
的重载运算符,因此,它返回到
void*
重载,它只是以实现定义的方式输出指针值

此外,
int*
重载不可能确定指针指向数组,更不可能确定这样的数组将有多少个元素。

std::ostream
(其类型为
std::cout
)没有任何专门用于
int*
的重载运算符,因此,它返回到
void*
重载,它只是以实现定义的方式输出指针值

cout << a << " * " << b << " = ";
    for( int i = 0; i < len_a + len_b; i++ ){
        cout << c[ i ];
    }

    cout << endl;
此外,
int*
重载不可能确定指针指向数组,更不可能确定这样一个数组将有多少个元素。

cout您的逻辑是正确的。
cout << a << " * " << b << " = ";
    for( int i = 0; i < len_a + len_b; i++ ){
        cout << c[ i ];
    }

    cout << endl;
只是一个快速提醒:当您创建一个整数指针并希望将其用作数组时,它指向“数组的第一个元素”,因此当您打印它时,您会看到数组c的第一个元素的地址,在您的示例中是“0x100100080”

要打印存储在c中的数字(字符),需要取消指针的引用,即逐个打印数组中的元素。或者,您也可以将数组转换为数字,然后立即打印。有关后者,请参阅:。 用于逐个打印字符,您可以替换

std::cout<<c; 
std::cout您的逻辑是正确的。
只是一个快速提醒:当您创建一个整数指针并希望将其用作数组时,它指向“数组的第一个元素”,因此当您打印它时,您会看到数组c的第一个元素的地址,在您的示例中是“0x100100080”

要打印存储在c中的数字(字符),需要取消指针的引用,即逐个打印数组中的元素。或者,您也可以将数组转换为数字,然后立即打印。有关后者,请参阅:。 用于逐个打印字符,您可以替换

std::cout<<c; 
std::coutfor(c++14)
我们可以用boost libarry

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
int main()
{
  mp::cpp_int s1("12368123681263817263863821638126328136218362182");
  mp::cpp_int s2("345897937325785470923092923709887329092470423707534025");
  mp::cpp_int S=s1*s2;
  std::cout << S << '\n';
  }
#包括
#包括
名称空间mp=boost::multiprecision;
int main()
{
mp::cpp_int s1(“12368123681263817263863821638136218362182”);
mp:cpp_int s2(“34589793732578547092309887329092470423707534025”);
mp::cpp_int S=s1*s2;
(c++14)的标准::cout
我们可以用boost libarry

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
int main()
{
  mp::cpp_int s1("12368123681263817263863821638126328136218362182");
  mp::cpp_int s2("345897937325785470923092923709887329092470423707534025");
  mp::cpp_int S=s1*s2;
  std::cout << S << '\n';
  }
#包括
#包括
名称空间mp=boost::multiprecision;
int main()
{
mp::cpp_int s1(“12368123681263817263863821638136218362182”);
mp:cpp_int s2(“34589793732578547092309887329092470423707534025”);
mp::cpp_int S=s1*s2;

std::cout
int*c
,当您输出时,它将输出指针。您不是想使
char*c
?噢,lurvly-旧的“我喜欢内存泄漏、双重删除和缓冲区溢出”代码风格。@BobFincheimer:是的,我想把它写成
char*c
。你能告诉我怎么才能把它写成char吗?@DeadMG我在他的代码中看不到这些。@BobFincheimer,如果有人把
cout
的异常掩码的位翻转了怎么办?这种类型的代码没有理由喜欢
new[]
vector
int*c
,当您输出时,它将输出指针。您不是想让
char*c
?哦,Lurvly-旧的“我喜欢内存泄漏、双重删除和缓冲区溢出”代码风格。@BobFincheimer:是的,我想把它写成
char*c
。你能告诉我怎么才能把它写成char吗?@DeadMG我在他的代码中看不到这些。@BobFincheimer,如果有人把
cout
的异常掩码的位翻转了怎么办?这种类型的代码没有理由喜欢
new[]
向量
。我该如何为
字符*
做这个操作呢?我该如何为
字符*
做这个操作呢?但是他使用c就像是
字符*
,我想他是想让c成为
字符*
,而不是
int*
他使用int*,因为它是他积累经验的地方中间乘法结果。如果他使用char*,它会溢出。我猜他是在用
int
为结果留出空间,但也许你是对的。他确实说他想要
char*
结果,但他的函数实际上只是让
c
超出范围并返回
void
,所以很难说。但是he使用c就像是一个
char*
,再看一遍,我想他是想让c是一个
char*
,而不是
int*
,他使用int*是因为它是他积累中间乘法结果的地方。如果他使用char*,它会溢出。我猜他使用
int
是为了给结果留出空间,但也许你说得对。他说他想要一个
char*
结果,但是他的函数