Advertisement
Razzim

MP8 zielonka 1 2 3 4 5 6

Nov 27th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. Zad1
  2. #include<iostream>
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct Student
  8. {
  9. string imie;
  10. string nazwisko;
  11. unsigned numer;
  12. };
  13.  
  14. struct Studenci
  15. {
  16. unsigned ile;
  17. Student * studenci;
  18. };
  19.  
  20. Student wczytaj(istream& in)
  21. {
  22. Student student;
  23. in>>student.imie;
  24. in>>student.nazwisko;
  25. in>>student.numer;
  26. return student;
  27. }
  28.  
  29. void wczytajs(Studenci& lista,istream& in)
  30. {
  31. in>>lista.ile;
  32. lista.studenci = new Student[lista.ile];
  33. for(unsigned i=0;i<lista.ile;i++)
  34. lista.studenci[i] = wczytaj(in);
  35. }
  36.  
  37. void wypisz(Studenci lista,ostream& out = cout)
  38. {
  39. out<<lista.ile<<"\n";
  40. for(unsigned i=0;i<lista.ile;i++)
  41. {
  42. out<<lista.studenci[i].imie<<" ";
  43. out<<lista.studenci[i].nazwisko<<" ";
  44. out<<lista.studenci[i].numer<<"\n";
  45. }
  46. }
  47.  
  48. void wypisz(Studenci lista,unsigned ktory, ostream& out = cout)
  49. {
  50. out<<lista.studenci[ktory-1].imie<<" ";
  51. out<<lista.studenci[ktory-1].nazwisko<<" ";
  52. out<<lista.studenci[ktory-1].numer<<"\n";
  53. }
  54.  
  55. void edytuj(Studenci lista,unsigned ktory)
  56. {
  57. cout<<"Imie: ";
  58. cin>>lista.studenci[ktory-1].imie;
  59. cout<<"Nazwisko: ";
  60. cin>>lista.studenci[ktory-1].nazwisko;
  61. cout<<"Indeks: ";
  62. cin>>lista.studenci[ktory-1].numer;
  63. }
  64.  
  65. int main(int ile, char*dane[])
  66. {
  67. ifstream txtin("in1.txt");
  68. ofstream txtout("out1.txt");
  69. Studenci listaS;
  70. wczytajs(listaS,txtin);
  71. unsigned n=1,t=0;
  72. while(n!=0)
  73. {
  74. cout<<"[1] Wyswietl wszystkie\n";
  75. cout<<"[2] Wyswietl konkretne\n";
  76. cout<<"[3] Edytuj konkretne\n";
  77. cout<<"[4] Zapisz\n";
  78. cout<<"[0] Zamknij\n";
  79. cout<<"Podaj nr: ";
  80. cin>>n;
  81.  
  82. switch(n)
  83. {
  84. case 1:
  85. wypisz(listaS);
  86. break;
  87. case 2:
  88. cout<<"Podaj nr studenta: ";
  89. cin>>t;
  90. if(t>=listaS.ile)
  91. {
  92. cout<<"Bledny nr studenta\n";
  93. break;
  94. }
  95. wypisz(listaS,t);
  96. break;
  97. case 3:
  98. cout<<"Podaj nr studenta: ";
  99. cin>>t;
  100. if(t>=listaS.ile)
  101. {
  102. cout<<"Bledny nr studenta\n";
  103. break;
  104. }
  105. edytuj(listaS,t);
  106. break;
  107. case 4:
  108. wypisz(listaS,txtout);
  109. break;
  110. case 0:
  111. n=0;
  112. break;
  113. default:
  114. cout<<"Bledny numer\n";
  115. }
  116. cout<<endl;
  117. }
  118. delete [] listaS.studenci;
  119. txtout.close();
  120. txtin.close();
  121. return 0;
  122. }
  123.  
  124. Zad2
  125. #include<iostream>
  126.  
  127. using namespace std;
  128.  
  129. class punkt
  130. {
  131. private:
  132. float _x,_y,_z;
  133.  
  134. public:
  135. float& x()
  136. {
  137. return _x;
  138. }
  139. float& y()
  140. {
  141. return _y;
  142. }
  143. float& z()
  144. {
  145. return _z;
  146. }
  147. const float& x() const
  148. {
  149. return _x;
  150. }
  151. const float& y() const
  152. {
  153. return _y;
  154. }
  155. const float& z() const
  156. {
  157. return _z;
  158. }
  159. punkt()
  160. {
  161. _x=0;
  162. _y=0;
  163. _z=0;
  164. }
  165. punkt(float a, float b, float c)
  166. {
  167. _x=a;
  168. _y=b;
  169. _z=c;
  170. }
  171.  
  172. };
  173.  
  174. /*
  175. class prostokat
  176. {
  177. private:
  178. float _a,_b;
  179. punkt _p;
  180.  
  181. public:
  182. prostokat()
  183. {
  184. _a=0;
  185. _b=0;
  186. _p.x() = 0;
  187. }
  188.  
  189.  
  190. };*/
  191.  
  192. int main(int ile,char*dane[])
  193. {
  194.  
  195. //...
  196. punkt p1, p2(1,2,3);
  197. const punkt p3(1.1,2.2,3.3);
  198. cout << p3.x() << '\t' << p3.y() << '\t' << p3.z() << endl;
  199. p1.x()=1; p1.y()=10; p1.x()=100;
  200. cout << p1.x() << '\t' << p1.y() << '\t' << p1.z() << endl;
  201. //...
  202.  
  203. return 0;
  204. }
  205.  
  206. Zad3
  207. #include<iostream>
  208. #include<cmath>
  209.  
  210. using namespace std;
  211.  
  212. class punkt
  213. {
  214. private:
  215. float _x,_y;
  216.  
  217. public:
  218. punkt()
  219. {
  220. _x=0;
  221. _y=0;
  222. }
  223. punkt(float a, float b)
  224. {
  225. _x=a;
  226. _y=b;
  227. }
  228. float& x()
  229. {
  230. return _x;
  231. }
  232. float& y()
  233. {
  234. return _y;
  235. }
  236. float odleglosc(punkt p)
  237. {
  238. return sqrt(pow(_x-p.x(),2)+pow(_y-p.y(),2));
  239. }
  240. };
  241.  
  242. class wielobok
  243. {
  244. private:
  245. punkt *tab;
  246. int n;
  247. public:
  248. wielobok(punkt * p,punkt * k)
  249. {
  250. n = k - p;
  251. tab = new punkt[n];
  252. int i = 0;
  253. while(p!=k)
  254. {
  255. tab[i] = *p;
  256. i++;
  257. p++;
  258. }
  259. }
  260. wielobok()
  261. {
  262. n = 0;
  263. tab = NULL;
  264. }
  265.  
  266. punkt &punkte(int nr)
  267. {
  268. return tab[nr];
  269. }
  270.  
  271. float obwod()
  272. {
  273. float obw=0;
  274. for(int i=0;i<n-1;i++)
  275. {
  276. obw+=tab[i].odleglosc(tab[i+1]);
  277. }
  278. obw+=tab[n].odleglosc(tab[0]);
  279. return obw;
  280. }
  281. void punkty(punkt * p,punkt * k)
  282. {
  283. n = k - p;
  284. tab = new punkt[n];
  285. int i = 0;
  286. while(p!=k)
  287. {
  288. tab[i] = *p;
  289. i++;
  290. p++;
  291. }
  292. }
  293. int ilosc()
  294. {
  295. return n;
  296. }
  297.  
  298. };
  299.  
  300.  
  301. int main(int ile,char* dane[])
  302. {
  303. punkt p(2, 3);
  304. cout << p.x() << ' ' << p.y() << '\n';
  305. p.x() = 1;
  306. p.y() = 1;
  307. cout << p.x() << ' ' << p.y() << '\n';
  308. cout << p.odleglosc(punkt()) << '\n';
  309.  
  310. punkt t[] = { punkt(0, 1), punkt(0, 0), punkt(1, 0), punkt(1, 1) };
  311.  
  312. wielobok w1(t, t+4);
  313. cout << w1.obwod() << '\n';
  314. w1.punkte(1) = punkt(0.5, 0.5);
  315. cout << w1.obwod() << '\n';
  316. wielobok w2;
  317. w2.punkty(t, t+3);
  318. cout << w2.obwod() << '\n';
  319. for (int i = 0; i < w2.ilosc(); ++i)
  320. cout << w2.punkte(i).x() << ' ' << w2.punkte(i).y() << '\n';
  321.  
  322. return 0;
  323. }
  324. Zad4
  325. #include<iostream>
  326.  
  327. using namespace std;
  328.  
  329. class adres
  330. {
  331. private:
  332. string _miasto, _ulica, _kod;
  333. unsigned int _nr;
  334. public:
  335. adres()
  336. {
  337.  
  338. }
  339. adres(string miasto, string kod, string ulica, unsigned nr)
  340. {
  341. _miasto = miasto;
  342. _ulica = ulica;
  343. _nr = nr;
  344. _kod = kod;
  345. }
  346. //ostream& operator <<()
  347. //{
  348. // out<<_mieszkanie;
  349.  
  350. // return out;
  351. // }
  352.  
  353. };
  354.  
  355. // Jesli jeden obiekt jest dynamiczny w klasie to musimy utworzyc:
  356. // 1. Konstruktor kopjujacy (domyslny kopjuje adres wskaznika, a nie dane)
  357. // osoba o2(o); ==to samo== osoba o2=o;
  358. // 2. Deztruktor
  359. // 3. Operator kopjujacy
  360.  
  361. class osoba
  362. {
  363. private:
  364. string _imie, _nazwisko;
  365. unsigned int _wiek;
  366. adres * _adres;
  367. public:
  368. osoba()
  369. {
  370. _adres = 0;
  371. }
  372. osoba(const osoba &o) // Konstruktor kopjujacy
  373. {
  374. _imie = o._imie;
  375. _nazwisko = o._nazwisko;
  376. _wiek = o._wiek;
  377.  
  378. _adres = new adres;
  379. *_adres = *o._adres;
  380. }
  381. osoba& operator =(const osoba &o) // Operator kopjujacy
  382. {
  383. if(&o != this) // Zbezpiczenie przed o2 = o2;
  384. {
  385. _imie = o._imie;
  386. _nazwisko = o._nazwisko;
  387. _wiek = o._wiek;
  388.  
  389. if (_adres != 0) delete _adres;
  390.  
  391. _adres = new adres;
  392. *_adres = *o._adres;
  393. }
  394. return *this;
  395. }
  396. osoba(string imie, string nazwisko, unsigned wiek,adres Adres)
  397. {
  398. _imie = imie;
  399. _nazwisko = nazwisko;
  400. _wiek = wiek;
  401. _adres = new adres;
  402. *_adres = Adres;
  403. }
  404. ~osoba() // Deztruktor
  405. {
  406. if (_adres != 0)
  407. {
  408. delete _adres;
  409. }
  410. }
  411.  
  412. };
  413.  
  414.  
  415. int main(int ile,char* dane[])
  416. {
  417.  
  418. adres* wsk = new adres("Czestochowa", "42-200", "Dabrowskiego", 73);
  419. //cout << *wsk << '\n';
  420.  
  421. adres a1(*wsk);
  422. delete wsk;
  423.  
  424. const adres* wsk1 = new adres("Warszawa", "00-950", "Mysliwiecka", 357);
  425. //cout << a1 << '\n';
  426. //cout << *wsk1 << '\n';
  427. adres a2;
  428. //cout << a2 << '\n';
  429. a2 = a1;
  430. //cout << a2 << '\n';
  431.  
  432. osoba o("Jan", "Kos", 25, *wsk1);
  433. //cout << o << '\n';
  434. osoba o1(o);
  435. //cout << o1 << '\n';
  436. osoba o2;
  437. //cout << o2 << '\n';
  438. o2 = o1;
  439. //cout << o2 << '\n';
  440. delete wsk1;
  441.  
  442. return 0;
  443. }
  444. Zad5
  445. #include<iostream>
  446.  
  447. using namespace std;
  448.  
  449. class punkt
  450. {
  451. private:
  452. double wsp[3];
  453.  
  454. public:
  455. punkt()
  456. {
  457. for(int i=0;i<3;i++)
  458. wsp[i]=0;
  459. }
  460. punkt(double p[])
  461. {
  462. for(int i=0;i<3;i++)
  463. wsp[i]=p[i];
  464. }
  465. punkt(const double a, const double b, const double c)
  466. {
  467. wsp[0]=a;
  468. wsp[1]=b;
  469. wsp[2]=c;
  470. }
  471. //operator przeciazony w klasie
  472. //zwraca operator'symbol'(argumenty)
  473. double& operator[](int i)
  474. {
  475. return wsp[i];
  476. }
  477. //operator dwuargumentowy w klasie
  478. punkt operator+(punkt p2)
  479. {
  480. punkt p3;
  481. for(int i=0;i<3;i++)
  482. p3[i]=wsp[i]+p2[i];
  483. return p3;
  484. }
  485. friend ostream& operator<<(ostream& out,const punkt& p)
  486. {
  487. out<<'('<<p[0]<<','<<p[1]<<','<<p[2]<<')';
  488. return out;
  489. }
  490. ostream& operator<<(ostream& out, punkt p)
  491. {
  492. out<<'('<<p[0]<<','<<p[1]<<','<<p[2]<<')';
  493. return out;
  494. }
  495. };
  496.  
  497. //operator dwuargumentowy poza klasa
  498. punkt operator+(punkt p1,punkt p2)
  499. {
  500. punkt p3;
  501. for(int i=0;i<3;i++)
  502. p3[i]=p1[i]+p2[i];
  503. return p3;
  504. }
  505.  
  506. punkt operator+(punkt p,double v)
  507. {
  508. punkt p3;
  509. for(int i=0;i<3;i++)
  510. p3[i]=p[i]+v;
  511. return p3;
  512. }
  513.  
  514. punkt operator+(double v,punkt p)
  515. {
  516. return p+v;
  517. }
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524. int main(int ile,char* dane[])
  525. {
  526. double x[2][3] = {{1.0, 1.0, 1.0},
  527. {1.0, 2.0, 3.0}};
  528. punkt p1(x[0]), p2(x[1]);
  529. const punkt p3(0.4, 0.2, 0.1);
  530. cout << p1 << ", " << p2 << "\n";
  531. cout << p3[0] << ' ' << p3[1] << ' ' << p3[2] << '\n';
  532. //cout << p1.distance(point()) << ", "<< p3.distance(p1) << '\n';
  533. //cout << p1 + p2 << ", " << p1 - p3 << '\n';
  534. //cout << 3.14 * p2 << ", " << p2 * 3.14 << '\n';
  535. //cout << (p1 < p3) << ", " << (p1 == punkt(1.0, 1.0, 1.0)) << '\n';
  536. //cin >> p1;
  537. //cout << p1 << '\n';
  538.  
  539. return 0;
  540. }
  541. Zad6
  542. #include<iostream>
  543. #include<fstream>
  544.  
  545.  
  546. using namespace std;
  547.  
  548. class punkt
  549. {
  550. private:
  551. double wsp[2];
  552.  
  553. public:
  554. punkt()
  555. {
  556. for(int i=0;i<2;i++)
  557. wsp[i]=0;
  558. }
  559. punkt(double p[])
  560. {
  561. for(int i=0;i<2;i++)
  562. wsp[i]=p[i];
  563. }
  564. punkt(const double a,const double b)
  565. {
  566. wsp[0]=a;
  567. wsp[1]=b;
  568. }
  569. double& operator[](int i)
  570. {
  571. return wsp[i];
  572. }
  573.  
  574. };
  575.  
  576.  
  577.  
  578.  
  579.  
  580. int main(int ile,char* dane[])
  581. {
  582. ifstream plik("z6.txt");
  583. if(!plik)
  584. {
  585. return 1;
  586. }
  587.  
  588. char tmp[255];
  589. int i=0;
  590. int non;
  591. int * poly;
  592. punkt * punkty;
  593. int n;
  594. double x,y;
  595.  
  596. while(i==0)
  597. {
  598. plik.getline(tmp,255);
  599. if(tmp[0] =='[')
  600. if(tmp[1] =='N')
  601. if(tmp[2] =='U')
  602. if(tmp[3] =='M')
  603. if(tmp[4] =='B')
  604. if(tmp[5] =='E')
  605. if(tmp[6] =='R')
  606. {
  607. cout<<tmp<<endl;
  608. plik>>non;
  609. cout<<non<<endl;
  610. punkty = new punkt[non];
  611. poly = new int[non];
  612. continue;
  613. }
  614.  
  615. if(tmp[0] =='[')
  616. if(tmp[1] =='N')
  617. if(tmp[2] =='O')
  618. if(tmp[3] =='D')
  619. if(tmp[4] =='E')
  620. if(tmp[5] =='S')
  621. if(tmp[6] ==']')
  622. {
  623. cout<<tmp<<endl;
  624. for(int i=0;i<non;++i)
  625. {
  626. plik>>n>>x>>y;
  627. //punkty[n-1].x() = x;
  628. punkty[n-1][0] = x;
  629. punkty[n-1][1] = y;
  630. cout<<n<<' '<<punkty[n-1][0]<<' '<<punkty[n-1][1]<<endl;
  631. }
  632. continue;
  633. }
  634.  
  635. if(tmp[0] =='[')
  636. if(tmp[1] =='P')
  637. if(tmp[2] =='O')
  638. if(tmp[3] =='L')
  639. if(tmp[4] =='Y')
  640. if(tmp[5] =='G')
  641. if(tmp[6] =='O')
  642. if(tmp[7] =='N')
  643. if(tmp[8] ==']')
  644. {
  645. cout<<tmp<<endl;
  646. for(int i=0;i<non;++i)
  647. {
  648. plik>>poly[i];
  649. cout<<poly[i]<<' ';
  650. }
  651. cout<<endl;
  652. break;
  653. }
  654.  
  655. }
  656.  
  657. //Pole
  658. double pole=0;
  659.  
  660. for(int q=0;q<non-1;q++)
  661. pole+=punkty[poly[q]-1][0]*punkty[poly[q]][1]-punkty[poly[q]][0]*punkty[poly[q]-1][1];
  662.  
  663. pole/=2;
  664. cout<<"Pole: "<<pole<<endl;
  665.  
  666.  
  667. return 0;
  668. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement