Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <iostream>
  2. #include "matrice.h"
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. matrice::matrice(int l,int c, int** buf){
  7. this->l=l;
  8. this->c=c;
  9. this->buf=new int*[l];
  10. for(int i=0;i<l;i++){
  11. this->buf[i]=new int[c];
  12. for(int j=0;j<c;j++)
  13. this->buf[i][j]=buf[i][j];
  14. }
  15. }
  16.  
  17. matrice::matrice(const matrice& m)
  18. {
  19. *this=m;
  20. }
  21.  
  22. void matrice::afis() const{
  23. cout<<"Nr linii: "<<l<<endl<<"Nr. coloane: "<<c<<endl;
  24. for(int i=0;i<l;i++){
  25. for(int j=0;j<c;j++){
  26. cout<<buf[i][j]<<" ";
  27. }
  28. cout<<endl;
  29. }
  30. }
  31.  
  32. matrice::~matrice(){
  33. if(buf!=NULL)
  34. {
  35. for(int i=0;i<l;i++)
  36. if(buf[i]!=NULL)
  37. delete[]buf[i];
  38. delete[] buf;
  39. }
  40. }
  41.  
  42. matrice& matrice::operator=(const matrice& m){
  43. l=m.l;
  44. c=m.c;
  45. buf=new int*[l];
  46. for(int i=0;i<l;i++){
  47. buf[i]=new int[c];
  48. for(int j=0;j<c;j++)
  49. buf[i][j]=m.buf[i][j];
  50. }
  51. return *this;
  52. }
  53.  
  54. matrice& matrice::operator+=(const matrice& m){
  55. if (this->buf== NULL || m.buf==NULL)
  56. {
  57. cout<<"Adunarea nu a fost facuta!"<<endl;
  58. return *this;
  59. }
  60. if (l!=m.l || m.c!=c)
  61. {
  62. cout<<"Adunarea nu a fost facuta!"<<endl;
  63. return *this;
  64. }
  65. for(int i=0;i<l;i++)
  66. for(int j=0;j<c;j++)
  67. buf[i][j]+=m.buf[i][j];
  68. return *this;
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. #include <iostream>
  94. #include "matrice.h"
  95. using namespace std;
  96.  
  97. const matrice operator+(const matrice& a,const matrice& b){
  98. if (a.buf== NULL || b.buf==NULL)
  99. {
  100. cout<<"Adunarea nu a fost facuta!"<<endl;
  101. return matrice();
  102. }
  103. if (a.l!=b.l || a.c!=b.c)
  104. {
  105. cout<<"Adunarea nu a fost facuta!"<<endl;
  106. return matrice();
  107. }
  108. int **v;
  109. v=new int*[3];
  110. for(int i=0;i<3;i++){
  111. v[i]=new int[3];
  112. }
  113. matrice m(a.l,b.c,v);
  114. for(int i=0;i<a.l;i++)
  115. for(int j=0;j<b.c;j++)
  116. m.buf[i][j]=a.buf[i][j]+b.buf[i][j];
  117. return m;
  118. }
  119.  
  120. bool operator==(const matrice&a,const matrice&b){
  121. if (a.buf== NULL || b.buf==NULL)
  122. {
  123. cout<<"Compararea nu poate fi facuta!"<<endl;
  124. return false;
  125. }
  126. if (a.l!=b.l || a.c!=b.c)
  127. {
  128. cout<<"Matricile au ordin diferit. Compararea nu poate fi facuta!"<<endl;
  129. return false;
  130. }
  131. for(int i=0;i<a.l;i++)
  132. for(int j=0;j<b.c;j++)
  133. if(a.buf[i][j]!=b.buf[i][j])
  134. return false;
  135. return true;
  136. }
  137.  
  138. bool operator!=(const matrice&a,const matrice&b){
  139. if (a.buf== NULL || b.buf==NULL)
  140. {
  141. cout<<"Compararea nu poate fi facuta!"<<endl;
  142. return false;
  143. }
  144. if (a.l!=b.l || a.c!=b.c)
  145. {
  146. cout<<"Matricile au ordin diferit. Compararea nu poate fi facuta!"<<endl;
  147. return true;
  148. }
  149. for(int i=0;i<a.l;i++)
  150. for(int j=0;j<b.c;j++)
  151. if(a.buf[i][j]!=b.buf[i][j])
  152. return true;
  153. return false;
  154. }
  155.  
  156. int main(int argc, char** argv) {
  157. int **v;
  158. v=new int*[3];
  159. for(int i=0;i<3;i++){
  160. v[i]=new int[3];
  161. for(int j=0;j<3;j++)
  162. v[i][j]=i+j;
  163. }
  164. matrice m1(3,3,v);
  165. m1.afis();
  166. system("PAUSE");
  167. system("CLS");
  168.  
  169. matrice m2;
  170. m2.afis();
  171. system("PAUSE");
  172. m2=m1;
  173. m2.afis();
  174. system("PAUSE");
  175. system("CLS");
  176.  
  177. m2+=m1;
  178. m2.afis();
  179. system("PAUSE");
  180. system("CLS");
  181.  
  182. matrice m3;
  183. m3=(m2+m1);
  184. m3.afis();
  185. system("PAUSE");
  186. system("CLS");
  187.  
  188. if (m1==m2){
  189. cout<<"Egale!"<<endl;
  190. }
  191. else
  192. cout<<"Nu egale!"<<endl;
  193. system("PAUSE");
  194. system("CLS");
  195.  
  196. if (m2==m2){
  197. cout<<"Egale!"<<endl;
  198. }
  199. else
  200. cout<<"Nu egale!"<<endl;
  201. system("PAUSE");
  202. system("CLS");
  203.  
  204. if (m1!=m2){
  205. cout<<"Diferite!"<<endl;
  206. }
  207. else
  208. cout<<"Nu diferite!"<<endl;
  209. system("PAUSE");
  210. system("CLS");
  211.  
  212. if (m2!=m2){
  213. cout<<"Diferite!"<<endl;
  214. }
  215. else
  216. cout<<"Nu diferite!"<<endl;
  217. system("PAUSE");
  218. system("CLS");
  219. return 0;
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. class matrice{
  243. int l,c;
  244. int **buf;
  245. public:
  246. matrice(int l=0,int c=0 ,int **buf=NULL);
  247. void afis()const; //sau suprascriu operator <<
  248. matrice(const matrice&);
  249. matrice& operator=(const matrice&);
  250. matrice& operator+=(const matrice&);//testare dimensiune
  251. friend const matrice operator+(const matrice&,const matrice&);
  252. friend bool operator==(const matrice&,const matrice&);
  253. friend bool operator!=(const matrice&,const matrice&);
  254. ~matrice();
  255. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement