Guest User

Untitled

a guest
Jun 25th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Wartosci {
  6. int * tab;
  7. int ilosc;
  8. string nazwa;
  9. public:
  10. Wartosci(): ilosc(0), nazwa("Brak nazwy") {
  11. tab = new int[0];
  12. }
  13. Wartosci(int * tab_, int ilosc_, string nazwa_): ilosc(ilosc_), nazwa(nazwa_) {
  14. tab = new int[ilosc_];
  15. for(int i = 0; i < ilosc_; i++) {
  16. tab[i] = tab_[i];
  17. }
  18. }
  19. int ile() {
  20. return ilosc;
  21. }
  22. string nazw() {
  23. return nazwa;
  24. }
  25. int pobierzWartosc(int i) {
  26. if(i >= 0 && i < ilosc) {
  27. return tab[i];
  28. }
  29. else {
  30. cout << "/nBłąd: Pobieranie wartosci poza zakresem!/n";
  31. }
  32. }
  33. void dodajWartosc(int w) {
  34.  
  35.  
  36. int * tabBuf = new int[ilosc];
  37. for(int i = 0; i < ilosc; i++) {
  38. tabBuf[i] = tab[i];
  39. }
  40. ilosc++;
  41. delete [] tab;
  42. tab = new int [ilosc];
  43. tab[ilosc - 1] = w;
  44. for(int i = 0; i < ilosc - 1; i++) {
  45. tab[i] = tabBuf[i];
  46. }
  47. delete [] tabBuf;
  48. }
  49. ~Wartosci() {
  50. delete [] tab;
  51. }
  52. };
  53.  
  54. ostream & operator <<(ostream & out, Wartosci w) {
  55. out << "Ilosc: " << w.ile() << ", nazwa zbioru: " << w.nazw() << ", wartosci:" << endl;
  56. for(int i = 0; i < w.ile(); i++) {
  57. out << w.pobierzWartosc(i) << " ";
  58. }
  59. return out;
  60. }
  61.  
  62. int main() {
  63. int tab[5] = {2, 5, 6, 4, 2};
  64. Wartosci a1(tab, 5, "Pomiar");
  65. cout << a1 << endl;
  66. a1.dodajWartosc(10);
  67. a1.dodajWartosc(20);
  68. a1.dodajWartosc(40);
  69. a1.dodajWartosc(50);
  70. cout << a1;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment