Advertisement
Lesnic

Untitled

Mar 7th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.93 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. class Fraction {
  6. private:
  7.     int sign, full, up, down;
  8.  
  9.     void check();
  10.     void redaction();
  11.  
  12. public:
  13.     Fraction();
  14.     Fraction(std::string line);
  15.     Fraction(int);
  16.     Fraction(double);
  17.     ~Fraction() {}
  18.  
  19.     operator int();
  20.     operator double();
  21.  
  22.     Fraction operator + (Fraction);
  23.     Fraction operator - (Fraction);
  24.     Fraction operator - ();
  25.     Fraction operator * (Fraction);
  26.     Fraction operator / (Fraction);
  27.  
  28.     bool operator > (Fraction);
  29.     bool operator < (Fraction);
  30.     bool operator == (Fraction);
  31.  
  32.     Fraction operator = (Fraction);
  33.  
  34.     friend std::ostream& operator<< (std::ostream& out, const Fraction& point);
  35.     friend std::istream& operator>> (std::istream& in, const Fraction& point);
  36. };
  37.  
  38. Fraction case1() {
  39.     int n;
  40.     std::cin >> n;
  41.     return (Fraction)n;
  42. }
  43.  
  44. int case2() {
  45.     std::string s;
  46.     getline(std::cin, s);
  47.     getline(std::cin, s);
  48.     Fraction n(s);
  49.     return (int)n;
  50. }
  51.  
  52. Fraction case3() {
  53.     double n;
  54.     std::cin >> n;
  55.     return (Fraction)n;
  56. }
  57.  
  58. double case4() {
  59.     std::string s;
  60.     getline(std::cin, s);
  61.     getline(std::cin, s);
  62.     Fraction n(s);
  63.     return (double)n;
  64. }
  65.  
  66. Fraction case5() {
  67.     std::string s, g;
  68.     getline(std::cin, s); getline(std::cin, s);
  69.     getline(std::cin, g);
  70.     Fraction a(s), b(g);
  71.     return a + b;
  72. }
  73.  
  74. Fraction case6() {
  75.     std::string s, g;
  76.     getline(std::cin, s); getline(std::cin, s);
  77.     getline(std::cin, g);
  78.     Fraction a(s), b(g);
  79.     return a - b;
  80. }
  81.  
  82. Fraction case7() {
  83.     std::string s, g;
  84.     getline(std::cin, s); getline(std::cin, s);
  85.     getline(std::cin, g);
  86.     Fraction a(s), b(g);
  87.     return a * b;
  88. }
  89.  
  90. Fraction case8() {
  91.     std::string s, g;
  92.     getline(std::cin, s); getline(std::cin, s);
  93.     getline(std::cin, g);
  94.     Fraction a(s), b(g);
  95.     return a / b;
  96. }
  97.  
  98. Fraction case9() {
  99.     std::string s;
  100.     getline(std::cin, s); getline(std::cin, s);
  101.     Fraction a(s);
  102.     return -a;
  103. }
  104.  
  105. void case10() {
  106.     std::string s, g;
  107.     getline(std::cin, s); getline(std::cin, s);
  108.     getline(std::cin, g);
  109.     Fraction a(s), b(g);
  110.     std::cout << a;
  111.     if (a > b)
  112.         std::cout << " is greater than ";
  113.     else if (a < b)
  114.         std::cout << " is less than ";
  115.     else
  116.         std::cout << " is equal to ";
  117.     std::cout << b << std::endl;
  118. }
  119.  
  120. int main() {
  121.     int n, c2;
  122.     double c4;
  123.     Fraction res;
  124.  
  125.     Fraction(*case_X[7]) () = { case1, case3, case5, case6, case7, case8, case9 };
  126.     std::cin >> n;
  127.     while (n != 0) {
  128.         if (n == 2) {
  129.             c2 = case2();
  130.             std::cout << c2;
  131.         }
  132.         else if (n == 4) {
  133.             c4 = case4();
  134.             std::cout << std::fixed << std::setprecision(2) << c4;
  135.         } else if (n == 10)
  136.             case10();
  137.         else {
  138.             res = case_X[n - (n > 2) - (n > 4) - 1]();
  139.             std::cout << res << std::endl;
  140.         }
  141.  
  142.         std::cin >> n;
  143.     }
  144. }
  145.  
  146. void Fraction::check() {
  147.     if (up == 0)
  148.         down = 1;
  149.     down += down == 0;
  150.     up = abs(up);
  151.     down = abs(down);
  152. }
  153. void Fraction::redaction() {
  154.     check();
  155.     full += up / down;
  156.     up %= down;
  157.     int a = up, b = down;
  158.     while (a != 0 && b != 0)
  159.         if (a > b)
  160.             a %= b;
  161.         else
  162.             b %= a;
  163.     up /= a + b;
  164.     down /= a + b;
  165. }
  166.  
  167.  
  168. Fraction::Fraction() {
  169.     sign = 1;
  170.     full = 0;
  171.     up = 0;
  172.     down = 1;
  173. }
  174. Fraction::Fraction(std::string line) {
  175.     full = 0;
  176.     up = 0;
  177.     down = 1;
  178.     sign = 1;
  179.  
  180.     if (line.find(".") != std::string::npos)
  181.     {
  182.         int i = 0;
  183.         if (line[i] == '-')
  184.         {
  185.             i++;
  186.             sign = 0;
  187.         }
  188.  
  189.         while (line[i] != '.')
  190.         {
  191.             full = 10 * full + line[i] - '0';
  192.             i++;
  193.         }
  194.  
  195.         i++;
  196.         while (i < (int)line.size())
  197.         {
  198.             up = 10 * up + line[i] - '0';
  199.             i++;
  200.             down *= 10;
  201.         }
  202.     }
  203.     else
  204.     {
  205.         int i = 0, now = 0;
  206.         if (line[i] == '-')
  207.         {
  208.             i++;
  209.             sign = 0;
  210.         }
  211.  
  212.         bool p = false;
  213.  
  214.         while (i < (int)line.size())
  215.         {
  216.             if (line[i] >= '0' && line[i] <= '9')
  217.             {
  218.                 now = now * 10 + line[i] - '0';
  219.             }
  220.             else
  221.             {
  222.                 if (line[i] == ' ')
  223.                     full = now;
  224.                 else if (line[i] == '/')
  225.                 {
  226.                     p = true;
  227.                     up = now;
  228.                 }
  229.                 now = 0;
  230.             }
  231.             i++;
  232.         }
  233.  
  234.         p == true ? down = now : full = now;
  235.     }
  236.     redaction();
  237. }
  238. Fraction::Fraction(double number) {
  239.     Fraction a(std::to_string(number));
  240.     *this = a;
  241. }
  242. Fraction::Fraction(int number) {
  243.     this->full = abs(number);
  244.     this->sign = number > 0 ? 1 : 0;
  245.     this->up = 0;
  246.     this->down = 1;
  247.     redaction();
  248. }
  249.  
  250.  
  251. Fraction::operator int() {
  252.     return full * ((sign == -1) ? -1 : 1);
  253. }
  254. Fraction::operator double() {
  255.     return (sign * 2 - 1) * (full + (double)up / down);
  256. }
  257.  
  258.  
  259. Fraction Fraction::operator+(Fraction arg) {
  260.     Fraction now = *this;
  261.     now.up = (-1 + now.sign * 2) * (now.full * now.down + now.up) * arg.down +
  262.         (-1 + arg.sign * 2) * (arg.full * arg.down + arg.up) * now.down;
  263.     now.down *= arg.down;
  264.     now.full = 0;
  265.     now.sign = (now.up >= 0) ? 1 : 0;
  266.     now.up = abs(now.up);
  267.     now.redaction();
  268.     return now;
  269. }
  270.  
  271.  
  272. Fraction Fraction::operator-(Fraction arg) {
  273.     Fraction r(arg);
  274.     r.sign = 1 - r.sign;
  275.     return *this + r;
  276. }
  277.  
  278.  
  279. Fraction Fraction::operator-() {
  280.     sign = 1 - sign;
  281.     return *this;
  282. }
  283.  
  284.  
  285. Fraction Fraction::operator*(Fraction arg) {
  286.     Fraction now = *this;
  287.     now.sign = now.sign == arg.sign;
  288.     now.up = (now.full * now.down + now.up) * (arg.full * arg.down + arg.up);
  289.     now.down *= arg.down;
  290.     now.full = 0;
  291.     now.redaction();
  292.     return now;
  293. }
  294.  
  295.  
  296. Fraction Fraction::operator/(Fraction arg) {
  297.     Fraction now = *this;
  298.     now.sign = now.sign == arg.sign;
  299.     now.up = (now.full * now.down + now.up) * arg.down;
  300.     now.down *= (arg.full * arg.down + arg.up);
  301.     now.full = 0;
  302.     now.redaction();
  303.     return now;
  304. }
  305.  
  306.  
  307. bool Fraction::operator<(Fraction arg) {
  308.     return sign * (full * down + up) * arg.down < arg.sign * (arg.full * arg.down + arg.up) * down;
  309. }
  310.  
  311. bool Fraction::operator>(Fraction arg) {
  312.     return sign * (full * down + up) * arg.down > arg.sign* (arg.full * arg.down + arg.up)* down;
  313. }
  314.  
  315. bool Fraction::operator==(Fraction arg) {
  316.     return sign * (full * down + up) * arg.down > arg.sign* (arg.full * arg.down + arg.up)* down;
  317. }
  318.  
  319.  
  320. Fraction Fraction::operator=(Fraction n) {
  321.     this->full = n.full;
  322.     this->sign = n.sign;
  323.     this->up = n.up;
  324.     this->down = n.down;
  325.     redaction();
  326.     return *this;
  327. }
  328.  
  329.  
  330. std::ostream& operator<< (std::ostream& out, const Fraction& point) {
  331.     if (point.full == 0 && point.up == 0)
  332.         std::cout << 0;
  333.     else
  334.     {
  335.         if (point.sign == 0)
  336.             std::cout << '-';
  337.         if (point.full != 0) {
  338.             std::cout << point.full;
  339.             if (point.up != 0)
  340.                 std::cout << " ";
  341.         }
  342.         if (point.up != 0)
  343.             std::cout << point.up << '/' << point.down;
  344.     }
  345.     return out;
  346. }
  347.  
  348. /*istream& operator>> (istream& cin, Fraction& point) {
  349.     string s;
  350.     getline(cin, s);
  351.     point = s;
  352.     return cin;
  353. }*/
  354.  
  355.  
  356. Fraction operator+(Fraction arg1, double arg2) { return arg1 + Fraction(arg2); }
  357. Fraction operator+(Fraction arg1, int arg2) { return arg1 + Fraction(arg2); }
  358. Fraction operator+(double arg1, Fraction arg2) { return Fraction(arg1) + arg2; }
  359. Fraction operator+(int arg1, Fraction arg2) { return Fraction(arg1) + arg2; }
  360.  
  361. Fraction operator-(Fraction arg1, double arg2) { return arg1 - Fraction(arg2); }
  362. Fraction operator-(Fraction arg1, int arg2) { return arg1 - Fraction(arg2); }
  363. Fraction operator-(double arg1, Fraction arg2) { return Fraction(arg1) - arg2; }
  364. Fraction operator-(int arg1, Fraction arg2) { return Fraction(arg1) - arg2; }
  365.  
  366. Fraction operator*(Fraction arg1, double arg2) { return arg1 * Fraction(arg2); }
  367. Fraction operator*(Fraction arg1, int arg2) { return arg1 * Fraction(arg2); }
  368. Fraction operator*(double arg1, Fraction arg2) { return Fraction(arg1) * arg2; }
  369. Fraction operator*(int arg1, Fraction arg2) { return Fraction(arg1) * arg2; }
  370.  
  371. Fraction operator/(Fraction arg1, double arg2) { return arg1 / Fraction(arg2); }
  372. Fraction operator/(Fraction arg1, int arg2) { return arg1 / Fraction(arg2); }
  373. Fraction operator/(double arg1, Fraction arg2) { return Fraction(arg1) / arg2; }
  374. Fraction operator/(int arg1, Fraction arg2) { return Fraction(arg1) / arg2; }
  375.  
  376. bool operator!=(Fraction arg1, Fraction arg2) { return !(arg1 == arg2); }
  377. bool operator<=(Fraction arg1, Fraction arg2) { return arg1 == arg2 || arg1 < arg2; }
  378. bool operator>=(Fraction arg1, Fraction arg2) { return arg1 == arg2 || arg1 > arg2; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement