Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.52 KB | None | 0 0
  1. main.cpp
  2.  
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. #include "ListArray.cpp"
  9. #include "config.h"
  10.  
  11. void print_help();
  12. void showTwoLists(List<char> list1, List<char> list2); // Displays two lists that are supposedly equivalent.
  13.  
  14. int main()
  15. {
  16.  
  17. #if LAB3_TEST1
  18. List<int> testList(8); // Test list to test with ints
  19. List<int> copyList(testList); // Used to test copy constructor
  20. List<int> assignList; // Used to test assignment operator
  21. int testData; // List data item
  22. #else
  23. List<char> testList(8); // Test list to test with chars
  24. List<char> copyList(testList); // Used to test copy constructor
  25. List<char> assignList; // Used to test assignment operator
  26. char testData; // List data item
  27. #endif
  28. int n; // Position within list
  29. char cmd; // Input command
  30.  
  31. print_help();
  32.  
  33. do
  34. {
  35. testList.showStructure(); // Output list
  36.  
  37. cout << endl << "Command: "; // Read command
  38. cin >> cmd;
  39. if ( cmd == '+' || cmd == '=' || cmd == '?' )
  40. cin >> testData;
  41. else if ( cmd == 'M' || cmd == 'm' )
  42. cin >> n;
  43.  
  44. switch ( cmd )
  45. {
  46. case 'H' : case 'h':
  47. print_help();
  48. break;
  49.  
  50. case '+' : // insert
  51. cout << "Insert " << testData << endl;
  52. try
  53. {
  54. testList.insert(testData);
  55. }
  56. catch (logic_error &e)
  57. {
  58. cerr << "EXCEPTION: A logic error occurred in the insert function.";
  59. }
  60. break;
  61.  
  62. case '-' : // remove
  63. cout << "Remove the data item marked by the cursor"
  64. << endl;
  65. try
  66. {
  67. testList.remove();
  68. }
  69. catch (logic_error &e)
  70. {
  71. cerr << "EXCEPTION: A logic error occurred in the remove function.";
  72. }
  73. break;
  74.  
  75. case '=' : // replace
  76. cout << "Replace the data item marked by the cursor "
  77. << "with " << testData << endl;
  78. try
  79. {
  80. testList.replace(testData);
  81. }
  82. catch (logic_error &e)
  83. {
  84. cerr << "EXCEPTION: A logic error occurred in the replace function.";
  85. }
  86. break;
  87.  
  88. case '@' : // getCursor
  89. try
  90. {
  91. cout << "Data item marked by the cursor is "
  92. << testList.getCursor() << endl;
  93. }
  94. catch (logic_error &e)
  95. {
  96. cerr << "EXCEPTION: A logic error occurred in the getCursor function.";
  97. }
  98. break;
  99.  
  100. case '<' : // gotoBeginning
  101. cout << "Go to the beginning of the list" << endl;
  102. try
  103. {
  104. testList.gotoBeginning();
  105. }
  106. catch (logic_error &e)
  107. {
  108. cerr << "EXCEPTION: A logic error occurred in the gotoBeginning function.";
  109. }
  110. break;
  111.  
  112. case '>' : // gotoEnd
  113. cout << "Go to the end of the list" << endl;
  114. try
  115. {
  116. testList.gotoEnd();
  117. }
  118. catch (logic_error &e)
  119. {
  120. cerr << "EXCEPTION: A logic error occurred in the gotoEnd function.";
  121. }
  122. break;
  123.  
  124. case 'N' : case 'n' : // gotoNext
  125. try
  126. {
  127. if ( testList.gotoNext() )
  128. cout << "Go to the next data item" << endl;
  129. else
  130. cout << "Failed -- either at the end of the list "
  131. << "or the list is empty" << endl;
  132. }
  133. catch (logic_error &e)
  134. {
  135. cerr << "EXCEPTION: A logic error occurred in the gotoNext function.";
  136. }
  137. break;
  138.  
  139. case 'P' : case 'p' : // gotoPrior
  140. try
  141. {
  142. if ( testList.gotoPrior() )
  143. cout << "Go to the prior data item" << endl;
  144. else
  145. cout << "Failed -- either at the beginning of the "
  146. << "list or the list is empty" << endl;
  147. }
  148. catch (logic_error &e)
  149. {
  150. cerr << "EXCEPTION: A logic error occurred in the gotoPrior function.";
  151. }
  152. break;
  153.  
  154. case 'C' : case 'c' : // clear
  155. cout << "Clear the list" << endl;
  156. testList.clear();
  157. break;
  158.  
  159. case 'E' : case 'e' : // isEmpty
  160. if ( testList.isEmpty() )
  161. cout << "List is empty" << endl;
  162. else
  163. cout << "List is NOT empty" << endl;
  164. break;
  165.  
  166. case 'F' : case 'f' : // isFull
  167. if ( testList.isFull() )
  168. cout << "List is full" << endl;
  169. else
  170. cout << "List is NOT full" << endl;
  171. break;
  172.  
  173. case '!' :
  174. // showTwoLists(copyList, testList);
  175. break;
  176. case '#' :
  177. assignList.insert('x');
  178. assignList = testList;
  179. // showTwoLists(assignList, testList);
  180. break;
  181.  
  182. #if LAB3_TEST2
  183. case 'M' : case 'm' : // In-lab Exercise 2
  184. cout << "Move the data item marked by the cursor to "
  185. << "posititon " << n << endl;
  186. try
  187. {
  188. testList.moveToNth(n);
  189. }
  190. catch (logic_error &e)
  191. {
  192. cerr << "EXCEPTION: A logic error occurred in the moveToNth function.";
  193. }
  194. break;
  195. #endif // LAB3_TEST1
  196.  
  197. #if LAB3_TEST3
  198. case '?' : // In-lab Exercise 3
  199. try
  200. {
  201. if ( testList.find(testData) )
  202. cout << "Found" << endl;
  203. else
  204. cout << "NOT found" << endl;
  205. }
  206. catch (logic_error &e)
  207. {
  208. cerr << "EXCEPTION: A logic error occurred in the find function.";
  209. }
  210. break;
  211. #endif // LAB3_TEST3
  212.  
  213. case 'Q' : case 'q' : // Quit test program
  214. break;
  215.  
  216. default : // Invalid command
  217. cout << "Inactive or invalid command" << endl;
  218. }
  219. }
  220. while ( cin && cmd != 'Q' && cmd != 'q' );
  221.  
  222. if( !cin ) {
  223. cout << "Input error" << endl;
  224. }
  225.  
  226. return 0;
  227. }
  228.  
  229. void showTwoLists(List<char> list1, List<char> list2) {
  230. // Variables should match, but dynamic memory buffer must be different
  231. cout << "Look at the two lists below and decide whether they are equivalent" << endl;
  232. cout << "List 1: ";
  233. list1.showStructure();
  234. cout << "List 2: ";
  235. list2.showStructure();
  236. cout << endl;
  237. }
  238.  
  239. void print_help()
  240. {
  241. cout << endl << "Commands:" << endl;
  242. cout << " H : Help (displays this message)" << endl;
  243. cout << " +x : Insert x after the cursor" << endl;
  244. cout << " - : Remove the data item marked by the cursor" << endl;
  245. cout << " =x : Replace the data item marked by the cursor with x"
  246. << endl;
  247. cout << " @ : Display the data item marked by the cursor" << endl;
  248. cout << " < : Go to the beginning of the list" << endl;
  249. cout << " > : Go to the end of the list" << endl;
  250. cout << " N : Go to the next data item" << endl;
  251. cout << " P : Go to the prior data item" << endl;
  252. cout << " C : Clear the list" << endl;
  253. cout << " E : Empty list?" << endl;
  254. cout << " F : Full list?" << endl;
  255. cout << " ! : Test copy constructor" << endl;
  256. cout << " # : Test assignment operator" << endl;
  257. cout << " M n : Move data item marked by cursor to pos. n ("
  258. #if LAB3_TEST2
  259. << "Active "
  260. #else
  261. << "Inactive "
  262. #endif // LAB3_TEST2
  263. << ": In-lab Ex. 2)" << endl;
  264. cout << " ?x : Search rest of list for x ("
  265. #if LAB3_TEST3
  266. << "Active "
  267. #else
  268. << "Inactive "
  269. #endif // LAB3_TEST3
  270. << ": In-lab Ex. 3)" << endl;
  271. cout << " Q : Quit the test program" << endl;
  272. cout << endl;
  273. }
  274.  
  275.  
  276. ListArray.h
  277.  
  278. /--------------------------------------------------------------------
  279.  
  280. #ifndef LISTARRAY_H
  281. #define LISTARRAY_H
  282.  
  283. #include <stdexcept>
  284. #include <iostream>
  285.  
  286. using namespace std;
  287.  
  288. template < typename DataType >
  289. class List
  290. {
  291. public:
  292.  
  293. static const int MAX_LIST_SIZE = 10; // Default maximum list size
  294.  
  295. // Constructors
  296. List ( int maxNumber = MAX_LIST_SIZE ); // Default constructor
  297. List ( const List& source ); // Copy constructor
  298.  
  299. // Overloaded assignment operator
  300. List& operator= ( const List& source );
  301.  
  302. // Destructor
  303. virtual ~List ();
  304.  
  305. // List manipulation operations
  306. virtual void insert ( const DataType& newDataItem ) // Insert after cursor
  307. throw ( logic_error );
  308. void remove () throw ( logic_error ); // Remove data item
  309. virtual void replace ( const DataType& newDataItem ) // Replace data item
  310. throw ( logic_error );
  311. void clear (); // Clear list
  312.  
  313. // List status operations
  314. bool isEmpty () const; // List is empty
  315. bool isFull () const; // List is full
  316.  
  317. // List iteration operations
  318. void gotoBeginning () // Go to beginning
  319. throw ( logic_error );
  320. void gotoEnd () // Go to end
  321. throw ( logic_error );
  322. bool gotoNext () // Go to next data item
  323. throw ( logic_error );
  324. bool gotoPrior () // Go to prior data item
  325. throw ( logic_error );
  326. DataType getCursor () const
  327. throw ( logic_error ); // Return data item
  328.  
  329. // Output the list structure -- used in testing/debugging
  330. virtual void showStructure () const;
  331.  
  332. // In-lab operations
  333. void moveToNth ( int n ) // Move data item to pos. n
  334. throw ( logic_error );
  335. bool find ( const DataType& searchDataItem ) // Find data item
  336. throw ( logic_error );
  337.  
  338. protected:
  339.  
  340. // Data members
  341. int maxSize,
  342. size, // Actual number of data item in the list
  343. cursor; // Cursor array index
  344. DataType *dataItems; // Array containing the list data item
  345. };
  346.  
  347. #endif
  348.  
  349. ListArray.cpp
  350.  
  351. /**
  352. *Lab3: List ListArray.cpp
  353. *Claire Ujeneza & Christian Uwimana & Jerry Groom
  354. *2/22/2015
  355. */
  356.  
  357. #include "ListArray.h"
  358. #include <iostream>
  359. #include <iomanip>
  360. using namespace std;
  361.  
  362. template <typename DataType>
  363. List<DataType>::List(int maxNumber)
  364. {
  365. maxSize=maxNumber;
  366. size=0;
  367. cursor=-1;
  368. dataItems=new DataType[maxSize];
  369. }
  370. //copy constructor
  371. template <typename DataType>
  372. List<DataType>::List(const List& source){
  373. maxSize=source.maxSize;
  374. size= source.size;
  375. cursor=source.cursor;
  376. dataItems = new DataType [maxSize];
  377. for ( int i = 0; i<size ; i++){
  378. dataItems[i]= source.dataItems[i];
  379. }
  380. }
  381.  
  382. template <typename DataType>
  383. List<DataType>& List<DataType>::operator=(const List& source)
  384. {
  385.  
  386. if (this!= & source){
  387.  
  388. delete [] dataItems;
  389. size=source.size;
  390. maxSize=source.maxSize;
  391. cursor=source.cursor;
  392. dataItems=new DataType [maxSize];
  393.  
  394. }
  395. for(int i=0;i<size;i++){
  396. dataItems[i]=source.dataItems[i];
  397. }
  398.  
  399. return *this;
  400. }
  401.  
  402. template <typename DataType>
  403. List<DataType>::~List()
  404. {
  405. delete[] dataItems;
  406. }
  407. template <typename DataType>
  408. void List<DataType>::insert(const DataType& newDataItem) throw (logic_error)
  409. {
  410. if (size==maxSize)
  411. throw logic_error("List is full");
  412. else
  413. {
  414.  
  415. for(int i = size-1; i>cursor; i--){
  416. DataType initialI= dataItems[i] ;
  417. dataItems[i]=dataItems[i+1];
  418. dataItems[i+1]=initialI;
  419. }
  420. dataItems[cursor+1] = newDataItem;
  421.  
  422. size++;
  423. cursor++;
  424. }
  425. }
  426.  
  427. template <typename DataType>
  428. void List<DataType>::remove() throw (logic_error)
  429. {
  430. if(size==0){
  431. throw logic_error("the list is empty");
  432. }else{
  433. if (cursor == size-1)
  434. {
  435. size--;
  436. gotoBeginning();
  437. }
  438. else
  439. {
  440. for (int i = cursor;i<size;i++)
  441. {
  442. dataItems[i]=dataItems[i+1];
  443. }
  444. size--;
  445. }
  446. }
  447.  
  448. }
  449. template <typename DataType>
  450. void List<DataType>::replace(const DataType& newDataItem) throw (logic_error)
  451. {
  452.  
  453. if(size==0){
  454. throw logic_error("The List is full");
  455. }
  456. else{
  457.  
  458.  
  459. dataItems[cursor]=newDataItem; //Set the dataItems cursor Index value = to newDataItems value.
  460. }
  461. }
  462.  
  463.  
  464. template <typename DataType>
  465. void List<DataType>::clear()
  466. {
  467.  
  468. size = 0;
  469. cursor=0;
  470.  
  471. }
  472. template <typename DataType>
  473. bool List<DataType>::isEmpty() const // isEmpty
  474. {
  475.  
  476. if(size==0)
  477. return true;
  478. else
  479. return false;
  480. }
  481.  
  482.  
  483. template <typename DataType> // is full method
  484. bool List<DataType>::isFull() const
  485. {
  486. if(size!= MAX_LIST_SIZE)
  487. return false;
  488. else
  489. return true;
  490. }
  491.  
  492. template <typename DataType>
  493. void List<DataType>::gotoBeginning() throw (logic_error) //go to the beginning
  494. {
  495. if(size==0){
  496. throw logic_error("The List is empty");
  497. }
  498. else{
  499. cursor = 0;
  500. }
  501.  
  502. }
  503.  
  504. template <typename DataType>
  505. void List<DataType>::gotoEnd() throw (logic_error) //go to the end
  506. {
  507. if(size==0){
  508. throw logic_error("The List is empty");
  509. }
  510. else{
  511. cursor = size - 1;
  512. }
  513. }
  514.  
  515.  
  516. template <typename DataType>
  517. bool List<DataType>::gotoNext() throw (logic_error)
  518. {
  519.  
  520. if(size==0){
  521. throw logic_error("The List is empty");
  522. }
  523. else{
  524. if(cursor<size-1){
  525. cursor++;
  526. return true;
  527. }
  528. }
  529. return false;
  530. }
  531.  
  532.  
  533. template <typename DataType>
  534. bool List<DataType>::gotoPrior() throw (logic_error)
  535. {
  536. if(isEmpty()){
  537. throw logic_error("The List is empty");
  538. }
  539. else{
  540. if(cursor!=0){
  541. cursor--;
  542. return true;
  543. }
  544. }
  545. return false;
  546. }
  547.  
  548.  
  549. template <typename DataType>
  550. DataType List<DataType>::getCursor() const throw (logic_error)
  551. {
  552.  
  553. if (isEmpty())
  554. throw logic_error("list is empty");
  555. else
  556. return dataItems[cursor];
  557.  
  558. }
  559.  
  560. template <typename DataType>
  561. void List<DataType>:: showStructure () const
  562.  
  563. // outputs the data items in a list. if the list is empty, outputs
  564. // "empty list". this operation is intended for testing/debugging
  565. // purposes only.
  566.  
  567. {
  568. int j; // loop counter
  569.  
  570. if ( size == 0 )
  571. cout << "empty list" << endl;
  572.  
  573. else
  574. {
  575. cout << "size = " << size
  576. << " cursor = " << cursor << endl;
  577. for ( j = 0 ; j < maxSize ; j++ )
  578. cout << j << "\t";
  579. cout << endl;
  580. for ( j = 0 ; j < size ; j++ ) {
  581. if( j == cursor ) {
  582. cout << "[";
  583. cout << dataItems[j]
  584. #ifdef ORDEREDLIST_CPP
  585. .getKey()
  586. #endif
  587. ;
  588. cout << "]";
  589. cout << "\t";
  590. }
  591. else
  592. cout << dataItems[j]
  593. #ifdef ORDEREDLIST_CPP
  594. .getKey()
  595. #endif
  596. << "\t";
  597. }
  598. cout << endl;
  599. }
  600. }
  601.  
  602. config.h
  603.  
  604.  
  605. #define LAB3_TEST1 0 // 0 => test with char, 1 => test with int
  606. #define LAB3_TEST2 0 // Prog exercise 2: moveToNth
  607. #define LAB3_TEST3 0 // Prog exercise 3: find
  608.  
  609. /**
  610. * Ordered list class tests.
  611. */
  612. #define LAB4_TEST1 1 // merge: programming exercise 2
  613. #define LAB4_TEST2 0 // subset: programming exercise 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement