Witam!
Czy można ulepszyć(i czy w ogóle potrzebne) metody klasy łączących dwóch ciągów w jeden,przeciążając operator +.
Metody:
-ciąg znaków +obiekt.
String operator+(char *s,String &st1)
{
String dodaj;
int l=std::strlen(s);
l+=st1.len;
dodaj.str=new char[l+1];
std::strcpy(dodaj.str,s);
std::strcat(dodaj.str,st1.str);
return dodaj.str;
}
-obiekt +obiekt
String operator+(String &st3,String &st4)
{
String dodaj1;
int l=st3.len+st4.len;
dodaj1.str=new char[l+1];
std::strcpy(dodaj1.str,st3.str);
std::strcat(dodaj1.str,st4.str);
return dodaj1.str;
}
całe kody:
//string1,h
#include <iostream>
#ifndef STRING1_H_INCLUDED
#define STRING1_H_INCLUDED
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM=80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length() const{return len;}
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
friend String operator+(char *s,String &st1);
friend String operator+(String &st3,String &st4);
friend bool operator<(const String &st1,const String &st2);
friend bool operator>(const String &st,const String &st2);
friend bool operator==(const String &st,const String &st2);
friend std::ostream & operator<<(std::ostream &os,const String &st);
friend std::istream & operator>>(std::istream &is,String &st);
static int Howmany();
String & stringlow();
String & stringup();
int ile_liter( char a);
};
#endif // STRING1_H_INCLUDED
//string1.cpp
#include <iostream>
#include <cstring>
#include <cctype>
#include "string1.h"
int String::num_strings=0;
String::String()
{
len=4;
str=new char[1];
str[0]='\0';
num_strings++;
}
String::String(const char *s)
{
len=std::strlen(s);
str=new char[len+1];
std::strcpy(str,s);
num_strings++;
}
String::String(const String &st)
{
len=st.len;
str=new char[len+1];
std::strcpy(str,st.str);
num_strings++;
}
String & String::operator=(const String &sta)
{
if(this==&sta)
return *this;
delete [] str;
len=sta.len;
str=new char[len+1];
std::strcpy(str,sta.str);
return *this;
}
String & String::operator=(const char *sta)
{
delete [] str;
len=std::strlen(sta);
str=new char[len+1];
std::strcpy(str,sta);
return *this;
}
String operator+(char *s,String &st1)
{
String dodaj;
int l=std::strlen(s);
l+=st1.len;
dodaj.str=new char[l+1];
std::strcpy(dodaj.str,s);
std::strcat(dodaj.str,st1.str);
return dodaj.str;
}
String operator+(String &st3,String &st4)
{
String dodaj1;
int l=st3.len+st4.len;
dodaj1.str=new char[l+1];
std::strcpy(dodaj1.str,st3.str);
std::strcat(dodaj1.str,st4.str);
return dodaj1.str;
}
char & String::operator[](int i)
{
return str[i];
}
const char & String::operator[](int i) const
{
return str[i];
}
bool operator<(const String &st1,const String &st2)
{
return (std::strcmp(st1.str,st2.str)<0);
}
bool operator>(const String &st,const String &st2)
{
return st2.str < st.str;
}
bool operator==(const String &st,const String &st2)
{
return (std::strcmp(st.str,st2.str)==0);
}
std::ostream & operator<<(std::ostream &os,const String &st)
{
os<<st.str;
return os;
}
std::istream &operator>>(std::istream &is,String &st)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st=temp;
while(is && is.get()!='\n')
continue;
return is;
}
int String::Howmany()
{
return num_strings;
}
String::~String()
{
--num_strings;
//std::cout<<num_strings<<std::endl;
delete[]str;
}
String & String::stringlow()
{
int i=0;
char c;
while(str[i])
{
c=str[i];
c=tolower(str[i]);
str[i]=c;
i++;
}
}
String & String::stringup()
{
String st;
char c;
int i=0;
while(str[i])
{
c=str[i];
c=toupper(c);
str[i]=c;
i++;
}
return *this;
}
int String::ile_liter( char a)
{
String st;
int ile;
ile=0;
int i=0;
while(str[i])
{
if(str[i]==a)
ile++;
i++;
}
return ile;
}
//main.cpp
#include <iostream>
#include "string1.h"
int main()
{
String s1(" i ucze sie C++.");
String s2="Podaj swoje imie: ";
String s3;
std::cout<<s2; //przeciazony operator<<
std::cin>>s3; //przeciazony operator>>
s2="Mam na imie "+s3; //przeciazany operator+(char*s,String &st1)
std::cout<<s2<<std::endl;
s2=s2+s1; //przeciazaony operator+(String &s3,String &s4)
s2.stringup(); //zamiana liter ciagu na wielkie
std::cout<<"ciag\n"<<s2<<"\nzawiera "<<s2.ile_liter('A') //zlicza ile razy w ciagu
//wystepuje dany znak
<<"A.\n";
s1="czerwona";
String rgb[3]={String(s1),String("zielona"),String("niebieska")};
std::cout<<"podaj nazwe barwy podstawowej: ";
String ans;
bool succes =false;
while(std::cin>>ans)
{
ans.stringlow(); //zamiana na male litery
for(int i=0;i<3;i++)
{
if(ans==rgb[i])
{
std::cout<<"prawodlowo\n";
succes=true;
break;
}
}
if(succes)
break;
else
std::cout<<"sprobuj ponownie\n";
}
std::cout<<"\nzegnam\n";
return 0;
}