算法-大整数加法

注意这里是整数,浮点数需要额外的操作,实现大整数的加减,三个栈就OK了,两个运算整数栈,一个结果栈,基本的逻辑的就是利用栈的先入后出的特点将高位push到栈底,低位push到栈顶,之后两个栈pop出来之后push到结果栈,结果栈pop出来就是我们想要的结果。

Stack.h:

@interface Stack : NSObject
//栈顶的元素
@property  (strong,nonatomic) Node  *first;

@property  (assign,nonatomic) NSInteger  count;

-(BOOL)isEmpty;

-(NSInteger)size;

-(void)push:(NSString *)value;

-(NSString *)pop;

-(void)remove:(NSString *)value;

@end

Stack.m代码:

@implementation Stack

-(BOOL)isEmpty{
    return self.count==0;
}

-(NSInteger)size{
    return self.count;
}

-(void)push:(NSString *)value{
    Node  *oldFirst=self.first;
    self.first=[[Node alloc]init];
    self.first.value=value;
    self.first.next=oldFirst;
    self.count=self.count+1;
}

-(NSString *)pop{
    if (!self.first) {
        return [NSString stringWithFormat:@"-1"];
    }
    NSString *value=self.first.value;
    self.first=self.first.next;
    self.count=self.count-1;
    return value;
}

-(void)remove:(NSString *)value{
    if ([self.first.value isEqualToString:value]) {
        self.first=self.first.next;
        self.count=self.count-1;
    }else{
        Node *node=self.first;
        while (node.next) {
            if ([node.next.value isEqualToString:value]){
                if (node.next.next) {
                    Node *tempNode=node.next.next;
                    node.next=tempNode;
                }else{
                    node.next=NULL;
                }
                self.count=self.count-1;
                break;
            }else{
                node=node.next;
            }
           
        }
    }
}

@end

进入重点了,整数的加减在StackSum.h中实现:

@interface StackSum : NSObject

-(NSInteger)sum:(NSInteger)firstNumber  secondNumber:(NSInteger)secondNumber;

@end

StackSum.m中的代码:

@implementation StackSum

//原文地址:http://www.cnblogs.com/xiaofeixiang
-(NSInteger)sum:(NSInteger)firstNumber secondNumber:(NSInteger)secondNumber{
    //第一个整数的栈
    Stack  *firstStack=[self getStackByNumber:firstNumber];
    //第二个整数的栈
    Stack  *secondStack=[self getStackByNumber:secondNumber];
    //结果栈
    Stack  *resultStack=[[Stack alloc]init];
    NSInteger  flag=0;//进位标记
    
    while (firstStack.count>0&&secondStack.count>0) {
        NSInteger  temp=[firstStack.pop integerValue]+[secondStack.pop integerValue]+flag;
        [resultStack push:[NSString stringWithFormat:@"%ld",temp%10]];
        flag=temp/10;
    }
    //第一个数字大于第二字数字的情况
    while (firstStack.count>0) {
        NSInteger  temp=[firstStack.pop integerValue]+flag;
        [resultStack push:[NSString stringWithFormat:@"%ld",temp%10]];
        flag=temp/10;
    }
    //第二个数字大于第一个数字
    while (secondStack.count>0) {
        NSInteger  temp=[secondStack.pop integerValue]+flag;
        [resultStack push:[NSString stringWithFormat:@"%ld",temp%10]];
        flag=temp/10;
    }
    //标记位有进位
    if (flag) {
        [resultStack push:[NSString stringWithFormat:@"%ld",flag]];
    }
    NSInteger  count=resultStack.count;
    NSString  *str=@"";
    //正序输出即为结果
    for (NSInteger i=0; i<count; i++) {
        str=[str stringByAppendingString:resultStack.pop];
   }
    return [str integerValue];
}

-(Stack *)getStackByNumber:(NSInteger)value{
    Stack  *stack=[[Stack alloc]init];
    NSString *stringValue=[NSString stringWithFormat:@"%ld",value];
    for (NSInteger i=0; i<[stringValue length]; i++) {
         [stack push:[NSString stringWithFormat:@"%@",[stringValue substringWithRange:NSMakeRange(i, 1)]]];
    }
    return stack;
}

@end

 简单的测试:

    StackSum  *sum=[[StackSum alloc]init];
    NSLog(@"大整数相加的结果为:%ld", [sum sum:9999999 secondNumber:888]);
    NSLog(@"iOS技术交流群:228407086");

结果如下:

随机附赠iOS技术交流群:228407086~

posted @ 2015-06-11 17:21  Fly_Elephant  阅读(1723)  评论(0编辑  收藏  举报