Convert Decimal number into Fraction

前端 未结 6 1660
挽巷
挽巷 2021-01-15 00:50

I am trying to convert decimal number into its fraction. Decimal numbers will be having a maximum 4 digits after the decimal place. example:- 12.34 = 1234/100 12.3456 = 1234

6条回答
  •  星月不相逢
    2021-01-15 01:14

    An algorithm created with c++ that does decimal to fraction.

    #include 
    using namespace std;
    
    
    // converts the string half of the inputed decimal number into numerical values
    void converting (string decimalNumber, float& numerator, float& denominator )
    
    {
    float number;
    string valueAfterPoint = decimalNumber.substr(decimalNumber.find(".") + 1,((decimalNumber.length() -1) )); // store the value after the decimal into a valueAfterPoint
    cout << valueAfterPoint<< " "<< endl;
    int length = valueAfterPoint.length(); //stores the length of the value after the decimal point into length
    
     numerator = atof(valueAfterPoint.c_str()); // converts the string type decimal number into a float value and stores it into the numerator
    
    // loop increases the decimal value of the numerator and the value of denominator by multiples of ten as long as the length is above zero of the decimal
    
    cout << length<< endl;
    for (; length > 0; length--)
    {
        numerator *= 10;
    
    }
    do
        denominator *=10;
        while  (denominator < numerator);
    
    }
    
    // simplifies the the converted values of the numerator and denominator into simpler values for          an easier to read output
      void simplifying (float& numerator, float& denominator)
    {
    int maximumNumber = 9; //Numbers in the tenths place can only range from zero to nine so the maximum number for a position in a poisitino for the decimal number will be nine
    
    bool isDivisble; // is used as a checker to verify whether the value of the numerator has the       found the dividing number that will a value of zero
    
    // Will check to see if the numerator divided denominator is will equal to zero
    
    
    
    if(int(numerator) % int(denominator) == 0)
    {
        numerator /= denominator;
        denominator = 1;
        return;
    }
    
    
    //check to see if the maximum number is greater than the denominator to simplify to lowest form
    while (maximumNumber < denominator)
    {
        maximumNumber *=10;
     }
    
    
    // the maximum number loops from nine to zero. This conditions stops if the function isDivisible is true
    for(; maximumNumber > 0; maximumNumber --)
    {
    
        isDivisble = ((int(numerator) % maximumNumber == 0) && int(denominator)% maximumNumber == 0);
        cout << numerator << denominator <<" " <> decimalNumber;
    
    //convert function
    converting(decimalNumber, numerator, denominator);
    
    
    //call simplyfication funcition
    simplifying(numerator, denominator);
    
    
    cout<< "Fraction: "<< numerator << "/" << denominator<< endl;
     return 0; 
    
    }
    

提交回复
热议问题