注意这里是整数,浮点数需要额外的操作,实现大整数的加减,三个栈就OK了,两个运算整数栈,一个结果栈,基本的逻辑的就是利用栈的先入后出的特点将高位push到栈底,低位push到栈顶,之后两个栈pop出来之后push到结果栈,结果栈pop出来就是我们想要的结果。看起来还不错,如果有兴趣就看下面的代码,代码通过OC实现,原理类似:
鉴于OC没有Stack,先简单实现一个栈吧:
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 *[email protected]""; //正序输出即为结果 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~
时间: 2024-10-12 23:32:37