Sunteți pe pagina 1din 13

Universitatea Tehnica a Moldovei

Raport
Lucrarea de laborator Nr.3
Programarea orientata pe obiecte

Var 17

Grupa: AI-211
Student: Sofroni Maxim
Profesor: Sergiu Scrob
Lucrarea de laborator nr.3

Sarcina Lucrarii:
Varianta 7
а) Să se creeze clasa Bool – variabile logice. Să se definească operatorii
"+" – SAU logic, "*" – ŞI logic, "^" – SAU EXCLUSIV, ca metode ale
clasei, iar operatorii "==" şi "!=" – ca funcţii prietene. Operatorii trebuie
să permită realizarea operaţiilor atît cu variabilele clasei date, cît şi cu
variabilele de tip predefinit int. (Dacă numărul întreg este diferit de zero,
se consideră că variabila este adevăr, altfel – fals.)
b) Să se creeze clasa String – şir, utilizînd memoria dinamică. Să se
definească operatorii "+" – adunarea şirurilor, "=" şi "+=" – atribuirea ca
funcţii prietene. Să se definească operatorii de comparare: "==", "!=",
"<", ">" ca metode ale clasei. Operatorii trebuie să lucreze atît cu String,
cît şi cu char*. Să se definească operatorul "[]" de acces la fiecare simbol
în parte. Să se supraîncarce operatorii "<<" şi ">>" pentru ieşiri/intrări
de obiecte.

Cod:
SArcina a);

#include <iostream>
using namespace std;
class Bool {
int x;
public:
Bool();
void get();
void show();
Bool operator ||(const Bool&);
Bool operator *(const Bool&);
Bool operator ^(const Bool&);
Bool operator ||(const int);
Bool operator *(const int);
Bool operator ^(const int);
friend bool operator ==(Bool, Bool);
friend bool operator !=(Bool, Bool);
};
Bool::Bool() {
x = 0;
}
void Bool::get()
{
int n;
cin >> n;
if (n == 0) { x = 0; }
else if (n == 1) { x = 1; }
else if (n != 0) { x = 1; }
}
void Bool::show()
{
cout << x << endl;
}
Bool Bool::operator ||(const Bool& obj)
{
Bool* sum = new Bool();
if ((x == obj.x) && (obj.x == 0)) {
sum->x = 0;
}
else if ((x == 1) || (obj.x == 1)) {
sum->x = 1;
}
return *sum;
}
Bool Bool::operator ||(const int a)
{
Bool* sum = new Bool();
if ((x == a) && (a == 0)) {
sum->x = 0;
}
else if ((x == 1) || (a == 1)) {
sum->x
= 1;
}
return *sum;
}
Bool Bool::operator *(const Bool& obj)
{
Bool* prod = new Bool();
if ((x == obj.x) && (obj.x == 1)) {
prod->x = 1;
}
else if ((x == 0) || (obj.x == 0)) {
prod->x = 0;
}
return *prod;
}
Bool Bool::operator *(const int a)
{
Bool* prod = new Bool();
if ((x == a) && (a == 1)) {
prod->x =
1;
}
else if ((x == 0) || (a == 0)) {
prod -> x = 0;
}
return *prod;
}
Bool Bool:: operator ^(const Bool& obj)
{
Bool* sum = new Bool();
if ((x == obj.x) && ((obj.x == 1) ||
(obj.x == 0))) {
sum->x = 0;
}
else if ((x == 1) || (obj.x == 1)) {
sum->x = 1;
}
return *sum;
}
Bool Bool:: operator ^(const int a)
{
Bool* sum = new Bool();
if ((x == a) && ((a == 1) || (a == 0)))
{
sum->x = 0;
}
else if ((x == 1) || (a == 1)) {
sum->x
= 1;
}
return *sum;
}
bool operator !=(Bool A, Bool B)
{
bool n;
if (A.x != B.x) { n = true; }
else { n = false; }
return n;
}
bool operator ==(Bool A, Bool B)
{
bool n;
if (A.x == B.x) { n = true; }
else { n = false; }
return n;
}
int main()
{
Bool A, B;
Bool* temp = new Bool();
int op;
while (1) {
system("cls");
cout << "\n Meniu" << endl;
cout << "[1] Introducerea variabelelor logice" << endl;
cout << "[2] Afisarea variabelelor logice" << endl;
cout << "[3] Operatiunea OR" << endl;
cout << "[4] Operatiunea AND" << endl;
cout << "[5] Operatirunea XOR" << endl;
cout << "[6] Verifica pentru egalitate" << endl;
cout << "[10] Iesire" << endl;
cin >> op;
switch (op) {
case 1: system("cls");
cout << "Introduceti prima variabila" << endl;
A.get();
cout << "Introduceti adoua variabila" << endl;
B.get();
cout << "Introducerea a avut loc cu succes!" << endl;system("pause");
break;
case 2:
system("cls");
cout << "Variabilele logice:\n";
A.show();
B.show();
system("pause");
break;
case 3:
system("cls");
*temp = A || B;
cout << " A + B = ";
temp->show();
cout << "\n";
system("pause");
break;
case 4:
system("cls");
*temp = A * B;
cout << " A * B = ";
temp->show();
cout << "\n";
system("pause");
break;
case 5:
system("cls");
*temp = A ^ B;
cout << " A ^ B = ";
temp->show();
cout << "\n";
system("pause");
break;
case 6:
system("cls");
if (A == B) {
cout <<
"Variabilele sunt egale" << endl;
}
else if (A != B) {
cout <<
"Variabilele nu sunt egale" << endl;
}
system("pause");
break;
case 10:
exit(0);
break;
}
}
return 0;

}
SArcina b);
#include <iostream>
using namespace std;
class String
{
private:
size_t _Size;
char* _String;
public:
String();
String(const char* Str);
String(const String& Str);
~String();
// Atribuirea sirurilor
String& operator=(const String& Str);
// Concatinarea sisurilor
String& operator+= (const char* Str);
String& operator+= (String& Str);
String& operator+= (char Ch);
String& Copy(const char* s);
String& Copy(String& s);
String& Cat(const char* Str);
String& Cat(char Ch);
String& Cat(String& Str);
size_t Length(void) { return _Size; }
string operator [] (string rhs)
{
int k = 0, x; char Ch;
for (rhs.begin(); k <
rhs.length();
rhs.end())
cout << "[" << rhs[k++] <<"]";
cout << endl;
k = 0;
for (rhs.begin(); k <
rhs.length(); rhs.end())
cout << "[" << k++ << "]";
cout << endl;
cout << "\nIntroduceti pozitia unui element x care doriti sa schimbati : ";
cin >> x;
cout << "Introduceti un character: ";
cin >> Ch;
k = 0;
for (rhs.begin(); k <
rhs.length(); rhs.end())
{
if (k == x)
rhs[k] = Ch;
k++;
}
cout << endl;
k = 0;
for (rhs.begin(); k <
rhs.length(); rhs.end())
cout << "[" << rhs[k++] <<
"]";
system("pause");
return rhs;
}
// Compararea sirurilor
bool operator== (const char* Str)
const;
bool operator== (const String& Str)
const;
bool operator!= (const char* Str)
const;
bool operator!= (const String& Str)
const;
bool operator < (const char* Str);
bool operator < (String& Str);
bool operator > (const char* Str);
bool operator > (String& Str);
int Compare(const char* Str) const;
int Compare(String& Str) const;
void Set(const char* Str);
void Set(const String& Str);
char* Get() const;
// Aflarea lungimei sirurlui
int StrLength() const;
friend ostream& operator<< (ostream&
Out, String& Str);
friend istream& operator>> (istream&
In, String& Str);
};
String::String()
{
_Size = 0;
_String = 0;
}
String::~String()
{
delete[] _String;
_String = NULL;
}
String::String(const char* Str)

{
_String = new char[strlen(Str) + 1];
strcpy(_String, Str);
}
String::String(const String& Str)
{
_String = new char[strlen(Str._String)
+ 1];
strcpy(_String, Str._String);
}
// supraincarcarea operatorului +
String operator+(const String& S1, const
String& S2)
{
char* Temp = new char[S1.StrLength() +
S2.StrLength() + 1];
strcpy(Temp, S1.Get());
strcat(Temp, S2.Get());
return String(Temp);
}
// copierea sirului *char
String& String::Copy(const char* Str)
{
delete[] _String;
_Size = strlen(Str);
_String = new char[_Size + 1];
strcpy(_String, Str);
return *this;
}
// copierea sirului tip string
String& String::Copy(String& Str)
{
Copy(Str._String);
return *this;
}
// \concatinarea sirului *char
String& String::Cat(const char* Str)
{
_Size = Length() + strlen(Str);
char* Temp = new char[_Size + 1];
strcpy(Temp, _String);
strcat(Temp, Str);
delete[] _String;
_String = Temp;
return *this;
}
// concatinarea sirului de tipul clasei
String& String::Cat(String& Str)
{
Cat(Str._String);
return *this;
}
// Concatinarea simbolului
String& String::Cat(char Ch)
{
_Size++;
char* Temp = new char[_Size + 1];
strcpy(Temp, _String);
delete[] _String;
_String = Temp;
Temp += _Size;
*Temp = '\0';
Temp--;
*Temp = Ch;
return *this;
}
// supraincarcarea operatorului += cu argumentul de tip* char
String& String::operator+= (const char* Str)
{
Cat(Str);
return *this;
}
// supraincarcarea operatorului += cu argumentul de tipul clasei
String& String::operator+= (String& Str)
{
Cat(Str);
return *this;
}
// supraincarcarea opertorului += cu argumentul pentru simbol
String& String::operator+= (char Ch)
{
Cat(Ch);
return *this;
}
// Supraincarcarea operatorului =
String& String::operator=(const String& Str)
{
char* Temp = new
char[strlen(Str._String) + 1];
strcpy(Temp, Str._String);
delete[] _String;
_String = Temp;
return *this;
}
// supraincarcarea operatorului == cu argumentul de tip* char
bool String::operator== (const char* Str)
const
{
if (Compare(Str) == 0)
return true;
return false;
}
// Перегрузка оператор operator== с аргументом типа класс
bool String::operator== (const String& Str)
const
{
if (Compare(Str._String) == 0)
return true;
return false;
}
// supraincarcarea operatorului != cu argumentul de tip* char
bool String::operator!= (const char* Str)
const
{
if (Compare(Str) != 0)
return true;
return false;
}
// supraincarcarea operatorului != cu argumentul de tip clasa
bool String::operator!= (const String & Str)
const
{
if (Compare(Str._String) != 0)
return true;
return false;
}
// supraincarcarea operatorului < cu argumentul de tip* char
bool String::operator < (const char* Str)
{
if (Compare(Str) < 0)
return true;
return false;
}
// supraincarcarea operatorului < cu argumentul de tip clasa
bool String::operator < (String & Str)
{
if (Compare(Str._String) < 0)
return true;
return false;
}
// supraincarcarea operatorului > cu argumentul de tip* char
bool String::operator > (const char* Str)
{
if (Compare(Str) > 0)
return true;
return false;
}
// supraincarcarea operatorului > cu argumentul de tip clasa
bool String::operator > (String & Str)
{
if (Compare(Str._String) > 0)
return true;
return false;
}
// Compararea sirurilor, argumentul de tip *char trimis de obiect se compara cu membru_String
int String::Compare(const char* Str) const
{
return (strcmp(_String, Str));
}
// Compararea sirurilor, argumentul de tip clasa trimis de obiect se compara cu membru_String
int String::Compare(String & Str) const
{
return (strcmp(_String, Str._String));
}
// Aflam lungimea sirului trimis ca argument si trimitem ca argument in functia Set
void String::Set(const char* Str)
{
Set(String(Str));
}
// Adaugam un sir
void String::Set(const String & Str)
{
*this = Str;
}
// returnam sirul _String
char* String::Get() const
{
return _String;
}
// Aflam lungimea sirului _String
int String::StrLength() const
{
return strlen(_String);
}
istream& operator>> (istream & Input, String &
Str)
{
Input >> Str._String; return Input;
}
ostream& operator<< (ostream & Output, String &
Str)
{
Output << Str._String; return Output;
}
int main()
{
int op;
String IsSpace = " ", ObjectA, ObjectB,
ObjectC;
char FirstStr[128], SecondStr[128];
String ObjectArray;
while (1)
{
system("cls");
cout << "Meniu\n\n";
cout << "[1]: Introducerea sirurilor de caractere\n";
cout << "[2]: Operator \"+ \" |Concatinarea sirurilor de caractere\n";
cout << "[3]: Operator \"= \" |Atribuire\n";
cout << "[4]: Operator \"+=\" |Concatinare cu atribuire\n";
cout << "[5]: Operator \"==\" |Compararea sirurilor pentru egalitate\n";
cout << "[6]: Operator \"!=\" |Compararea sirurilor pentru inegalitate\n";
cout << "[7]: Operator \"< \" |Compararea sirului care este mai mic decit\n";
cout << "[8]: Operator \"> \" |Compararea sirului care este mai mare decit\n";
cout << "[9]: Operator \"[]\" |Acces la fiecare simbol\n";
cout << "[0]: Iesire\n\n";
cout << "Introduceti optiunea:"
<< endl;
cin >> op;
switch (op)
{
case 1:
system("cls");
cout << "Introduceti primul sir : ";
cin >> FirstStr;
cout << "Introducetial doilea sir : ";
cin >> SecondStr;
ObjectA = FirstStr;
ObjectB = SecondStr;
system("cls");
cout << "Sirurile sunt :"
<< endl;
cout << " << " << ObjectA
<< " >> \n";
cout << " << " << ObjectB
<< " >> \n";
break;
case 2:
system("cls");
cout << "Sirurile sunt :"
<< endl;
cout << " << " << ObjectA
<< " >> \n";
cout << " << " << ObjectB
<< " >> \n";
cout << "Sirul concatinat:";
ObjectC = ObjectA +
IsSpace + ObjectB;
ObjectC.Get();
cout << " << " << ObjectC
<< " >> \n";
break;
case 3:
system("cls");
ObjectC = "Acesta este un exemplu de sir atribuit...\n";
ObjectC.Get();
cout << ObjectC;
break;
case 4:
system("cls");
ObjectA += ObjectB;
ObjectA.Get();
cout << "\nAtribuirea sirurilor combinate : ";
cout << ObjectA << endl;
break;
case 5:
system("cls");
if (ObjectA == ObjectB)
cout << "Sirurile sunt egale!" << endl;
else
{
cout << "Sirurile nu sunt egale!" << endl;
}
break;
case 6:
system("cls");
if (ObjectA != ObjectB)
{
cout << "Sirurile nu sunt egale!" << endl;
}
else
cout << "Sirurile sunt egale" << endl;
break;
case 7:
system("cls");
cout << " << " << ObjectA
<< " >> ";
cout << " << " << ObjectB
<< " >> \n";
if (ObjectA < ObjectB)
cout << "Sirul << "
<< ObjectA << " >> este mai mic!" << endl;
else
cout << "Sirul << "
<< ObjectB << " >> este mai mic!" << endl;;
break;
case 8:
system("cls");
cout << " << " << ObjectA
<< " >> ";
cout << " << " << ObjectB
<< " >> \n";
if (ObjectA > ObjectB)
cout << "Sirul << "
<< ObjectA << " >> este mai mare!" << endl;
else
cout << "Sirul << "
<< ObjectB << " >> este mai mare!" << endl;
break;
case 9:
system("CLS");
ObjectArray["C++. The best programming language!"];
break;
case 0:
return 0;
default:
system("cls");
cout << "Ati introdus o optiune gresita!" << endl;
break;
}
system("pause");
}
}

Rezultat:
a)

b)

S-ar putea să vă placă și