C # Big Data calculator-Addition

Source: Internet
Author: User

///
 

///
Base class, abstract class, defining common methods
///
Abstract
Class bigcalculate
{

Public abstract string evaluate (string num1, string num2 );


// Reverse the numerical order

Protected string invertorder (string Str)

{

Char [] chtemp = new char [Str. Length];


For (INT I = 0; I <Str. length; I ++)

{

Chtemp [I] = STR [Str. Length-i-1];

}


Return new string (chtemp. toarray ());

}


// Exchange numbers

Protected void changenum (ref string num1, ref string num2)

{

String strtemp = num1;

Num1 = num2;

Num2 = strtemp;

}

// Obtain a large number

Protected string max (string num1, string num2)

{

If (num1.length> num2.length)

{

Return num1;

}

Else if (num1.length = num2.length)

{

For (INT I = num1.length-1; I> = 0; I --)

{

If (num1 [I]. compareto (num2 [I]) <0)

{

Return num2;

}

}

Return num1;

}

Return num2;

}
}
--------------------------------------------------------------------------

class
bigadd: bigcalculate
{< br>
Public override string evaluate (string num1, string num2)

{< br>
// obtain a large number of digits
int Len = num1.length> num2.length? Num1.length:
num2.length;
// Add 0 to the decimal digit
string tempnum1 = new string ('0 ', len-num1.length) + num1;
string tempnum2 = new string ('0', len-num2.length) + num2;


int flag = 0; // carry character
List Lich = new list (); // store the number after calculation
for (INT I = len-1; I >=0; I --)

{< br>
Lich. add (char) (tempnum1 [I] + tempnum2 [I] + flag-'0');
flag = (Lich [Lich. count-1]-'0')/10;
Lich [Lich. count-1] = (char) (Lich [Lich. count-1]-'0') % 10) +
'0');

}


// If the input character is not 0 after the computation, and the highest bit carry, add another digit.

If (flag> 0)

{

Lich. Add (char) (flag + '0 '));

}


Return invertorder (new string (lich. toarray ()));

}
}

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.