C # Big Data calculator-Subtraction

Source: Internet
Author: User

Class bigreduce: bigcalculate
 

{

Public override string evaluate (string num1, string num2)

{

Bool isminus = false;

// Determine whether the calculation result is a positive number (that is, whether num1 is greater than num2). If it is equal, 0 is directly returned.

// If the result is negative, replace num1 and num2 and add '-' before the return value '-'

If (num1.equals (num2 ))

{

Return "0 ";

}

Else if (max (num1, num2). Equals (num2) // if the expected calculation result is negative, set num1
Num2 exchange

{

Changenum (ref num1, ref num2 );

Isminus = true;

}


# Region myregion

// Obtain a large number of digits

Int Len = num1.length> num2.length? Num1.length:
Num2.length;

// Add the decimal difference to 0

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 (); // stores the calculated number

For (INT I = len-1; I> = 0; I --)

{

If (tempnum1 [I]> = tempnum2 [I] + flag)

{

Lich. Add (char) (tempnum1 [I]-tempnum2 [I]-flag + '0 '));

Flag = 0;

}

Else

{

Lich. Add (char) (tempnum1 [I]-tempnum2 [I]-flag + 10 +
'0 '));

Flag = 1;

}

}


// If the result is 0 in the upper order, remove it.

// If the result is negative, the returned result is added "-"

If (isminus = true)

{

Return "-" + invertorder (New
String (lich. toarray (). trimstart ('0 ');

}

Return invertorder (New
String (lich. toarray (). trimstart ('0 ');

# Endregion


}
}

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.