Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double x;
- cin >> x;
- string a = to_string(x);
- int counter = 0; //подсчет кол-во знаков после запятой, исключая лидирующие нули
- int f = 0;
- int out = 0;
- for (int i = 0; i < a.length(); i++) {
- if (a[i] == ',') break;
- if (a[i] == '0' && i == 0) break;
- out = out * 10 + (a[i] - '0');
- }
- for (int i = a.length() - 1; i > -1; i--) {
- if (a[i] != '0') f = 1;
- if (f == 1) {
- if (a[i] == ',') break;
- counter++;
- }
- }
- int num; //номер символа, который после точки
- for (int i = 0; i < a.length(); i++) {
- if (a[i] == ',') num = i+1;
- }
- int up = 0; //знаменатель
- int low = pow(10, counter); //числитель
- for (int i = num; i < counter+num; i++) {
- up = up * 10 + (a[i] - '0');
- }
- int nd = NOD(up, low);
- if (out != 0) cout << out << " ";
- cout << up / nd << "/" << low / nd << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement