Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. void wypisz(int** t,int N,int M)
  8. {
  9. for(int w=0;w<N;w++)
  10. {
  11. for(int k=0;k<M;k++)
  12. {
  13. cout << t[w][k] << "\t";
  14. }
  15. cout << endl;
  16. }
  17. }
  18.  
  19. int** losuj(int N,int M)
  20. {
  21. int** t=new int*[N];
  22. for(int i=0;i<N;i++)
  23. {
  24. t[i]=new int[M];
  25. }
  26.  
  27. for(int w=0;w<N;w++)
  28. {
  29. for(int k=0;k<M;k++)
  30. {
  31. t[w][k]=rand()%10;
  32. }
  33. }
  34. return t;
  35. }
  36.  
  37. int** pomnoz(int **t,int N,int M,int a)
  38. {
  39. int** t2=new int*[N];
  40. for(int i=0;i<N;i++)
  41. {
  42. t2[i]=new int[M];
  43. }
  44.  
  45. for(int w=0;w<N;w++)
  46. {
  47. for(int k=0;k<M;k++)
  48. {
  49. t2[w][k]=t[w][k]*a;
  50. }
  51. }
  52. return t2;
  53. }
  54.  
  55. float srednia(int **t,int N,int M)
  56. {
  57. float s=0;
  58. for(int w=0;w<N;w++)
  59. {
  60. for(int k=0;k<M;k++)
  61. {
  62. s+=t[w][k];
  63. }
  64. }
  65. return s/(N*M);
  66. }
  67.  
  68. int maks(int **t,int N,int M)
  69. {
  70. int m=t[0][0];
  71. for(int w=0;w<N;w++)
  72. {
  73. for(int k=0;k<M;k++)
  74. {
  75. if(t[w][k]>m)
  76. m=t[w][k];
  77. }
  78. }
  79. return m;
  80. }
  81.  
  82. int mini(int **t,int N,int M)
  83. {
  84. int m=t[0][0];
  85. for(int w=0;w<N;w++)
  86. {
  87. for(int k=0;k<M;k++)
  88. {
  89. if(t[w][k]<m)
  90. m=t[w][k];
  91. }
  92. }
  93. return m;
  94. }
  95.  
  96. int** dodaj(int **t1,int **t2,int N,int M)
  97. {
  98. int** t3=new int*[N];
  99. for(int i=0;i<N;i++)
  100. {
  101. t3[i]=new int[M];
  102. }
  103. for(int w=0;w<N;w++)
  104. {
  105. for(int k=0;k<M;k++)
  106. {
  107. t3[w][k]=t1[w][k]+t2[w][k];
  108. }
  109. }
  110. return t3;
  111. }
  112.  
  113. int** odejmij(int **t1,int **t2,int N,int M)
  114. {
  115. int** t3=new int*[N];
  116. for(int i=0;i<N;i++)
  117. {
  118. t3[i]=new int[M];
  119. }
  120. for(int w=0;w<N;w++)
  121. {
  122. for(int k=0;k<M;k++)
  123. {
  124. t3[w][k]=t1[w][k]-t2[w][k];
  125. }
  126. }
  127. return t3;
  128. }
  129.  
  130. int** pomnoz(int **t1,int **t2,int N,int M, int L)
  131. {
  132. int** t3=new int*[N];
  133. for(int i=0;i<N;i++)
  134. {
  135. t3[i]=new int[L];
  136. }
  137.  
  138. for(int w=0;w<N;w++)
  139. {
  140. for(int k=0;k<L;k++)
  141. {
  142. t3[w][k]=0;
  143. for(int i=0;i<M;i++)
  144. {
  145. t3[w][k]+=t1[w][i]*t2[i][k];
  146. }
  147. }
  148. }
  149. return t3;
  150. }
  151.  
  152. int wyznacznik(int **t,int N)
  153. {
  154. if(N==2)
  155. {
  156. return t[0][0]*t[1][1]-t[0][1]*t[1][0];
  157. }
  158. else if(N==1)
  159. {
  160. return t[0][0];
  161. }
  162. else if(N==3)
  163. {
  164. return t[0][0]*t[1][1]*t[2][2];
  165. }
  166. }
  167.  
  168. char wyborDzialania()
  169. {
  170. char x;
  171. cout << "podaj dzialanie: + dodawanie, - odejmowanie, * mnozenie, d wyznacznik, k koniec" << endl;
  172. cin >> x;
  173. return x;
  174. }
  175.  
  176. int main()
  177. {
  178. char x='a';
  179.  
  180. while(x!='k')
  181. {
  182. int a,b,c,d;
  183. x=wyborDzialania();
  184. int **A;
  185. int **B;
  186. if(x=='+' || x=='-' || x=='*')
  187. {
  188. cout << "podaj wymiary pierwszej macierzy" << endl;
  189. cin >> a;
  190. cin >> b;
  191. cout << "podaj wymiary drugiej macierzy" << endl;
  192. cin >> c;
  193. cin >> d;
  194. A=new int*[a];
  195. for(int i=0;i<a;i++)
  196. {
  197. A[i]=new int[b];
  198. }
  199. B=new int*[c];
  200. for(int i=0;i<c;i++)
  201. {
  202. B[i]=new int[d];
  203. }
  204. cout << "podaj wspolczynniki 1 macierzy (wierszami)" << endl;
  205. for(int w=0;w<a;w++)
  206. {
  207. cout << "Wiersz: " << w+1 << endl;
  208. for(int k=0;k<b;k++)
  209. {
  210. cin >> A[w][k];
  211. }
  212. }
  213. cout << "podaj wspolczynniki 2 macierzy (wierszami)" << endl;
  214. for(int w=0;w<c;w++)
  215. {
  216. cout << "Wiersz: " << w+1 << endl;
  217. for(int k=0;k<d;k++)
  218. {
  219. cin >> B[w][k];
  220. }
  221. }
  222. }
  223. else if(x!='k')
  224. {
  225. cout << "podaj wymiary macierzy" << endl;
  226. cin >> a;
  227. cin >> b;
  228. A=new int*[a];
  229. for(int i=0;i<a;i++)
  230. {
  231. A[i]=new int[b];
  232. }
  233. cout << "podaj wspolczynniki macierzy (wierszami)" << endl;
  234. for(int w=0;w<a;w++)
  235. {
  236. cout << "Wiersz: " << w+1 << endl;
  237. for(int k=0;k<b;k++)
  238. {
  239. cin >> A[w][k];
  240. }
  241. }
  242. }
  243.  
  244. if(x=='p')
  245. {
  246. int l;
  247. cout << "podaj liczbe" << endl;
  248. cin >> l;
  249. }
  250.  
  251. if(x=='+')
  252. {
  253. if(a==c && b==d)
  254. {
  255. cout << "suma macierzy: " << endl;
  256. wypisz(A,a,b);
  257. cout << "i macierzy: " << endl;
  258. wypisz(B,c,d);
  259. cout << "wynosi: " << endl;
  260. wypisz(dodaj(A,B,a,b),a,b);
  261. }
  262. else
  263. cout << "wymiary sie nie zgadzaja" << endl;
  264. }
  265. else if(x=='-')
  266. {
  267.  
  268. }
  269. else if(x=='*')
  270. {
  271.  
  272. }
  273. else if(x=='d')
  274. {
  275. if(a==b)
  276. {
  277. cout << "wyznacznik macierzy: " << endl;
  278. wypisz(A,a,b);
  279. cout << "wynosi: " << wyznacznik(A,a) << endl;
  280. }
  281. else
  282. cout << "macierz nie jest kwadratowa" << endl;
  283.  
  284. }
  285. else if(x=='p')
  286. {
  287.  
  288. }
  289. else if(x=='k')
  290. {
  291. cout << "dziekuje za uwage" << endl;
  292. }
  293. else
  294. {
  295. cout << "wprowadziles zly znak" << endl;
  296. }
  297. }
  298.  
  299. return 0;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement