Zadatak c++, pomoc

Zadatak c++, pomoc

offline
  • Pridružio: 18 Jan 2013
  • Poruke: 13

Nikako da rijesim problem konkatencije dva stringa pomocu tri objekta. Inicializovao bih prva dva objekta pa preko preklopljenog operatora "+" bih da spojim ta dva objekta pa da dodijelim trecem objektu. Uradio sam na naci da spojim dva objekta preko preklopljenog operatora "+=". Kod izgleda ovako

  1. #include<iostream>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. class Tekst
  6. {
  7.     friend ostream &operator<<(ostream &,const Tekst &);
  8.     friend istream &operator>>(istream &,Tekst &);
  9. public:
  10.     Tekst();
  11.     Tekst(const char *);
  12.     Tekst(const Tekst &);
  13.     Tekst &operator=(Tekst &t);
  14.     Tekst &operator+(Tekst);
  15.     Tekst &operator+=(Tekst);
  16.     ~Tekst();
  17. private:
  18.     char *txt;
  19. };
  20. ostream &operator<<(ostream &out,const Tekst &t)
  21. {
  22.     out<<t.txt; return out;
  23. }
  24. istream &operator>>(istream &in,Tekst &t)
  25. {
  26.     in>>t.txt; return in;
  27. }
  28. Tekst::Tekst()
  29. {
  30.     txt=new char(0);
  31. }
  32. Tekst::Tekst(const char *t)
  33. {
  34.         txt=new char[strlen(t)+1];
  35.         strcpy(txt,t);
  36. }
  37. Tekst::Tekst(const Tekst &t)
  38. {
  39.         txt=new char[strlen(t.txt)+1];
  40.         strcpy(txt,t.txt);
  41. }
  42. [color=red]Tekst &Tekst::operator=(Tekst &t)
  43. {
  44.     if(this!=&t)
  45.     {
  46.         delete [] txt;
  47.         txt=new char(strlen(t.txt)+1);
  48.         strcpy(txt,t.txt);
  49.     }
  50.     return *this;
  51. }
  52. Tekst &Tekst::operator+(const Tekst t)
  53. {
  54.     Tekst r(t);
  55.     int n; n=strlen(txt)+1;
  56.     r.txt=(char*)realloc(r.txt,n*sizeof(char));
  57.     //strcat(r.txt," ");
  58.     strcat(r.txt,txt);
  59.     return r;
  60. }
  61. Tekst &Tekst::operator+=(Tekst t)
  62. {
  63.         int n; n=strlen(t.txt)+strlen(txt)+1;
  64.     txt=(char*)realloc(txt,n*sizeof(char));
  65.     strcat(txt," ");
  66.     strcat(txt,t.txt);
  67.     return *this;
  68. }[/color]
  69. Tekst::~Tekst(){ delete [] txt;}
  70. main()
  71. {
  72.     int i=0;
  73.     Tekst a,b("Beograd"),c(b),d,f;
  74.     cout<<"a: "; cout<<a<<endl;
  75.     cout<<"b: "; cout<<b<<endl;
  76.     cout<<"c: "; cout<<c<<endl;
  77.     d=b;
  78.     cout<<"d: "; cout<<d<<endl;
  79.     c+=d;
  80.     cout<<"Prva konkatenacija: "<<c;
  81.     f=c+d;
  82.     cout<<"\nDruga konkatenacija: "<<f;
  83.  
  84. }


Kriticni dio je ovaj:
  1. Tekst &Tekst::operator+(const Tekst t)
  2. {
  3.     Tekst r(t);
  4.     int n; n=strlen(txt)+1;
  5.     r.txt=(char*)realloc(r.txt,n*sizeof(char));
  6.     //strcat(r.txt," ");
  7.     strcat(r.txt,txt);
  8.     return r;
  9. }

Ako neko ima resenje neka ponudi.. unaprijed hvala



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • Pridružio: 19 Maj 2011
  • Poruke: 297

Ne mixuj realloc sa new/delete!!!
Za operatore +, += argument treba da bude referenca

  1. Tekst& Tekst::operator+(const Tekst& t)
  2. {
  3.     char* temp = new char[ strlen(t.txt) + strlen(txt) + 1 ];
  4.     strcpy(temp, txt);
  5.     delete [] txt;
  6.     strcat(temp, t.txt);
  7.     txt = temp;
  8.     return *this;
  9. }


Pisem napamet, mislim da nisam pogresio.



offline
  • Pridružio: 18 Jan 2013
  • Poruke: 13

Hvala na odgovoru, znam da ne bi trebalo mijesati, na to nas je i profesor upozoravao, ali u nedostatku ideja posegnuo sam za nekim ne tako dobrim resenjima. Imas samo malu gresku u kodu stavljao si samo 't' kao argument kod funkcije 'strlen' i 'strcat' a trebalo je 't.txt', nakon te prepravke program radi eksta. Jos jednom hvala na pomoci. Smile

  1. Tekst &Tekst::operator+(const Tekst& t)
  2. {
  3.     char* temp= new char[ strlen(t.txt) + strlen(txt) + 1 ];
  4.     strcpy(temp, txt);
  5.     delete [] txt;
  6.     strcat(temp, t.txt);
  7.     txt = temp;
  8.     return *this;
  9. }

offline
  • Pridružio: 19 Maj 2011
  • Poruke: 297

Ipak sam se zeznuo, operator + treba da vrati privremen objekat dok += moze da ostane kao sto sam napisao. Kad zavrsim neka posla napisacu kod da valja, mada moze neko i u medjuvremenu da pomogne.

offline
  • Pridružio: 18 Jan 2013
  • Poruke: 13

Uradio sam ja, al' sam zaboravio da azuriram odgovor Smile.
Kod izgleda ovako:

  1. Tekst &Tekst::operator+(const Tekst& t)
  2. {
  3.     Tekst k;
  4.     k.txt= new char[ strlen(t.txt) + strlen(txt) + 1 ];
  5.     strcpy(k.txt, txt);
  6.     strcat(k.txt, t.txt);
  7.     return k;
  8. }

ili sva tri operatora '+','=','+='
  1. Tekst &Tekst::operator=(Tekst &t)
  2. {
  3.     if(this!=&t)
  4.     {
  5.         delete [] txt;
  6.         txt=new char(strlen(t.txt)+1);
  7.         strcpy(txt,t.txt);
  8.     }
  9.     return *this;
  10. }
  11. Tekst &Tekst::operator+(const Tekst& t)
  12. {
  13.     Tekst k;
  14.     k.txt= new char[ strlen(t.txt) + strlen(txt) + 1 ];
  15.     strcpy(k.txt, txt);
  16.     strcat(k.txt, t.txt);
  17.     return k;
  18. }
  19. Tekst &Tekst::operator+=(Tekst t)
  20. {
  21.     char* temp= new char[ strlen(t.txt) + strlen(txt) + 1 ];
  22.     strcpy(temp, txt);
  23.     delete [] txt;
  24.     strcat(temp, t.txt);
  25.     txt = temp;
  26.     return *this;
  27. }

offline
  • Pridružio: 19 Maj 2011
  • Poruke: 297

Samo male izmene da bude "savrseno":

  1. // ovo
  2. Tekst &Tekst::operator=(Tekst &t)
  3.  
  4. // u ovo, jer ne menjamo t objekat
  5. Tekst &Tekst::operator=(const Tekst &t)


  1. // ovo
  2. Tekst& Tekst::operator+(const Tekst& t)
  3.  
  4. // u ovo jer ne mozes vratiti referencu na privremen objekat (bez posledica)
  5. Tekst Tekst::operator+(const Tekst& t)


  1. // ovo
  2. Tekst &Tekst::operator+=(Tekst t)
  3.  
  4. // u ovo
  5. Tekst &Tekst::operator+=(const Tekst& t)

offline
  • Pridružio: 18 Jan 2013
  • Poruke: 13

Kad prebacim ovo

  1. Tekst& Tekst::operator+(const Tekst& t)

u ovo
  1. Tekst Tekst::operator+(const Tekst& t)

program mi "pukne". Odradi konkatenciju stringova i izbaci gresku "Tekst.exe has stopped working"

offline
  • Pridružio: 19 Maj 2011
  • Poruke: 297

Evo ti ceo kod ispravljen:

  1. #include<iostream>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. class Tekst
  8. {
  9.    friend ostream &operator<<(ostream &,const Tekst &);
  10.    friend istream &operator>>(istream &,Tekst &);
  11. public:
  12.    Tekst();
  13.    Tekst(const char *);
  14.    Tekst(const Tekst &);
  15.    Tekst &operator=(const Tekst &t);
  16.    Tekst operator+(const Tekst &t);
  17.    Tekst &operator+=(const Tekst &t);
  18.    ~Tekst();
  19. private:
  20.    char *txt;
  21. };
  22. ostream &operator<<(ostream &out,const Tekst &t)
  23. {
  24.    out<<t.txt; return out;
  25. }
  26.  
  27. istream &operator>>(istream &in,Tekst &t)
  28. {
  29.    in>>t.txt; return in;
  30. }
  31.  
  32. Tekst::Tekst()
  33. {
  34.    txt= 0;//new char(0);
  35. }
  36.  
  37. Tekst::Tekst(const char *t)
  38. {
  39.    txt=new char[strlen(t)+1];
  40.    strcpy(txt,t);
  41. }
  42.  
  43. Tekst::Tekst(const Tekst &t)
  44. {
  45.    txt=new char[strlen(t.txt)+1];
  46.    strcpy(txt,t.txt);
  47. }
  48.  
  49. Tekst &Tekst::operator=(const Tekst &t)
  50. {
  51.     if(this!=&t)
  52.     {
  53.         delete [] txt;
  54.         txt=new char[strlen(t.txt)+1];
  55.         strcpy(txt,t.txt);
  56.     }
  57.     return *this;
  58. }
  59. Tekst Tekst::operator+(const Tekst& t)
  60. {
  61.     Tekst k;
  62.     k.txt= new char[ strlen(t.txt) + strlen(txt) + 1 ];
  63.     strcpy(k.txt, txt);
  64.     strcat(k.txt, t.txt);
  65.     return k;
  66. }
  67. Tekst &Tekst::operator+=(const Tekst& t)
  68. {
  69.     char* temp= new char[ strlen(t.txt) + strlen(txt) + 1 ];
  70.     strcpy(temp, txt);
  71.     delete [] txt;
  72.     strcat(temp, t.txt);
  73.     txt = temp;
  74.     return *this;
  75. }
  76.  
  77. Tekst::~Tekst()
  78. { delete [] txt; txt = 0; }
  79.  
  80. int main()
  81. {
  82.    int i=0;
  83.    Tekst a,b("Beograd"),c(b),d,f;
  84.    //cout<<"a: "; cout<<a<<endl;
  85.    cout<<"b: "; cout<<b<<endl;
  86.    cout<<"c: "; cout<<c<<endl;
  87.    d=b;
  88.    cout<<"d: "; cout<<d<<endl;
  89.    c+=d;
  90.    cout<<"Prva konkatenacija: "<<c;
  91.    f=c+d;
  92.    cout<<"\nDruga konkatenacija: "<<f;
  93.  
  94.    return 0;
  95. }


Imao si gresku u operator = , txt treba da bude pokazivac na niz karaktera a ti si ga alocirao tamo kao pokazivac na 1 char:
  1. txt=new char(strlen(t.txt)+1);


Takodje sam izmenio default konstruktor jer ne mora nista da alocira.

Ko je trenutno na forumu
 

Ukupno su 1043 korisnika na forumu :: 12 registrovanih, 0 sakrivenih i 1031 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: 4thFlavian, BZ, Ca6otep, comi, drpera, edman, Malahit, PrincipL, RD84, Rebel Frank, shajone, Zukov