Beruflich Dokumente
Kultur Dokumente
/*
program to implement the class 'String'
overload the operator
+ : to concatenate 2 strings
- : to eliminate one string from another
= : to assign 2 strings
*/
#include <iostream>
using namespace std;
class String {
private:
// data
char *p;
public:
// constructor, it reads from cin (or stdin ? :)) )
String();
// constructor 2
String(char *);
// destructor
~String(void);
// operators
String & operator = (String);
friend String operator + (String &, String &);
friend String operator - (String &, String &);
// functions
char *getstr(void){
return p;
}
};
// main
int main(void){
String a,b;
cout<<"a+b (concatenation) result:
"<<(a+b).getstr(); // nice C++ syntax
cout<<"\na-b (elimination) result: "<<(a-b).getstr();
return 0;
}
// constructor, it reads from cin (or stdin ? :)) )
String::String(){
char buf[200];
cout<<"reading a string: "; cin>>buf;
p = new char[ strlen(buf)+1];
strcpy(p,buf);
}
// constructor 2
String::String(char *buf){
p = new char[ strlen(buf)+1];
strcpy(p,buf);
}
// destructor
String::~String(void){
delete [] p;
}
// operators
// =
// NOTE: operator = returns String _&_
String& String::operator = (String x){
if( this == &x)
return *this;
if( p != NULL)
delete [] p;
p = new char[strlen(x.p)+1];
strcpy(p,x.p);
return *this;
}
// +
String operator + (String &a,String &b){
char buf[200];
strcpy(buf,a.p);
strcat(buf,b.p);
return String(buf);
}
// String operator - (String &o, String &x){
char *pp;
pp = strstr(o.p, x.p);
if(pp == NULL)
return String(o.p);
else {
// a little pointer arithmetic here
char buf[200];
strncpy(buf,o.p,pp-o.p);
strcpy(buf+(pp-o.p),pp+strlen(x.p));
return String(buf);
}
}
if(!p2){
printf("\n Nu am putut aloca memorie pt. p2 ");
exit(1);
}
p1->arata();
p2->arata();
delete(p1);
delete(p2);
}
Exemple carte 2011:
/* O alta clasa de tip string pentru efectuarea de operatii pe siruri de caractere*/
#include <iostream>
#include <string.h>
using namespace std;
class CString {
char *p;
int len;
public:
CString(char *);
CString();
CString(const CString &);
~CString() {delete []p;}
void show() { cout << p << "\n"; }
CString operator=(CString &);
CString operator+(CString &);
CString operator-(CString &);
int operator==(CString &s) { return !strcmp(p, s.p);}
int operator!=(CString &s) { return strcmp(p, s.p);}
int operator<(CString &s) { return strcmp(p, s.p) < 0;}
int operator<=(CString &s) { return strcmp(p, s.p) <= 0;}
int operator>(CString &s) { return strcmp(p, s.p) > 0;}
int operator>=(CString &s) { return strcmp(p, s.p) >= 0;}
int operator==(char *s) { return !strcmp(p, s);}
int operator!=(char *s) { return strcmp(p, s);}
};
CString::CString()
{
len = 1;
p = new char[len];
if(!p)
{
if(!temp.p)
{
cout << "eroare de alocare \n";
exit(1);
}
strcpy(temp.p, p);
strcat(temp.p, s.p);
return temp;
}
CString CString::operator-(CString &s)
{
// aici trebuie scris codul ce face supraincarcarea
}
CString dictionar[][2] = {
{"carte", "volum, tom"},
{"magazin", "pravalie, shop, butic"},
{"",""}};
void main(void)
{
char temp[16];
cout << "Introdu un cuvant : ";
cin >> temp;
CString x(temp);
for(int i=0; dictionar[i][0] != ""; i++)
if(dictionar[i][0] == x)
{
cout << "\tSinonime : ";
dictionar[i][1].show();
}
}
}
Matrix operator + (Matrix &p, Matrix &q)
{
if (p.rows == q.rows && p.cols == q.cols)
{
Matrix m(p.rows, p.cols);
for (register int r = 0; r < p.rows; ++r)
for (register int c = 0; c < p.cols; ++c)
m(r,c) = p(r,c) + q(r,c);
return m;
}
else
{
/*
Matrix operator - (Matrix &p, Matrix &q)
{
if (p.rows == q.rows && p.cols == q.cols)
{
Matrix m(p.rows, p.cols);
for (register int r = 0; r < p.rows; ++r)
for (register int c = 0; c < p.cols; ++c)
m(r,c) = p(r,c) - q(r,c);
return m;
}
else
{
cout<<"\n Matricile nu au acelasi numar de linii si coloane! \n";
exit(0);
}
}
*/
Matrix operator * (Matrix &p, Matrix &q)
{
if (p.cols == q.rows)
{
Matrix m(p.rows, q.cols);
for (register int r = 0; r < p.rows; ++r)
for (register int c = 0; c < q.cols; ++c) {
m(r,c) = 0.0;
for (register int i = 0; i < p.cols; ++i)
m(r,c) += p(r,i) * q(i,c);
}
return m;
}
else
{
cout<<"\n Numar de linii si colane incorect
exit(0);
}
}
void main (void)
{
int i, j;
Matrix m(2,3);
m(0,0) = 10; m(0,1) = 20;
m(1,0) = 15; m(1,1) = 25;
m(0,2) = 30;
m(1,2) = 35;
for(i=0;i<2;i++) {
cout<<endl;
for(j=0;j<3;j++)
cout<<m(i,j)<<"\t";
}
Matrix n=m;
cout<<endl;
for(i=0;i<2;i++) {
cout<<endl;
for(j=0;j<3;j++)
cout<<n(i,j)<<"\t";
}
Matrix dif =n-m;
cout<<endl;
for(i=0;i<2;i++) {
cout<<endl;
for(j=0;j<3;j++)
cout<<dif(i,j)<<"\t";
}
_getch();
}