Advertisement
Lesnic

Untitled

Mar 7th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <typeinfo>
  5.  
  6. using namespace std;
  7.  
  8. class Fraction {
  9. private:
  10.     int sign, full, up, down;
  11.  
  12.     void check();
  13.     void redaction();
  14.  
  15. public:
  16.     Fraction();
  17.     Fraction(std::string line);
  18.     Fraction(int);
  19.     Fraction(double);
  20.     ~Fraction() {}
  21.  
  22.     Fraction operator + (Fraction);
  23.     Fraction operator * (Fraction);
  24.    
  25.     Fraction operator = (Fraction);
  26.  
  27.     friend istream& operator>> (istream&, Fraction&);
  28.     friend ostream& operator<< (ostream&, const Fraction&);
  29. };
  30.  
  31. void Fraction::check() {
  32.     if (up == 0)
  33.         down = 1;
  34.     down += down == 0;
  35.     up = abs(up);
  36.     down = abs(down);
  37. }
  38. void Fraction::redaction() {
  39.     check();
  40.     full += up / down;
  41.     up %= down;
  42.     int a = up, b = down;
  43.     while (a != 0 && b != 0)
  44.         if (a > b)
  45.             a %= b;
  46.         else
  47.             b %= a;
  48.     up /= a + b;
  49.     down /= a + b;
  50. }
  51.  
  52.  
  53. Fraction::Fraction() {
  54.     sign = 1;
  55.     full = 0;
  56.     up = 0;
  57.     down = 1;
  58. }
  59. Fraction::Fraction(string line) : full(0), up(0), down(0), sign(1) {
  60.     int i = 0, now = 0;
  61.     if (line[i] == '-'){
  62.         i++;
  63.         sign = 0;
  64.     }
  65.  
  66.  
  67.     if (line.find("/") != string::npos) {
  68.         for ( ; i < (int)line.size(); i++) {
  69.             if (line[i] >= '0' && line[i] <= '9') {
  70.                 now = 10 * now + line[i] - '0';
  71.             } else {
  72.                 if (line[i] == '.')
  73.                     full = now;
  74.                 else if (line[i] == '/')
  75.                     up = now;
  76.                 now = 0;
  77.             }
  78.         }
  79.         down = now;
  80.     } else {
  81.         down = 1;
  82.         while (line[i] != '.' && i < (int)line.size())
  83.         {
  84.             full = 10 * full + line[i] - '0';
  85.             i++;
  86.         }
  87.  
  88.         i++;
  89.         while (i < (int)line.size())
  90.         {
  91.             up = 10 * up + line[i] - '0';
  92.             i++;
  93.             down *= 10;
  94.         }
  95.     }
  96.     redaction();
  97. }
  98. Fraction::Fraction(double number) {
  99.     Fraction a(to_string(number));
  100.     *this = a;
  101. }
  102. Fraction::Fraction(int number) {
  103.     this->full = abs(number);
  104.     this->sign = number > 0 ? 1 : 0;
  105.     this->up = 0;
  106.     this->down = 1;
  107.     redaction();
  108. }
  109.  
  110. Fraction Fraction::operator+(Fraction arg) {
  111.     Fraction now = *this;
  112.     now.up = (-1 + now.sign * 2) * (now.full * now.down + now.up) * arg.down +
  113.         (-1 + arg.sign * 2) * (arg.full * arg.down + arg.up) * now.down;
  114.     now.down *= arg.down;
  115.     now.full = 0;
  116.     now.sign = (now.up >= 0) ? 1 : 0;
  117.     now.up = abs(now.up);
  118.     now.redaction();
  119.     return now;
  120. }
  121.  
  122.  
  123. Fraction Fraction::operator*(Fraction arg) {
  124.     Fraction now = *this;
  125.     now.sign = now.sign == arg.sign;
  126.     now.up = (now.full * now.down + now.up) * (arg.full * arg.down + arg.up);
  127.     now.down *= arg.down;
  128.     now.full = 0;
  129.     now.redaction();
  130.     return now;
  131. }
  132.  
  133.  
  134. Fraction Fraction::operator=(Fraction n) {
  135.     this->full = n.full;
  136.     this->sign = n.sign;
  137.     this->up = n.up;
  138.     this->down = n.down;
  139.     redaction();
  140.     return *this;
  141. }
  142.  
  143.  
  144. ostream& operator<< (ostream& out, const Fraction& point) {
  145.     if (point.full == 0 && point.up == 0)
  146.         cout << 0;
  147.     else
  148.     {
  149.         if (point.sign == 0)
  150.             cout << '-';
  151.         if (point.full != 0) {
  152.             cout << point.full;
  153.             if (point.up != 0)
  154.                 cout << ".";
  155.         }
  156.         if (point.up != 0)
  157.             cout << point.up << '/' << point.down;
  158.     }
  159.     return out;
  160. }
  161.  
  162. istream& operator>> (istream& cin, Fraction& point) {
  163.     string s;
  164.     cin >> s;
  165. /*  int n;
  166.     cin >> n;
  167.     s = to_string(n);
  168.     char now = getchar();
  169.     while (now != '\n' && now != ' ') {
  170.         s += now;
  171.         now = getchar();
  172.     }*/
  173.     point = s;
  174.     return cin;
  175. }
  176.  
  177. template <typename T>
  178. Fraction operator+(Fraction arg1, T arg2) { return arg1 + Fraction(arg2); }
  179. template <typename T>
  180. Fraction operator+(T arg1, Fraction arg2) { return Fraction(arg1) + arg2; }
  181.  
  182. template <typename T>
  183. Fraction operator*(Fraction arg1, T arg2) { return arg1 * Fraction(arg2); }
  184. template <typename T>
  185. Fraction operator*(T arg1, Fraction arg2) { return Fraction(arg1) * arg2; }
  186.  
  187. template < typename T>
  188. class Matrix
  189. {
  190. private:
  191.     int row, col;
  192.     T** matrix;
  193.  
  194. public:
  195.     int getRow() { return row; }
  196.     int getCol() { return col; }
  197.     T getElement(int i, int j) { return matrix[i][j]; }
  198.     void setElement(T el, int i, int j, bool p = false) {
  199.         if (p)
  200.             matrix[i][j] = el;
  201.         else
  202.             matrix[i][j] = matrix[i][j] + el;
  203.     }
  204.     Matrix(int row, int col) {
  205.         (*this).row = row;
  206.         (*this).col = col;
  207.         matrix = new T* [row];
  208.         for (int i = 0; i < row; i++)
  209.             matrix[i] = new T[col];
  210.     }
  211.    
  212.     Matrix& operator=(const Matrix<T>& right) {
  213.         Matrix<T> res(right.row, right.col);
  214.         for (int i = 0; i < res.row; i++)
  215.             for (int j = 0; j < res.col; j++)
  216.                 res.matrix[i][j] = right.matrix[i][j];
  217.         (*this).matrix = res.matrix;
  218.         (*this).row = res.row;
  219.         (*this).col = res.col;
  220.         return *this;
  221.     }
  222.  
  223.     friend ostream& operator<<(ostream& cout, Matrix<T> matrix) {
  224.         for (int i = 0; i < matrix.row; i++) {
  225.             for (int j = 0; j < matrix.col; j++) {
  226.                 if (typeid(T) == typeid(double))
  227.                     cout << fixed << setprecision(2);
  228.                 cout << matrix.matrix[i][j];
  229.                 if (j != matrix.col - 1)
  230.                     cout << " ";
  231.             }
  232.             cout << endl;
  233.         }
  234.         return cout;
  235.     }
  236.     friend istream& operator>>(istream& cin, Matrix<T> matrix) {
  237.         for (int i = 0; i < matrix.row; i++)
  238.             for (int j = 0; j < matrix.col; j++)
  239.                 cin >> matrix.matrix[i][j];
  240.         return cin;
  241.     }
  242. };
  243.  
  244. template <typename T>
  245. Matrix<T> operator*(Matrix<T> a, Matrix<T> b) {
  246.     Matrix<T> res(a.getRow(), b.getCol());
  247.     for (int i = 0; i < res.getRow(); i++) {
  248.         for (int j = 0; j < res.getCol(); j++) {
  249.             res.setElement(0, i, j, true);
  250.             for (int k = 0; k < a.getCol(); k++)
  251.                 res.setElement(a.getElement(i, k) * b.getElement(k, j), i, j);
  252.         }
  253.     }
  254.     return res;
  255. }
  256. template <typename T>
  257. Matrix<T> operator+(Matrix<T> a, Matrix<T>& b) {
  258.     Matrix<T> res(a.getRow(), b.getCol());
  259.     for (int i = 0; i < a.getRow(); i++)
  260.         for (int j = 0; j < a.getCol(); j++)
  261.             res.setElement(a.getElement(i, j) + b.getElement(i, j), i, j, true);
  262.     return res;
  263. }
  264.  
  265. int main() {
  266.     int n, m;
  267.     cin >> n >> m;
  268.     Matrix<double> a(n, m);
  269.     cin >> a;
  270.     cin >> n >> m;
  271.     Matrix<double> b(n, m);
  272.     cin >> b;
  273.     cout << a + b;
  274.  
  275.     cin >> n >> m;
  276.     Matrix<Fraction> c(n, m);
  277.     cin >> c;
  278.     cin >> n >> m;
  279.     Matrix<Fraction> d(n, m);
  280.     cin >> d;
  281.     cout << c * d;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement