Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- class Matrix
- {
- private:
- int n;
- double **arr;
- public:
- Matrix(int numb)
- {
- n = numb;
- arr = new double *[n];
- for (int i = 0; i < n; i++)
- {
- arr[i] = new double[n];
- for (int j = 0; j < n; j++)
- {
- cout << "Введите " << "элемент " << "столбца " << i + 1 << " " << "строки " << j + 1 << ": ";
- cin >> arr[i][j];
- }
- }
- cout << "Ваша матрица: " << endl;
- }
- Matrix(int numb, int one)
- {
- n = numb;
- arr = new double *[n];
- for (int i = 0; i < n; i++)
- {
- arr[i] = new double[n];
- for (int j = 0; j < n; j++)
- {
- arr[i][j] = one;
- }
- }
- }
- double **get_arr()
- {
- return arr;
- }
- void set_arr(double **arr_buf)
- {
- arr = arr_buf;
- }
- Matrix pl(Matrix ob)
- {
- Matrix rez(n, 0);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- rez.arr[i][j] = arr[i][j] + ob.arr[i][j];
- }
- }
- return rez;
- }
- Matrix mns(Matrix ob)
- {
- Matrix rez(n, 0);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- rez.arr[i][j] = arr[i][j] - ob.arr[i][j];
- }
- }
- return rez;
- }
- Matrix umn(Matrix ob)
- {
- Matrix rez(n, 0);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- int skal = 0;
- for (int k = 0; k < n; k++)
- rez.arr[i][j] += arr[i][j] * ob.arr[i][j];
- }
- }
- return rez;
- }
- void matr_na_chisl(Matrix ob, int k)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- arr[i][j] = ob.arr[i][j] * k;
- }
- }
- };
- void matr_div_chisl(Matrix ob, int k)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- arr[i][j] = ob.arr[i][j] / k;
- }
- }
- };
- void print(int n)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cout << "|" << arr[i][j] << "|" << " ";
- }
- cout << endl;
- }
- }
- };
- class formula : public Matrix
- {
- public:
- formula(int n, int k = 0) :Matrix(n, k)
- {
- }
- Matrix sin(Matrix object, int n)
- {
- double **arr_buf = new double*[n];
- for (int i = 0; i < n; i++)
- {
- arr_buf[i] = new double[n];
- }
- Matrix result(n, 0);
- double fact;
- int k;
- double sinx = 0;
- double st;
- double zn;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- k = 0;
- sinx = 0;
- st = object.get_arr()[i][j];
- fact = 1;
- zn = 1;
- while (k < 10)
- {
- sinx = sinx + st * zn / fact;
- k = k + 2;
- st = st * object.get_arr()[i][j] * object.get_arr()[i][j];
- fact = fact * (k - 1) * k;
- zn = zn * -1;
- }
- arr_buf[i][j] = sinx;
- }
- }
- result.set_arr(arr_buf);
- return result;
- }
- };
- void menu_for2()
- {
- cout << "1)Сложение матриц" << endl;
- cout << "2)Разность матриц" << endl;
- cout << "3)Произведение матриц" << endl;
- cout << "4)Умножение матрицы на число" << endl;
- cout << "5)Деление матрицы на число" << endl;
- cout << "6)Формула " << endl;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- cout << "Введите размерность матрицы: ";
- int n;
- cin >> n;
- Matrix ob1(n);
- Matrix ob3(n, 0);
- cout << endl;
- menu_for2();
- int choise_2;
- cin >> choise_2;
- switch (choise_2)
- {
- case 1: {
- cout << "Введите вторую матрицу" << endl;
- Matrix ob2(n);
- ob3 = ob1.pl(ob2);
- ob3.print(n);
- break;
- }
- case 2: {
- cout << "Введите вторую матрицу" << endl;
- Matrix ob2(n);
- ob3 = ob1.mns(ob2);
- ob3.print(n);
- break;
- }
- case 3: {
- cout << "Введите вторую матрицу" << endl;
- Matrix ob2(n);
- ob3 = ob1.umn(ob2);
- ob3.print(n);
- break;
- }
- case 4: {
- float k;
- cout << "Введите число, на которое собираетесь умножить матрицу: ";
- cin >> k;
- ob1.matr_na_chisl(ob1, k);
- ob1.print(n);
- break;
- }
- case 5: {
- float k;
- cout << "Введите число, на которое собираетесь делить матрицу: ";
- cin >> k;
- if (k == 0)
- {
- do
- {
- cout << "Введите корректное значение!";
- cin >> k;
- } while (k == 0);
- }
- ob1.matr_div_chisl(ob1,k);
- ob1.print(n);
- break;
- }
- case 6: {
- formula res(n);
- ob3 = res.sin(ob1, n);
- ob3.print(n);
- break;
- }
- default:
- break;
- };
- system ("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement