Advertisement
ithoran

V5 Z10C++

Dec 21st, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. // Matrica.h
  2. #pragma once
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. template <class T>
  7. class matrica {
  8. private:
  9.     T **a;
  10.     int n;
  11.     int m;
  12. public:
  13.     matrica() {
  14.         n = m = 10;
  15.         a = new T*[n];
  16.         for (int i = 0; i < n; i++) {
  17.             a[i] = new T[m];
  18.         }
  19.     }
  20.  
  21.     matrica(int x, int y) {
  22.         n = x;
  23.         m = y;
  24.         a = new T*[n];
  25.         for (int i = 0; i < n; i++) {
  26.             a[i] = new T[m];
  27.         }
  28.     }
  29.  
  30.     ~matrica() {
  31.         if (a) {
  32.             for (int i = 0; i < n; i++) {
  33.                 delete[] a[i];
  34.             }
  35.             delete[] a;
  36.         }
  37.     }
  38.  
  39.     void getElement(int x, int y) {
  40.         cout << a[x][y];
  41.     }
  42.  
  43.     void setElement(int x, int y, T newElement) {
  44.         a[x][y] = newElement;
  45.     }
  46.  
  47.     int getX() {
  48.         return n;
  49.     }
  50.  
  51.     int getY() {
  52.         return m;
  53.     }
  54.  
  55.     void setX(int x) {
  56.         if (a) {
  57.             for (int i = 0; i < n; i++) {
  58.                 delete[] a[i];
  59.             }
  60.             delete[] a;
  61.         }
  62.         n = x;
  63.         a = new T*[n];
  64.         for (int i = 0; i < n; i++) {
  65.             a[i] = new T[m];
  66.         }
  67.     }
  68.  
  69.     void setY(int y) {
  70.         if (a) {
  71.             for (int i = 0; i < n; i++) {
  72.                 delete[] a[i];
  73.             }
  74.             delete[] a;
  75.         }
  76.         m = y;
  77.         a = new T*[n];
  78.         for (int i = 0; i < n; i++) {
  79.             a[i] = new T[m];
  80.         }
  81.     }
  82.  
  83.     void filter() {
  84.         T p1, p2, p3, p4;
  85.         for (int i = 0; i < n; i++) {
  86.             for (int j = 0; j < m; j++) {
  87.                 p1 = (i == 0 ? a[i][j] : a[i - 1][j]);
  88.                 p2 = (i == n - 1 ? a[i][j] : a[i + 1][j]);
  89.                 p3 = (j == 0 ? a[i][j] : a[i][j - 1]);
  90.                 p4 = (j == m - 1 ? a[i][j] : a[i][j + 1]);
  91.                 a[i][j] += (p1 + p2 + p3 + p4) / a[i][j];
  92.             }
  93.         }
  94.     }
  95.     T * operator[](int i) {
  96.             return a[i];
  97.     }
  98. };
  99.  
  100. // Complex.h
  101. #pragma once
  102. #include <iostream>
  103. using namespace std;
  104.  
  105. class complex {
  106. private:
  107.     double real;
  108.     double imag;
  109. public:
  110.     complex();
  111.     complex(double r, double i);
  112.     ~complex();
  113.     friend complex operator+(complex x, complex y);
  114.     friend complex operator*(complex x, complex y);
  115.     friend complex operator-(complex x, complex y);
  116.     friend complex operator/(complex x, complex y);
  117.     friend ostream& operator<<(ostream& str, complex& a);
  118.     friend istream& operator>>(istream& str, complex& a);
  119. };
  120.  
  121. // Complex.cpp
  122. #include "Complex.h"
  123.  
  124. complex::complex()
  125. {
  126.     real = imag = 0;
  127. }
  128.  
  129. complex::complex(double r, double i)
  130. {
  131.     real = r;
  132.     imag = i;
  133. }
  134.  
  135. complex::~complex()
  136. {
  137. }
  138.  
  139. complex operator+(complex x, complex y)
  140. {
  141.     complex vrati;
  142.     vrati.real = x.real + y.real;
  143.     vrati.imag = x.imag + y.imag;
  144.     return vrati;
  145. }
  146.  
  147. complex operator*(complex x, complex y)
  148. {
  149.     complex vrati;
  150.     vrati.real = x.real * y.real;
  151.     vrati.imag = x.imag * y.imag;
  152.     return vrati;
  153. }
  154.  
  155. complex operator-(complex x, complex y)
  156. {
  157.     complex vrati;
  158.     vrati.real = x.real - y.real;
  159.     vrati.imag = x.imag - y.imag;
  160.     return vrati;
  161. }
  162.  
  163. complex operator/(complex x, complex y)
  164. {
  165.     complex vrati;
  166.     vrati.real = x.real / y.real;
  167.     vrati.imag = x.imag / y.imag;
  168.     return vrati;
  169. }
  170.  
  171. ostream & operator<<(ostream & str, complex & a)
  172. {
  173.     str << a.real << " + " << a.imag <<"i";
  174.     return str;
  175. }
  176.  
  177. istream & operator>>(istream & str, complex & a)
  178. {
  179.     str >> a.real >> a.imag;
  180.     return str;
  181. }
  182.  
  183. // main.cpp
  184. #include "Matrica.h"
  185. #include "Complex.h"
  186.  
  187. void main() {
  188.     matrica<char> a(3, 3);
  189.     char x;
  190.     for (int i = 0; i < 3; i++) {
  191.         for (int j = 0; j < 3; j++) {
  192.             cin >> x;
  193.             a.setElement(i, j, x);
  194.         }
  195.     }
  196.     a.getElement(2, 2);
  197.     cout << endl << a.getX() << endl << a.getY() << endl;
  198.     a.setX(2);
  199.     cout << "Matrica je sada 2 x 3\n";
  200.     for (int i = 0; i < 2; i++) {
  201.         for (int j = 0; j < 3; j++) {
  202.             cin >> x;
  203.             a.setElement(i, j, x);
  204.         }
  205.     }
  206.     a.getElement(1, 2);
  207.     cout << endl;
  208.     a.setY(2);
  209.     cout << "Matrica je sada 2 x 2\n";
  210.     for (int i = 0; i < 2; i++) {
  211.         for (int j = 0; j < 2; j++) {
  212.             cin >> x;
  213.             a.setElement(i, j, x);
  214.         }
  215.     }
  216.     a.getElement(1, 1);
  217.     cout << endl;
  218.     a.filter();
  219.     cout << "Filter funkcija:\n";
  220.     for (int i = 0; i < 2; i++) {
  221.         for (int j = 0; j < 2; j++) {
  222.             a.getElement(i, j);
  223.             cout << " ";
  224.         }
  225.         cout << endl;
  226.     }
  227.     cout << "Unesite kompleksne brojeve:\n";
  228.     matrica<complex> b(2, 2);
  229.     for (int i = 0; i < 2; i++) {
  230.         for (int j = 0; j < 2; j++) {
  231.             cin >> b[i][j];
  232.         }
  233.     }
  234.     for (int i = 0; i < 2; i++) {
  235.         for (int j = 0; j < 2; j++) {
  236.             cout << b[i][j] << " | ";
  237.         }
  238.         cout << endl;
  239.     }
  240.     cout << b[0][0] + b[0][1] << endl;
  241.     cout << b[0][0] * b[0][1] << endl;
  242.     cout << b[0][0] - b[0][1] << endl;
  243.     cout << b[0][0] / b[0][1] << endl;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement