Laborator Nr.3 C++
Laborator Nr.3 C++
Laborator Nr.3 C++
RAPORT
Lucrare de laborator Nr.3
la Programarea in Limbajul C++
A efectuat:
A verificat:
Varianta 4
) S se creeze clasa Date data, care conine cmpurile: ziua, luna, anul. S se defineasc
operatorii "+" i "-" ca metode ale clasei, iar operatorii "++" i "--" n ambele variante(prefix i
postfix) ca funcii prietene. Operatorul "+" trebuie s permit realizarea operaiilor numai cu
variabilele de tip predefinit int (x=y+5). De prevzut prelucrarea corect a anilor biseci.
Textul Programului:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
using namespace std;
class Date
{
int day,year,month;
public:
Date();
Date(int,int,int);
Date(const Date&);
~Date();
Date& operator + (int);
Date& operator - (int);
friend Date& operator ++ (Date&); //Prefix ++
}
this->month=month;
if(this->month == 2)
{
this->month=month+add;
if(this->day == 29) { this->day=day-add; this->day=day+add; }
else if(this->day > 28) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 3)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 4)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 5)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 6)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 7)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 8)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 9)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 10)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 11)
{
this->month=month+add;
if(this->day > 30) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
this->month=month;
if(this->month == 31)
{
this->month=month+add;
if(this->day > 31) { this->day=day-add; }
if(this->month > 12) { this->month=month-add; }
}
return *this;
}
Date& Date::operator - (int add)
{
this->day=day-add;
this->year=year-add;
this->month=month-add;
if(this->day < 0) { this->day=day+add; }
if(this->month < 0) { this->month=month+add; }
if(this->year < 0) { this->year=year+year; }
return *this;
}
//Prefix++
Date& operator ++ (Date& obj)
{
++obj.day;
++obj.month;
++obj.year;
return obj;
}
//Postfix ++
Date operator ++ (Date& obj, int in)
{ Date copy_(obj);
obj.day++;
obj.month++;
obj.year++;
return copy_;
}
b) S se creeze clasa List coad. S se defineasc operatorii "+" de adunare a listelor, "=" de
atribuire ca metode ale clasei. S se defineasc operatorii de comparare "==", "!=", "<", ">" ca
funcii prietene. S se suprancarce operatorii "<<" i ">>" pentru ieiri/intrri de obiecte,
precum i pentru inserarea/eliminarea elementelor n/din coad. Clasa trebuie s fie absolut
funcional, adic s conin toi constructorii necesari i destructorul. Pentru simplificarea
lucrului s se utilizeze clasa sau structura ListItem pentru reprezentarea elementelor listei spre
care refer List.
Textul Programului:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
class Queue
{
private:
struct element
{
int var;
element *next;
};
element *head;
element *tail;
int nr;
public:
Queue()
{
head=tail=NULL;
nr=0;
};
~Queue();
void add_el(int);
void del_el();
int if_head() { if(if_empty()) cout<<"Eroare"; return head->var; }
int if_tail() { if(if_empty()) cout<<"Eroare"; return tail->var; }
int nr_el() const {return nr;};
bool if_empty() { return head==NULL;}
Queue& operator + (int);
Queue& operator = (Queue&);
};
Queue::~Queue()
{
while(head != NULL)
{
element *el=head;
head=head->next;
delete el;
}
}
void Queue::add_el(int obj)
{
if(if_empty())
{
head=new element;
head->var=obj;
head->next=NULL;
tail=head;
nr=1;
}
else
{
element *el= new element;
el->var=obj;
el->next=NULL;
tail->next=el;
tail=el;
++nr;
}
}
void Queue::del_el()
{
if(if_empty()) { cout<<"No queue!!!";}
}
int operator==(Queue& obj1,Queue& obj2){
if(obj1.nr_el()==obj2.nr_el()) { cout<<"Sunt egale! "; return 1;}
else { cout<<"Nu sunt egale "; return 0;}
}
int operator!=(Queue&obj1,Queue&obj2){
if(obj1.nr_el()!=obj2.nr_el()){ cout<<"Nu sunt egale "; return 1;}
else { cout<<"Sunt egale "; return 0;}
}
int operator<(Queue&obj1,Queue&obj2){
if(obj1.nr_el()<obj2.nr_el()){ cout<<"Prima lista este mai mica "; return 1;}
else { cout<<"A 2-a lista este mai mare"; return 0;}
}
int operator>(Queue&obj1,Queue&obj2){
if(obj1.nr_el()>obj2.nr_el()){ cout<<"Prima lista este mai mare "; return 1;}
else { cout<<"A 2-a lista este mai mica"; return 0;}
}
ostream& operator<<(ostream& out, Queue& obj)
{
Queue temp;
while (!obj.if_empty())
{
out << obj.if_head() << ' ';
temp.add_el(obj.if_head());
obj.del_el();
}
while (!temp.if_empty()){
obj.add_el(temp.if_head());
temp.del_el();
}
return out;
}
istream& operator>>(istream& in, Queue& obj)
{
int ob,n;
cout<<"Introduceti nr.de elemente a listei:";
in>>n;
cout<<"Introduceti lista:\n";
for(int i = 1; i <= n; i++){
cout<<"["<<i<<"]=";
in>>ob;
obj.add_el(ob);
}
return in;
}
int main()
{
Queue el1,el2,el3;
int k, op;
while(k=1)
{
system("cls");
cout<<"[1]Introducerea datelor primei liste";
cout<<"\n[2]Introducerea datelor a 2-a liste";
cout<<"\n[3]Afisarea listei";
cout<<"\n[4]Adunarea listei";
cout<<"\n[5]Comparare < ";
cout<<"\n[6]Comparare > ";
cout<<"\n[7]Operatia ==";
cout<<"\n[8]Atribuirea";
cout<<"\n\nComanda: ";
cin>>op;
switch(op)
{
case 0: system("cls");
getch(); exit(0);
case 1: system("cls");
cin>>el1;
getch();
break;
case 2: system("cls");
cin>>el2;
getch();
break;
case 3: system("cls");
cout<<"Prima lista:";
cout<<el1;
return 0;
}
Concluzie
In lucrarea data am obtinut deprinderi practice de utilizare functiilor de supraincarcarea
operatorilor. Am aflat ce operatori trebuie de supraincarcat si ce operatori nu se
recomanda de supraincarcat .Au fost obtinute cunostinte noi in limbajul de programare
C++ si anume supraincarcarea operatorilor si necesitatea lor.