Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. double x;
  2. cin >> x;
  3. string a = to_string(x);
  4. int counter = 0; //подсчет кол-во знаков после запятой, исключая лидирующие нули
  5. int f = 0;
  6. int out = 0;
  7. for (int i = 0; i < a.length(); i++) {
  8.     if (a[i] == ',') break;
  9.     if (a[i] == '0' && i == 0) break;
  10.     out = out * 10 + (a[i] - '0');
  11. }
  12. for (int i = a.length() - 1; i > -1; i--) {
  13.     if (a[i] != '0') f = 1;
  14.     if (f == 1) {
  15.         if (a[i] == ',') break;
  16.         counter++;
  17.     }
  18. }
  19. int num; //номер символа, который после точки
  20. for (int i = 0; i < a.length(); i++) {
  21.     if (a[i] == ',') num = i+1;
  22. }
  23. int up = 0; //знаменатель
  24. int low = pow(10, counter); //числитель
  25. for (int i = num; i < counter+num; i++) {
  26.     up = up * 10 + (a[i] - '0');
  27. }
  28. int nd = NOD(up, low);
  29. if (out != 0) cout << out << " ";
  30. cout << up / nd << "/" << low / nd << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement