Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################################################################################
- TEMA 2 - PROBLEMA 1.1
- ###########################################################################################
- #include <iostream>
- using namespace std;
- class Point {
- private:
- int x;
- int y;
- public:
- Point(int _x, int _y);
- Point(Point& y);
- Point& operator=(Point& p);
- void setX(int _x);
- void setY(int _y);
- int getX();
- int getY();
- void print();
- };
- Point::Point(int _x, int _y) {
- x = _x;
- y = _y;
- }
- Point::Point(Point& y) {
- x = y.x;
- this->y = y.y;
- }
- Point& Point::operator=(Point& p) {
- if (this != &p) {
- this->x = p.x;
- this->y = p.y;
- }
- return *this;
- }
- void Point::setX(int _x) {
- x = _x;
- }
- void Point::setY(int _y) {
- y = _y;
- }
- int Point::getX() {
- return x;
- }
- int Point::getY() {
- return y;
- }
- void Point::print() {
- cout << "\nx = " << x << "\n";
- cout << "y = " << y << "\n";
- }
- void main() {
- Point a(5, 9);
- Point b = a;
- a.print();
- b.print();
- Point* pc = new Point(a.getY() * 2, a.getX() * 2);
- pc->print();
- b.setX(99);
- *pc = b;
- pc->print();
- cin.get();
- }
- ############################################################################################
- TEMA 2 - PROBLEMA 1.2
- ############################################################################################
- #include <iostream>
- #include <math.h>
- using namespace std;
- class Triunghi {
- private:
- float a;
- float b;
- float c;
- static float checkA;
- static float checkB;
- static float checkC;
- public:
- Triunghi(float a, float b, float c);
- static bool verifica();
- void arie();
- void perimetru();
- void esteDreptunghic();
- };
- float Triunghi::checkA = 0;
- float Triunghi::checkB = 0;
- float Triunghi::checkC = 0;
- Triunghi::Triunghi(float a, float b, float c) {
- this->a = a;
- this->b = b;
- this->c = c;
- checkA = a;
- checkB = b;
- checkC = c;
- }
- void Triunghi::arie() {
- float p = (a + b + c) / 2;
- cout << "Aria triunghiului este : " << sqrt(p*(p - a)*(p - b)*(p - c));
- }
- void Triunghi::perimetru() {
- cout << "\nPerimetrul triunghilui este : " << a + b + c;
- }
- void Triunghi::esteDreptunghic() {
- float x = pow(c,2);
- float y = pow(a, 2) + pow(b, 2);
- if (x == y) {
- cout << "\nTriunghiul este dreptunghic!";
- }
- else {
- cout << "\nTriunghiul nu este dreptunghic!";
- }
- }
- bool Triunghi::verifica() {
- if ((checkA == 0) || (checkB == 0) || (checkC == 0)) {
- return false;
- }
- else {
- return true;
- }
- }
- void main() {
- Triunghi a(3,4,5);
- if (a.verifica() == false) {
- cout << "Laturile date nu pot forma un triunghi!\n";
- }
- else {
- a.arie();
- a.perimetru();
- a.esteDreptunghic();
- }
- cin.get();
- }
- ###################################################################################
- TEMA 2 - PROBLEMA 1.3
- ###################################################################################
- #include <iostream>
- #include <conio.h>
- using namespace std;
- class Pacient {
- private:
- char* nume;
- char* prenume;
- int varsta;
- char *diagnostic;
- static int nrPacienti;
- public:
- Pacient(const char* nume = "NA", const char* prenume = "NA", int varsta = 0, const char* diagnostic = "NA");
- Pacient(Pacient& p);
- ~Pacient();
- void citire();
- void afisare();
- static void setNrPacienti(int one);
- };
- int Pacient::nrPacienti = 1;
- Pacient::Pacient(const char* nume,const char* prenume, int varsta,const char* diagnostic) {
- this->nume = new char[strlen(nume) + 1];
- strcpy_s(this->nume, strlen(nume) + 1, nume);
- this->prenume = new char[strlen(prenume) + 1];
- strcpy_s(this->prenume, strlen(prenume) + 1, prenume);
- this->varsta = varsta;
- this->diagnostic = new char[strlen(diagnostic) + 1];
- strcpy_s(this->diagnostic, strlen(diagnostic) + 1, diagnostic);
- }
- Pacient::Pacient(Pacient& p) {
- nume = p.nume;
- prenume = p.prenume;
- varsta = p.varsta;
- diagnostic = p.diagnostic;
- }
- Pacient::~Pacient() {
- if (nume != NULL && prenume != NULL && diagnostic != NULL) {
- delete nume;
- delete prenume;
- delete diagnostic;
- }
- }
- void Pacient::citire() {
- nume = new char[25];
- prenume = new char[30];
- diagnostic = new char[40];
- cout << "Pacient #" << nrPacienti << "\n";
- cout << "Numele pacientului : ";
- cin >> nume;
- cout << "Prenumele pacientului : ";
- cin >> prenume;
- cout << "Varsta pacientului : ";
- cin >> varsta;
- cout << "Diagnosticul pacientului : ";
- cin >> diagnostic;
- nrPacienti++;
- }
- void Pacient::afisare() {
- cout << "Pacient #" << nrPacienti <<"\n";
- cout << " Nume : " << nume << "\n";
- cout << " Prenume : " << prenume << "\n";
- cout << " Varsta : " << varsta << "\n";
- cout << " Diagnostic : " << diagnostic << "\n";
- nrPacienti++;
- }
- void Pacient::setNrPacienti(int one) {
- nrPacienti = one;
- }
- //PROTOTIPURI DE FUNCTII
- void cicluCitire(Pacient pacient[], int& n);
- void cicluAfisare(Pacient pacient[], int& n);
- void callDestructors(Pacient pacient[], int& n);
- void main(){
- int n;
- Pacient pacient[10];
- cicluCitire(pacient, n);
- Pacient::setNrPacienti(1);
- cicluAfisare(pacient, n);
- callDestructors(pacient, n);
- _getch();
- }
- void cicluCitire(Pacient pacient[], int& n) {
- cout << "Introduceti numarul de pacienti : ";
- cin >> n;
- for (int i = 0; i < n; i++) {
- pacient[i].citire();
- }
- }
- void cicluAfisare(Pacient pacient[], int& n) {
- for (int i = 0; i < n; i++) {
- pacient[i].afisare();
- }
- }
- void callDestructors(Pacient pacient[], int& n) {
- for (int i = 0; i < n; i++) {
- pacient[i].~Pacient();
- }
- }
- ###################################################################################
- TEMA 2 - PROBLEMA 1.4
- ###################################################################################
- #include <iostream>
- #include <conio.h>
- using namespace std;
- class DataCalendaristica {
- private:
- int zi;
- int luna;
- int an;
- public:
- DataCalendaristica(int _zi = 1, int _luna = 1, int _an = 1700);
- static bool isA_ValidDate(int _zi, int _luna, int _an);
- void readDate();
- void displayDate();
- static DataCalendaristica nextDay(DataCalendaristica& ob);
- };
- //constructor de initializare
- DataCalendaristica::DataCalendaristica(int _zi, int _luna, int _an) {
- zi = _zi;
- luna = _luna;
- an = _an;
- }
- //Functie pentru verificare datei
- bool DataCalendaristica::isA_ValidDate(int _zi, int _luna, int _an) {
- int nrZileLuna[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- if (_an % 4 == 0) {
- nrZileLuna[1] = 29;
- }
- if (_luna > 12 || _zi > nrZileLuna[_luna - 1] || _luna < 1 || _zi < 1) {
- return false;
- }
- else {
- return true;
- }
- }
- void DataCalendaristica::readDate() {
- cout << "Zi : ";
- cin >> zi;
- cout << "Luna : ";
- cin >> luna;
- cout << "An : ";
- cin >> an;
- }
- //afisarea unei date
- void DataCalendaristica::displayDate() {
- int format;
- if (isA_ValidDate(zi, luna, an)) {
- do {
- cout << "Introduceti tipul formatului : \n";
- cout << "1. zz/ll/aaaa \n";
- cout << "2. ll/zz/aaaa \n";
- cout << "3. aaaa/ll/zz \n";
- cin >> format;
- } while (format < 1 || format > 3);
- switch (format) {
- case 1:
- cout << "\nData este : \n";
- cout << "\n" << zi << "/" << luna << "/" << an << "\n";
- break;
- case 2:
- cout << "\nData este : \n";
- cout << "\n" << luna << "/" << zi << "/" << an << "\n";
- break;
- case 3:
- cout << "\nData este : \n";
- cout << "\n" << an << "/" << luna << "/" << zi << "\n";
- break;
- }
- }
- else {
- cout << "\nData introdusa nu este una valida!\n";
- }
- }
- DataCalendaristica DataCalendaristica::nextDay(DataCalendaristica& ob) {
- int nrZileLuna[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
- DataCalendaristica obiect;
- if (ob.an % 4 == 0) {
- nrZileLuna[1] = 29;
- }
- obiect.zi = ob.zi + 1;
- obiect.luna = ob.luna;
- obiect.an = ob.an;
- if ((ob.zi + 1) > nrZileLuna[ob.luna - 1] && ob.luna != 12) {
- obiect.luna += 1;
- obiect.zi = 1;
- }
- else
- if ((ob.zi + 1) > nrZileLuna[ob.luna - 1] && ob.luna == 12) {
- obiect.zi = 1;
- obiect.luna = 1;
- obiect.an += 1;
- }
- return obiect;
- }
- void main() {
- DataCalendaristica data, data1;
- data.displayDate();
- cout << "\nApasati o tasta pentru a continua...\n";
- _getch();
- data.readDate();
- data.displayDate();
- cout << "\nApasati o tasta pentru a continua...\n";
- _getch();
- data1 = data.nextDay(data);
- data1.displayDate();
- _getch();
- }
- ###################################################################################
- TEMA 2 - PROBLEMA 1.5
- ###################################################################################
- #include <iostream>
- #include <conio.h>
- using namespace std;
- #define MAX 500
- class String {
- char text[MAX];
- public:
- String(const char tab[]); //constructor
- String(String& ob); //constructor de copiere
- char chartAt(int index); //intoarce caracterul de pe pozitia index
- String concat(String& str); //concateneaza sirul curent cu sirul str
- void print(); //afiseaza sirul de caractere curent
- };
- String::String(const char tab[]) {
- for (int i = 0; i < (strlen(tab)+1); i++) {
- text[i] = tab[i];
- }
- }
- String::String(String& ob) {
- for (int i = 0; text[i] != '\0'; i++) {
- text[i] = ob.text[i];
- }
- }
- char String::chartAt(int index) {
- cout << "Pozitia caracterului: "; cin >> index;
- return text[index];
- }
- String String::concat(String& str) {
- int mem = 0;
- for (int i = 0; text[i] != '\0'; i++) {
- mem++;
- }
- int i;
- for (i = 0; str.text[i] != '\0'; i++) {
- text[mem + i] = str.text[i];
- }
- text[mem + i] = '\0';
- return text;
- }
- void String::print() {
- for (int i = 0; text[i] != '\0'; i++) {
- cout << text[i];
- }
- }
- int main() {
- String str1("Luceafarul ");
- String str2("de dimineata");
- str1.print();
- str2.print();
- cout << "\n";
- str2 = str1.concat(str2);
- str2.print();
- _getch();
- return 0;
- }
- ###################################################################################
- TEMA 2 - PROBLEMA 1.6
- ###################################################################################
- #include <iostream>
- #include <conio.h>
- using namespace std;
- #define MAX 500
- class String {
- char text[MAX];
- public:
- String(const char tab[]); //constructor
- String(String& ob); //constructor de copiere
- char operator[](int index); //intoarece caracterul de pe pozitia index;
- String operator+(String& str); //concateneaza sirul curent cu stringul str
- friend ostream& operator<<(ostream& os, String& ob); // afiseaza sirul de caractere curent
- String toLowerCase(); //intoarce situl transformat in litere mici
- String substring(int start, int len); //returneaza un string ce incepe pe poztia
- //start si are len caractere
- };
- String::String(const char tab[]) {
- for (int i = 0; i < (strlen(tab)+1); i++) {
- text[i] = tab[i];
- }
- }
- String::String(String& ob) {
- strcpy_s(text,ob.text);
- }
- char String::operator[](int index) {
- return text[index];
- }
- String String::operator+(String& str) {
- int mem = 0;
- for (int i = 0; text[i] != '\0'; i++) {
- mem++;
- }
- int i;
- for (i = 0; str.text[i] != '\0'; i++) {
- text[mem + i] = str.text[i];
- }
- text[mem + i] = '\0';
- return text;
- }
- ostream& operator<<(ostream& os, String& ob) {
- for (int i = 0; ob.text[i] != '\0'; i++) {
- cout << ob.text[i];
- }
- return os;
- }
- String String::toLowerCase() {
- for (int i = 0; text[i] != '\0'; i++) {
- if ((int)text[i] >= 64 && (int)text[i] <= 90) {
- text[i] = (int)text[i] + 32;
- }
- }
- return text;
- }
- String String::substring(int start, int len) {
- String str3("");
- int k = 0;
- for(int i = start; k < len;i++){
- str3.text[k] = text[i];
- k++;
- }
- str3.text[k] = '\0';
- return str3;
- }
- void main() {
- String str1("Luceafarul ");
- String str2("de dimineata");
- cout << str1;
- cout << "\n";
- cout << str1[6];
- str1 = str1 + str2;
- str1.toLowerCase();
- cout << "\n";
- cout << str1;
- cout << "\n";
- String str3("");
- str3 = str1.substring(2, 3);
- cout << str3;
- _getch();
- }
- ###################################################################################
- TEMA 2 - PROBLEMA 1.7
- ###################################################################################
- #include <iostream>
- #include <conio.h>
- using namespace std;
- class String {
- char* text;
- public:
- String(const char tab[]); //constructor
- String(String& ob); //constructor de copiere
- String operator=(String& str); //operatorul '=' supraincarcat
- char chartAt(int index); //intoarce caracterul de pe pozitia index
- String concat(String& str); //concateneaza sirul curent cu sirul str
- int lenght(); //intoarce lungimea sirului
- void trim(); //sterge spatiile de la capetele sirului
- void print(); // Am ales sa introduc functia de print doar pentru a putea verifica
- };
- String::String(const char tab[]) {
- text = new char[strlen(tab) + 1];
- strcpy_s(text,strlen(tab) + 1,tab);
- }
- String::String(String& ob) {
- text = new char[strlen(ob.text)];
- strcpy_s(text,strlen(ob.text) + 1,ob.text);
- }
- String String::operator=(String& str) {
- int k, max;
- k = strlen(text);
- max = strlen(str.text);
- if (max != k) {
- if (k > max) {
- max = k;
- }
- for (int i = 0; i < max; i++) {
- strcpy_s(text + i,strlen(str.text) + 1,str.text + i);
- }
- }
- return text;
- }
- char String::chartAt(int index) {
- return *(text + index);
- }
- String String::concat(String& str) {
- int c1, c2; //contori
- c1 = strlen(text);
- c2 = strlen(str.text);
- for (int i = 0; i < c2; i++) {
- *(text + c1 + i) = str.text[i];
- }
- text[c1 + c2] = '\0';
- return text;
- }
- void String::print() {
- int n = strlen(text);
- for (int i = 0; i < n; i++) {
- cout << *(text + i);
- }
- }
- int String::lenght() {
- return strlen(text) + 1;
- }
- void String::trim() {
- for (int i = strlen(text) + 1; i >= 0; i--) {
- if (text + i == '\0') {
- delete (text + i);
- }
- }
- }
- void main() {
- String str1("Lucifer");
- String str2(" Morningstar");
- str1.print();
- cout << "\n";
- cout << str1.chartAt(0);
- cout << "\n";
- str1.concat(str2);
- str1.print();
- str1.trim();
- int k1 = str1.lenght();
- cout << "\n" << k1;
- _getch();
- }
- ###################################################################################
- TEMA 2 - PROBLEMA 1.8
- ###################################################################################
- #include <iostream>
- #include <conio.h>
- using namespace std;
- class Polinom {
- private:
- int grad;
- int* coef;
- public:
- Polinom(int n);
- Polinom(Polinom& ob);
- ~Polinom();
- int operator[](int i);
- Polinom& operator+(Polinom& p);
- friend Polinom& operator*(Polinom& p1, Polinom& p2);
- friend istream& operator>>(istream& is, Polinom& p);
- friend ostream& operator<<(ostream& os, Polinom& p);
- };
- Polinom::Polinom(int n) {
- grad = n;
- coef = new int[grad + 1];
- memset(coef, 0, sizeof(int) * (n + 1));
- }
- Polinom::Polinom(Polinom& ob) {
- grad = ob.grad;
- coef = new int[grad + 1];
- for (int i = 0; i <= grad; i++) {
- *(coef + i) = *(ob.coef + i);
- }
- }
- Polinom::~Polinom() {
- delete[] coef;
- }
- int max(int x, int y) {
- return ((x > y) ? x : y);
- }
- int Polinom::operator[](int i) {
- return((i >= 0 && i <= grad) ? *(coef + i) : 0);
- }
- Polinom& Polinom::operator+(Polinom& p) {
- Polinom *T;
- T = new Polinom(max(grad, p.grad));
- for (int i = 0; i <= T->grad; i++) {
- *(T->coef + i) = (*this)[i] + p[i];
- }
- return *T;
- }
- Polinom& operator *(Polinom& P1, Polinom& P2) {
- Polinom *T;
- T = new Polinom(P1.grad + P2.grad);
- for (int i = 0; i <= P1.grad; i++)
- for (int j = 0; j <= P2.grad; j++)
- T->coef[i + j] += P1[i] * P2[j];
- return *T;
- }
- istream& operator>>(istream& is, Polinom& p) {
- for (int i = 0; i <= p.grad; i++) {
- cout << "Coef[" << i << "] = ";
- is >> *(p.coef + i);
- }
- return is;
- }
- ostream& operator<<(ostream& os, Polinom& p) {
- os << "P(x) = ";
- for (int i = p.grad; i >= 0; i--) {
- if ((p.coef + i)) {
- if (i != p.grad) {
- os << ((*(p.coef + i) > 0) ? '+' : ' ');
- }
- os << *(p.coef + i) << "*x^" << i;
- }
- }
- os << endl;
- return os;
- }
- void main() {
- Polinom P(1), Q(2);
- cout << "Dati valorile primului polinom : \n";
- cin >> P;
- cout << "Dati valorile pentru al doilea polinom: \n";
- cin >> Q;
- Polinom R = P + Q;
- cout << "Suma : \n";
- cout << R;
- R = P * Q;
- cout << "Produsul : \n";
- cout << R;
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement