大数减法运算

标题 大数减法运算

/**
*大数减法运算
**/
-(NSString *)reductionActionString:(NSString *)str1 andString:(NSString *)str2{

NSMutableString *muString1 = [[NSMutableString alloc] init];
NSMutableString *muString2 = [[NSMutableString alloc] init];

//正负数判断
NSString *negative = @"";

//如果相加的两个数的长度不一样 则通过补0补成长度一样的
if (str1.length > str2.length) {
    
    NSInteger count = str1.length - str2.length;
    
    NSInteger i = 0;
    while (i < count) {
        
        //在指定下标 index 插入一个字符串
        [muString2 insertString:@"0" atIndex:i];
        i++;
    }
    
    [muString1 appendString:str1];
    [muString2 appendString:str2];
    
}else if (str1.length < str2.length){
    
    NSInteger count = str2.length - str1.length;
    
    NSInteger i = 0;
    while (i < count) {
        
        //在指定下标 index 插入一个字符串
        [muString2 insertString:@"0" atIndex:i];
        i++;
    }
    
    [muString1 appendString:str2];
    [muString2 appendString:str1];
    negative = @"-";
}else{
    //长度相等时,从首位到末尾逐步判断大小
    NSInteger i = 0;
    while (i < str1.length) {
        
        //取出当前需要判断的数据
        NSString *number1 = [str1 substringWithRange:NSMakeRange(i, 1)];
        NSString *number2 = [str2 substringWithRange:NSMakeRange(i, 1)];
        
        NSInteger sub1 = [number1 integerValue];
        NSInteger sub2 = [number2 integerValue];
        
        if(sub1 < sub2){
            negative = @"-";
            break;
        }
        i++;
    }
    
    if ([negative isEqualToString:@"-"]) {
        [muString1 appendString:str2];
        [muString2 appendString:str1];
    }else{
        [muString1 appendString:str1];
        [muString2 appendString:str2];
    }
    
}

NSMutableString *numberString = [[NSMutableString alloc] init];
if (muString1.length == muString2.length) {
    NSInteger i = 0;
    NSUInteger borrow = 0;
    
    while (i < muString1.length) {
        
        //取出当前需要计算的数据
        NSString *str1 = [muString1 substringWithRange:NSMakeRange(muString1.length -1 -i, 1)];
        NSString *str2 = [muString2 substringWithRange:NSMakeRange(muString2.length -1 -i, 1)];

        NSInteger sub1 = [str1 integerValue];
        NSInteger sub2 = [str2 integerValue];
        
        //判断当前位数是否被借1
        if (borrow == 1) {
            sub1 = sub1 -1;
        }
        
        NSInteger number = 0;
        if(sub1 < sub2){
            //如果被减数大于减数,则向高位借一
            number = sub1 + 10 - sub2;
            borrow = 1;
            
        }else{
            number = sub1 - sub2;
            borrow = 0;
        }
        
        [numberString insertString:[NSString stringWithFormat:@"%ld",(long)number] atIndex:0];
        
        i++;
    }
}

//去掉无效的0
NSInteger k = 0;
NSInteger numberLength = numberString.length;
while (k < numberLength -1) {
    
    NSString *str1 = [numberString substringWithRange:NSMakeRange(0, 1)];
    
    if ([str1 integerValue] == 0) {
        //去掉无效的0
        [numberString deleteCharactersInRange:NSMakeRange(0, 1)];
    }else{
        break;
    }
    k++;
}

return [NSString stringWithFormat:@"%@%@",negative,numberString];

}

上一篇:day11总结和作业


下一篇:运用python编写简单用户注册与登录(列表+文件操作+流程判断)