How to output fraction instead of decimal number?

后端 未结 13 1987
余生分开走
余生分开走 2020-12-01 21:59

In C++, When I calculate 2/3, it will output decimal values, how can I just get the original format (i.e.g 2/3) instead of 0.66666667

Thanks

相关标签:
13条回答
  • 2020-12-01 22:29

    write your own Rational class to calculate divisions

    class Rational
    {
    public:
        int numerator, denominator;
    
        Rational(int num, int den=1){
            numerator = num;
            denominator=den;
        }
        Rational(Rational other){
            numerator = other.numerator;
            denominator = other.denominator;
        }
        double operator / (int divisor){
                denominator *= divisor;
                simplificate();
                return getrealformat();
        }
        Rational& operator / (int divisor){
                denominator *= divisor;
                simplificate();
                return this;
        }
        Rational& operator / (Rational &divisor){
                numerator *= divisor.numerator;
                denominator *= divisor.denominator;
                simplificate();
                return this;
        }
        double operator / (int divisor){
                denominator *= divisor;
                simplificate();
            return getrealformat();
        }
        double getrealformat(){
            return numerator/denominator;
        }
        simplificate(){
            int commondivisor = 1;
            for(int i=2;i<=min(abs(numerator), abs(denominator));i++)
                if( numerator%i == 0 && denominator%i == 0 )
                    commondivisor = i;
            numerator /= commondivisor;
            denominator /= commondivisor;
        }
    };
    

    use

    Rational r1(45), r2(90), r3=r1/r2;
    cout<<r3.numerator<<'/'<<r3.denominator;
    cout<<r3.getrealformat();
    
    0 讨论(0)
  • 2020-12-01 22:31

    This is impossible in general: floating point numbers are not precise and do not retain sufficient information to fully reconstruct a fraction.

    You could, however, write a function that heuristically finds an "optimal" approximation, whereby fractions with small numerators and denominators are preferred, as are fractions that have almost the same value as the floating point number.

    If you're in full control of the code, Oli's idea is better: don't throw away the information in the first place.

    0 讨论(0)
  • 2020-12-01 22:32

    To simplify efforts, I suggest you stick with known denominators if possible.

    I'm working with an application where the fractions are restricted to denominators of powers of 2 or using 3 (for thirds).

    I convert to these fractions using an approximation (rounding to the nearest 1.0/24.0).

    Without some restrictions, finding the denominator can be quite a chore and take up a lot of the execution time.

    0 讨论(0)
  • 2020-12-01 22:40

    I am beginner and this way that I use may not be a proper way

    #include <iostream>
    
    using namespace std;
    int main ()
    {
      double a;
      double b;
      double c;
    
      cout << "first number: ";
      cin >> a;
      cout << "second number: ";
      cin >> b;
    
      c = a/b;
      cout << "result is: " << c << endl;
    
      if (b != 0) {
        if (a > 0) {
          if (c - (int)c > 0 && c - (int)c < 1)
            cout << "fraction: " << a << "/" << b;
        } else {
          if (c - (int)c < 0 && c - (int)c < 1)
            cout << "fraction: " << a << "/" << b;
        }
      }
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 22:40

    This is a program to convert a decimal number into a fraction

    #include<iostream>
    using namespace std;
    
    int main()
    {
    
        float num, origNum, rem = 1;
        int den = 1, i, count=0, gcd=1;
    
        cout << "Enter any float number to convert it into mixed fraction: ";
        cin >> origNum;
    
        num = origNum - static_cast<int>(origNum);
    
        if (num > 0.1)
        {
            while ( (rem > 0.1) )
            {
                num = num * 10;
                rem = num - static_cast<int>(num);
                count++;
            }
    
            for (i = 1; i <= count; i++) // counter is for the calculation of denominator part of mixed fraction 
            {
                den = den * 10;
            }
    
            for (i = 2; i <= num|| i<=rem; i++)
            {
                if( (static_cast<int>(num) % i == 0) && (den % i == 0) )
                {
                    gcd = i;
                }   
            }
    
            cout << (static_cast<int>(origNum)) << " and " << (static_cast<int>(num))/gcd << "/" << den/gcd;
        }
        else
            cout << (static_cast<int>(origNum));
    
        return 0;   
    }
    
    0 讨论(0)
  • 2020-12-01 22:43

    You can't. You would need to write a class dedicated to holding rational numbers (i.e. fractions). Or maybe just use the Boost Rational Number library.

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