//================= string2212.cpp - hlavička třídy =========
// trasujte a divejte se co se deje
#ifndef STRING_H_DEF
#define STRING_H_DEF
#include <stdio.h>
#include <string.h>
class String {
static int Poradi;
static int Aktivnich;
char *txt;
int delka;
int Index;
public:
inline String(void) ;
String( const String &b );
String(double d);
String( const char *t );
~String(void) {Aktivnich--;if (txt != NULL) delete [ ] txt; txt = NULL;}
String Soucet (String const & s) ;
String& Prirad (String const & s) ;
void PriradSpojeni(String const & s1,String const& s2);
int SrovnejDelky(String const & s) const
{if (delka == s.delka) return 0;
if (delka < s.delka) return -1; else return 1;}
int StrCmp(String const &s) // vynechám - li const, potom se při volání const objektem
// vytvoří (s jako) tmp objekt, který se může měnit
{return SrovnejDelky(s);}
bool JeMensi(String const &s) {if (StrCmp(s) < 0) return true;else return false;}
bool JeVetsi(String const &s) {if (StrCmp(s) > 0) return true;else return false;}
bool JeRovno(String const &s) {if (StrCmp(s) == 0) return true;else return false;}
};
inline String::String(void)
{
Index = Poradi;
Poradi++;
Aktivnich++;
txt = NULL;
delka = 0;
}
#endif
//================= string2212.cpp - zdrojový kód třídy =========
// trasujte a divejte se co se deje
#include "string2212.h"
int String::Poradi = 0;
int String::Aktivnich=0;
String::String( const String &b )
{
Index = Poradi;
Poradi++;
Aktivnich++;
delka = b.delka;
if (delka > 0)
{
txt = (char *)new char [ delka ] ;
int i;
for (i = 0; i <delka; i++) txt [ i ] = b.txt [ i ];
}
else
txt = NULL;
}
String::String(double d)
{
Index = Poradi;Poradi++;Aktivnich++;
char tmp[50] = "";
sprintf ( tmp, " %lf ", d );
delka = strlen ( tmp ) + 1;
txt = (char *) new char [ delka ] ;
int i;
for (i = 0; i <delka-1; i++) txt[i] = tmp[ i ];
txt [ delka -1] ='\0';
}
String::String( const char *t )
{
Index = Poradi;
Poradi++;
Aktivnich++;
delka = strlen ( t ) + 1;
if (delka > 1)
{
txt = new (char [ delka ]);
int i;
for (i = 0; i < delka; i++) txt [ i ] = t [ i ];txt [ delka -1] ='\0';
}
else
{
delka = 0;
txt = NULL;
}
}
String String::Soucet(String const & s)
{
String a ;
int i , j ;
a.delka = delka + s.delka - 1;
if ( a. delka == 0 )
a.txt = NULL;
else
{
a.txt = new char [ a.delka ] ;
for ( i = 0 ; i < delka-1 ; i ++ ) a. txt [ i ] = txt [ i ] ;
for ( j = 0 ; j < s.delka ; j ++ , i ++ ) a. txt [ i ] = s.txt [ j ] ;
a.txt [ a.delka-1 ] = '\0' ;
}
return a;
}
String& String::Prirad (String const &s)
{
if (&s == this) return (*this);
if (txt) delete[] txt;
delka = s.delka;
if (delka > 0)
{
txt = (char *)new char [ delka ] ;
int i;
for (i = 0; i <delka; i++)
txt [ i ] = s.txt [ i ];
}
else
txt = NULL;
return *this;
}
void String::PriradSpojeni(String const &s1,String const & s2)
{
char *pom = (char *) new char[s1.delka+s2.delka+1];
int i , j ;
delka = s1.delka + s2.delka - 1;
if ( delka == 0 )
pom = NULL;
else
{
for ( i = 0 ; i < s1.delka-1 ; i ++ ) pom [ i ] = s1.txt [ i ] ;
for ( j = 0 ; j < s2.delka ; j ++ , i ++ ) pom [ i ] = s2.txt [ j ] ;
pom [ i ] = '\0' ;
if (txt) delete[] txt;
txt = pom;
}
}
//================= string2212p.cpp =========
// trasujte a divejte se co se deje
#include "string2212.h"
int main() {
String a;
String b(8.3),c(3),d("sadfl");
String e(b),f=d;
String g="akdf ",h=5;
a.PriradSpojeni(b,d);
e.Prirad(a.Soucet(c));
d.PriradSpojeni(5," askdlf ");
return 1;
}