Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <iomanip>
- class Fraction {
- private:
- int sign, full, up, down;
- void check();
- void redaction();
- public:
- Fraction();
- Fraction(std::string line);
- Fraction(int);
- Fraction(double);
- ~Fraction() {}
- operator int();
- operator double();
- Fraction operator + (Fraction);
- Fraction operator - (Fraction);
- Fraction operator - ();
- Fraction operator * (Fraction);
- Fraction operator / (Fraction);
- bool operator > (Fraction);
- bool operator < (Fraction);
- bool operator == (Fraction);
- Fraction operator = (Fraction);
- friend std::ostream& operator<< (std::ostream& out, const Fraction& point);
- friend std::istream& operator>> (std::istream& in, const Fraction& point);
- };
- Fraction case1() {
- int n;
- std::cin >> n;
- return (Fraction)n;
- }
- int case2() {
- std::string s;
- getline(std::cin, s);
- getline(std::cin, s);
- Fraction n(s);
- return (int)n;
- }
- Fraction case3() {
- double n;
- std::cin >> n;
- return (Fraction)n;
- }
- double case4() {
- std::string s;
- getline(std::cin, s);
- getline(std::cin, s);
- Fraction n(s);
- return (double)n;
- }
- Fraction case5() {
- std::string s, g;
- getline(std::cin, s); getline(std::cin, s);
- getline(std::cin, g);
- Fraction a(s), b(g);
- return a + b;
- }
- Fraction case6() {
- std::string s, g;
- getline(std::cin, s); getline(std::cin, s);
- getline(std::cin, g);
- Fraction a(s), b(g);
- return a - b;
- }
- Fraction case7() {
- std::string s, g;
- getline(std::cin, s); getline(std::cin, s);
- getline(std::cin, g);
- Fraction a(s), b(g);
- return a * b;
- }
- Fraction case8() {
- std::string s, g;
- getline(std::cin, s); getline(std::cin, s);
- getline(std::cin, g);
- Fraction a(s), b(g);
- return a / b;
- }
- Fraction case9() {
- std::string s;
- getline(std::cin, s); getline(std::cin, s);
- Fraction a(s);
- return -a;
- }
- void case10() {
- std::string s, g;
- getline(std::cin, s); getline(std::cin, s);
- getline(std::cin, g);
- Fraction a(s), b(g);
- std::cout << a;
- if (a > b)
- std::cout << " is greater than ";
- else if (a < b)
- std::cout << " is less than ";
- else
- std::cout << " is equal to ";
- std::cout << b << std::endl;
- }
- int main() {
- int n, c2;
- double c4;
- Fraction res;
- Fraction(*case_X[7]) () = { case1, case3, case5, case6, case7, case8, case9 };
- std::cin >> n;
- while (n != 0) {
- if (n == 2) {
- c2 = case2();
- std::cout << c2;
- }
- else if (n == 4) {
- c4 = case4();
- std::cout << std::fixed << std::setprecision(2) << c4;
- } else if (n == 10)
- case10();
- else {
- res = case_X[n - (n > 2) - (n > 4) - 1]();
- std::cout << res << std::endl;
- }
- std::cin >> n;
- }
- }
- void Fraction::check() {
- if (up == 0)
- down = 1;
- down += down == 0;
- up = abs(up);
- down = abs(down);
- }
- void Fraction::redaction() {
- check();
- full += up / down;
- up %= down;
- int a = up, b = down;
- while (a != 0 && b != 0)
- if (a > b)
- a %= b;
- else
- b %= a;
- up /= a + b;
- down /= a + b;
- }
- Fraction::Fraction() {
- sign = 1;
- full = 0;
- up = 0;
- down = 1;
- }
- Fraction::Fraction(std::string line) {
- full = 0;
- up = 0;
- down = 1;
- sign = 1;
- if (line.find(".") != std::string::npos)
- {
- int i = 0;
- if (line[i] == '-')
- {
- i++;
- sign = 0;
- }
- while (line[i] != '.')
- {
- full = 10 * full + line[i] - '0';
- i++;
- }
- i++;
- while (i < (int)line.size())
- {
- up = 10 * up + line[i] - '0';
- i++;
- down *= 10;
- }
- }
- else
- {
- int i = 0, now = 0;
- if (line[i] == '-')
- {
- i++;
- sign = 0;
- }
- bool p = false;
- while (i < (int)line.size())
- {
- if (line[i] >= '0' && line[i] <= '9')
- {
- now = now * 10 + line[i] - '0';
- }
- else
- {
- if (line[i] == ' ')
- full = now;
- else if (line[i] == '/')
- {
- p = true;
- up = now;
- }
- now = 0;
- }
- i++;
- }
- p == true ? down = now : full = now;
- }
- redaction();
- }
- Fraction::Fraction(double number) {
- Fraction a(std::to_string(number));
- *this = a;
- }
- Fraction::Fraction(int number) {
- this->full = abs(number);
- this->sign = number > 0 ? 1 : 0;
- this->up = 0;
- this->down = 1;
- redaction();
- }
- Fraction::operator int() {
- return full * ((sign == -1) ? -1 : 1);
- }
- Fraction::operator double() {
- return (sign * 2 - 1) * (full + (double)up / down);
- }
- Fraction Fraction::operator+(Fraction arg) {
- Fraction now = *this;
- now.up = (-1 + now.sign * 2) * (now.full * now.down + now.up) * arg.down +
- (-1 + arg.sign * 2) * (arg.full * arg.down + arg.up) * now.down;
- now.down *= arg.down;
- now.full = 0;
- now.sign = (now.up >= 0) ? 1 : 0;
- now.up = abs(now.up);
- now.redaction();
- return now;
- }
- Fraction Fraction::operator-(Fraction arg) {
- Fraction r(arg);
- r.sign = 1 - r.sign;
- return *this + r;
- }
- Fraction Fraction::operator-() {
- sign = 1 - sign;
- return *this;
- }
- Fraction Fraction::operator*(Fraction arg) {
- Fraction now = *this;
- now.sign = now.sign == arg.sign;
- now.up = (now.full * now.down + now.up) * (arg.full * arg.down + arg.up);
- now.down *= arg.down;
- now.full = 0;
- now.redaction();
- return now;
- }
- Fraction Fraction::operator/(Fraction arg) {
- Fraction now = *this;
- now.sign = now.sign == arg.sign;
- now.up = (now.full * now.down + now.up) * arg.down;
- now.down *= (arg.full * arg.down + arg.up);
- now.full = 0;
- now.redaction();
- return now;
- }
- bool Fraction::operator<(Fraction arg) {
- return sign * (full * down + up) * arg.down < arg.sign * (arg.full * arg.down + arg.up) * down;
- }
- bool Fraction::operator>(Fraction arg) {
- return sign * (full * down + up) * arg.down > arg.sign* (arg.full * arg.down + arg.up)* down;
- }
- bool Fraction::operator==(Fraction arg) {
- return sign * (full * down + up) * arg.down > arg.sign* (arg.full * arg.down + arg.up)* down;
- }
- Fraction Fraction::operator=(Fraction n) {
- this->full = n.full;
- this->sign = n.sign;
- this->up = n.up;
- this->down = n.down;
- redaction();
- return *this;
- }
- std::ostream& operator<< (std::ostream& out, const Fraction& point) {
- if (point.full == 0 && point.up == 0)
- std::cout << 0;
- else
- {
- if (point.sign == 0)
- std::cout << '-';
- if (point.full != 0) {
- std::cout << point.full;
- if (point.up != 0)
- std::cout << " ";
- }
- if (point.up != 0)
- std::cout << point.up << '/' << point.down;
- }
- return out;
- }
- /*istream& operator>> (istream& cin, Fraction& point) {
- string s;
- getline(cin, s);
- point = s;
- return cin;
- }*/
- Fraction operator+(Fraction arg1, double arg2) { return arg1 + Fraction(arg2); }
- Fraction operator+(Fraction arg1, int arg2) { return arg1 + Fraction(arg2); }
- Fraction operator+(double arg1, Fraction arg2) { return Fraction(arg1) + arg2; }
- Fraction operator+(int arg1, Fraction arg2) { return Fraction(arg1) + arg2; }
- Fraction operator-(Fraction arg1, double arg2) { return arg1 - Fraction(arg2); }
- Fraction operator-(Fraction arg1, int arg2) { return arg1 - Fraction(arg2); }
- Fraction operator-(double arg1, Fraction arg2) { return Fraction(arg1) - arg2; }
- Fraction operator-(int arg1, Fraction arg2) { return Fraction(arg1) - arg2; }
- Fraction operator*(Fraction arg1, double arg2) { return arg1 * Fraction(arg2); }
- Fraction operator*(Fraction arg1, int arg2) { return arg1 * Fraction(arg2); }
- Fraction operator*(double arg1, Fraction arg2) { return Fraction(arg1) * arg2; }
- Fraction operator*(int arg1, Fraction arg2) { return Fraction(arg1) * arg2; }
- Fraction operator/(Fraction arg1, double arg2) { return arg1 / Fraction(arg2); }
- Fraction operator/(Fraction arg1, int arg2) { return arg1 / Fraction(arg2); }
- Fraction operator/(double arg1, Fraction arg2) { return Fraction(arg1) / arg2; }
- Fraction operator/(int arg1, Fraction arg2) { return Fraction(arg1) / arg2; }
- bool operator!=(Fraction arg1, Fraction arg2) { return !(arg1 == arg2); }
- bool operator<=(Fraction arg1, Fraction arg2) { return arg1 == arg2 || arg1 < arg2; }
- bool operator>=(Fraction arg1, Fraction arg2) { return arg1 == arg2 || arg1 > arg2; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement