私は長い加算のように機能する add メソッドを作成しようとしているので、最後から加算を開始し、キャリーを正しく取得できるように逆方向に作業したいと考えています。配列。たとえば、私がやろうとしていること。文字 123456789 の 2 つの配列があり、それらを 9 + 9 から始めて追加し、次に 8 + 8 に移動したい
したがって、配列を逆方向に反復する正しい方法を使用していると確信していますが、試行するたびに実行時エラー、範囲外のインデックスが発生し、その理由がわかりません。どんな助けでも素晴らしいでしょう、なぜそれが例外をスローし続けるのか分かりません。
-(MPInteger *) add: (MPInteger *) x
{
NSMutableArray *a = self->intString;
NSMutableArray *b = x->intString;
NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];
//for (int i = 0; i < [a count]; i++) {
for (NSInteger i = [a count] - 1; i > 0; i--) {
int num = 10;
NSNumber *ourNum = [NSNumber numberWithInt:num];
NSNumber *total = [NSNumber numberWithInt:[[a objectAtIndex:i] intValue] + [[b objectAtIndex:i] intValue]];
if ([total intValue] >= [ourNum intValue]) {
total = [NSNumber numberWithInt:([total intValue] - [ourNum intValue])];
[c addObject:[NSNumber numberWithInt:([total intValue])]];
} else {
[c addObject:[NSNumber numberWithInt:[[a objectAtIndex:i] intValue]+[[b objectAtIndex:i] intValue]]];
}
NSLog(@"%@", c[i]);
}
return x;
}