//大数除法运算
+(NSArray *)exceptActionString:(NSString *)string1 andString:(NSString *)type{
NSMutableArray *numberArray = [[NSMutableArray alloc] init];
//整除
NSInteger number;
NSString *str1;
NSInteger remainder = 0;
//判断除数的长度是否比被除数长
if(string1.length >= type.length){
for(int i = 0; i < string1.length - (type.length -1);i++){
if (i > 0) {
//取出下一次运算的数据
str1 = [string1 substringWithRange:NSMakeRange(i + type.length -1,1)];
if (remainder != 0) {
//把余数 和 下一次运算的数据取出 组成新的除数
str1 = [NSString stringWithFormat:@"%ld%@",(long)remainder,str1];
}
}else{
//取出第一次运算的除数
str1 = [string1 substringWithRange:NSMakeRange(i, type.length)];
}
//整除
number = [str1 integerValue]/[type integerValue];
//取余
remainder = [str1 integerValue]%[type integerValue];
[numberArray insertObject:[NSString stringWithFormat:@"%ld",(long)number] atIndex:i];
}
//去掉无效的0
if (numberArray.count > 0) {
NSInteger i = 0;
while (i < numberArray.count -1) {
NSString *str1 = numberArray[0];
if ([str1 integerValue] == 0) {
//去掉无效的0
[numberArray removeObjectAtIndex:i];
}else{
break;
}
i++;
}
}
NSString *string = [numberArray componentsJoinedByString:@""];
NSArray *arr = @[string,[NSString stringWithFormat:@"%ld",(long)remainder]];
return arr;
}else{
return @[@"运算错误"];
}
}
注:返回的数据类型为数组,存放计算结果和余数
版权声明:本文为qq_39242343原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。