Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- static char *unk = "unknown";
- int zero=0;
- class CarList;
- class Car{
- private:
- char* brand;
- int license_number;
- int owner_id;
- int price;
- Car* next;
- public:
- friend class CarList;
- friend CarList& operator-(const CarList &, const CarList &);
- Car(); //default constructor
- explicit Car(const char* brandp, int number, int id, int nprice);
- ~Car();
- void print();
- };
- Car::Car() : brand(unk), license_number(zero), owner_id(zero), price(zero), next(nullptr){};
- Car::Car(const char* brandp, int number, int id, int nprice) : license_number(number), owner_id(id), price(nprice), next(nullptr){
- brand = new char[strlen(brandp) + 1];
- strcpy(brand, brandp);
- }
- Car::~Car(){
- delete[] brand;
- }
- void Car::print(){
- cout << brand << license_number << owner_id << price <<endl;
- }
- class CarList{
- Car *head;
- public:
- CarList();
- ~CarList();
- CarList(const CarList& x);
- CarList& operator = (const CarList &x);
- CarList operator + (const CarList &);
- friend CarList& operator-(const CarList &, const CarList &);
- void add_car(const char* brand, int number, int id, int price);
- void remove_brand(int owner_id);
- void print();
- int& price(const char *brand, int number, int owner_id);
- Car* get_head() const { return head; };
- void CopyAllElements (const CarList &);
- };
- CarList::CarList() : head(nullptr){};Brand: l2test1 License number:4 Owner ID:14 Price:31231
- Brand: l2test2 License number:5 Owner ID:25 Price:3131
- Brand: repeat brand License number:101 Owner ID:1235 Price:1231
- Brand: repeat brand License number:102 Owner ID:1235 Price:1231
- CarList::~CarList(){
- Car *temp = head;
- Car *ntemp = nullptr;
- while(temp->next){
- ntemp = temp->next;
- delete temp;
- temp = ntemp;
- }
- }
- CarList::CarList(const CarList& x){
- Car *etr, *xtr = x.head;
- head=nullptr;
- while(xtr){
- Car *ntr = new Car(xtr->brand, xtr->license_number,xtr->owner_id, xtr->price);
- if(!head) head=ntr;
- else {etr->next=ntr;}
- etr = ntr;
- xtr = xtr->next;
- }
- }
- CarList& CarList::operator=(const CarList &x){
- if (this!=&x) {
- Car *etr = head;
- while (etr) {
- etr = etr->next;
- delete head;
- head = etr;
- }
- Car *xtr = x.head;
- while (xtr) {
- Car *ntr = new Car(xtr->brand, xtr->license_number, xtr->owner_id, xtr->price);
- ntr->next = nullptr;
- if (!head) head = ntr;
- else etr->next = ntr;
- etr = ntr;
- xtr = xtr->next;
- }
- }
- return *this;
- }
- CarList CarList::operator+(const CarList &rhs) {
- CarList n_list;
- n_list.CopyAllElements(rhs);
- n_list.CopyAllElements(*this);
- return n_list;
- }
- CarList& operator-(const CarList &lhs, const CarList &rhs){
- Car *xtr = lhs.head;
- Car *etr;
- Car *temp;
- int license_counter;
- CarList* n_list = new CarList();
- while(xtr){
- license_counter = 0;
- etr=rhs.head;
- while(etr){
- if(etr->license_number == xtr->license_number){
- /*temp = xtr; xtr = xtr->next; delete[] temp->brand; delete temp;*/
- license_counter++;}
- etr= etr->next;
- }
- if(!license_counter) n_list->add_car(xtr->brand, xtr->license_number, xtr->owner_id, xtr->price);
- xtr = xtr->next;
- }
- return *n_list;
- }
- void CarList::CopyAllElements(const CarList &rhs) {
- Car *xtr = rhs.head, *tempx;
- Car *etr = head, *tempe;
- int license_counter;
- int brand_counter;
- while(xtr){
- license_counter = 0;
- brand_counter = 0;
- etr=head;
- tempe = head;
- while(tempe){
- if(!strcmp(tempe->brand, xtr->brand) && tempe->license_number != xtr->license_number) brand_counter++;
- tempe = tempe->next;
- }
- while(etr){
- if(etr->license_number == xtr->license_number) license_counter++;
- etr = etr->next;
- }
- if(!license_counter && brand_counter < 4) add_car(xtr->brand, xtr->license_number, xtr->owner_id, xtr->price);
- xtr = xtr->next;
- }
- }
- void CarList::add_car(const char* brand, int number, int id, int price) {
- Car *car = new Car(brand, number, id, price);
- if(head == nullptr){
- head = car;
- }
- else if(head->license_number > car->license_number){
- Car *tempor = head;
- car->next = tempor;
- head = car;
- }
- else{
- Car *temp = head;
- while(temp->next&& temp->next->license_number < car->license_number){
- temp = temp->next;
- }
- Car* prev = temp->next;
- temp->next = car;
- car->next = prev;
- }
- };
- void CarList::remove_brand(int owner_id){
- if(head->owner_id == owner_id){
- Car* temph = head;
- head = temph->next;
- }
- Car* temp = head;
- while(temp->next){
- if(temp->next->owner_id == owner_id){
- if(temp->next->next == nullptr){
- Car *nTemp = temp->next;
- temp->next=nullptr;
- delete []nTemp->brand;
- delete nTemp;
- break;
- }
- else{
- Car *nTemp2 = temp->next;
- temp->next = temp->next->next;
- delete []nTemp2->brand;
- delete nTemp2;
- }
- }
- temp = temp->next;
- }
- }
- void CarList::print(){
- Car *temp = head;
- while(temp){
- cout << "Brand: " << temp->brand << " License number:" << temp->license_number << " Owner ID:" << temp->owner_id << " Price:" << temp->price << endl;
- temp = temp->next;
- }
- }
- int& CarList::price(const char *brand, int number, int owner_id){
- Car *temp = head;
- Car *nTemp = head;
- int brandCounter =0;
- int lowestPrice = 0;
- while(temp){
- if(temp->license_number == number){
- return temp->price;
- }
- else if(!strcmp(temp->brand, brand)){
- brandCounter += 1;
- if(brandCounter == 1){
- lowestPrice = temp->price;
- }
- else{
- if(lowestPrice > temp->price){
- lowestPrice = temp->price;
- }
- }
- }
- temp = temp->next;
- }
- if(brandCounter<4){
- add_car(brand, number, owner_id, 1000);
- return price(brand, number,owner_id);
- }
- else{
- while(nTemp){
- if(!strcmp(nTemp->brand, brand) && nTemp->price == lowestPrice){
- return nTemp->price;
- }
- nTemp = nTemp->next;
- }
- }
- }
- int main(){
- CarList* my_list1 = new CarList();
- CarList* my_list2 = new CarList();
- Car* furka = new Car();
- furka->print();
- cout<< "-----------------"<< endl <<"mlist"<<endl;
- my_list1->add_car("l1test1", 1, 14, 31231);
- my_list1->add_car("l1test2", 2, 25, 3131);
- my_list1->add_car("l1test3", 3, 26, 3131);
- my_list1->add_car("repeat brand", 105, 1235, 1231);
- my_list1->add_car("repeat brand", 107, 1235, 1231);
- my_list1->print();
- cout<< "-----------------"<< endl <<"mlist2"<<endl;
- my_list2->add_car("l2test1", 4, 14, 31231);
- my_list2->add_car("l2test2", 5, 25, 3131);
- my_list2->add_car("repeatedlicense", 3, 26, 3131);
- my_list2->add_car("repeat brand", 101, 1235, 1231);
- my_list2->add_car("repeat brand", 102, 1235, 1231);
- my_list2->add_car("repeat brand", 103, 1235, 1231);
- my_list2->print();
- cout<<"--------------"<< endl <<"mlist3" <<endl;
- CarList* m_list3 = new CarList();
- *m_list3 = *my_list1 + *my_list2;
- m_list3->print();
- cout<<"--------------"<< endl <<"mlist3 - m2" <<endl;
- CarList* cleaned1 = new CarList();
- *cleaned1 = *my_list1 - *my_list2;
- cleaned1->print();
- delete my_list1;
- delete my_list2;
- delete m_list3;
- CarList my1_list;
- CarList m2_list;
- cout<<"------------"<<endl<<"AUTO"<<endl;
- cout<<"------------"<<endl<<"m1_list"<<endl;
- my1_list.add_car("l1test1", 1, 231, 23233);
- my1_list.add_car("l1test2", 2, 1235, 1231);
- my1_list.add_car("repeat brand", 101, 1235, 1231);
- my1_list.add_car("repeat brand", 120, 1235, 1231);
- m2_list.add_car("l2repeatednumber", 2, 1235, 1231);
- m2_list.add_car("repeat brand", 102, 1235, 1231);
- m2_list.add_car("repeat brand", 103, 1235, 1231);
- m2_list.add_car("repeat brand", 106, 1235, 1231);
- m2_list.add_car("repeat brand", 107, 1235, 1231);
- m2_list.add_car("repeat brand", 108, 1235, 1231);
- m2_list.add_car("l2test3", 3, 1231, 123);
- m2_list.add_car("l2test4", 4, 123, 123);
- my1_list.price("l1test5", 5, 2);
- my1_list.print();
- cout<<"--------------"<< endl <<"m2list" <<endl;
- m2_list.print();
- cout<<"--------------"<< endl <<"m3list" <<endl;
- CarList m3_list = m2_list + my1_list;
- m3_list.print();
- cout<<"--------------"<< endl <<"m3list - m1" <<endl;
- CarList cleaned = m3_list - my1_list;
- cleaned.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement