Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <typeinfo>
- using namespace std;
- class Fraction {
- private:
- int sign, full, up, down;
- void check();
- void redaction();
- public:
- Fraction();
- Fraction(std::string line);
- Fraction(int);
- Fraction(double);
- ~Fraction() {}
- Fraction operator + (Fraction);
- Fraction operator * (Fraction);
- Fraction operator = (Fraction);
- friend istream& operator>> (istream&, Fraction&);
- friend ostream& operator<< (ostream&, const Fraction&);
- };
- 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(string line) : full(0), up(0), down(0), sign(1) {
- int i = 0, now = 0;
- if (line[i] == '-'){
- i++;
- sign = 0;
- }
- if (line.find("/") != string::npos) {
- for ( ; i < (int)line.size(); i++) {
- if (line[i] >= '0' && line[i] <= '9') {
- now = 10 * now + line[i] - '0';
- } else {
- if (line[i] == '.')
- full = now;
- else if (line[i] == '/')
- up = now;
- now = 0;
- }
- }
- down = now;
- } else {
- down = 1;
- while (line[i] != '.' && i < (int)line.size())
- {
- full = 10 * full + line[i] - '0';
- i++;
- }
- i++;
- while (i < (int)line.size())
- {
- up = 10 * up + line[i] - '0';
- i++;
- down *= 10;
- }
- }
- redaction();
- }
- Fraction::Fraction(double number) {
- Fraction a(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 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 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 n) {
- this->full = n.full;
- this->sign = n.sign;
- this->up = n.up;
- this->down = n.down;
- redaction();
- return *this;
- }
- ostream& operator<< (ostream& out, const Fraction& point) {
- if (point.full == 0 && point.up == 0)
- cout << 0;
- else
- {
- if (point.sign == 0)
- cout << '-';
- if (point.full != 0) {
- cout << point.full;
- if (point.up != 0)
- cout << ".";
- }
- if (point.up != 0)
- cout << point.up << '/' << point.down;
- }
- return out;
- }
- istream& operator>> (istream& cin, Fraction& point) {
- string s;
- cin >> s;
- /* int n;
- cin >> n;
- s = to_string(n);
- char now = getchar();
- while (now != '\n' && now != ' ') {
- s += now;
- now = getchar();
- }*/
- point = s;
- return cin;
- }
- template <typename T>
- Fraction operator+(Fraction arg1, T arg2) { return arg1 + Fraction(arg2); }
- template <typename T>
- Fraction operator+(T arg1, Fraction arg2) { return Fraction(arg1) + arg2; }
- template <typename T>
- Fraction operator*(Fraction arg1, T arg2) { return arg1 * Fraction(arg2); }
- template <typename T>
- Fraction operator*(T arg1, Fraction arg2) { return Fraction(arg1) * arg2; }
- template < typename T>
- class Matrix
- {
- private:
- int row, col;
- T** matrix;
- public:
- int getRow() { return row; }
- int getCol() { return col; }
- T getElement(int i, int j) { return matrix[i][j]; }
- void setElement(T el, int i, int j, bool p = false) {
- if (p)
- matrix[i][j] = el;
- else
- matrix[i][j] = matrix[i][j] + el;
- }
- Matrix(int row, int col) {
- (*this).row = row;
- (*this).col = col;
- matrix = new T* [row];
- for (int i = 0; i < row; i++)
- matrix[i] = new T[col];
- }
- Matrix& operator=(const Matrix<T>& right) {
- Matrix<T> res(right.row, right.col);
- for (int i = 0; i < res.row; i++)
- for (int j = 0; j < res.col; j++)
- res.matrix[i][j] = right.matrix[i][j];
- (*this).matrix = res.matrix;
- (*this).row = res.row;
- (*this).col = res.col;
- return *this;
- }
- friend ostream& operator<<(ostream& cout, Matrix<T> matrix) {
- for (int i = 0; i < matrix.row; i++) {
- for (int j = 0; j < matrix.col; j++) {
- if (typeid(T) == typeid(double))
- cout << fixed << setprecision(2);
- cout << matrix.matrix[i][j];
- if (j != matrix.col - 1)
- cout << " ";
- }
- cout << endl;
- }
- return cout;
- }
- friend istream& operator>>(istream& cin, Matrix<T> matrix) {
- for (int i = 0; i < matrix.row; i++)
- for (int j = 0; j < matrix.col; j++)
- cin >> matrix.matrix[i][j];
- return cin;
- }
- };
- template <typename T>
- Matrix<T> operator*(Matrix<T> a, Matrix<T> b) {
- Matrix<T> res(a.getRow(), b.getCol());
- for (int i = 0; i < res.getRow(); i++) {
- for (int j = 0; j < res.getCol(); j++) {
- res.setElement(0, i, j, true);
- for (int k = 0; k < a.getCol(); k++)
- res.setElement(a.getElement(i, k) * b.getElement(k, j), i, j);
- }
- }
- return res;
- }
- template <typename T>
- Matrix<T> operator+(Matrix<T> a, Matrix<T>& b) {
- Matrix<T> res(a.getRow(), b.getCol());
- for (int i = 0; i < a.getRow(); i++)
- for (int j = 0; j < a.getCol(); j++)
- res.setElement(a.getElement(i, j) + b.getElement(i, j), i, j, true);
- return res;
- }
- int main() {
- int n, m;
- cin >> n >> m;
- Matrix<double> a(n, m);
- cin >> a;
- cin >> n >> m;
- Matrix<double> b(n, m);
- cin >> b;
- cout << a + b;
- cin >> n >> m;
- Matrix<Fraction> c(n, m);
- cin >> c;
- cin >> n >> m;
- Matrix<Fraction> d(n, m);
- cin >> d;
- cout << c * d;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement