Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1.  
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <vector>
  5. #include <array>
  6. #include <string>
  7. #include <random>
  8. #include <fstream>
  9. using namespace std;
  10. vector <int> v;
  11. using IntMatrix = vector<vector<int>>;
  12. using IntMatrix_db = vector<vector<double>>;
  13. //INFORMACJA
  14. /* Zadanie 4 jest zawarte pod case 3 razem z zadaniem 3
  15. Zadania 8 nie ma poniewaz jest z nim problem */
  16. void Print(const vector <int> &v) //zad1 -odp: modyfikator const mozemy dodac gdy nie chcemy modyfikowac wektora/zmiennej
  17. {
  18. cout << "[";
  19. for (auto i : v)
  20. {
  21. cout << i;
  22. if (i != v[v.size() - 1])
  23. {
  24. cout << ',';
  25. }
  26. }
  27. cout << "]";
  28. cout << endl << endl;
  29. }
  30. float sum(vector<float> &v1)
  31. {
  32. float suma = 0;
  33. for (vector<float>::iterator i = v1.begin(); i != v1.end(); i++) suma += *i;
  34. return suma;
  35. }
  36. float average(vector<float> &v1)
  37. {
  38. float sum = 0;
  39. for (vector<float>::iterator i = v1.begin(); i != v1.end(); i++)sum += *i;
  40. return sum / v1.size();
  41. }
  42. array<float, 2> minmax(const vector<float> &v)
  43. {
  44. array<float, 2> tab;
  45. double min = v[0], max = v[0];
  46. for (auto e : v)
  47. {
  48. if (e < min)
  49. {
  50. min = e;
  51. }
  52. if (e > max)
  53. {
  54. max = e;
  55. }
  56. }
  57. tab[0] = min;
  58. tab[1] = max;
  59. return tab;
  60. }
  61. bool equals(const vector<int> &v2, const vector<int> &v4, int w1)
  62. {
  63. int i;
  64. for (int i = 0; i < w1; i++)
  65. {
  66. if (v2[i] == v4[i]) return true;
  67. else return false;
  68. }
  69. }
  70. //zadanie 2
  71. vector <int> add(const vector<int> &v2, const vector<int> &v4, int w1)
  72. {
  73. int i;
  74. int sum = 0;
  75. v.clear();
  76. for (int i = 0; i < w1; i++)
  77. {
  78. sum = (v2[i] + v4[i]);
  79. v.push_back(sum);
  80.  
  81. }
  82. Print(v);
  83. return v;
  84. }
  85. vector <int> subtract(const vector<int> &v2, const vector<int> &v4, int w1)
  86. {
  87. v.clear();
  88. int i;
  89. int sum = 0;
  90. for (int i = 0; i < w1; i++)
  91. {
  92. sum = (v2[i] - v4[i]);
  93. v.push_back(sum);
  94. }
  95. Print(v);
  96. return v;
  97. }
  98. vector <int> multiply(const vector<int> &v2, int w1, int skalar)
  99. {
  100. v.clear();
  101. int i;
  102. int sum = 0;
  103.  
  104. for (int i = 0; i < w1; i++)
  105. {
  106. sum = (v2[i] * skalar);
  107. v.push_back(sum);
  108. }
  109. Print(v);
  110. return v;
  111. }
  112. //zadanie 3
  113. int randomInt(int min, int max)
  114. {
  115. static default_random_engine e{};
  116. uniform_int_distribution<int > d(min, max);
  117. return d(e);
  118. }
  119. vector<int> randomVector(unsigned int size, int min, int max) //zad4
  120. {
  121. vector<int> v;
  122. for (int i = 0; i < size; i++) v.push_back(randomInt(min, max));
  123. return v;
  124. }
  125. fstream plik;
  126. void writeVector(const vector<int >& v2, const string& fileName)
  127. {
  128. plik.open(fileName.c_str(), ios::out);
  129. if (!plik.good())
  130. {
  131. cout << "Nie udalo sie utworzyc pliku!\n";
  132. }
  133. else
  134. {
  135. cout << "Plik utworzony!\n";
  136. }
  137. for (int i = 0; i < v2.size(); i++)
  138. {
  139.  
  140. plik << v2[i]<<",";
  141. }
  142. plik.close();
  143. }
  144. //zadanie 4-- -- - -- - blad
  145. void readVector(const string& fileName)
  146. {
  147. vector<int> v9;
  148. ifstream plik;
  149. string linia;
  150. plik.open(fileName.c_str(), ios::out);
  151. if(plik)
  152. {
  153. string buffer;
  154. while (!plik.eof())
  155. {
  156. getline(plik, linia);
  157. cout <<"["<< linia<<"]";
  158. }
  159. }
  160.  
  161.  
  162. }
  163. vector<int> bubbleSort(vector <int> &v2)
  164. {
  165. for (int i = 0; i < v2.size(); i++)
  166. for (int j = 1; j < v2.size() - i; j++) //pętla wewnętrzna
  167. if (v2[j - 1] > v2[j])
  168. //zamiana miejscami
  169. swap(v2[j - 1], v2[j]);
  170. Print(v2);
  171. return v2;
  172. }
  173. //zadanie 6
  174. IntMatrix createMatrix(array<unsigned int, 2> shape)
  175. {
  176. IntMatrix v;
  177. for (int i = 0; i < shape[0]; i++)
  178. {
  179. vector<int> row;
  180. for (int j = 0; j < shape[1]; j++)
  181. {
  182. row.push_back(0);
  183. }
  184. v.push_back(row);
  185. }
  186. return v;
  187. }
  188.  
  189. IntMatrix randomMatrix(array<unsigned int, 2> shape, int min, int max)
  190. {
  191. IntMatrix v;
  192. for (int i = 0; i < shape[0]; i++)
  193. {
  194. vector<int> row;
  195. for (int j = 0; j < shape[1]; j++)
  196. {
  197. row.push_back(randomInt(min, max));
  198. }
  199. v.push_back(row);
  200. }
  201. return v;
  202. }
  203.  
  204. void print(const IntMatrix &matrix, array<unsigned int, 2> shape)
  205. {
  206. for (int i = 0; i < shape[0]; i++)
  207. {
  208. for (int j = 0; j < shape[1]; j++)
  209. {
  210. cout << matrix[i][j] << " ";
  211. }
  212. cout << endl;
  213. }
  214. }
  215. vector<vector<int>> v_macierz;
  216. IntMatrix add1(array<unsigned int, 2> shape, int min, int max,IntMatrix &v7,IntMatrix &v8)
  217. {
  218. cout << "DODAWANIE" << endl;
  219. for (int i = 0; i < shape[0]; i++)
  220. {
  221. vector<int> row;
  222. for (int j = 0; j < shape[1]; j++)
  223. {
  224. row.push_back(v8[i][j] + v7[i][j]);
  225. }
  226. v_macierz.push_back(row);
  227. }
  228. return v_macierz;
  229. }
  230.  
  231. IntMatrix subtract1(array<unsigned int, 2> shape, int min, int max, IntMatrix &v7, IntMatrix &v8)
  232. {
  233. v_macierz.clear();
  234. cout << "ODEJMOWANIE" << endl;
  235. for (int i = 0; i < shape[0]; i++)
  236. {
  237. vector<int> row;
  238. for (int j = 0; j < shape[1]; j++)
  239. {
  240. row.push_back(v8[i][j] - v7[i][j]);
  241. }
  242. v_macierz.push_back(row);
  243. }
  244. return v_macierz;
  245. }
  246. //zadania 8 nie ma, posiadam z nim problem
  247.  
  248. //zadanie 9
  249. vector<vector<double>> m1;
  250. double randomDouble(double min, double max)
  251. {
  252. double f = (double)rand() / RAND_MAX;
  253. return min + f * (max - min);
  254. }
  255. IntMatrix_db createMatrix1(array<double, 2> shape)
  256. {
  257. IntMatrix_db v;
  258. for (int i = 0; i < shape[0]; i++)
  259. {
  260. vector<double> row;
  261. for (int j = 0; j < shape[1]; j++)
  262. {
  263. row.push_back(0);
  264. }
  265. v.push_back(row);
  266. }
  267. return v;
  268. }
  269.  
  270. IntMatrix_db randomMatrix1(array<double, 2> shape, double min, double max)
  271. {
  272. IntMatrix_db v;
  273. for (int i = 0; i < shape[0]; i++)
  274. {
  275. vector < double > row;
  276. for (int j = 0; j < shape[1]; j++)
  277. {
  278. row.push_back(randomDouble(min, max));
  279. }
  280. v.push_back(row);
  281. }
  282. return v;
  283. }
  284.  
  285. void print1(const IntMatrix_db &matrix, array< double, 2> shape)
  286. {
  287. for (int i = 0; i < shape[0]; i++)
  288. {
  289. for (int j = 0; j < shape[1]; j++)
  290. {
  291. cout << matrix[i][j] << " ";
  292. }
  293. cout << endl;
  294. }
  295. }
  296. double inv(double x)
  297. {
  298. return (1 / x);
  299. }
  300. double inv_square(double x)
  301. {
  302. return (1 / (x*x));
  303. }
  304. double sqrt(double x)
  305. {
  306. return sqrt(x);
  307. }
  308. void forEach(IntMatrix_db &v8 ,double (&fun) (double))
  309. {
  310.  
  311. IntMatrix_db m2;
  312.  
  313. for (auto i : v8)
  314. {
  315. vector<double> v20;
  316. for (auto j : i)
  317. v20.push_back(fun(j));
  318. m2.push_back(v20);
  319.  
  320. }
  321. for (auto i : m2)
  322. {
  323. for (auto j : i)
  324. cout<<j << ' ';
  325. cout << endl;
  326. }
  327. }
  328. //funkcja main
  329. int main()
  330. { string wybor;
  331. //petla do while
  332. do {
  333. int option;
  334. cout << "ktore zadanie: ";
  335. cin >> option;
  336. //uzyte wektory
  337. vector <float> v1;
  338. vector <int> v2;
  339. vector <int> v4;
  340. vector<vector<int>> v6;
  341. vector<vector<int>> v7;
  342. IntMatrix v8;
  343.  
  344. fstream plik;
  345. string fileName;
  346. unsigned int minn = 0, maxx = 0;
  347. unsigned int minnx=0, maxxx=0;
  348. double minnx1 = 0, maxxx1 = 0;
  349. //switch i wybor zadania
  350. switch (option)
  351. {
  352. //zadanie 1
  353. case 1:
  354.  
  355.  
  356. float x;
  357. int n;
  358.  
  359. cout << "podaj ile liczb ma miec vector: ";
  360. cin >> n;
  361. for (int i = 0; i < n; i++)
  362. {
  363. cout << "podaj " << i + 1 << endl;
  364. cin >> x;
  365. v1.push_back(x);
  366. }
  367.  
  368. cout << "suma elementow wektora to: " << sum(v1) << endl;
  369. cout << "srednia elementow wektora to: " << average(v1) << endl;
  370. array<float, 2> v3 = minmax(v1);
  371. cout << "min i max: " << v3[0] << " " << v3[1] << endl;
  372.  
  373. break;
  374. //zadnaie 2
  375. case 2:
  376. int w1;
  377. int i;
  378. int s;
  379. cout << "porownywanie wektorow: " << endl;
  380. cout << "podaj ilu elementowy maja byc wektory: ";
  381. cin >> w1;
  382. cout << "wektor 1: " << endl;
  383. //wektor 1
  384. for (int i = 0; i < w1; i++) v2.push_back(rand() % 9);
  385. Print(v2);
  386.  
  387. cout << endl;
  388. cout << "wektor 2: " << endl;
  389. //wektor 2
  390. for (int i = 0; i < w1; i++) v4.push_back(rand() % 9);
  391. Print(v4);
  392.  
  393. if (equals(v2, v4, w1) == true) cout << endl << "Wektory sa rowne." << endl;
  394. else cout << endl << "Wektory nie sa rowne." << endl;
  395. //suma
  396. cout << "suma wektorow: " << endl;
  397. add(v2, v4, w1);
  398. cout << endl;
  399. //roznica
  400. cout << "roznica wektorow: " << endl;
  401. subtract(v2, v4, w1);
  402. //mnozenie przez skalar
  403. int skalar;
  404. cout << endl;
  405. cout << "podaj skalar: ";
  406. cin >> skalar;
  407. cout << "1 wektor razy skalar: " << endl;
  408. multiply(v2, w1, skalar);
  409. break;
  410. //zadanie 3 i 4
  411. case 3:
  412. v2.clear();
  413. int m;
  414. int min, max;
  415.  
  416. cout << "Podaj nazwe pliku: ";
  417. cin >> fileName;
  418. fileName += ".txt";
  419. cout << "ile liczb ma miec wektor: ";
  420. cin >> m;
  421. cout << "podaj min: ";
  422. cin >> min;
  423. cout << "podaj max: ";
  424. cin >> max;
  425. v2 = randomVector(m, min, max);
  426. writeVector(v2, fileName);
  427.  
  428. //zadanie 4
  429. cout << "zadanie 4" << endl;
  430. readVector(fileName);
  431. break;
  432. //zadanie 5
  433. case 5:
  434. cout << "ile liczb ma miec wektor: ";
  435. cin >> m;
  436. cout << "podaj min: ";
  437. cin >> min;
  438. cout << "podaj max: ";
  439. cin >> max;
  440. v2 = randomVector(m, min, max);
  441. bubbleSort(v2);
  442. break;
  443. case 6:
  444.  
  445. unsigned int q, w;
  446. cout << "podaj ilosc wierszy a potem kolumn: ";
  447. cin >> q;
  448. cin >> w;
  449. v6 = randomMatrix({ q,w },minn,maxx);
  450. print(v6, { q,w });
  451. cout << "podaj min i max: ";
  452. cin >> minn >> maxx;
  453. v7 = randomMatrix({ q,w }, minn, maxx);
  454. print(v7, { q,w });
  455. v6.clear();
  456. break;
  457.  
  458.  
  459. //zadanie 7
  460. case 7:
  461.  
  462. unsigned int v,l ;
  463. cout << "podaj ilosc wierszy a potem kolumn: ";
  464. cin >> v;
  465. cin >> l;
  466. cout << "podaj minimalna liczbe: ";
  467. cin >> minnx;
  468. cout << "podaj maxymalna liczbe: ";
  469. cin >> maxxx;
  470. v6 = randomMatrix({ v,l },minnx,maxxx);
  471. v7 = randomMatrix({ v,l }, minnx, maxxx);
  472. print(v6, { v,l });
  473. cout << endl;
  474. print(v7, { v,l });
  475. cout << endl;
  476. v8 = add1({ v,l },minnx,maxxx,v6, v7);
  477. print(v8, { v,l });
  478. v8.clear();
  479. v8 = subtract1({ v,l }, minnx, maxxx, v6, v7);
  480. print(v8, { v,l });
  481. //zadanie 8
  482.  
  483. //zadanie 9
  484. case 9:
  485. double v1, l1;
  486. cout << "podaj ilosc wierszy a potem kolumn: ";
  487. cin >> v1;
  488. cin >> l1;
  489. cout << "podaj minimalna liczbe: ";
  490. cin >> minnx1;
  491. cout << "podaj maxymalna liczbe: ";
  492. cin >> maxxx1;
  493. m1 = randomMatrix1({ v1,l1 }, minnx1, maxxx1);
  494. forEach(m1, inv);
  495. cout << endl << endl;
  496. forEach(m1, inv_square);
  497. cout << endl;
  498. forEach(m1, sqrt);
  499. break;
  500. }
  501. cout << endl;
  502. cout << "ponowic? " << endl;
  503. cin >> wybor;
  504. } while (wybor == "tak");
  505. return 0;
  506. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement