liamthomas5

Code

Nov 24th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. -------Practical1.cpp-------
  2.  
  3. #include "lib.h"
  4. #include "product.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int main ()//Main Program
  9. {
  10. int selection = 0;
  11. int productnumber=0;
  12. int count2 =0;
  13. funct function;
  14.  
  15. do{
  16. cout << endl;
  17. cout << "Enter a menu aoption" << endl;
  18. cout << "1 = Add new item" << endl;
  19. cout << "2 = Print items" << endl;
  20. cout << "3 = Find a price" << endl;
  21. cout << "4 = Sort by ID" << endl;
  22. cout << "5 = Delete by ID" << endl;
  23. cout << "6 = Find the Lowest Price" << endl;
  24. cout << "7 = Exit" << endl << endl;;
  25. cin >> selection;
  26.  
  27. if(selection==1)
  28. {
  29. cout << "Enter the the new ID" << endl;
  30. cin >> products[count2].id;
  31. cout << "Enter the the new price" << endl;
  32. cin >> products[count2].price;
  33. count2++;
  34. }
  35. else if(selection==2)
  36. {
  37. int count =0;
  38. cout <<"ID PRICE";
  39. cout <<"" << endl;
  40. while(count<count2)
  41. {
  42. cout <<products[count].id;
  43. cout <<" ";
  44. cout <<products[count].price << endl;
  45. count++;
  46. }
  47. }
  48. else if(selection==3)
  49. {
  50. int pricematch = function.GetPrice();
  51. }
  52. else if(selection==4)
  53. {
  54. cout <<"Sorting by ID" << endl;
  55. function.Sort(count2);
  56. cout <<"Sorted!" << endl;
  57. }
  58. else if(selection==5)
  59. {
  60. int selection=0;
  61. cout <<"Please select an Id to delete" << endl;
  62. cin >> selection;
  63. function.SetToZero(selection,count2);
  64. count2--;
  65.  
  66. }
  67. else if(selection==6)
  68. {
  69. unsigned int lowestprice = function.FindLowest(count2);
  70. cout << "lowest price is" << endl;
  71. cout << "ID Price" << endl;
  72. cout << products[lowestprice].id << " " << products[lowestprice].price;
  73. }
  74. }
  75. while(selection!=7);
  76. return 0;
  77. }
  78.  
  79. --------lib.cpp------
  80.  
  81. #include <iostream>
  82. using namespace std;
  83. #include "lib.h"
  84. #include "product.h"
  85.  
  86. unsigned int funct::GetPrice()
  87. {
  88.  
  89. int productchoice=0;
  90. int tempvaule=0;
  91. cout << "Please enter in a ID" << endl;
  92. cin >> productchoice;
  93. cout << "your price for that item is" << endl;
  94. cin >> products[productchoice].price;
  95. tempvaule=products[productchoice].price;
  96. return tempvaule;
  97. }
  98.  
  99. void funct::Sort(int count2)
  100. {
  101. int i,j;
  102. for(i=0;i<count2;i++)
  103. {
  104. for(j=0;j<i;j++)
  105. {
  106. if(products[i].id<products[j].id)
  107. {
  108. int temp=products[i].id; //swap
  109. int temp1=products[i].price;
  110. products[i].id=products[j].id;
  111. products[i].price=products[j].price;
  112. products[j].id=temp;
  113. products[j].price=temp1;
  114. }
  115. }
  116. }
  117. }
  118. void funct::SetToZero(int selection,int count2)
  119. {
  120. selection--;
  121. products[selection].id=0;
  122. products[selection].price=0;
  123. int i = selection;
  124. int j = i;
  125. while (i<count2)
  126. {
  127. if (products[i].id == 0)
  128. {
  129. j++;
  130. int temp = products[j].id;
  131. int temp1 = products[j].price;
  132. products[i].id = temp;
  133. products[i].price = temp1;
  134. products[j].id = 0;
  135. products[j].price = 0;
  136. }
  137. i++;
  138. }
  139. }
  140. unsigned int funct::FindLowest(int count2)
  141. {
  142. int i;
  143. int counter=0;
  144. unsigned int tempprice=100;
  145. for(i=0;i<count2;i++)
  146. {
  147. if(products[i].price<tempprice)
  148. {
  149. tempprice=i;
  150. }
  151. }
  152. return tempprice;
  153. }
  154.  
  155. -------product.h-----------
  156.  
  157. #pragma once
  158. class product//struct to hold products
  159. {
  160. public:
  161. int id;//ID FOR EACH PRODUCT
  162. unsigned int price;//price for each product
  163. }products[10];
  164.  
  165.  
  166. -----lib.h----
  167.  
  168. #pragma once
  169. #include "product.h"
  170. class funct
  171. {
  172. public:
  173. unsigned int GetPrice();//Function to get a price from an ID
  174. void Sort(int count2);//Sorts by ID
  175. void SetToZero(int selection, int count2);//Sets price and ID to zero
  176. unsigned int FindLowest(int count2);//Finds the ID and price of the lowest price
  177. };
  178.  
  179.  
  180.  
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment