Advertisement
Guest User

lab5A

a guest
Nov 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.40 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. static char *unk = "unknown";
  7. int zero=0;
  8. class CarList;
  9. class Car{
  10.     private:
  11.         char* brand;
  12.         int license_number;
  13.         int owner_id;
  14.         int price;
  15.         Car* next;
  16.     public:
  17.         friend class CarList;
  18.         friend CarList& operator-(const CarList &, const CarList &);
  19.  
  20.         Car(); //default constructor
  21.         explicit Car(const char* brandp, int number, int id, int nprice);
  22.         ~Car();
  23.         void print();
  24. };
  25.  
  26. Car::Car() : brand(unk), license_number(zero), owner_id(zero), price(zero), next(nullptr){};
  27.  
  28. Car::Car(const char* brandp, int number, int id, int nprice) : license_number(number), owner_id(id), price(nprice), next(nullptr){
  29.     brand = new char[strlen(brandp) + 1];
  30.     strcpy(brand, brandp);
  31. }
  32.  
  33. Car::~Car(){
  34.     delete[] brand;
  35. }
  36.  
  37. void Car::print(){
  38.     cout << brand << license_number << owner_id <<  price <<endl;
  39. }
  40.  
  41.  
  42. class CarList{
  43.     Car *head;
  44.  
  45. public:
  46.  
  47.     CarList();
  48.     ~CarList();
  49.     CarList(const CarList& x);
  50.  
  51.     CarList& operator = (const CarList &x);
  52.     CarList operator + (const CarList &);
  53.     friend CarList& operator-(const CarList &, const CarList &);
  54.  
  55.  
  56.     void add_car(const char* brand, int number, int id, int price);
  57.     void remove_brand(int owner_id);
  58.     void print();
  59.     int& price(const char *brand, int number, int owner_id);
  60.     Car* get_head() const { return head; };
  61.     void CopyAllElements (const CarList &);
  62.  
  63.  
  64.  
  65. };
  66.  
  67. CarList::CarList() : head(nullptr){};Brand: l2test1 License number:4 Owner ID:14 Price:31231
  68. Brand: l2test2 License number:5 Owner ID:25 Price:3131
  69. Brand: repeat brand License number:101 Owner ID:1235 Price:1231
  70. Brand: repeat brand License number:102 Owner ID:1235 Price:1231
  71.  
  72.  
  73. CarList::~CarList(){
  74.     Car *temp = head;
  75.     Car *ntemp = nullptr;
  76.     while(temp->next){
  77.         ntemp = temp->next;
  78.         delete temp;
  79.  
  80.         temp = ntemp;
  81.     }
  82. }
  83.  
  84. CarList::CarList(const CarList& x){
  85.     Car *etr, *xtr = x.head;
  86.     head=nullptr;
  87.     while(xtr){
  88.         Car *ntr = new Car(xtr->brand, xtr->license_number,xtr->owner_id, xtr->price);
  89.         if(!head) head=ntr;
  90.         else {etr->next=ntr;}
  91.         etr = ntr;
  92.         xtr = xtr->next;
  93.     }
  94. }
  95.  
  96. CarList& CarList::operator=(const CarList &x){
  97.     if (this!=&x) {
  98.         Car *etr = head;
  99.         while (etr) {
  100.             etr = etr->next;
  101.             delete head;
  102.             head = etr;
  103.         }
  104.         Car *xtr = x.head;
  105.         while (xtr) {
  106.             Car *ntr = new Car(xtr->brand, xtr->license_number, xtr->owner_id, xtr->price);
  107.             ntr->next = nullptr;
  108.             if (!head) head = ntr;
  109.             else etr->next = ntr;
  110.             etr = ntr;
  111.             xtr = xtr->next;
  112.         }
  113.     }
  114.     return *this;
  115. }
  116.  
  117. CarList CarList::operator+(const CarList &rhs) {
  118.     CarList n_list;
  119.     n_list.CopyAllElements(rhs);
  120.     n_list.CopyAllElements(*this);
  121.     return n_list;
  122. }
  123.  
  124. CarList& operator-(const CarList &lhs, const CarList &rhs){
  125.     Car *xtr = lhs.head;
  126.     Car *etr;
  127.     Car *temp;
  128.     int license_counter;
  129.  
  130.     CarList* n_list = new CarList();
  131.  
  132.     while(xtr){
  133.         license_counter = 0;
  134.         etr=rhs.head;
  135.         while(etr){
  136.             if(etr->license_number == xtr->license_number){
  137.                 /*temp = xtr; xtr = xtr->next; delete[] temp->brand; delete temp;*/
  138.                 license_counter++;}
  139.             etr= etr->next;
  140.         }
  141.         if(!license_counter) n_list->add_car(xtr->brand, xtr->license_number, xtr->owner_id, xtr->price);
  142.         xtr = xtr->next;
  143.     }
  144.     return *n_list;
  145. }
  146.  
  147.  
  148. void CarList::CopyAllElements(const CarList &rhs) {
  149.     Car *xtr = rhs.head, *tempx;
  150.     Car *etr = head, *tempe;
  151.     int license_counter;
  152.     int brand_counter;
  153.  
  154.     while(xtr){
  155.         license_counter = 0;
  156.         brand_counter = 0;
  157.         etr=head;
  158.         tempe = head;
  159.         while(tempe){
  160.             if(!strcmp(tempe->brand, xtr->brand) && tempe->license_number != xtr->license_number) brand_counter++;
  161.             tempe = tempe->next;
  162.         }
  163.         while(etr){
  164.             if(etr->license_number == xtr->license_number) license_counter++;
  165.             etr = etr->next;
  166.         }
  167.         if(!license_counter && brand_counter < 4) add_car(xtr->brand, xtr->license_number, xtr->owner_id, xtr->price);
  168.         xtr = xtr->next;
  169.     }
  170. }
  171.  
  172. void CarList::add_car(const char* brand, int number, int id, int price) {
  173.         Car *car = new Car(brand, number, id, price);
  174.  
  175.  
  176.         if(head == nullptr){
  177.             head = car;
  178.         }
  179.         else if(head->license_number > car->license_number){
  180.                 Car *tempor = head;
  181.                 car->next = tempor;
  182.                 head = car;
  183.             }
  184.         else{
  185.             Car *temp = head;
  186.             while(temp->next&& temp->next->license_number < car->license_number){
  187.                 temp = temp->next;
  188.             }
  189.             Car* prev = temp->next;
  190.             temp->next = car;
  191.             car->next = prev;
  192.         }
  193. };
  194.  
  195. void CarList::remove_brand(int owner_id){
  196.     if(head->owner_id == owner_id){
  197.         Car* temph = head;
  198.         head = temph->next;
  199.     }
  200.  
  201.     Car* temp = head;
  202.  
  203.     while(temp->next){
  204.         if(temp->next->owner_id == owner_id){
  205.                 if(temp->next->next == nullptr){
  206.                     Car *nTemp = temp->next;
  207.                     temp->next=nullptr;
  208.                     delete []nTemp->brand;
  209.                     delete nTemp;
  210.                     break;
  211.                 }
  212.  
  213.                 else{
  214.                     Car *nTemp2 = temp->next;
  215.                     temp->next = temp->next->next;
  216.                     delete []nTemp2->brand;
  217.                     delete nTemp2;
  218.                 }
  219.             }
  220.         temp = temp->next;
  221.     }
  222. }
  223.  
  224. void CarList::print(){
  225.     Car *temp = head;
  226.     while(temp){
  227.         cout << "Brand: " << temp->brand << " License number:" << temp->license_number << " Owner ID:" << temp->owner_id << " Price:" << temp->price << endl;
  228.         temp = temp->next;
  229.     }
  230. }
  231.  
  232. int& CarList::price(const char *brand, int number, int owner_id){
  233.     Car *temp = head;
  234.     Car *nTemp = head;
  235.     int brandCounter =0;
  236.     int lowestPrice = 0;
  237.  
  238.     while(temp){
  239.         if(temp->license_number == number){
  240.             return temp->price;
  241.         }
  242.         else if(!strcmp(temp->brand, brand)){
  243.             brandCounter += 1;
  244.             if(brandCounter == 1){
  245.                 lowestPrice = temp->price;
  246.             }
  247.             else{
  248.                 if(lowestPrice > temp->price){
  249.                     lowestPrice = temp->price;
  250.                 }
  251.             }
  252.         }
  253.         temp = temp->next;
  254.     }
  255.  
  256.     if(brandCounter<4){
  257.         add_car(brand, number, owner_id, 1000);
  258.         return price(brand, number,owner_id);
  259.     }
  260.     else{
  261.         while(nTemp){
  262.             if(!strcmp(nTemp->brand, brand) && nTemp->price == lowestPrice){
  263.                 return nTemp->price;
  264.             }
  265.             nTemp = nTemp->next;
  266.         }
  267.     }
  268. }
  269.  
  270. int main(){
  271.  
  272.      CarList* my_list1 = new CarList();
  273.      CarList* my_list2 = new CarList();
  274.  
  275.      Car* furka = new Car();
  276.      furka->print();
  277.  
  278.      cout<< "-----------------"<< endl <<"mlist"<<endl;
  279.      my_list1->add_car("l1test1", 1, 14, 31231);
  280.      my_list1->add_car("l1test2", 2, 25, 3131);
  281.      my_list1->add_car("l1test3", 3, 26, 3131);
  282.      my_list1->add_car("repeat brand", 105, 1235, 1231);
  283.      my_list1->add_car("repeat brand", 107, 1235, 1231);
  284.  
  285.      my_list1->print();
  286.      cout<< "-----------------"<< endl <<"mlist2"<<endl;
  287.      my_list2->add_car("l2test1", 4, 14, 31231);
  288.      my_list2->add_car("l2test2", 5, 25, 3131);
  289.      my_list2->add_car("repeatedlicense", 3, 26, 3131);
  290.      my_list2->add_car("repeat brand", 101, 1235, 1231);
  291.      my_list2->add_car("repeat brand", 102, 1235, 1231);
  292.      my_list2->add_car("repeat brand", 103, 1235, 1231);
  293.      my_list2->print();
  294.  
  295.      cout<<"--------------"<< endl <<"mlist3" <<endl;
  296.      CarList* m_list3 = new CarList();
  297.      *m_list3 = *my_list1 + *my_list2;
  298.      m_list3->print();
  299.  
  300.      cout<<"--------------"<< endl <<"mlist3 - m2" <<endl;
  301.      CarList* cleaned1 = new CarList();
  302.      *cleaned1 = *my_list1 - *my_list2;
  303.      cleaned1->print();
  304.  
  305.  
  306.  
  307.  
  308.  
  309.     delete my_list1;
  310.     delete my_list2;
  311.     delete m_list3;
  312.  
  313.     CarList my1_list;
  314.     CarList m2_list;
  315.     cout<<"------------"<<endl<<"AUTO"<<endl;
  316.     cout<<"------------"<<endl<<"m1_list"<<endl;
  317.     my1_list.add_car("l1test1", 1, 231, 23233);
  318.     my1_list.add_car("l1test2", 2, 1235, 1231);
  319.     my1_list.add_car("repeat brand", 101, 1235, 1231);
  320.     my1_list.add_car("repeat brand", 120, 1235, 1231);
  321.     m2_list.add_car("l2repeatednumber", 2, 1235, 1231);
  322.     m2_list.add_car("repeat brand", 102, 1235, 1231);
  323.     m2_list.add_car("repeat brand", 103, 1235, 1231);
  324.     m2_list.add_car("repeat brand", 106, 1235, 1231);
  325.     m2_list.add_car("repeat brand", 107, 1235, 1231);
  326.     m2_list.add_car("repeat brand", 108, 1235, 1231);
  327.     m2_list.add_car("l2test3", 3, 1231, 123);
  328.     m2_list.add_car("l2test4", 4, 123, 123);
  329.     my1_list.price("l1test5", 5, 2);
  330.     my1_list.print();
  331.  
  332.     cout<<"--------------"<< endl <<"m2list" <<endl;
  333.     m2_list.print();
  334.     cout<<"--------------"<< endl <<"m3list" <<endl;
  335.     CarList m3_list = m2_list + my1_list;
  336.     m3_list.print();
  337.     cout<<"--------------"<< endl <<"m3list - m1" <<endl;
  338.     CarList cleaned = m3_list - my1_list;
  339.     cleaned.print();
  340.  
  341.  
  342.  
  343.  
  344.     return 0;
  345.  
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement