Advertisement
A0D1MQ

lab3

Mar 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. ///Working with default constructors and destructors
  8.  
  9. class Product{
  10.     int code;
  11.     int value;
  12.     int price;
  13.     char* name;
  14.     Product *next;
  15. public:
  16.     //Product();
  17.     //Product(int code, int value, int price,const char* name);
  18.     Product(int ncode, int nvalue, int nprice,const char* nname);
  19.     ~Product();
  20.     void print(){cout<<"\nProduct: "<< code <<"\t"<< value <<"\t"<< price <<"\t"<< name <<" \n";}
  21.     int getCode(){return code;}
  22.     void modCode(int ncode){code=ncode;}
  23.     int getValue(){return value;}
  24.     void modValue(int nvalue){value=nvalue;}
  25.     void addValue(int avalue){value+=avalue;}
  26.     int getPrice(){return price;}
  27.     int &returnPrice(){return price;}
  28.     void modPrice(int nprice){price=nprice;}
  29.     const char*getName(){return name;}
  30.     void modName(const char*nname){
  31.         delete[] name;
  32.         char* c=new char[strlen(nname)+1];
  33.         strcpy(c,nname);
  34.         name=c;
  35.     }
  36.     void modNext(Product*nnext){next=nnext;}
  37.     Product *getNext(){return next;}
  38. /*    int getName(){return name;}
  39.     int getNext(){return next;}*/
  40. };
  41.  
  42. class ProductList{
  43. private:
  44.     Product *head;
  45.     Product *tmp;
  46.     Product *n;
  47. public:
  48.     ProductList();
  49.     ~ProductList();
  50.     int addProduct(int code, int value, int price,const char* name);
  51.     int removeProduct(int code);
  52.     void print();
  53.     int& modify(int code,const char *name);
  54. };
  55. /////////////////////////////
  56. Product::Product(int ncode, int nvalue, int nprice,const char* nname){
  57.     code = ncode;
  58.     value = nvalue;
  59.     price = nprice;
  60.     char* c=new char[strlen(nname)+1];
  61.     strcpy(c,nname);
  62.     name=c;
  63.     print();
  64. }
  65. Product::~Product(){
  66.     print();
  67.     delete[] name;
  68. }
  69.  
  70. ProductList::ProductList(){
  71.     head=NULL;
  72.     tmp=NULL;
  73.     n=NULL;
  74. }
  75. ProductList::~ProductList(){
  76.     n=head;
  77.     while(n){
  78.         n = n->getNext();
  79.         //delete[] head->getName();
  80.         delete head;
  81.         head = n;
  82.     }
  83. }
  84. int ProductList::addProduct(int code, int value, int price,const char* name){
  85.     cout<<"Adding: "<<code<<"\t"<<value<<"\t"<<price<<"\t"<<name;
  86.  
  87.     if (head == NULL || head->getCode() > code){
  88.  
  89.         n = new Product(code, value, price, name);
  90. //        n = new Product();
  91. //        n->modCode(code);
  92. //        n->modValue(value);
  93. //        n->modPrice(price);
  94. //        n->modName(name);
  95.  
  96.  
  97.         n->modNext(head);
  98.         head = n;
  99.     }
  100.     else{
  101.         tmp = head;
  102.         while (tmp->getNext() && tmp->getNext()->getCode() < code){
  103.             tmp = tmp->getNext();
  104.         }
  105.         //cout<<tmp->code<<endl;
  106.         if (tmp->getCode() == code){ //if there is a product of this code
  107.  
  108.             if((!strcmp(tmp->getName(),name)) && ((tmp->getPrice())==price)){ //if code and name is the same change value
  109.  
  110.                 tmp->addValue(value);
  111.                 cout<<endl;
  112.                 return 1;
  113.             }
  114.             else{               //if only code is the same update everything
  115.  
  116. //                tmp->value=value;
  117. //                tmp->price=price;
  118.                 //strcpy(tmp->name,name);
  119.                 cout<<" Failed\n";
  120.                 return 0;
  121.             }
  122.         }
  123.         else{
  124.                 n = new Product(code, value, price, name);
  125. //        n = new Product();
  126. //        n->modCode(code);
  127. //        n->modValue(value);
  128. //        n->modPrice(price);
  129. //        n->modName(name);
  130.  
  131.  
  132.         n->modNext(tmp->getNext());
  133.         tmp->modNext(n);
  134.         }
  135.     }
  136.     cout<<endl;
  137. }
  138.  
  139. int ProductList::removeProduct(int code){
  140.  
  141.     Product* curr;
  142.     Product* del=NULL;
  143.     tmp=head;
  144.     curr=head;
  145.     while(curr!=NULL && curr->getCode()!=code){
  146.         tmp=curr;
  147.         curr=curr->getNext();
  148.     }
  149.     if(curr==NULL){
  150.         cout<<"Product was not in the list\n";
  151.         return 0;
  152.     }
  153.     else{
  154.         del=curr;
  155.         curr=curr->getNext();
  156.         tmp->modNext(curr);
  157.         if(del==head){
  158.             head=head->getNext();
  159.             tmp=NULL;
  160.         }
  161.         cout<<"Product " << del->getCode() << " was deleted\n";
  162.        
  163.         delete del;
  164.         return 1;
  165.     }
  166.  
  167.  
  168. }
  169.  
  170. void ProductList::print(){
  171.  
  172.     tmp=head;
  173.     cout<<"---------------------------\n";
  174.     cout<<"Code\t"<<"Value\t"<<"Price\t"<<"Name\t\n";
  175.     while(tmp!=NULL){
  176.         cout<< tmp->getCode() <<"\t"<< tmp->getValue() <<"\t"<< tmp->getPrice() <<"\t"<< tmp->getName() <<" \n";
  177.         tmp=tmp->getNext();
  178.     }
  179.     cout<<"---------------------------\n";
  180. }
  181.  
  182. int& ProductList::modify(int code,const char *name){
  183.     if (head!=NULL){
  184.         tmp = head;
  185.         if (tmp->getCode()!=code){
  186.             while (tmp->getNext()!=NULL && tmp->getNext()->getCode() == code){
  187.                 tmp = tmp->getNext();
  188.             }
  189.         }
  190.         if (tmp->getCode() == code){
  191.             if (!strcmp(tmp->getName(),name)){
  192.                 cout<<"Modify("<< code << ", "<<name<<")\n";
  193.                 return tmp->returnPrice();
  194.             }
  195.             else{
  196.  
  197.                 //strcpy(tmp->name,name);
  198.                 delete[] tmp->getName();
  199.                 char* c=new char[strlen(name)+1];
  200.                 strcpy(c,name);
  201.                 tmp->modName(c);
  202.                 //////////////////////////
  203.  
  204.                 cout<<"Modify("<< code << ", "<<name<<")\n";
  205.                 return tmp->returnPrice();
  206.             }
  207.         }
  208.     }
  209.     addProduct(code, 0, 0, name);
  210.     tmp = head;
  211.     while (tmp->getNext()!=NULL && tmp->getNext()->getCode() == code){
  212.         tmp = tmp->getNext();
  213.     }
  214.     cout<<"Modify("<< code << ", "<<name<<")\n";
  215.     return tmp->returnPrice();
  216.  
  217. }
  218. /////////////////////////////
  219. ///MAIN//////////////////
  220. int main(){
  221. /*
  222.     ProductList List;
  223.     List.removeProduct(1);
  224.     List.addProduct(1,2,3,"test_name_1");
  225.     List.print();
  226.     List.addProduct(2,2,3,"test_name_2");
  227.     List.addProduct(1,2,3,"test_name_1");
  228.     List.addProduct(1,2,3,"test_name_1");
  229.     List.modify(2,"testtest");
  230.     List.removeProduct(1);
  231.     List.print();
  232.     List.addProduct(1,2,3,"test_name_1");
  233.     List.print();
  234.     //List.~ProductList();
  235.     List.print();
  236. */
  237.     ProductList ListA;
  238.     ProductList* ListB = new ProductList;
  239.    
  240.     ListA.addProduct(1,1,1,"product1");
  241.     ListA.removeProduct(1);
  242.     ListA.print();
  243.     ListB->addProduct(2,1,1,"product2");
  244.     ListB->print();
  245.     ListB->removeProduct(2);
  246.  
  247.     delete ListB;
  248.  
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement