Let's talk about the infinite number of digits computation written in c #.

Source: Internet
Author: User

It's a bit boring to be idle. I remember I used c to write the addition and subtraction of infinite digits in acm before. What about multiplication and division? Now I wrote one in c:

Addition and subtraction are simple. They use an array to save the numbers on each digit, and then add or subtract one digit from the back to the back. Remember to add and subtract the first digit, remove the excess "0" from the subtraction rule;

Multiplication, you just need to calculate it on the draft paper, that is, to multiply the numbers that lie to the corresponding digits of two arrays, respectively adding the "0" after the two digits, for example:

234*678 = 2*6*100*100 + 2*7*100*10 + .... + 4*8

Clear? It's just how the elementary school teacher taught you how to calculate it, and how to use the computer as a primary school student. here we need to use the above addition;

Division: first compare the number of the same digits before the divisor and the multiple of the divisor. Here, the multiple calculation is the multiplication above. Put the maximum multiple into the highest digit of the answer, and so on, then, calculate the remaining number * 10 + next digit until the last digit;

The following is the code I wrote. I first pasted the bottom-layer unsigned floating-point operation. The calculation parameters are of the StringBuilder type for easy conversion.

(This code is for reference only and cannot be used in commercial activities)

 

Static internal class Calculate
{
Static private int [] operand1;
Static private int [] operand2;
Static private IList <int> result;

/// <Summary>
/// Addition
/// </Summary>
/// <Param name = "a"> </param>
/// <Param name = "B"> </param>
/// <Returns> </returns>
Static internal StringBuilder Addition (StringBuilder a, StringBuilder B)
{
SetOperand (a, B );

Try
{
Reverse ();
Int count = (operand1.Length> operand2.Length )? Operand1.Length: operand2.Length;
Int op1, op2, upNum = 0;
For (int I = 0; I <count; I ++)
{
Op1 = (I> = operand1.Length )? 0: operand1 [I];
Op2 = (I> = operand2.Length )? 0: operand2 [I];
Int temp = op1 + op2 + upNum;
If (temp <10)
{
Result. Insert (0, temp );
UpNum = 0;
}
Else
{
Result. Insert (0, temp % 10 );
UpNum = temp/10;
}
If (I = (count-1) & upNum> 0) result. Insert (0, upNum );
}
}
Catch {result = new List <int> () {0, 0 };}

Return GetResult ();
}

/// <Summary>
/// Subtraction
/// </Summary>
/// <Param name = "a"> </param>
/// <Param name = "B"> </param>
/// <Returns> </returns>
Static internal StringBuilder Subtraction (StringBuilder a, StringBuilder B)
{
SetOperand (a, B );

Try
{
Reverse ();
Bool desc = isDesc (a, B );
Int [] opList1 = desc? Operand2: operand1;
Int [] opList2 = desc? Operand1: operand2;
Int count = (opList1.Length> opList2.Length )? OpList1.Length: opList2.Length;
Int op1, op2, upNum = 0;
For (int I = 0; I <count; I ++)
{
Op1 = (I> = opList1.Length )? 0: opList1 [I];
Op2 = (I> = opList2.Length )? 0: opList2 [I];
&

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.