简体   繁体   中英

Add a digit into a double

I'm trying to make a complex division calculator.

I have a double[] and I want to add the mod of the previous array position to the start of the next number.

For example, if I were to divide 1951 with 2, I split 19 and 51 into the double[], (meaning double[0] = 19 and double[1] = 51) and want to add the mod of 19 with 2 to the start of the next position, in this case add "1" to double[1] and have it equal to 151 to continue. Example code matching with the examples above.

    static double[] number = new double[2];
    static int toDiv;    

    static void Main(string[] args)
    {

        ///TESTING ONLY
        number[0] = 19;
        number[1] = 51;

    }//end of main

    void Calculate()
    {
            for (int i= 0; i<number.Length; i++)
            {

                if (number[i] % numsToDiv[0] == 0)
                {//if the program gets in here, % = 0

                    if (i - 1 == number.Length)
                    {
                        return false;
                    }//end of if (i-1==number.Length)
                    else
                    {
                        toDiv = (int)number[i] % numsToDiv[0];

                    }//end of else
                }//end of if
            }//end of "i" for
    }//end of Calculate()

lets first create a function that returns the 0s needed to split the numerator into two and find the 0s to add for placing the mod value of the next number

int zeroPlaceHolder(ZerosNeeded){
    int zeros = 1;
    for(int i = 0; i < ZerosNeeded;i++)
       zeros *= 10;
    return zeros;
}

find the length of digits in the numerator i would go about this by changing the numerator to string then find the length of the string

int numerator = 1951;
int denominator = 2;
//this is a convetion used in programming so am assuming C# has this too
int len = numerator.toString().length;

split the numerator into 2 using half the 0s of the digit, this will give us both parts of the split

int divider = zeroPlaceHolder(len/2); 
//the first part can be found by
number[0] = numerator/divider;
//second part
number[1] = numerator%divider;
//find the number needed to shift the mod of previous to first digit of next
len = number[1].toString().length;
divider = zeroPlaceHolder(len);
//shift the mod of the 1st number(number[0])/denominator to the right
number[1] = number[1] + (divider * (number[0]%denominator));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM