Using stack stacks to set big data operations

Source: Internet
Author: User

Using stack stacks to set big data operations
 Packagecom.sta.to;ImportJava.util.Iterator;ImportJava.util.Stack; Public  class Damax {Public   void Jiafa(String value1, String value2) {/** * More information Welcome to the Kay Academy website: http://kaige123.com * @author Small Mo * * //ToCharArray the string into characters Char[] C1 = Value1.tochararray ();Char[] C2 = Value2.tochararray ();//apply for 2 stack sets ready to put the characters in.Stack numvalue1 =NewStack (); Stack numvalue2 =NewStack ();//into the stack  for(CharC:C1) {Numvalue1.push (c);} for(CharC:C2) {Numvalue2.push (c);}//new good one cache pool, receiving sumStringBuffer buffer =NewStringBuffer ();intJW =0;//Rounding //Use the usual class math to get the longest length intCount = Math.max (C1.length, c2.length);//Take the long loop  for(inti =0; I < count; i++) {//To store the value of the stack intNUM1;intnum2;//each time the value of the stack is converted to int to perform the operation Try{NUM1 = Integer.parseint (Numvalue1.pop (). toString ());}Catch(Exception e) {NUM1 =0;//If the exception is thrown 0}Try{num2 = Integer.parseint (Numvalue2.pop (). toString ());}Catch(Exception e) {num2 =0;}//Add the values quoted by NUM1 and Num2 plus jw to sum intsum = num1 + num2 + jw;if(Sum >=Ten) {//sum is greater than or equal to 10 then the previous one will be counted into 1JW =1; Buffer.append (sum%Ten);//Put the remainder of the sum after the touch into the buffer pool}Else{JW =0;//not greater than or equal to 0 JW to give the sum directly to the buffer poolBuffer.append (sum);}}if(JW = =1) {//Ask JW to wait not equal to 1, also equals to indicate that the number of first digits requires more than 1 such as 99+1=100Buffer.append (1);} Buffer.reverse ();//Put the value in reverseSYSTEM.OUT.PRINTLN (buffer);}}
Test class:
 Packagecom.sta.to;ImportJava.nio.Buffer; Public  class Test {Public   static void main(string[] args) {Damax ys =NewDamax (); Ys.jiafa ("Ten","5");//10+5Ys.jiafa (" the"," the");//15+50Ys.jiafa ("999999999999999","1");//999...+1Ys.jiafa (" the","1");//99+1Ys.jiafa ("a","a");//80+80Ys.jiafa ("8","7");//8+7}}

Using stack stacks to set big data operations

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.