C/C++

Задача на C++, помогите решить.

Условие
Дано целое число z и дробь x/y. Найдите остаток z при делении на x/y.

Напомним, что остатком при делении z на xy называется такое неотрицательное число r, что r<x/y и z=x/y⋅t+r, где t — целое частное.

При решении задачи необходимо перегрузить операцию взятия остатка для целого числа и класса Fraction.

Формат входных данных
Во первой строке вводится целое число z (0<∣∣z∣∣≤109).

Во второй строке даются два целых числа x и y, разделенные символом «/» (0≤x,y≤109, y≠0).

Формат выходных данных
Выведите числитель и знаменатель дроби-остатка через символ «/». Дробь сокращать не обязательно (но можно). Числитель и знаменатель дроби не должны превышать 2⋅1018 по абсолютному значению.
-------------------------------------------------------------------------------------------------------------------
Примеры:
входные данныевыходные данные
17
2/3 | 1/3
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Frac {
public:
using vtype = long long;
Frac() : val({0, 1}) {}
Frac(const string& str) {
val = split(str, '/');
normalize();
}
string value()const { return to_string(val.first) + '/' + to_string(val.second); }
Frac absolute()const { return Frac(abs(val.first), val.second); }
Frac& operator-() {
val.first *= -1;
return *this;
}
private:
Frac(vtype a, vtype b) : val{ a, b } { normalize(); }
pair val;
void normalize() {
auto x = gcd(val);
if (x) {
val.first /= x;
val.second /= x;
if (val.second < 0) {
val.first *= -1;
val.second *= -1;
}
}
}
vtype gcd(pair v) {
v.first = abs(v.first);
if (v.first) {
v.second = abs(v.second);
while (v.first != v.second) {
if (v.first > v.second) swap(v.first, v.second);
v.second -= v.first;
}
}
return v.first;
}
pair split(const string& str, char delim) {
stringstream ss(str);
string token;
pair box;
getline(ss, token, delim);
box.first = stoll(token);
getline(ss, token);
box.second = stoll(token);
return box;
}
friend Frac operator-(const Frac& a, const Frac& b) {
const auto an = a.val.first * b.val.second;
const auto bn = b.val.first * a.val.second;
const auto abn = an - bn;
const auto abd = a.val.second * b.val.second;
return Frac(to_string(abn) + '/' + to_string(abd));
}
friend bool operator<(const Frac& a, const Frac& b) { return double(a.val.first) / a.val.second < double(b.val.first) / b.val.second; }
friend bool operator<(const Frac& a, const double b) { return double(a.val.first) / a.val.second < b; }
friend bool operator<(const double a, const Frac& b) { return a < double(b.val.first) / b.val.second; }
friend bool operator==(const Frac& a, const Frac& b) { return a.val.first == b.val.first && a.val.second == b.val.second; }
friend bool operator==(const Frac& a, const double b) { return double(a.val.first) / a.val.second == b; }
friend bool operator>(const Frac& a, const Frac& b) { return double(a.val.first) / a.val.second > double(b.val.first) / b.val.second; }
friend bool operator>(const Frac& a, const double b) { return double(a.val.first) / a.val.second > b; }
friend Frac operator%(const Frac& a, const Frac& b) {
auto xa = a.absolute();
auto xb = b.absolute();
if (xa < xb) return a;
auto x = xa - xb;
if (x == 0) return Frac(0, 1);
while (x > xb) x = x - xb;
return a.val.first > 0? x : -x;
}
friend Frac operator%(const vtype a, const Frac& b) {
Frac x(a, 1);
return operator%(x, b);
}
friend Frac operator*(const Frac& a, const vtype b) { return Frac(a.val.first * b, a.val.second); }
friend Frac operator*(const vtype a, const Frac& b) { return Frac(b.val.first * a, b.val.second); }
friend Frac operator*(const Frac& a, const Frac& b) { return Frac(a.val.first * b.val.first, a.val.second * b.val.second); }
friend Frac operator/(const Frac& a, const Frac& b) { return Frac(a.val.first * b.val.second, a.val.second * b.val.first); }
friend Frac operator+(const Frac& a, const Frac& b) {
auto d = a.val.second * b.val.second;
auto n = a.val.first * b.val.second + b.val.first * a.val.second;
return Frac(n, d);
}
friend Frac operator+(const Frac& a, const vtype b) {
auto d = a.val.second;
auto n = a.val.first + b * a.val.second;
return Frac(n, d);
}
friend Frac operator+(const vtype b, const Frac& a) {
auto d = a.val.second;
auto n = a.val.first + b * a.val.second;
return Frac(n, d);
}
friend ostream& operator<<(ostream& out, const Frac& fr) { return out << fr.val.first << '/' << fr.val.second; }
friend istream& operator>>(istream& inp, Frac& fr) {
string str;
inp >> str;
fr.val = fr.split(str, '/');
fr.normalize();
return inp;
}
};
Светослав Тодоров
Светослав Тодоров
51 511
Лучший ответ
Светослав Тодоров Весь код уже не помещается, поэтому main в комментариях

int main() {
int a;
Frac b;
cin >> a >> b;
cout << a % b << '\n';
system("pause > nul");
}