#include #include

class math_tool { public: static void getvalue(const char *first, const char *second); };

void math_tool::getvalue(const char *first, const char *second) { int length1 = strlen(first); int length2 = strlen(second);

// 用于存储结果的数组,最大长度为 length1 + length2
int result[length1 + length2 + 1] = {0};

for (int i = 0; i < length1; i++)
{
    for (int j = 0; j < length2; j++)
    {
        result[i + j + 1] += (first[i] - '0') * (second[j] - '0');
    }
}

for (int i = length1 + length2; i >= 0; i--)
{
    if (result[i] >= 10)
    {
        result[i - 1] += result[i] / 10;
        result[i] %= 10;
    }
}

// 打印结果
for (int i = 0; i < length1 + length2; i++)
{
    std::cout << result[i];
}
std::cout << std::endl;

}

int main() { math_tool::getvalue('11', '11');

return 0;

}

这段代码的结果是打印出 '121'。

C++ 大数乘法实现:使用字符串进行计算

原文地址: https://www.cveoy.top/t/topic/o9cw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录