Algorithm-Large integer addition

Source: Internet
Author: User

Note Here is an integer, floating-point numbers need extra operations, to achieve the addition and subtraction of large integers, three stacks ok, two arithmetic integer stack, a result stack, the basic logic is the use of the first-in-one stack of the characteristics of the top push to the bottom of the stack, low push to the stack, and then push to the result stack after two , the result stack pops out is the result we want. Look good, if you are interested in the following code, code through OC Implementation, the principle is similar:

Since OC does not have a stack, it is easy to implement a stack first:

Stack.h:

Element @property  (strong,nonatomic) Node *first @interface stack:nsobject//stack top,  @property  (assign,nonatomic ) Nsinteger  count;-(BOOL) isempty;-(Nsinteger) size;-(void) Push: (NSString *) value;-(NSString *) pop;-(void) Remove: (NSString *) value; @end

STACK.M Code:

@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

into focus, the addition and subtraction of integers is implemented in StackSum.h:

@interface stacksum:nsobject-(Nsinteger) sum: (Nsinteger) Firstnumber  secondnumber: (Nsinteger) secondnumber;@ End

Code in STACKSUM.M:

@implementation stacksum//Original address: http://www.cnblogs.com/xiaofeixiang-(nsinteger) sum: (Nsinteger) Firstnumber    Secondnumber: (Nsinteger) secondnumber{//stack stack for the first integer *firststack=[self Getstackbynumber:firstnumber];    Stack stack of the second integer *secondstack=[self Getstackbynumber:secondnumber];    Result Stack stack *resultstack=[[stack alloc]init]; Nsinteger flag=0;//carry Tag while (firststack.count>0&&secondstack.count>0) {Nsinteger Temp=[fi        Rststack.pop Integervalue]+[secondstack.pop Integervalue]+flag;        [Resultstack push:[nsstring stringwithformat:@ "%ld", temp%10]];    FLAG=TEMP/10;        }//The first number is greater than the number of the second word while (firststack.count>0) {Nsinteger temp=[firststack.pop integervalue]+flag;        [Resultstack push:[nsstring stringwithformat:@ "%ld", temp%10]];    FLAG=TEMP/10;        }//The second number is greater than the first number while (secondstack.count>0) {Nsinteger temp=[secondstack.pop integervalue]+flag; [Resultstack push:[nsstring Stringwithformat:@ "%ld", temp%10]];    FLAG=TEMP/10;    }//Mark Bit has carry if (flag) {[Resultstack push:[nsstring stringwithformat:@ "%ld", Flag]];    } Nsinteger Count=resultstack.count;    NSString *[email protected] "";   The positive sequence output is the result 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 su    Bstringwithrange:nsmakerange (i, 1)]]; } return stack; @end

A simple test:

    Stacksum  *sum=[[stacksum Alloc]init];    NSLog (@ "Large integers add the result:%ld", [sum sum:9999999 secondnumber:888]);    NSLog (@ "iOS technology Group: 228407086");

The results are as follows:

Randomly attached iOS technology group: 228407086~

Algorithm-Large integer addition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.