0

所以我正在制作一个大整数程序,并且在添加两个长度不同的数组时遇到了问题。我遇到的问题是 add 方法。如果我正在遍历数组,是否有任何方法可以测试元素是否超出范围。我试过测试 a 中的元素是否等于 nil,但我仍然得到异常。任何帮助都会非常感谢。

 #import <Foundation/Foundation.h>
    #import "MPInteger.h"

    @implementation MPInteger

    {
    }

    -(id) initWithString: (NSString *) x
    {
        self = [super init];
        if (self) {
            intString = [NSMutableArray array];
            for (int i = 0; i < [x length]; i++) {
                NSString *ch = [x substringWithRange:NSMakeRange(i, 1)];
                [intString addObject:ch];

            }

        }

        return self;
    }

    -(NSString *) description
    {
        return self.description;
    }


    - (MPInteger *) add: (MPInteger *) x {
        NSMutableArray *a = self->intString;
        NSMutableArray *b = x->intString;
        NSMutableArray *c = [NSMutableArray array];
        NSInteger arrayCount;
        if (a < b) {
            arrayCount = [b count];
        } else {
            arrayCount = [a count];
        }
        int num = 10;
        int carry = 1;
        NSNumber *total;
        NSNumber *carrySum;
        for (int i = 0; i < arrayCount; i++) {
            if (a[i] == nil) {

                total = @([b[i] intValue]);
                [c addObject:total];
            } else if (b[i] == nil) {
                total = @([a[i] intValue]);
                [c addObject:total];
            } else {
            total = @([a[i] intValue] + [b[i] intValue]);
            [c addObject:total];
            }
        }
        for (NSInteger j = [c count]-1; j >=0; j--) {
            if ([c[j] intValue] >= num) {
                total = @([c[j] intValue] - num);
                carrySum = @([c[j-1] intValue] + carry);
                [c replaceObjectAtIndex:j withObject:total];
                [c replaceObjectAtIndex:j-1 withObject: carrySum];
            }

        }
        NSString *str = [c componentsJoinedByString:@""];
        NSLog(@"%@", str);

        return x;
    }

    -(MPInteger *) multiply: (MPInteger *) x
    {

        NSMutableArray *a = self->intString;
        NSMutableArray *b = x->intString;
        NSMutableArray *c = [NSMutableArray array];
        NSMutableArray *sum = [NSMutableArray array];
        NSNumber *total;
        NSNumber *carrySum;
        int num = 10;
        NSNumber *endZero = 0;
        NSInteger bottomCount = [b count]-1;
        while (bottomCount != -1) {

            for (int i = 0; i < [a count]; i++) {
                total = @([a[i] intValue] * [[b objectAtIndex:bottomCount] intValue]);
                if (bottomCount == [b count] -1) {
                    [c addObject:total];
                } else {
                    [c replaceObjectAtIndex:i withObject:total];
                }
            }


            for (NSInteger j = [c count]-1; j>=0; j--) {
                NSString *carry = [NSString stringWithFormat:@"%d", [c[j] intValue]];
                NSString *carry2 = [carry substringToIndex:1];
                int carryFinal = [carry2 intValue];
                NSString *carry3 = [carry2 stringByAppendingString:@"0"];
                int carry4 = [carry3 intValue];

                if ([c[j] intValue] >= num) {
                    total = @([c[j] intValue] - carry4);
                    carrySum = @([c[j-1] intValue] + carryFinal);
                    [c replaceObjectAtIndex:j withObject:total];
                    [c replaceObjectAtIndex:j-1 withObject: carrySum];
                } else {
                    if(j == 0) {
                        if (bottomCount == [b count] -1) {
                            bottomCount = bottomCount - 1;
                            NSString *str = [c componentsJoinedByString:@""];
                            [sum addObject: str];
                        } else {
                            [c addObject:@([endZero intValue])];
                            bottomCount = bottomCount - 1;
                            NSString *str = [c componentsJoinedByString:@""];
                            [sum addObject: str];
                        }
                    }
                }
            }
        }

        NSMutableArray *finalSum = [NSMutableArray array];
        MPInteger *ele1;
        MPInteger *ele2;
        MPInteger *eleSum;

        NSNumber *endZ= @(0);
        [finalSum insertObject:endZ atIndex:0];
        for (int k = 0; k < [sum count]; k++) {
            NSString *str= [NSString stringWithFormat:@"%d", [sum[k] intValue]];
            NSString *str2 = [NSString stringWithFormat:@"%d", [sum[k+1] intValue]];
            ele1 = [[MPInteger alloc] initWithString:str];
            ele2 = [[MPInteger alloc] initWithString:str2];
            eleSum = [ele1 add: ele2];
            NSLog(@"%@", eleSum);
        }

        NSLog(@"%@", sum);



            return self;
        }

更新了这个

  for (int i = 0; i < arrayCount; i++) {
            if (a[i] == nil) {

                total = @([b[i] intValue]);
                [c addObject:total];
            } else if (b[i] == nil) {
                total = @([a[i] intValue]);
                [c addObject:total];
            } else {
            total = @([a[i] intValue] + [b[i] intValue]);
            [c addObject:total];
            }
        }

现在变成了:

NSMutableArray *c = a.count > b.count ? [a mutableCopy] : [b mutableCopy];
NSArray *shortestArray = a.count > b.count ? b : a;

[shortestArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSNumber *currentNumber, NSUInteger idx, BOOL *stop) {
        c[idx] = @(currentNumber.integerValue + [c[idx] integerValue]);
        NSLog(@"%@", c[idx]);
}];

我认为我需要做的是数组 a 而不是 b 中的每个索引,反之亦然,是添加开头的零,但我不知道该怎么做。

我打印出它在每次迭代后做了什么,它给出了:

2013-09-02 12:31:42.630 Asgn1[42471:303] 5
2013-09-02 12:31:42.632 Asgn1[42471:303] 3
2013-09-02 12:31:42.632 Asgn1[42471:303] 1
And a final answer of:
2013-09-02 12:31:42.633 Asgn1[42471:303] 353
4

1 回答 1

1

对于失败的代码,取一个mutableCopy大数组然后循环遍历较小的数组进行计算不是更简单吗?

也许是这样的:

NSMutableArray *c             = a.count > b.count ? [a mutableCopy] : [b mutableCopy];
NSArray        *shortestArray = a.count > b.count ? b : a;

[shortestArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSNumber *currentNumber, NSUInteger idx, BOOL *stop) {
  c[idx] = @(currentNumber.integerValue + [c[idx] integerValue]);
}];
于 2013-09-01T23:26:23.847 回答