Advertisement
Guest User

P2

a guest
Jan 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. -------------------
  2. Trzeba napisać program który wrzuci do kontenera typu set pory roku, wypisze na ekran ilość elementów w kontenerze za pomocą metody size () i czy dana pora roku jest w kontenerze count ()
  3.  
  4. :#include <iostream>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, char** argv) {
  10. set <string> pory_roku;
  11. pory_roku.insert("wiosna");
  12. pory_roku.insert("lato");
  13. pory_roku.insert("jesien");
  14. pory_roku.insert("zima");
  15. cout << "Ilosc elementow w zbiorze: " << pory_roku.size() << endl;
  16. if(pory_roku.count("zima")!=0)
  17. {
  18. cout << "Zima jest w kalendarzu";
  19. }
  20.  
  21. return 0;
  22. }
  23.  
  24. -------------
  25.  
  26. Program ktory wyswietli wprowadzone pory roku, wypisze w odwrotnej kolejności, usunie pierwsza pore roku, sprawdzi czy zostało usunięte, wyswietlic ilosc elementow
  27.  
  28. #include <iostream>
  29. #include <map>
  30.  
  31. using namespace std;
  32.  
  33. int main(){
  34.  
  35. map <int, string> Seasons;
  36. Seasons[1] = "Wiosna";
  37. Seasons[2] = "Lato";
  38. Seasons[3] = "Jesien";
  39. Seasons[4] = "Zima";
  40.  
  41. map <int, string> :: reverse_iterator it;
  42. for(it=Seasons.rbegin(); it!=Seasons.rend(); ++it)
  43. cout << it -> second << endl << endl;
  44.  
  45. Seasons.erase(1); // Delete first item
  46.  
  47. if(Seasons.find(1) == Seasons.end())
  48. cout << "Nie znalazlem";
  49.  
  50. cout << "W mapie sa: " << Seasons.size() << " elementy" << endl;
  51.  
  52.  
  53. return EXIT_SUCCESS;
  54. }
  55.  
  56. ------------
  57. Trzeba było napisać funkcje liczące pole kwadratu, koła i chyba trapezu (wszystkie osobno) i je wywołać w mainie
  58.  
  59. #include <iostream>
  60.  
  61. using namespace std;
  62.  
  63. template <typename T> T square( T a ){
  64. return a*a;
  65. }
  66.  
  67. template <typename T> T trapeze( T a, T b, T h ){
  68. return 0.5*(a+b)*h;
  69. }
  70.  
  71. template <typename T> T circle( T r ){
  72. return 3.14*r*r;
  73. }
  74.  
  75. int main(){
  76. short option = 0;
  77. cout << "1 kwadrat" << endl << "2 trapez" << endl << "3 kolo" << endl << endl;
  78. cin >> option;
  79.  
  80. double a = 0, b = 0, c = 0;
  81. switch(option){
  82. case 1:
  83. cout << "Podaj a: ";
  84. cin >> a;
  85. cout << "Pole kwadratu: " << square(a);
  86. break;
  87.  
  88. case 2:
  89. cout << "Podaj a: ";
  90. cin >> a;
  91. cout << "Podaj b: ";
  92. cin >> b;
  93. cout << "Podaj h: ";
  94. cin >> c;
  95. cout << "Pole trapezu: " << trapeze(a, b, c);
  96. break;
  97.  
  98. case 3:
  99. cout << "Podaj r: ";
  100. cin >> a;
  101. cout << "Pole kola: " << circle(a);
  102. break;
  103. }
  104.  
  105.  
  106. return 0;
  107. }
  108.  
  109. --------------
  110. #include <iostream>
  111. #include <list>
  112.  
  113. using namespace std;
  114.  
  115. template <class T>
  116. class Lista_dwukierunkowa
  117. {
  118. list<T>lista;
  119. public:
  120. void dodaj(T a, T b, T c, T d)
  121. {
  122. lista.push_back(a);
  123. lista.push_back(b);
  124. lista.push_back(c);
  125. lista.push_back(d);
  126. }
  127. void usun()
  128. {
  129. while(!lista.empty())
  130. {
  131. cout << lista.back() << '\n';
  132. lista.pop_back();
  133. }
  134. }
  135. };
  136.  
  137. int main()
  138. { int e,f,g,h;
  139. cin>>e>>f>>g>>h;
  140. Lista_dwukierunkowa<int> test;
  141. test.dodaj(e,f,g,h);
  142. test.usun();
  143.  
  144. float e1,f1,g1,h1;
  145. cin>>e1>>f1>>g1>>h1;
  146. Lista_dwukierunkowa<float> test2;
  147. test2.dodaj(e1,f1,g1,h1);
  148. test2.usun();
  149. return 0;
  150. }
  151. ------------
  152. Napisz program który przeciąży operatory == oraz != w klasie wektor (dwa wymiary) przeciazenia maja byc zaprzyjaznione
  153.  
  154. #include <iostream>
  155.  
  156. using namespace std;
  157.  
  158. class wektor{
  159. int x,y;
  160. public:
  161. wektor(int x, int y)
  162. {
  163. this->x = x;
  164. this->y = y;
  165. }
  166. friend bool operator ==(wektor liczba_1,wektor liczba_2 );
  167. friend bool operator !=(wektor liczba_1, wektor liczba_2);
  168. };
  169.  
  170. bool operator ==(wektor liczba_1, wektor liczba_2)
  171. {
  172. if(( liczba_1.x == liczba_2.x ) &&( liczba_1.y == liczba_2.y ) )
  173. return true;
  174. else
  175. return false;
  176.  
  177. }
  178. bool operator !=(wektor liczba_1, wektor liczba_2)
  179. {
  180. if(( liczba_1.x == liczba_2.x ) &&( liczba_1.y == liczba_2.y ) )
  181. return false;
  182. else
  183. return true;
  184.  
  185. }
  186.  
  187.  
  188. int main(int argc, char** argv) {
  189. wektor a(2,2);
  190. wektor b(2,2);
  191. if(a==b) cout << "rowne" << endl;
  192. wektor c(3,3);
  193. if(a!=c) cout << "Rozne" << endl;
  194. return 0;
  195. }
  196.  
  197. -------------
  198. Napisz program w którym użytkownik stworzy klasę Punkt, współrzędne x,y. Przeciazy operator wejściowy i przetestuje program
  199. str. wyjściowy <<
  200. str. wejsciowy >>
  201.  
  202. #include <iostream>
  203.  
  204. using namespace std;
  205.  
  206. class punkt
  207. {
  208. public:
  209. int x, y;
  210.  
  211.  
  212. friend istream &operator >>(istream &wejscie, punkt &a)
  213. {
  214. wejscie >> a.x >> a.y;
  215. }
  216. };
  217.  
  218. int main(int argc, char** argv) {
  219. punkt a;
  220. cin >> a;
  221. return 0;
  222. }
  223.  
  224. --------
  225. Napisać program "Studniówka" który sprawdza czy każda osoba ma parę, i jeśli kobiet jest więcej od mężczyzn to wyrzucić wyjątek oraz pokazać ile osób nie ma pary
  226.  
  227. #include<iostream>
  228. #include<string>
  229. #include<cmath>
  230. #include<cstdlib>
  231. #include <cstdio>
  232. #include <sstream>
  233. using namespace std;
  234. double pary(double, double);
  235. int main()
  236. {
  237. try
  238. {
  239. pary(10, 2);
  240. }
  241. catch(string w)
  242. {
  243. cout<<"Wyjatek: "<< w;
  244. }
  245. cin.get();
  246. return 0;
  247. }
  248. double pary(double chlopcy, double kobiety) //funkcja zwraca iloraz a / b
  249. {
  250. int zmienna;
  251.  
  252. if (kobiety>chlopcy) {
  253.  
  254. zmienna = kobiety - chlopcy;
  255.  
  256. std::ostringstream oss;
  257. oss << "KOBIET ZA DUZO , MEZCZYZN NIE DO PARY: " << zmienna;
  258. string adi = oss.str();
  259.  
  260.  
  261. throw adi;
  262.  
  263. }
  264. if(kobiety<chlopcy){
  265. string wyjatek = "Nie do pary , za duzo mezczyzn !";
  266. zmienna = chlopcy - kobiety;
  267. std::ostringstream oss;
  268. oss << "MEZCZYZN ZA DUZO , KOBIET NIE DO PARY: " << zmienna;
  269. string adi = oss.str();
  270. throw adi;
  271. }
  272.  
  273. }
  274. -------
  275. napisac na podstawie kontenera list liste osob zawieraja imie i nazwisko
  276. piszac jakies 2 dowolne metody i stosujac dwa dowolne wyjatki
  277. poprzednia grupa miala cos podobnego tyko, ze na innych kontenerach
  278.  
  279. #include <iostream>
  280. #include <exception>
  281. #include <list>
  282.  
  283. using namespace std;
  284.  
  285. struct osoba
  286. {
  287. string imie, nazwisko;
  288. };
  289.  
  290. class osoby
  291. {
  292. list <osoba> nowa;
  293. public:
  294. void dodaj()
  295. {
  296. try
  297. {
  298. string imie, nazwisko;
  299. cout << "Podaj imie: " <<endl;
  300. cin >> imie;
  301. cout << "Podaj nazwisko: " <<endl;
  302. cin >> nazwisko;
  303. if(imie.size() > 20)
  304. throw (string) "Za dlugie";
  305. if(nazwisko.size() > 20)
  306. throw (string) "Za dlugie";
  307. struct osoba a;
  308. a.imie = imie;
  309. a.nazwisko = nazwisko;
  310. nowa.push_back(a);
  311. }
  312. catch (string e)
  313. {
  314. cout << "Blad: " << e <<endl;
  315. }
  316. }
  317. void wyswietl()
  318. {
  319. try
  320. {
  321. if(nowa.size()==0)
  322. throw (string) "Brak osob na liscie";
  323. cout << "Lista osob: " <<endl;
  324. list <osoba>::iterator i;
  325. for(i=nowa.begin();i!=nowa.end();i++)
  326. cout << (*i).imie << " " << (*i).nazwisko <<endl;
  327. }
  328. catch (string e)
  329. {
  330. cout << "Blad: " << e <<endl;
  331. }
  332. }
  333. };
  334. int main()
  335. {
  336. osoby obiekt;
  337. obiekt.dodaj();
  338. obiekt.dodaj();
  339. obiekt.dodaj();
  340. obiekt.wyswietl();
  341.  
  342. return 0;
  343. }
  344. Jeżeli dostaniecie na wejściówce kontener set to nie używajcie struktury
  345. -------
  346. lista
  347. #include <iostream>
  348. #include <list>
  349. #include <set>
  350.  
  351. using namespace std;
  352.  
  353. int main(){
  354. list<string> lista;
  355. int i =0;
  356. while(i<100){
  357. i++;
  358. int iloscliczb;
  359. string fullname;
  360.  
  361. cout << "Podaj ile elementow chcesz dodac" << endl;
  362. cin >> iloscliczb;
  363. for( int it=0; it<iloscliczb; it++ ){
  364. cout << "Podaj nazwe" << endl;
  365. cin >> fullname;
  366. lista.push_back(fullname);
  367. }
  368.  
  369. list<string>::reverse_iterator it;
  370. for( it=lista.rbegin(); it!=lista.rend(); ++it )
  371. {
  372. cout<< *it << " ";
  373. }
  374. cout << endl;
  375.  
  376. return 0;
  377. }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement