Advertisement
A0D1MQ

lab4 - raw

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