Advertisement
madalinaradu

Planeta

Apr 15th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. class SistemSolar;
  8. class Planeta{
  9.         private:
  10.         char nume[10];
  11.         double raza;
  12.         double distantaSoare;
  13.         float durRotatie;
  14.     public:
  15.         Planeta(const char *nume="", double raza=0, double distantaSoare=0, float durRotatie=0 );
  16.         void afisare();
  17.         friend class SistemSolar;
  18.         friend int comparaDupaNume(Planeta &p1, Planeta &p2);
  19.         friend int comparaDupaMarime(Planeta &p1, Planeta &p2);
  20.  
  21. };
  22. Planeta::Planeta(const char *nume, double raza, double distantaSoare, float durRotatie){
  23.     strcpy(this->nume, nume);
  24.     this->raza=raza;
  25.     this->distantaSoare=distantaSoare;
  26.     this->durRotatie=durRotatie;
  27. }
  28. void Planeta::afisare(){
  29.     cout<<"Nume: "<<nume<<endl;
  30.     cout<<"Raza: "<<raza<<endl;
  31.     cout<<"Distanta pana la Soare: "<<distantaSoare<<endl;
  32.     cout<<"Durata perioadei de rotatie: "<<durRotatie<<endl;
  33.     cout<<"--------------------------------------------------------"<<endl;
  34.  
  35. }
  36.  
  37. class SistemSolar{
  38.     private:
  39.         int capacitate;
  40.         char *nume;
  41.         int nrPlanete;
  42.         Planeta *planete;
  43.     public:
  44.         SistemSolar(const int capacitate );
  45.         SistemSolar(const SistemSolar &s );
  46.         void adaugare(Planeta &p);
  47.         void afisare();
  48.         //complicat cu pointer de functie
  49.     void sortare(int (*compara)(Planeta &p1, Planeta &p2));
  50.  
  51. };
  52.  
  53. SistemSolar::SistemSolar(const int capacitate ){
  54.     this->capacitate=capacitate;
  55.     strcpy(this->nume, nume);
  56.     this->nrPlanete=0;
  57.     this->planete=new Planeta[capacitate];
  58.         }
  59. SistemSolar::SistemSolar(const SistemSolar &s ){
  60.     this->capacitate=s.capacitate;
  61.     strcpy(this->nume, s.nume);
  62.     this->nrPlanete=s.nrPlanete;
  63.     this->planete=new Planeta[s.capacitate];
  64.     for(int i=0; i<s.capacitate; i++){
  65.         this->planete[i]=s.planete[i];
  66.     }
  67. }
  68.  
  69. void SistemSolar:: adaugare(Planeta &p){
  70.     if(nrPlanete<=capacitate){
  71.         planete[nrPlanete]=p;//
  72.         nrPlanete++;
  73.     }else{
  74.         cout<<"asteptati suplimentarea";
  75.     }
  76. }
  77. void SistemSolar::afisare(){
  78.      cout<<endl;
  79.      cout<<"sistemul solar are"<<nrPlanete<<" planete si este compus din: ";
  80.      for(int i=0; i<nrPlanete; i++){
  81.         planete[i].afisare();
  82.      }
  83. }
  84.  
  85. void SistemSolar:: sortare(int (*compara)(Planeta &p1, Planeta &p2)) {
  86.     int ok,i;
  87.     Planeta aux;
  88.     do {
  89.         ok=1;
  90.         for(i=0; i<nrPlanete - 1; i++)
  91.             if(compara(planete[i],planete[i+1] )>0) {
  92.                 ok=0;
  93.                 aux=planete[i];
  94.                 planete[i]=planete[i+1];
  95.                 planete[i+1]=aux;
  96.             }
  97.     } while(ok!=1);
  98. }
  99.  
  100. int comparaDupaNume(Planeta &p1, Planeta &p2) {
  101.     return strcmp(p1.nume, p2.nume);
  102. }
  103.  
  104.  
  105. int comparaDupaMarime(Planeta &p1, Planeta &p2) {
  106.     return p2.raza-p1.raza;
  107. }
  108.  
  109. int main(){
  110. Planeta p1("Mercur",2439.64,57909175,58.64 );
  111. Planeta p2("Venus",6051.59,108208930, -243);
  112. Planeta p3("Pamant",6378, 149597890, 0.997);
  113. Planeta p4("Marte", 3397, 227936640, 1.0259);
  114. Planeta p5("Jupiter", 71492.68, 778412010, 0.4135);
  115. Planeta p6("Saturn", 60267.14, 1426725400, 0.44401);
  116. Planeta p7("Uranus", 25557.25, 2870972200, -0.71833);
  117. Planeta p8("Neptun", 24766.36, 4498252900, 0.67125);
  118. Planeta p9("Pluto");
  119. cout<<"------------------------------------------"<<endl;
  120. SistemSolar s(10);
  121. s.adaugare(p1);
  122. s.adaugare(p2);
  123. s.adaugare(p3);
  124. s.adaugare(p4);
  125. s.adaugare(p5);
  126. s.adaugare(p6);
  127. s.adaugare(p7);
  128. s.adaugare(p8);
  129. s.adaugare(p9);
  130. s.afisare();
  131. cout<<"-------------------"<<endl;
  132. cout<<"Planete sortate dupa nume"<<endl;
  133. s.sortare(comparaDupaNume);
  134. s.afisare();
  135. cout<<"-------------------"<<endl;
  136. cout<<"Planete sortate dupa Marime"<<endl;
  137. s.sortare(comparaDupaMarime);
  138. s.afisare();
  139. cout<<"-------------------"<<endl;
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement