Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class block_books {
  7.     int type;
  8.  
  9.     public:
  10.     block_books(){ type=0;};
  11.     block_books(int);
  12.     void set_type(int);
  13.     int get_type();
  14.     friend ostream& operator <<(ostream &,block_books&);
  15.     friend istream& operator >>(istream &,block_books&);
  16. };
  17.  
  18. block_books::block_books(int type):type(type){};
  19.  
  20. void block_books::set_type(int type){this->type=type;};
  21.  
  22. int block_books::get_type(){ return type;};
  23.  
  24. istream& operator >>(istream& in,block_books& obj){
  25.     int temp;
  26.     cout<<"Dati tipul cartii(0-intoarsa , 1-deschisa): ";
  27.     in>>temp;
  28.     if((temp==0)||(temp==1)){
  29.     obj.set_type(temp);
  30.     }
  31.     else {cout<<"A fost introdusa cifra incorecta! ";}
  32.  
  33.     return in;
  34. }
  35.  
  36. ostream& operator <<(ostream& out,block_books&obj){
  37.  
  38.     if((obj.type=0)){
  39.     out<<"Tipul cartii: intoarsa"<<endl;
  40.     }
  41.      if((obj.type=1)){
  42.     out<<"Tipul cartii: deschisa"<<endl;
  43.     }
  44.  
  45. return out;
  46.  
  47. }
  48.  
  49. class book:public block_books {
  50.     char* grade;
  51.     char* color;
  52.  
  53.     public:
  54.     book(){grade=0; color=0;};
  55.     ~book(){delete[]grade; delete[]color;}
  56.     void set_grade(char*);
  57.     void set_color(char*);
  58.     char* get_grade();
  59.     char* get_color();
  60.     friend ostream& operator <<(ostream &,book&);
  61.     friend istream& operator >>(istream &,book&);
  62. };
  63.  
  64.  
  65. void book::set_grade(char *grade){
  66.      if(this->grade!=NULL)delete[]this->grade;
  67.      this->grade=new char[strlen(grade)+1];
  68.      strcpy(this->grade,grade);
  69. };
  70.  
  71. void book::set_color(char *color){
  72.      if(this->color!=NULL)delete[]this->color;
  73.      this->color=new char[strlen(color)+1];
  74.      strcpy(this->color,color);
  75. };
  76.  
  77.  
  78. char* book::get_grade(){ return grade;};
  79. char* book::get_color(){ return color;};
  80.  
  81. istream& operator >>(istream& in,book& obj){
  82.      char temp1[100],temp2[100];
  83.      cout<<"Dati gradul cartii: "<<endl;
  84.      in>>temp1;
  85.      cout<<"Dati culoarea cartii: "<<endl;
  86.      in>>temp2;
  87.  
  88.      obj.set_grade(temp1);
  89.      obj.set_color(temp2);
  90.      return in;
  91. }
  92.  
  93. ostream& operator <<(ostream& out,book&obj){
  94.  
  95.  
  96.      out<<"Gradul cartii: "<<obj.get_grade()<<endl;
  97.      out<<"Culoarea cartii: "<<obj.get_color()<<endl;
  98.  
  99.      return out;
  100. }
  101.  
  102. class post_order:public book {
  103.     int n_order;
  104.     public:
  105.     friend ostream& operator <<(ostream &,post_order&);
  106.     friend istream& operator >>(istream &,post_order&);
  107. };
  108.  
  109.  
  110. istream& operator >>(istream& in,post_order& obj){
  111.      char temp2[100],temp3[100];
  112.      int temp1;
  113.      cout<<"Dati tipul cartii(0-intoarsa , 1-deschisa): ";
  114.      in>>temp1;
  115.      if((temp1==0)||(temp1==1)){
  116.      obj.set_type(temp1);
  117.      }
  118.      else {cout<<"A fost introdusa cifra incorecta! "; obj.set_type(1);}
  119.  
  120.      cout<<"Dati gradul cartii: "<<endl;
  121.      in>>temp2;
  122.      obj.set_grade(temp2);
  123.      cout<<"Dati culoarea cartii: "<<endl;
  124.      in>>temp3;
  125.      obj.set_color(temp3);
  126.      return in;
  127. }
  128.  
  129. ostream& operator <<(ostream& out,post_order&obj){
  130.  
  131.      if((obj.get_type()==0)){
  132.      out<<"Tipul cartii: intoarsa"<<endl;
  133.      }
  134.      if((obj.get_type()==1)){
  135.      out<<"Tipul cartii: deschisa"<<endl;
  136.      }
  137.      out<<"Gradul cartii: "<<obj.get_grade()<<endl;
  138.      out<<"Culoarea cartii: "<<obj.get_color()<<endl;
  139.      return out;
  140. }
  141.  
  142.  
  143. class post_random:public book {
  144.  
  145.     public:
  146.     friend ostream& operator <<(ostream &,post_random&);
  147.     friend istream& operator >>(istream &,post_random&);
  148. };
  149.  
  150. ostream& operator <<(ostream& out,post_random&obj){
  151.  
  152.      out<<"Tipul cartii: "<<obj.get_type()<<endl;
  153.      out<<"Gradul cartii: "<<obj.get_grade()<<endl;
  154.      out<<"Culoarea cartii: "<<obj.get_color()<<endl;
  155.  
  156.      return out;
  157. }
  158.  
  159. int main(){
  160.  
  161.    book b;
  162.    post_order p1,p2;
  163.    cout<<"Dati datele pentru cartea 1"<<endl;
  164.    cin>>p1;
  165.    cout<<"Dati datele pentru cartea 2"<<endl;
  166.    cin>>p2;
  167.    post_order p[2]={p1,p2};
  168.  
  169.    cout<<endl<<"Afisare in ordine:"<<endl;
  170.    cout<<"Cartea 1"<<endl;
  171.    cout<<p[0];
  172.    cout<<"Cartea 2"<<endl;
  173.    cout<<p[1];
  174.  
  175.    srand(time(0));
  176.    int i=rand()%1;
  177.  
  178.    cout<<endl<<"Afisare random:"<<endl;
  179.    cout<<"Cartea "<<i+1<<endl;
  180.    cout<<p[i];
  181.    int k;
  182.    if (i==0) k=1;
  183.    else if (i==1) k=0;
  184.    cout<<"Cartea "<<k+1<<endl;
  185.    cout<<p[k];
  186.  
  187.    return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement