Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Matrica.h
- #pragma once
- #include <iostream>
- using namespace std;
- template <class T>
- class matrica {
- private:
- T **a;
- int n;
- int m;
- public:
- matrica() {
- n = m = 10;
- a = new T*[n];
- for (int i = 0; i < n; i++) {
- a[i] = new T[m];
- }
- }
- matrica(int x, int y) {
- n = x;
- m = y;
- a = new T*[n];
- for (int i = 0; i < n; i++) {
- a[i] = new T[m];
- }
- }
- ~matrica() {
- if (a) {
- for (int i = 0; i < n; i++) {
- delete[] a[i];
- }
- delete[] a;
- }
- }
- void getElement(int x, int y) {
- cout << a[x][y];
- }
- void setElement(int x, int y, T newElement) {
- a[x][y] = newElement;
- }
- int getX() {
- return n;
- }
- int getY() {
- return m;
- }
- void setX(int x) {
- if (a) {
- for (int i = 0; i < n; i++) {
- delete[] a[i];
- }
- delete[] a;
- }
- n = x;
- a = new T*[n];
- for (int i = 0; i < n; i++) {
- a[i] = new T[m];
- }
- }
- void setY(int y) {
- if (a) {
- for (int i = 0; i < n; i++) {
- delete[] a[i];
- }
- delete[] a;
- }
- m = y;
- a = new T*[n];
- for (int i = 0; i < n; i++) {
- a[i] = new T[m];
- }
- }
- void filter() {
- T p1, p2, p3, p4;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- p1 = (i == 0 ? a[i][j] : a[i - 1][j]);
- p2 = (i == n - 1 ? a[i][j] : a[i + 1][j]);
- p3 = (j == 0 ? a[i][j] : a[i][j - 1]);
- p4 = (j == m - 1 ? a[i][j] : a[i][j + 1]);
- a[i][j] += (p1 + p2 + p3 + p4) / a[i][j];
- }
- }
- }
- T * operator[](int i) {
- return a[i];
- }
- };
- // Complex.h
- #pragma once
- #include <iostream>
- using namespace std;
- class complex {
- private:
- double real;
- double imag;
- public:
- complex();
- complex(double r, double i);
- ~complex();
- friend complex operator+(complex x, complex y);
- friend complex operator*(complex x, complex y);
- friend complex operator-(complex x, complex y);
- friend complex operator/(complex x, complex y);
- friend ostream& operator<<(ostream& str, complex& a);
- friend istream& operator>>(istream& str, complex& a);
- };
- // Complex.cpp
- #include "Complex.h"
- complex::complex()
- {
- real = imag = 0;
- }
- complex::complex(double r, double i)
- {
- real = r;
- imag = i;
- }
- complex::~complex()
- {
- }
- complex operator+(complex x, complex y)
- {
- complex vrati;
- vrati.real = x.real + y.real;
- vrati.imag = x.imag + y.imag;
- return vrati;
- }
- complex operator*(complex x, complex y)
- {
- complex vrati;
- vrati.real = x.real * y.real;
- vrati.imag = x.imag * y.imag;
- return vrati;
- }
- complex operator-(complex x, complex y)
- {
- complex vrati;
- vrati.real = x.real - y.real;
- vrati.imag = x.imag - y.imag;
- return vrati;
- }
- complex operator/(complex x, complex y)
- {
- complex vrati;
- vrati.real = x.real / y.real;
- vrati.imag = x.imag / y.imag;
- return vrati;
- }
- ostream & operator<<(ostream & str, complex & a)
- {
- str << a.real << " + " << a.imag <<"i";
- return str;
- }
- istream & operator>>(istream & str, complex & a)
- {
- str >> a.real >> a.imag;
- return str;
- }
- // main.cpp
- #include "Matrica.h"
- #include "Complex.h"
- void main() {
- matrica<char> a(3, 3);
- char x;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- cin >> x;
- a.setElement(i, j, x);
- }
- }
- a.getElement(2, 2);
- cout << endl << a.getX() << endl << a.getY() << endl;
- a.setX(2);
- cout << "Matrica je sada 2 x 3\n";
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 3; j++) {
- cin >> x;
- a.setElement(i, j, x);
- }
- }
- a.getElement(1, 2);
- cout << endl;
- a.setY(2);
- cout << "Matrica je sada 2 x 2\n";
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 2; j++) {
- cin >> x;
- a.setElement(i, j, x);
- }
- }
- a.getElement(1, 1);
- cout << endl;
- a.filter();
- cout << "Filter funkcija:\n";
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 2; j++) {
- a.getElement(i, j);
- cout << " ";
- }
- cout << endl;
- }
- cout << "Unesite kompleksne brojeve:\n";
- matrica<complex> b(2, 2);
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 2; j++) {
- cin >> b[i][j];
- }
- }
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < 2; j++) {
- cout << b[i][j] << " | ";
- }
- cout << endl;
- }
- cout << b[0][0] + b[0][1] << endl;
- cout << b[0][0] * b[0][1] << endl;
- cout << b[0][0] - b[0][1] << endl;
- cout << b[0][0] / b[0][1] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement