Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. namespace MILLS_CIS279
  2. {
  3.  
  4. const int MAX_ITEMS = 5;
  5. enum RelationType {LESS, GREATER, EQUAL};
  6.  
  7. class ItemType
  8. {
  9. public:
  10. ItemType();
  11. RelationType ComparedTo(ItemType otherItem);
  12. void Print(std::ofstream&);
  13. void Initialize(int number);
  14.  
  15.  
  16. int value;
  17.  
  18.  
  19. };
  20. }
  21. #endif
  22.  
  23. ItemType::ItemType()
  24. {
  25. value = 0;
  26.  
  27. }
  28.  
  29. void ItemType::Print(std::ofstream&)
  30. {
  31. cout << value << endl;
  32. }
  33. void ItemType::Initialize(int number)
  34. {
  35. value = number;
  36.  
  37.  
  38. }
  39. RelationType ItemType::ComparedTo(ItemType otherItem)
  40. {
  41. if (value < otherItem.value)
  42. {
  43. return LESS;
  44. }
  45. else if (value > otherItem.value)
  46. {
  47. return GREATER;
  48. }
  49. else return EQUAL;
  50. }
  51.  
  52. }
  53.  
  54. #ifndef UNSORTED_H_INCLUDED
  55. #define UNSORTED_H_INCLUDED
  56. #include "itemType.h"
  57.  
  58. namespace MILLS_CIS279
  59. {
  60.  
  61.  
  62.  
  63. class Unsorted
  64. {
  65. public:
  66. Unsorted();
  67. void MakeEmpty();
  68. int GetLength();
  69. bool isFull();
  70. ItemType GetItem(ItemType item, bool& found);
  71. void PutItem(ItemType item);
  72. void DeleteItem(ItemType item);
  73. void ResetList();
  74. ItemType GetNextItem();
  75. void PrintList(std::ofstream&);
  76.  
  77. private:
  78. struct NodeType
  79. {
  80. ItemType key;
  81. NodeType* newItem;
  82. };
  83.  
  84. int length;
  85.  
  86. NodeType* listData;
  87. NodeType* currentPos;
  88.  
  89.  
  90.  
  91.  
  92.  
  93. };
  94. }
  95.  
  96. #endif
  97.  
  98. namespace MILLS_CIS279
  99. {
  100.  
  101. Unsorted::Unsorted()
  102. {
  103. listData = NULL;
  104. length = 0;
  105. }
  106. void Unsorted::MakeEmpty()
  107. {
  108. length = 0;
  109. }
  110. int Unsorted::GetLength()
  111. {
  112. return length;
  113. }
  114. bool Unsorted::isFull()
  115. {
  116. if (length >= MAX_ITEMS)
  117. {
  118. return true;
  119. }
  120. return false;
  121. }
  122. ItemType Unsorted::GetItem(ItemType item, bool& found)
  123. {
  124. NodeType* position;
  125. bool stillSearching;
  126.  
  127. position = listData;
  128. found = false;
  129.  
  130. stillSearching = (position != NULL);
  131.  
  132.  
  133. while (stillSearching && !found)
  134. {
  135. switch(item.ComparedTo(position->key))
  136. {
  137. case LESS:
  138. case GREATER:
  139. position = position->newItem;
  140. stillSearching = (position != NULL);
  141. break;
  142. case EQUAL:
  143. found = true;
  144. item = position->key;
  145. break;
  146. }
  147. }
  148. return item;
  149.  
  150. }
  151.  
  152. void Unsorted::PutItem(ItemType item)
  153. {
  154. NodeType* position = new NodeType;
  155. position->key = item;
  156. position->newItem = listData;
  157. listData = position;
  158.  
  159.  
  160. length++;
  161. }
  162. void Unsorted::DeleteItem(ItemType item)
  163. {
  164. NodeType* position;
  165. NodeType* temp;
  166. position = listData;
  167.  
  168. if (item.ComparedTo(position->key) == EQUAL)
  169. {
  170. temp = position;
  171. listData = listData->newItem;
  172. }
  173. else
  174. while (!((item.ComparedTo((position->newItem)->key) == EQUAL)))
  175. {
  176. position = position->newItem;
  177. temp = position->newItem;
  178. position->newItem = (position->newItem)->newItem;
  179. }
  180. length--;
  181.  
  182. }
  183. void Unsorted::ResetList()
  184. {
  185. currentPos = NULL;
  186. }
  187. ItemType Unsorted::GetNextItem()
  188. {
  189. if (currentPos == NULL)
  190. {
  191. currentPos = listData;
  192. }
  193. else
  194. {
  195. currentPos = currentPos->newItem;
  196. }
  197. return currentPos->key;
  198.  
  199. }
  200. void Unsorted::PrintList(std::ofstream& outFile)
  201. {
  202. int length = this->GetLength();
  203. ItemType item;
  204. this->ResetList();
  205. int i;
  206.  
  207. cout << "nList is: " << endl;
  208. outFile << "nList is: ";
  209.  
  210.  
  211. for (i = 1; i <= length; i++)
  212. {
  213. this->GetNextItem();
  214. cout << item.value << " ";
  215. outFile << item.value << " ";
  216. }
  217. cout << endl;
  218. outFile << endl;
  219.  
  220. }
  221.  
  222.  
  223. }
  224.  
  225. #include <iostream>
  226. #include "itemType.h"
  227. #include "Unsorted.h"
  228. #include <fstream>
  229. #include <string>
  230. #include <ostream>
  231.  
  232.  
  233. using namespace std;
  234. using namespace MILLS_CIS279;
  235.  
  236.  
  237. void PrintList(Unsorted&, std::ofstream&);
  238.  
  239.  
  240. int main()
  241. {
  242. Unsorted list;
  243. ItemType item;
  244.  
  245. item.value = 7;
  246.  
  247.  
  248. ifstream inFile ("listData.txt");
  249. ofstream outFile;
  250. outFile.open ("a3testStephenMills.txt");
  251.  
  252. if (outFile.is_open())
  253. {
  254.  
  255. while (inFile >> item.value)
  256. {
  257. list.PutItem(item);
  258.  
  259. if (list.isFull())
  260. {
  261. break;
  262. }
  263. }
  264. }
  265. cout << "Length of the List: " << list.GetLength() << endl;
  266. outFile << "Length of the List: " << list.GetLength() << endl;
  267.  
  268. // list.PrintList(outFile);
  269.  
  270. item.value = 1;
  271. list.DeleteItem(item);
  272.  
  273. cout << "Length of the List after delete item 1 : " << list.GetLength() << endl;
  274. outFile << "Length of the List after delete item 1 : " << list.GetLength() << endl;
  275. inFile >> item.value;
  276.  
  277.  
  278.  
  279.  
  280.  
  281. if (!list.isFull())
  282. {
  283. item.value = 7;
  284. list.PutItem(item);
  285. }
  286.  
  287.  
  288.  
  289. cout << "Length of the List after put new item : " << list.GetLength() << endl;
  290. outFile << "Length of the List after put new item: " << list.GetLength() << endl;
  291. // list.PrintList(outFile);
  292. inFile.close();
  293. outFile.close();
  294.  
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement