iOS 大数乘法运算

//大数乘法运算
+(NSString *)multiplyActionString:(NSString *)str1 andString:(NSString *)str2{

NSMutableArray *muArray = [[NSMutableArray alloc] init];

NSMutableString *string1 = [[NSMutableString alloc] initWithString:str1];
NSMutableString *string2 = [[NSMutableString alloc] initWithString:str2];

//第一步 取出两个乘数值后面相连的0 最后运算结果在补0
NSInteger i = 0;
while (i < str1.length) {
    
    NSString *number = [str1 substringWithRange:NSMakeRange(str1.length -1 -i, 1)];
    
    if (![number isEqualToString:@"0"]) {
        //不为0 跳出循环
        break;
    }
    
    //把取出的0添加到数组
    [muArray addObject:number];
    string1 = [str1 substringToIndex:str1.length -1-i].mutableCopy;
    
    i++;
}


NSInteger j = 0;
while (j < str2.length) {
    
    NSString *number = [str2 substringWithRange:NSMakeRange(str2.length -1 -j, 1)];
    
    if (![number isEqualToString:@"0"]) {
        //不为0则跳出循环
        break;
    }
    
    //把取出的0添加到数组
    [muArray addObject:number];
    
    string2 = [str2 substringToIndex:str2.length -1 -j].mutableCopy;
    
    j++;
}

//开始计算
NSMutableString *muStr2 = [[NSMutableString alloc] init];

NSMutableArray *muArray1 = [[NSMutableArray alloc] init];
NSInteger k = 0;

while (k < string1.length) {
    
    NSString *str1 = [string1 substringWithRange:NSMakeRange(string1.length - 1 -k, 1)];

    NSInteger value = 0;//进位数值
    NSInteger h = 0;
    while (h < string2.length) {
        
        NSString *str2 = [string2 substringWithRange:NSMakeRange(string2.length -1 -h, 1)];
        
        NSInteger sum = [str1 integerValue] * [str2 integerValue] +value;
        
        NSString *s;
        if (sum >= 10) {
            //取出个位数
            s = [[NSString stringWithFormat:@"%ld",sum] substringFromIndex:1];
            value = [[[NSString stringWithFormat:@"%ld",sum] substringToIndex:1] integerValue];
        }else{
            s = [NSString stringWithFormat:@"%ld",sum];
            value = 0;
        }
        
        if (h == string2.length -1) {
            //最后一次运算时 判断是否有进位值
            if (value > 0) {
                
                s = [NSString stringWithFormat:@"%ld%@",(long)value,s];
            }
        }
        [muStr2 insertString:s atIndex:0];
        h++;
    }
    
    if (k > 0) {
        //补0
        NSInteger m = 0;
        while (m < k-1) {
            
            [muStr2 insertString:@"0" atIndex:muStr2.length];
            
            m++;
        }
    }
    //把每次运算的结果添加到数组
    [muArray1 addObject:muStr2];
    muStr2 = @"0".mutableCopy;
    k++;
}

//对数组里面的值进行大数相加运算
NSMutableString *number = [[NSMutableString alloc] initWithString:@"0"];
NSInteger n = 0;
while (n < muArray1.count) {
    //调用大数加法运算
    number = [self addActionString:muArray1[n] andString:number].mutableCopy;
    n++;
}

//添加补零
NSString *sumber = [muArray componentsJoinedByString:@""];

NSString *string = [NSString stringWithFormat:@"%@%@",number.mutableCopy,sumber];

return string;

}

注:此运算必须为整数的字符串,不支持有小数点的运算。

    原文作者:大整数乘法问题
    原文地址: https://blog.csdn.net/qq_39242343/article/details/88714377
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞