Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- struct Car{
- char* brand;
- int license_number;
- int owner_id;
- int price;
- Car* next;
- void add_car(const char* brand, int number, int id, int price);
- void remove_brand(int owner_id);
- void print();
- };
- Car* head = NULL;
- int& price(const char* brand, int number, int owner_id);
- int main(){
- Car* furki = head;
- furki->add_car("xd4dx", 21, 1, 1949);
- furki->add_car("xd3dx", 11, 11, 1949);
- furki->add_car("xdd2x", 222, 21, 1399);
- furki->add_car("xdd1x", 16, 166, 1299);
- furki->add_car("xdd1x", 162, 166, 1299);
- furki->add_car("xd5dx", 7, 15, 19913);
- furki->add_car("xd5dx", 71, 15, 1);
- furki->add_car("xd5dx", 73, 15, 19941);
- furki->add_car("xd5dx", 45, 15, 199144);
- furki->print();
- cout<< "--------------------------------"<<endl;
- furki->remove_brand(11);
- furki->print();
- cout<< "--------------------------------"<<endl;
- price("xd5dx", 71, 15) =120;
- price("xdd1x", 162, 166) = 9999999;
- price("xdd1x", 16111, 166);
- furki->print();
- }
- void Car::add_car(const char* brand, int number, int id, int price){
- Car *ncar = new Car;
- ncar->brand = new char[strlen(brand) + 1];
- strcpy(ncar->brand, brand);
- ncar->license_number = number;
- ncar->owner_id = id;
- ncar->price= price;
- if(head == NULL){
- head = ncar;
- }
- else if(head->license_number > ncar->license_number){
- Car *tempor = head;
- ncar->next = tempor;
- head = ncar;
- }
- else{
- Car *temp = head;
- while(temp->next&& temp->next->license_number < ncar->license_number){
- temp = temp->next;
- }
- Car* prev = temp->next;
- temp->next = ncar;
- ncar->next = prev;
- }
- };
- void Car::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 == NULL){
- Car *nTemp = temp->next;
- temp->next=NULL;
- 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 Car::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& 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){
- head->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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement