Primary school students fun C ++ programming lesson 53 Huagong Dafa (complete)

Link: https://pan.baidu.com/s/1qDdBkFPakLtCvh8_W2wiog 
extraction code: x3zg

#include <iostream> 
#include <cmath> // Call the absolute value function fabs () 
using namespace std; 
int main () 
{ 
  double x, y; 
  long long a, b, i, j; 
  cout << "Please enter a pure decimal number" << endl; 
  do 
  { 
    cout << "x ="; 
    cin >> x; 
  } while (x> = 1 || x <= 0); 
  a = 1; 
  y = x; 
  while (fabs (y- (int) y)> 1e-10) // Pure decimals are converted to integers 
  { 
    a * = 10; 
    y = x * a; // Cannot be written as y = y * 10; 
  } // Due to errors Stop expansion, there will be an endless loop 
  b = y; 
  cout << b << '/' << a << endl; 
  for (i = b; i> = 1; i--) // Find a and b The largest convention 
    if (b% i == 0 &&a% i == 0) 
    { 
      j = i; // After finding the greatest common divisor, assign it to j  
      break; // Exit the loop
    }
  cout<<"最简分数为:";
  cout<<b/j<<'/'<<a/j<<endl;	 
  return 0;
}

  

Guess you like

Origin www.cnblogs.com/kixiaoyuan/p/12702353.html