Advertisement
Guest User

Lab2A

a guest
Oct 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. struct Car{
  7. char* brand;
  8. int license_number;
  9. int owner_id;
  10. int price;
  11. Car* next;
  12.  
  13. void add_car(const char* brand, int number, int id, int price);
  14. void remove_brand(int owner_id);
  15. void print();
  16. };
  17.  
  18. Car* head = NULL;
  19.  
  20. int& price(const char* brand, int number, int owner_id);
  21.  
  22.  
  23. int main(){
  24.     Car* furki = head;
  25.     furki->add_car("xd4dx", 21, 1, 1949);
  26.     furki->add_car("xd3dx", 11, 11, 1949);
  27.     furki->add_car("xdd2x", 222, 21, 1399);
  28.     furki->add_car("xdd1x", 16, 166, 1299);
  29.     furki->add_car("xdd1x", 162, 166, 1299);
  30.     furki->add_car("xd5dx", 7, 15, 19913);
  31.     furki->add_car("xd5dx", 71, 15, 1);
  32.     furki->add_car("xd5dx", 73, 15, 19941);
  33.     furki->add_car("xd5dx", 45, 15, 199144);
  34.  
  35.  
  36.     furki->print();
  37.     cout<< "--------------------------------"<<endl;
  38.     furki->remove_brand(11);
  39.     furki->print();
  40.     cout<< "--------------------------------"<<endl;
  41.     price("xd5dx", 71, 15) =120;
  42.     price("xdd1x", 162, 166) = 9999999;
  43.     price("xdd1x", 16111, 166);
  44.  
  45.     furki->print();
  46.  
  47.  
  48. }
  49.  
  50. void Car::add_car(const char* brand, int number, int id, int price){
  51. Car *ncar = new Car;
  52.  
  53. ncar->brand = new char[strlen(brand) + 1];
  54. strcpy(ncar->brand, brand);
  55. ncar->license_number = number;
  56. ncar->owner_id = id;
  57. ncar->price= price;
  58.  
  59.     if(head == NULL){
  60.         head = ncar;
  61.     }
  62.     else if(head->license_number > ncar->license_number){
  63.             Car *tempor = head;
  64.             ncar->next = tempor;
  65.             head = ncar;
  66.         }
  67.  
  68.     else{
  69.         Car *temp = head;
  70.         while(temp->next&& temp->next->license_number < ncar->license_number){
  71.             temp = temp->next;
  72.         }
  73.         Car* prev = temp->next;
  74.         temp->next = ncar;
  75.         ncar->next = prev;
  76.     }
  77.  
  78. };
  79. void Car::remove_brand(int owner_id){
  80.     if(head->owner_id == owner_id){
  81.         Car* temph = head;
  82.         head = temph->next;
  83.     }
  84.  
  85.     Car* temp = head;
  86.  
  87.     while(temp->next){
  88.         if(temp->next->owner_id == owner_id){
  89.                 if(temp->next->next == NULL){
  90.                     Car *nTemp = temp->next;
  91.                     temp->next=NULL;
  92.                     delete []nTemp->brand;
  93.                     delete nTemp;
  94.                     break;
  95.                 }
  96.  
  97.                 else{
  98.                     Car *nTemp2 = temp->next;
  99.                     temp->next = temp->next->next;
  100.                     delete []nTemp2->brand;
  101.                     delete nTemp2;
  102.                 }
  103.             }
  104.         temp = temp->next;
  105.     }
  106. }
  107. void Car::print(){
  108.     Car *temp = head;
  109.     while(temp){
  110.         cout << "Brand: " << temp->brand << " License number:" << temp->license_number << " Owner ID:" << temp->owner_id << " Price:" << temp->price << endl;
  111.         temp = temp->next;
  112.     }
  113. }
  114.  
  115.  
  116. int& price(const char *brand, int number, int owner_id){
  117.     Car *temp = head;
  118.     Car *nTemp = head;
  119.     int brandCounter =0;
  120.     int lowestPrice = 0;
  121.  
  122.  
  123.     while(temp){
  124.         if(temp->license_number == number){
  125.             return temp->price;
  126.         }
  127.         else if(!strcmp(temp->brand, brand)){
  128.             brandCounter += 1;
  129.             if(brandCounter == 1){
  130.                 lowestPrice = temp->price;
  131.             }
  132.             else{
  133.                 if(lowestPrice > temp->price){
  134.                     lowestPrice = temp->price;
  135.                 }
  136.             }
  137.         }
  138.         temp = temp->next;
  139.     }
  140.  
  141.     if(brandCounter<4){
  142.         head->add_car(brand, number, owner_id, 1000);
  143.         return price(brand, number,owner_id);
  144.     }
  145.     else{
  146.         while(nTemp){
  147.             if(!strcmp(nTemp->brand, brand) && nTemp->price == lowestPrice){
  148.                 return nTemp->price;
  149.             }
  150.             nTemp = nTemp->next;
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement