Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <string.h>
- using namespace std;
- class SistemSolar;
- class Planeta{
- private:
- char nume[10];
- double raza;
- double distantaSoare;
- float durRotatie;
- public:
- Planeta(const char *nume="", double raza=0, double distantaSoare=0, float durRotatie=0 );
- void afisare();
- friend class SistemSolar;
- friend int comparaDupaNume(Planeta &p1, Planeta &p2);
- friend int comparaDupaMarime(Planeta &p1, Planeta &p2);
- };
- Planeta::Planeta(const char *nume, double raza, double distantaSoare, float durRotatie){
- strcpy(this->nume, nume);
- this->raza=raza;
- this->distantaSoare=distantaSoare;
- this->durRotatie=durRotatie;
- }
- void Planeta::afisare(){
- cout<<"Nume: "<<nume<<endl;
- cout<<"Raza: "<<raza<<endl;
- cout<<"Distanta pana la Soare: "<<distantaSoare<<endl;
- cout<<"Durata perioadei de rotatie: "<<durRotatie<<endl;
- cout<<"--------------------------------------------------------"<<endl;
- }
- class SistemSolar{
- private:
- int capacitate;
- char *nume;
- int nrPlanete;
- Planeta *planete;
- public:
- SistemSolar(const int capacitate );
- SistemSolar(const SistemSolar &s );
- void adaugare(Planeta &p);
- void afisare();
- //complicat cu pointer de functie
- void sortare(int (*compara)(Planeta &p1, Planeta &p2));
- };
- SistemSolar::SistemSolar(const int capacitate ){
- this->capacitate=capacitate;
- strcpy(this->nume, nume);
- this->nrPlanete=0;
- this->planete=new Planeta[capacitate];
- }
- SistemSolar::SistemSolar(const SistemSolar &s ){
- this->capacitate=s.capacitate;
- strcpy(this->nume, s.nume);
- this->nrPlanete=s.nrPlanete;
- this->planete=new Planeta[s.capacitate];
- for(int i=0; i<s.capacitate; i++){
- this->planete[i]=s.planete[i];
- }
- }
- void SistemSolar:: adaugare(Planeta &p){
- if(nrPlanete<=capacitate){
- planete[nrPlanete]=p;//
- nrPlanete++;
- }else{
- cout<<"asteptati suplimentarea";
- }
- }
- void SistemSolar::afisare(){
- cout<<endl;
- cout<<"sistemul solar are"<<nrPlanete<<" planete si este compus din: ";
- for(int i=0; i<nrPlanete; i++){
- planete[i].afisare();
- }
- }
- void SistemSolar:: sortare(int (*compara)(Planeta &p1, Planeta &p2)) {
- int ok,i;
- Planeta aux;
- do {
- ok=1;
- for(i=0; i<nrPlanete - 1; i++)
- if(compara(planete[i],planete[i+1] )>0) {
- ok=0;
- aux=planete[i];
- planete[i]=planete[i+1];
- planete[i+1]=aux;
- }
- } while(ok!=1);
- }
- int comparaDupaNume(Planeta &p1, Planeta &p2) {
- return strcmp(p1.nume, p2.nume);
- }
- int comparaDupaMarime(Planeta &p1, Planeta &p2) {
- return p2.raza-p1.raza;
- }
- int main(){
- Planeta p1("Mercur",2439.64,57909175,58.64 );
- Planeta p2("Venus",6051.59,108208930, -243);
- Planeta p3("Pamant",6378, 149597890, 0.997);
- Planeta p4("Marte", 3397, 227936640, 1.0259);
- Planeta p5("Jupiter", 71492.68, 778412010, 0.4135);
- Planeta p6("Saturn", 60267.14, 1426725400, 0.44401);
- Planeta p7("Uranus", 25557.25, 2870972200, -0.71833);
- Planeta p8("Neptun", 24766.36, 4498252900, 0.67125);
- Planeta p9("Pluto");
- cout<<"------------------------------------------"<<endl;
- SistemSolar s(10);
- s.adaugare(p1);
- s.adaugare(p2);
- s.adaugare(p3);
- s.adaugare(p4);
- s.adaugare(p5);
- s.adaugare(p6);
- s.adaugare(p7);
- s.adaugare(p8);
- s.adaugare(p9);
- s.afisare();
- cout<<"-------------------"<<endl;
- cout<<"Planete sortate dupa nume"<<endl;
- s.sortare(comparaDupaNume);
- s.afisare();
- cout<<"-------------------"<<endl;
- cout<<"Planete sortate dupa Marime"<<endl;
- s.sortare(comparaDupaMarime);
- s.afisare();
- cout<<"-------------------"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement