/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: Alessandro Barazzuol
*
* Created on XX-XX-XX, 13.17
*/
#include <cstdlib>
#include<iostream>
#include<stdio.h>
using namespace std;
class stringa
{
private:
int lunghezza;
char *array;
char *ptr;
/*metodi di utilità*/
int abs(int a)
{
if(a>=0)
return a;
else
return -a;
}
int lung( char *str)
{
int i;
i=0;
while(str[i]!='\0')
i++;
return i;
}
int lung(const char *str)
{
int i;
i=0;
while(str[i]!='\0')
i++;
return i;
}
void add_char(char *str,char c)
{
int l=lung(str);
str[l]=c;
str[l+1]='\0';
}
public:
/*costruttore di default, costruisce una stringa nulla, di lunghezza 0*/
stringa()
{
array =new char[1];
array[0]='\0';
lunghezza=lung(array);
ptr=nullptr;
}
/*costuttore a parametri che costruisce una determinata stringa*/
stringa(const char *str)
{
int i;
lunghezza=lung(str);
array=new char[lunghezza];
for(i=0;i<lung(str);i++)
array[i]=str[i];
array[i]='\0';
}
/*lunghezza di una stringa */
int strlung()
{
return lung(array);
}
/*riscrittura operator = per assegnare stringhe a stringhe */
stringa& operator=(stringa &str)
{
this->lunghezza=lung(str.array);
this->array=new char[lung(str.array)];
for(int i=0;i<lung(str.array);i++)
this->array[i]=str.array[i];
return *this;
}
/*riscrittura operator = per assegnare array a stringhe */
stringa& operator=(const char *str)
{
this->lunghezza=lung(str);
this->array=new char[lung(str)];
for(int i=0;i<lung(str);i++)
this->array[i]=str[i];
return *this;
}
/*riscrittura operatore <<*/
friend ostream& operator<<( ostream &o, stringa& str)
{
/*for(int i=0;i<strlen(str.array);i++)
{
o<<str.array[i];
}*/
o<<str.array;
return o;
}
/*concatena due stringhe */
stringa& operator +=(stringa& str)
{
int i;
int l=lung(this->array)+lung(str.array)+1;//perche cè '\0'
char *temp=new char[l];
for( i=0;i<lung(this->array);i++)
temp[i]=this->array[i];
for(int j=0;j<lung(str.array);j++)
{
temp[i++]=str.array[j];
}
temp[i]='\0';
/*libero la memoria puntatata da array*/
delete this->array;
/*rifaccio puntare array alla nuovo stringa*/
this->array=temp;
/*libero la memoria temp*/
delete [] temp;
this->lunghezza=l;
return *this;
}
/*concatena una stringa con un array */
stringa& operator +=(char *str)
{
int i;
int l=lung(this->array)+lung(str)+1;//perche cè '\0'
char *temp=new char[l];
for( i=0;i<lung(this->array);i++)
temp[i]=this->array[i];
for(int j=0;j<lung(str);j++)
{
temp[i++]=str[j];
}
temp[i]='\0';
/*libero la memoria puntatata da array*/
delete this->array;
/*rifaccio puntare array alla nuovo stringa*/
this->array=temp;
/*libero la memoria temp*/
delete [] temp;
this->lunghezza=l;
return *this;
}
/*legge un carrattere della stringa*/
char operator[](int n)const
{
if(n<0 || n>=lunghezza)
return '\0';
else
return array[n];
}
/*assegna un carrattere della stringa*/
char& operator[](int n)
{
if(n<0 || n>=lunghezza)
return array[lunghezza-1];
else
return array[n];
}
/*calcola una sottostringa con l'uso del puntatore ptr*/
char* sub(int n, int m)
{
int k=0,i;
ptr=array+n;
char *temp=new char[abs(n-m)];
temp[0]='\0';
for( i=n;i<n+abs(n-m);i++)
{
add_char(temp,*(ptr));
ptr++;
k++;
}
ptr=nullptr;
temp[k]='\0';
return temp;
}
};
Post Views: 981