Calculate a Ratio in C#

前端 未结 5 1957
半阙折子戏
半阙折子戏 2020-12-03 05:36

I thought this would be simple, but searching Google didn\'t seem to help.

I\'m basically trying to write a function which will return a ratio as a string (eg 4:3) w

相关标签:
5条回答
  • 2020-12-03 05:37

    Are you basically trying to get the greatest common denominator - GCD for the two numbers and then dividing them by that and thus getting your string ?

    I.e: 800 : 600 ; the greatest common denominator = 200 thus 4:3.

    This would be able to deal with all integer numbers. Sorry for not sending the code, but I think that from this on it should be simple enough.

    public int GCD(int a, int b)
    
    {
        while (a != 0 && b != 0)
        {
             if (a > b)
                a %= b;
             else
                b %= a;
        }
         if (a == 0)
             return b;
         else
             return a;
    }
    
    // Using Konrad's code: 
    
    var gcd = GCD(A, B);
    return string.Format("{0}:{1}", A / gcd, B / gcd)
    
    0 讨论(0)
  • 2020-12-03 05:44

    With Using following 2 functions, you will be able tow get ratio of two numbers without using division operations.

    static int GCD(int p, int q)//find greatest common divisor
    {
        if (q == 0)
        {
            return p;
        }
    
        int r = p % q;
    
        return GCD(q, r);
    }
    static string  FindRatio(int num1, int num2)
    {
    
        string oran = "";
        int gcd;
        int quotient = 0;
        while (num1 >= num2)
        {
            num1 = num1 - num2;
            quotient++;
        }
    
        gcd = GCD(num1, num2);
    
        //without using division finding ration of num1 i1
        int i1 = 1;
        while (gcd*i1 != num1)
        {
            i1++;
        }
        //without using division finding ration of num1 i2
        int i2 = 1;
        while (gcd * i2 != num2)
        {
            i2++;
        }
    
        oran = string.Concat(quotient, " ", i1,"/",i2);
        return oran;
    }
    

    Output will be as follow:
    coff num1 / num2

    0 讨论(0)
  • 2020-12-03 05:45

    You can simplify fractions by dividing numerator and denominator by their GCD:

    var gcd = GCD(A, B);
    return string.Format("{0}:{1}", A / gcd, B / gcd)
    

    And a very basic function for calculating the GCD, using the Euclidean algorithm:

    static int GCD(int a, int b) {
        return b == 0 ? Math.Abs(a) : GCD(b, a % b);
    }
    
    0 讨论(0)
  • 2020-12-03 05:54

    Having played with such things in the past, I'll just add that dealing with signed values can get ugly. Let me suggest that the simplest way to handle signed values is to apply Konrad's approach to the absolute values of your original numbers, then prepend a '-' to the resulting string if the original values have different signs.

    Using this approach, the Greatest Common Divisor of -100 and -35 is 5, for a ratio of 20:7. If the original inputs had been either of the pairs (-100 and 35) or (100 and -35), you'd still get a GCD of 5, and an initial result of 20:7, but the final answer would be -20:7 (i.e. standardized form regardless of which input was negative, just as both -6/2 and 6/-2 = -3).

    0 讨论(0)
  • 2020-12-03 06:03

    Other commentators have given good solutions for integers; if you really have to deal with floating-point values, though, you'll need something else. In general, two real numbers won't have a clean ratio that can be prettily printed; what you want is the closest rational approximation. Probably the best way to go about finding that is just to compute the continued fraction expansion of the quotient; Mark Dominus gives a good introduction to those on his blog.

    0 讨论(0)
提交回复
热议问题