Data structure (Java language description) the use of sequential stacks (two large numbers added)

Source: Internet
Author: User

The addition calculation of two numbers exceeding the upper bound of integers is realized by using the definition of sequential stack and the method of http://www.cnblogs.com/xleer/p/5289661.html in the stack.

Algorithm:

Package stack;
/**********************************************************************
* @author Sch
Use the stack to calculate the number of two large numbers. The value of the large number exceeds the range of the int store *******************
********************************************************************* */
Import Java.util.Scanner;
Import java.math.*;
public class Sumdata {
public string Add (string A,string B) throws exception{
int L1=a.length ();
int L2=b.length ();
int l3=0;
if (L1>L2)
l3=l1+1;
Else
l3=l2+1;
Sqstack sum=new Sqstack (L3);
Sqstack Sa=numsplit (a);//Put the number of home as a character into the stack
Sqstack Sb=numsplit (b);
int partialsum;
Boolean iscarry=false;
while (!sa.isempty () &&!sb.isempty ())
{//Addend and non-addend are not empty at the same time.
partialsum= (integer) Sa.pop () + (integer) sb.pop ();//Calculate the sum of the top elements of the stack
if (Iscarry)
{
partialsum++;
Iscarry=false;
}//first to determine whether to carry, and partialsum to do the processing
if (partialsum>=10)
{//Divide the partialsum into stack processing
partialsum-=10;
Sum.push (partialsum);
Iscarry=true;
}
Else
{
Sum.push (partialsum);
}
}
Sqstack temp=!sa.isempty ()? sa:sb;//references to Addend and summand non-empty stacks
while (!temp.isempty ())
{//Sub-case, if there is only one non-null in two stacks
if (Iscarry)
{//If carry is still present at this time
int t= (Integer) Temp.pop ();
++t;
if (t>=10)
{
t-=10;
Sum.push (t);
Iscarry=true;
}else
{
Sum.push (t);
iscarry=false;//there is no carry at this time, will be in position 0
}
}else//the Iscarry is false at this time, no carry is required, and the next round of the while is judged
Sum.push (Temp.pop ());//Do not need carry in order to execute the addition at last, put the addend or the value of the addend into the stack
}//while's judgment
if (Iscarry) {//highest bit, that is, two stacks are empty after the carry-forward sum stack
Sum.push (1);
}
String Str=new string ();
while (!sum.isempty ())//Convert all values in the Asum stack to string
Str=str.concat (Sum.pop (). toString ());
return str;
}//to this point, do and the algorithm is complete
Public sqstack numsplit (String str) throws exception{//the input characters into the stack
Sqstack s=new Sqstack (Str.length ());
for (int i=0;i<str.length (); i++) {
Char C=str.charat (i);
if (' ==c ')
Continue
else if (' 0 ' <=c&& ' 9 ' >=c) {
S.push (Integer.valueof (string.valueof (c)));
}else
throw new Exception ("error: a non-numeric character entered! ");
}
return s;
}
public static void Main (string[] args) throws exception{
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter the two integers you want to calculate:");
String A=sc.next ();
String B=sc.next ();
Sumdata e=new sumdata ();
String Sum=e.add (A, b);
System.out.println ("Two large numbers of and for:" +sum);

}
}

Data structure (Java language description) the use of sequential stacks (two large numbers added)

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.