Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class Product
  7. {
  8. int code;
  9. int value;
  10. int price;
  11. char *name;
  12. public: Product* next;
  13. Product(int ncode,int nvalue,int nprice,char* nname); //prints all data of the product
  14. ~Product(); //prints data of the product
  15. void print(); //prints data of the product
  16. int getCode();
  17. int getValue();
  18. int getPrice();
  19. char* getName();
  20. int modCode(int ncode);
  21. int modValue(int nvalue);
  22. int modPrice(int nprice);
  23. char* modName(char *nnmae);
  24. // Can add more methods
  25. };
  26.  
  27. class ProductList
  28. {
  29. Product* head;
  30. public: ProductList(); //creates an empty list
  31. ~ProductList(); //deletes the whole list
  32. int addProduct(int code,int value,int price,char *name); //adding in a simmilar way, that we have added in task 1 !!!CODE UNIQUE!!!
  33. void removeProduct(int code); //removing a product with an unique code
  34. void print(); //print the whole list
  35. };
  36.  
  37. void Product::print()
  38. {
  39. cout<<"Code: "<<code<<endl;
  40. cout<<"Value: "<<value<<endl;
  41. cout<<"Price: "<<price<<endl;
  42. cout<<"Name: "<<name<<endl;
  43. }
  44.  
  45. Product::Product(int ncode,int nvalue,int nprice,char* nname)
  46. {
  47. code=ncode;
  48. value=nvalue;
  49. price=nprice;
  50. if(name!=NULL)
  51. {
  52. name=new char[strlen(nname)+1];
  53. strcpy(name,nname);
  54. }
  55. next=NULL;
  56. print();
  57. }
  58.  
  59. Product::~Product()
  60. {
  61. delete[](name);
  62. print();
  63. }
  64.  
  65. int Product::getCode()
  66. {
  67. return code;
  68. }
  69.  
  70. int Product::getValue()
  71. {
  72. return value;
  73. }
  74.  
  75. int Product::getPrice()
  76. {
  77. return price;
  78. }
  79.  
  80. char* Product::getName()
  81. {
  82. return name;
  83. }
  84.  
  85. int Product::modCode(int ncode)
  86. {
  87. code=ncode;
  88. }
  89.  
  90. int Product::modValue(int nvalue)
  91. {
  92. value=nvalue;
  93. }
  94.  
  95. int Product::modPrice(int nprice)
  96. {
  97. price=nprice;
  98. }
  99.  
  100. char* Product::modName(char* nname)
  101. {
  102. char* n=new char[strlen(nname)+1];
  103. strcpy(n,nname);
  104. name=n;
  105. }
  106.  
  107. ProductList::ProductList()
  108. {
  109. head=new Product(0,0,0,NULL);
  110. }
  111.  
  112. ProductList::~ProductList()
  113. {
  114. while(head != NULL)
  115. {
  116. Product *n=head->next;
  117. //delete[](head->name);
  118. delete head;
  119. head = n;
  120. }
  121. }
  122.  
  123. void ProductList::removeProduct(int code)
  124. {
  125. head=new Product(0,0,0,NULL);
  126. ProductList();
  127. int c=head->getCode();
  128. if(c==code)
  129. {
  130. head=head->next;
  131. }
  132. Product* curr;
  133. Product* nex;
  134. curr=head;
  135. nex=curr->next;
  136. while(nex!=NULL)
  137. {
  138. int n=nex->getCode();
  139. if(n==code)
  140. {
  141. curr->next=nex->next;
  142. //delete[](nex->name);
  143. delete nex;
  144. nex=curr->next;
  145. }
  146. else
  147. {
  148. curr=nex;
  149. nex=nex->next;
  150. }
  151. }
  152. }
  153.  
  154. void ProductList::print()
  155. {
  156. Product *current=head;
  157. while(current!=NULL)
  158. {
  159. int c=current->getCode();
  160. int v=current->getValue();
  161. int p=current->getPrice();
  162. cout<<"Code: "<<c<<endl;
  163. cout<<"Value: "<<v<<endl;
  164. cout<<"Price: "<<p<<endl;
  165. //cout<<"Name: "<<current->name<<endl<<endl;
  166. current=current->next;
  167. }
  168. }
  169.  
  170. int ProductList::addProduct(int code,int value,int price,char* name)
  171. {
  172. Product* curr=head;
  173. Product* tmp=NULL;
  174. while(curr!=NULL)
  175. {
  176. int c=curr->getCode();
  177. if(code==c)
  178. {
  179. int p=curr->getPrice();
  180. char* s=curr->getName();
  181. if(p==price&&strcmp(name,s)==0)
  182. {
  183. int nval=curr->getValue();
  184. curr->modValue(value+nval);
  185. return 1;
  186. }
  187. else return 0;
  188. }
  189. curr=curr->next;
  190. }
  191. if(head==NULL)
  192. {
  193. head=new Product(code,value,price,name);
  194. head->next=NULL;
  195.  
  196. }
  197. else
  198. {
  199. Product *tmp;
  200. tmp=head;
  201. while(tmp->next!=NULL)
  202. {
  203. tmp=tmp->next;
  204. }
  205. tmp->next=new Product(0,0,0,NULL);
  206. tmp=tmp->next;
  207. tmp=new Product(code,value,price,name);
  208. tmp->next=NULL;
  209. }
  210. int g=0,s=0;
  211. Product* sizer=head;
  212. while(sizer!=NULL)
  213. {
  214. s++;
  215. sizer=sizer->next;
  216. }
  217. while(g!=s)
  218. {
  219. Product* sorter=head;
  220. Product* nex=sorter->next;
  221. while(nex!=NULL)
  222. {
  223. int sc=sorter->getCode();
  224. int sv=sorter->getValue();
  225. int sp=sorter->getPrice();
  226. char* sn=sorter->getName();
  227. int nc=nex->getCode();
  228. int np=nex->getPrice();
  229. int nv=nex->getValue();
  230. char* nn=nex->getName();
  231. if(sc>nc)
  232. {
  233. swap(nc,sc);
  234. swap(nv,sv);
  235. swap(np,sp);
  236. swap(nn,sn);
  237. }
  238. else
  239. {
  240. sorter=nex;
  241. nex=nex->next;
  242. }
  243. }
  244. g++;
  245. }
  246. }
  247.  
  248. int main()
  249. {
  250. ProductList List;
  251. //List.removeProduct(1);
  252. List.addProduct(2,3,2,"B");
  253. //ProductList::addProduct(1,1,3,"C");
  254. List.~ProductList();
  255. return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement