Large integer subtraction (beyond the representation range of the shaping)

Source: Internet
Author: User

Algorithm idea: preprocessing input integer string, remove the beginning of ' 0 ', then subtraction operation, subtraction operation, if the meiosis is less than the meiosis, then exchange two number assigned to Minuend and meiosis, always keep large number minus decimal. Finally, the positive and negative values are determined by exchanging assignments.

Code implementation:

1#include <iostream>2#include <string>3 using namespacestd;4 5 //Remove the 0 from the beginning of the character6 voidPROCESSSTR (string&str)7 {8     if(Str.size () <=0)9         return;Ten     intlen=str.size (); One     inti; A      for(i=0; i<len&&str[i]=='0'; i++); -  -Str=str.substr (i,len-i); the              - } -  - //subtraction Function Implementation + voidBigintsubtract (stringStra,intLenA,stringStrB,intLenB,Char* Res,intlenres) - { +     //Invalid input A     BOOLinvaildinput=false; at     if(lena<=0|| lenb<=0) -     { -invaildinput=true; -         return; -     } -invaildinput=false; in  -     //determine meiosis and meiotic to     stringSubtrahend=stra;//be meiosis +     stringSUBTRACTER=STRB;//meiosis -  the     if(lenb>LenA) *     { $Subtrahend=StrB;Panax NotoginsengSubtracter=Stra; -     } the     if(LENA==LENB)//when the length is equal, determine the size +     { A         if(stra>StrB) the         { +Subtrahend=Stra; -Subtracter=StrB; $         } $         Else if(stra<StrB) -         { -Subtrahend=StrB; theSubtracter=Stra; -         }Wuyi         Else the             return; -     } Wu      -     //Subtraction Operations About     intLenl=subtrahend.size (); $     intlens=subtracter.size (); -     intflag=0;//Mark whether borrow -     intnum=0;//Store Subtraction Operation Intermediate number -  A     intil=lenl-1, is=lens-1, ir=lenres-2;//Note the initial value of the IR because the subscript and the +      for(; il>=0&&is>=0&&ir>=0; IL--, is--, ir--) the     { -num=subtrahend[il]-'0'-(subtracter[is]-'0')-Flag; $         if(num>=0) the         { theres[ir]=num+'0'; theflag=0; the         } -         Else in         { theres[ir]=Ten+num+'0'; theflag=1; About         } the          the     } the  +     if(1==flag)//The middle appears borrow, 12345-789 this situation -res[ir--]=subtrahend[il--]-Flag; the Bayi      while(il>=0)//long short, long remaining part direct copy the     { theres[ir--]=subtrahend[il--]; -     } -  the     if(SUBTRAHEND==STRB)//swap storage, which is a negative theres[ir]='-'; the     Elseres[ir]='+'; the  -     return; the } the  the voidMain ()94 { the     stringSTRA,STRB; the  thecout<<"input Stra"<<Endl;98Cin>>Stra; Aboutcout<<"input StrB"<<Endl; -Cin>>StrB;101 102Processstr (Stra);//subtract 0 from the beginning of the number, for example 000123 into 123103 processstr (StrB);104      the     //storing result arrays, applying and initializing106     intLena=stra.size ();107     intlenb=strb.size ();108     intLenres=lena>=lenb? (lena+2):(lenb+2);109  the     Char* res=New Char[lenres];111memset (Res,'0', lenres*sizeof(Char)); theres[lenres-1]=' /';113  the     //Subtraction Function Call the bigintsubtract (stra,lena,strb,lenb,res,lenres); the     117     //+0123 or 00123 or 000 case processing118      while((*res=='+'|| *res=='0') &&*res!=' /')119res++; - 121     //if all zero conditions122     if(*res==' /')123     {124cout<<"The result is:"<<0<<Endl; theSystem"Pause");126         return;127     } - 129cout<<"The result is:"<<res<<Endl; theSystem"Pause");131     return; the}

Large integer subtraction (beyond the representation range of the shaping)

Related Article

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.