Advertisement
ewa_tabor

Macierze JIMP2

Mar 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. class MACIERZ{
  6. friend ostream & operator<< (ostream & s1, MACIERZ & o1);
  7. friend istream & operator>>(istream & s1, MACIERZ & o1);
  8.  
  9.  
  10. double *ws;
  11. int rozm;
  12. public:
  13.  
  14. MACIERZ(){
  15. ws=NULL;
  16. }
  17. MACIERZ(int rozm, int wart){
  18. rozm = rozm;
  19. ws = new double[rozm*rozm];
  20. for ( int i = 0; i < rozm*rozm; i++){
  21. ws[i] = wart;
  22. }
  23.  
  24. }
  25.  
  26. MACIERZ(const MACIERZ &o1){
  27. rozm = o1.rozm;
  28. ws = new double[o1.rozm*o1.rozm];
  29. for ( int i = 0; i < rozm*rozm; i++){
  30. ws[i] = o1.ws[i];
  31. }
  32. }
  33.  
  34. ~MACIERZ(){
  35. if (ws!=NULL){
  36. delete [] ws;
  37. ws=NULL;
  38. }
  39. }
  40.  
  41. MACIERZ & operator=(const MACIERZ & o1){
  42. if (this == &o1) return *this;
  43.  
  44. this->rozm = o1.rozm;
  45. if (this->ws!=NULL){
  46. delete [] this->ws;
  47. }
  48. if (o1.rozm>0){
  49.  
  50. this->ws = new double[o1.rozm*o1.rozm];
  51. for (int i=0;i<(o1.rozm*o1.rozm);i++){
  52. this->ws[i] = o1.ws[i];
  53. }
  54. }
  55.  
  56. return *this;
  57. }
  58.  
  59.  
  60. MACIERZ operator+ (const MACIERZ &o1){
  61. MACIERZ wynik(*this);
  62. if (this->rozm != o1.rozm){
  63. cout <<"Nie można dodać macierzy o różnych wymiarach!";
  64. return wynik;
  65. }
  66. else{
  67. for (int i=0;i< o1.rozm*o1.rozm; i++){
  68. wynik.ws[i] += o1.ws[i];
  69. }
  70. return wynik;
  71. }
  72.  
  73. }
  74.  
  75. MACIERZ operator- (const MACIERZ &o1){
  76. MACIERZ wynik= MACIERZ(*this);
  77. if (this->rozm != o1.rozm){
  78. cout <<"Nie można wykonać" <<endl;
  79. return wynik;
  80. }
  81. else{
  82. for (int i=0;i< o1.rozm*o1.rozm; i++){
  83. wynik.ws[i] -= o1.ws[i];
  84. }
  85. return wynik;
  86.  
  87. }
  88. }
  89.  
  90. MACIERZ operator*(const MACIERZ &o1){
  91. MACIERZ wynik= MACIERZ(*this);
  92. for (int k=0; k<o1.rozm*o1.rozm;k++){
  93. wynik.ws[k]=0;
  94. }
  95. if (this->rozm != o1.rozm){
  96. cout <<"Nie można mnozyc macierzy roznych rozmiarow!" <<endl;
  97. return wynik;
  98. }
  99. else{
  100. for (int i=0; i<o1.rozm*o1.rozm;i+=o1.rozm){
  101. for (int j=0; j<o1.rozm;j++){
  102. for (int k=0; k<o1.rozm;k++){
  103. wynik.ws[i+j]+=this->ws[k+i]*o1.ws[k*o1.rozm+j];
  104. }
  105. }
  106. }
  107.  
  108.  
  109. return wynik;
  110.  
  111. }
  112. }
  113.  
  114.  
  115. int operator==(const MACIERZ &o1){
  116. if (this->rozm != o1.rozm){
  117. cout <<"Macierze różnych rozmów!" <<endl;
  118. return 0;
  119. }
  120. else{
  121. int flaga=0;
  122. for(int i=0;i< o1.rozm*o1.rozm; i++){
  123. if(this->ws[i] != o1.ws[i]) flaga+=1;
  124. }
  125.  
  126. if(flaga==0) return 1;
  127. else{
  128. return 0;
  129. }
  130. }
  131. }
  132.  
  133. int operator>=(const MACIERZ &o1){
  134. //operatory >= i <= porownuja sume elemntow macierzy
  135. if (this->rozm != o1.rozm){
  136. cout <<"Macierze różnych rozmiarów!" <<endl;
  137. return 0;
  138. }
  139. else{
  140. int s1=0;
  141. int s2=0;
  142. for(int i=0;i< this->rozm*this->rozm; i++){
  143. s1+=this->ws[i];
  144. }
  145.  
  146. for(int i=0;i< o1.rozm*o1.rozm; i++){
  147. s2+=o1.ws[i];
  148. }
  149.  
  150. if(s1>=s2) return 1;
  151. else{
  152. return 0;
  153. }
  154. }
  155. }
  156.  
  157. int operator<=(const MACIERZ &o1){
  158. if (this->rozm != o1.rozm){
  159. cout <<"Macierze sa różnych rozmiarów!" <<endl;
  160. return 0;
  161. }
  162. else{
  163. int s1=0;
  164. int s2=0;
  165. for(int i=0;i< this->rozm*this->rozm; i++){
  166. s1+=this->ws[i];
  167. }
  168.  
  169. for(int i=0;i< o1.rozm*o1.rozm; i++){
  170. s2+=o1.ws[i];
  171. }
  172.  
  173. if(s1<=s2) return 1;
  174. else{
  175. return 0;
  176. }
  177. }
  178. }
  179.  
  180. int operator!=(const MACIERZ &o1){
  181. if (this->rozm != o1.rozm){
  182. cout <<"Macierze sa różnych rozmiarów!" <<endl;
  183. return 0;
  184. }
  185. else{
  186. int flaga=0;
  187. for(int i=0;i< this->rozm*this->rozm; i++){
  188. if (this->ws[i] != o1.ws[i])flaga+=1;
  189. }
  190.  
  191.  
  192. if(flaga!=0) return 1;
  193. else{
  194. return 0;
  195. }
  196. }
  197. }
  198.  
  199. //koniec klasy MACIERZ
  200. };
  201.  
  202. ostream & operator<< (ostream & s1, MACIERZ & o1){
  203. if (o1.ws!=NULL){
  204. for(int i=0, j=1;i<(o1.rozm*o1.rozm);i++, j++){
  205. cout << o1.ws[i] <<" " ;
  206. if (j==o1.rozm){
  207. s1<<endl;
  208. j=0;
  209. }
  210. }
  211. }
  212. return s1;
  213. }
  214.  
  215. istream & operator>>(istream & s1, MACIERZ & o1){
  216. s1>>o1.rozm;
  217. if (o1.ws!=NULL){
  218. delete [] o1.ws;
  219. o1.ws=NULL;
  220. }
  221. if (o1.rozm>0){
  222. o1.ws = new double[o1.rozm*o1.rozm];
  223. for (int i=0;i<(o1.rozm*o1.rozm);i++){
  224. cin >> o1.ws[i];
  225. }
  226. }
  227. return s1;
  228. }
  229.  
  230.  
  231.  
  232.  
  233. int main(){
  234. MACIERZ zm, zm2, zm3, zm4, zm5;
  235. cout << "Podaj macierz z1."<< endl;
  236. cin >> zm;
  237.  
  238. cout<<endl;
  239. cout << "Macierz 1" <<endl;
  240. cout<<zm<<endl;
  241. zm2 = zm;
  242. cout << "Macierz 2 po dzialaniu ="<<endl;
  243. cout<<zm2<<endl;
  244. zm3=zm*zm2;
  245. cout<<zm3<<endl;
  246. zm3 = zm+zm2;
  247. cout <<"Macierz 3 po dzialaniu zm1+zm2"<<endl;
  248. cout<<zm <<endl;
  249. cout <<zm3 <<endl;
  250. cout<<zm4<<endl;
  251. zm4 = zm-zm2;
  252. cout <<"Macierz 4 po odejmowaniu zm-zm2" <<endl;
  253. cout<<zm4 <<endl;
  254. if (zm>=zm2) cout<<"Tak, zm>=zm2" <<endl;
  255. if (zm<=zm2) cout<<"Tak, zm<=zm3" <<endl;
  256. if (zm>=zm3) cout<<"Tak, zm>=zm3"<<endl;
  257. if (zm!=zm3) cout<<"Tak,zm!=zm3" <<endl;
  258.  
  259. return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement