大整数乘法运算

cpp 复制代码
//
// Created by Administrator on 2026/3/28.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 1000  // 大整数支持的最大位数

// 大整数结构体定义(与教材完全一致)
typedef struct {
    int digits[MAXSIZE]; // 低位到高位依次存储,digits[0] 是个位
    int length;          // 有效数字位数
    int sign;            // 符号:1 为正,-1 为负
} BigInt;

// ------------------- 1. 字符串转大整数 -------------------
BigInt StrToBigInt(const char *s) {
    BigInt num;
    num.length = 0;
    num.sign = 1;

    int len = strlen(s);
    int i = 0;
    if (s[0] == '-') {
        num.sign = -1;
        i = 1;
    }

    // 从后往前读,低位存到数组前面
    for (int j = len - 1; j >= i; j--) {
        num.digits[num.length++] = s[j] - '0';
    }

    return num;
}

// ------------------- 2. 打印大整数 -------------------
void PrintBigInt(BigInt num) {
    if (num.length == 0) {
        printf("0\n");
        return;
    }

    if (num.sign == -1) {
        printf("-");
    }

    // 从高位到低位打印
    for (int i = num.length - 1; i >= 0; i--) {
        printf("%d", num.digits[i]);
    }
    printf("\n");
}

// ------------------- 3. 大整数乘法(教材伪代码实现) -------------------
BigInt BigIntMultiply(BigInt a, BigInt b) {
    BigInt c;

    // 1. 处理结果为0的特殊情况
    if (a.length == 0 || b.length == 0) {
        c.sign = 1;
        c.length = 0;
        return c;
    }

    // 2. 判断结果的符号位
    if (a.sign == b.sign) {
        c.sign = 1;
    } else {
        c.sign = -1;
    }

    // 3. 确定结果的位数,并初始化
    c.length = a.length + b.length - 1;
    for (int i = 0; i < c.length; i++) {
        c.digits[i] = 0;
    }

    // 4. 按位相乘并累加
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < b.length; j++) {
            c.digits[i + j] += a.digits[i] * b.digits[j];
        }
    }

    // 5. 从低位到高位处理进位
    int carry = 0;
    for (int i = 0; i < c.length; i++) {
        int temp = c.digits[i] + carry;
        c.digits[i] = temp % 10;
        carry = temp / 10;
    }

    // 6. 处理最高位进位
    if (carry > 0) {
        c.digits[c.length++] = carry;
    }

    // 7. 消除高位前导0
    while (c.length > 0 && c.digits[c.length - 1] == 0) {
        c.length--;
    }

    return c;
}

// ------------------- 测试主函数 -------------------
int main() {
    // 测试用例1:正数相乘 123 × 456 = 56088
    BigInt a = StrToBigInt("123");
    BigInt b = StrToBigInt("456");
    printf("a = "); PrintBigInt(a);
    printf("b = "); PrintBigInt(b);
    BigInt product = BigIntMultiply(a, b);
    printf("a × b = "); PrintBigInt(product);

    // 测试用例2:带负数相乘 -123 × 456 = -56088
    BigInt c = StrToBigInt("-123");
    printf("\nc = "); PrintBigInt(c);
    printf("b = "); PrintBigInt(b);
    BigInt product2 = BigIntMultiply(c, b);
    printf("c × b = "); PrintBigInt(product2);

    // 测试用例3:乘以0 999 × 0 = 0
    BigInt d = StrToBigInt("999");
    BigInt zero = StrToBigInt("0");
    printf("\nd = "); PrintBigInt(d);
    printf("zero = "); PrintBigInt(zero);
    BigInt product3 = BigIntMultiply(d, zero);
    printf("d × zero = "); PrintBigInt(product3);

    return 0;
}

两数相乘,其结果的位数至少是两数位数之和-1.

乘法思想:个位乘个位算个位,个位累加。

个位乘百位算百位,百位乘个位算百位,十位乘十位算百位。百位累加。

相关推荐
半夢半醒120 分钟前
[原创]《C#高级GDI+实战:从零开发一个流程图》第章:增加菱形、平行四边形、圆角矩形,文本居中显示
算法·c#·流程图
Jerry28 分钟前
LeetCode 90. 子集 II
算法
机器学习之心44 分钟前
CCD实验设计+SVM代理建模+改进NSGA-II工艺参数多目标优化,MATLAB代码
算法·支持向量机·matlab·多目标优化
SOLECA_1 小时前
Ascend C 算子实战(二)|SigmoidCustom 逐元素激活算子完整开发指南
算法
Jerry1 小时前
LeetCode 78. 子集
算法
DuHz2 小时前
论文解读:用于数据分析的极值点对称模态分解方法 (Extreme-point Symmetric Mode Decomposition,ESMD)
论文阅读·算法·信息与通信·信号处理
HZZD_HZZD2 小时前
用电负荷聚类分析实战:基于`scikit-learn`的`K-Means`与`DBSCAN`双算法对比、`PCA`降维可视化与智能电表负荷画像全流程
算法·scikit-learn·kmeans
MIngYaaa5202 小时前
2026暑期牛客多校1 2026-7-17
数据结构·算法
wabs6662 小时前
关于图论【卡码网107.寻找存在的路线的思考】
数据结构·算法·图论
半兽先生2 小时前
大模型技术开发与应用——4.大模型提示词工程实战
人工智能·算法