Advertisement
Guest User

entire file with main commented

a guest
Dec 7th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 KB | None | 0 0
  1. //Written by:  Mohamed Hamida (And Jennifer Martinez)
  2. //Assignment:  Project 2
  3. //Class:       Co Sci 243
  4. //Date:        11/26/19
  5. //Description: This is a project.z
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <cstdlib>
  10. #include <conio.h>
  11. #include <iomanip>
  12. using namespace std;
  13.  
  14. void displayDescription();// X
  15. void showMainMenu();// X
  16. void showList(const int list[], int activeIndex, int numUsed);// X
  17. void selectRight(int& activeIndex, int numUsed); // X
  18. void selectLeft(int& activeIndex, int numUsed);// X
  19. //void shiftLeft(int list[], int activeIndex, int numUsed);
  20. //void shiftRight();
  21. void moveLeft(int list[], int& activeIndex, int numUsed);// X
  22. void moveRight(int list[], int& activeIndex, int numUsed);// X
  23. void insertElem();
  24. void deleteElem(int list[], int& activeIndex, int& numUsed);
  25. void sortArray();
  26. bool isLastSelected(int activeIndex, int numUsed);// X
  27. bool isFirstSelected(int activeIndex, int numUsed);// X
  28. bool isArrayFull(int numUsed);// X
  29. bool isArrayEmpty(int numUsed);// X
  30.  
  31. int main()
  32. {
  33.     int list[10],activeIndex(0), scanCode(-1), numUsed(0),index(0); // variable declaration
  34.     ifstream fin("input.txt"); // input
  35.  
  36.     if(fin.fail()){
  37.         cout <<"File not found";
  38.         exit(1);
  39.     }else{
  40.             fin >> activeIndex; // getting the first number from text file, putting it into activeIndex.
  41.             while(fin >> list[index]){ // filling the rest of the array
  42.                 index++; // adding one to index so it can act like a for loop
  43.                 numUsed++; // adding one to array size (numUsed is how many elements in the array)
  44.             }
  45.  
  46.             //  cout << numUsed << "$" ;
  47.             //   cout << index << endl;
  48.  
  49.             do{
  50.                 system("CLS"); // clearing the screen
  51.                 showMainMenu(); //showing main menu
  52.                 showList(list, activeIndex, numUsed); // showing the array in correct format.
  53.  
  54.                 scanCode = getch(); // getting the scanCode from user
  55.                 if(scanCode == 224 || scanCode ==0){ // if the scanCode starts with 224 or 0, get the scanCode again so it can read the full value
  56.                     scanCode = getch();
  57.                 }
  58.  
  59.                 switch(scanCode){ // this entire thing reads in all the numbers and stuff
  60.                     case '1':
  61.                     case 77:
  62.                         //cout << "Option 1--> Select right";
  63.                         selectRight(activeIndex, numUsed);
  64.                         break;
  65.                     case '2':
  66.                     case 75:
  67.                         //cout << "Option 2 --> Select Left";
  68.                         selectLeft(activeIndex, numUsed);
  69.                         break;
  70.                     case '3':
  71.                     case 72:
  72.                         //cout << "Option 3 --> Move Right";
  73.                         moveRight(list,activeIndex,numUsed);
  74.                         break;
  75.                     case '4':
  76.                     case 80:
  77.                         //cout <<"option 4 --> Move Left";
  78.                         moveLeft(list,activeIndex,numUsed);
  79.                         break;
  80.                     case '5':
  81.                     case 82:
  82.                         cout << "option 5 --> Insert";
  83.                         //insertElem();
  84.                         break;
  85.                     case '6':
  86.                     case 83:
  87.                         cout << "option 6 --> Delete";
  88.                         deleteElem(list,activeIndex,numUsed);
  89.                         break;
  90.                     case '7':
  91.                     case 60:
  92.                         cout << "option 7 --> Sort";
  93.                         //insertElem();
  94.                         break;
  95.                     case '8':
  96.                     case 59:
  97.                         cout << "option 8 --> exit!";
  98.                         //insertElem()
  99.                         break;
  100.                 }
  101.             //showMainMenu();
  102.             //showList(list,activeIndex,numUsed);
  103.         }while(scanCode != 59); //this loop runs until f1 is hit.
  104.     }
  105.     return 0;
  106. }
  107.  
  108. void showList(const int list[], int activeIndex, int numUsed){
  109.     for(int i = 0; i < numUsed; i++){
  110.         if(i==activeIndex){
  111.             cout << "<" << list[i] << "> " ;
  112.         }else{
  113.             cout << list[i] << " ";
  114.         }
  115.     }
  116.     cout << endl;
  117. }
  118.  
  119. void showMainMenu(){
  120.     cout <<endl<< "Select Right......\"1\" or \"Right Arrow\" key "<< endl <<
  121.             "Select Left.......\"2\" or \"Left Arrow\" key "<<endl <<
  122.             "Move Right........\"3\" or \"Up Arrow\" key " <<endl <<
  123.             "Move Left.........\"4\" or \"Down Arrow\" key " <<endl <<
  124.             "Insert............\"5\" or \"Insert\" key " <<endl <<
  125.             "Delete............\"6\" or \"Delete\" key " <<endl <<
  126.             "Sort..............\"7\" or \"F2\" key " <<endl <<
  127.             "Exit..............\"8\" or \"F1\" key " << endl <<endl;
  128. }
  129.  
  130. void moveLeft(int list[], int& activeIndex, int numUsed)
  131. {
  132.     int tempVal;
  133.     if(!(isFirstSelected(activeIndex,numUsed))){
  134.         tempVal = list[activeIndex -1];
  135.         list[activeIndex-1] = list[activeIndex];
  136.         activeIndex = activeIndex -1;
  137.         list[activeIndex+1] = tempVal;
  138.     }
  139. }
  140.  
  141. void moveRight(int list[], int& activeIndex, int numUsed){
  142.     int tempVal;
  143.     if(!(isLastSelected(activeIndex,numUsed))){
  144.         tempVal = list[activeIndex +1];
  145.         list[activeIndex+1] = list[activeIndex];
  146.         activeIndex = activeIndex +1;
  147.         list[activeIndex-1] = tempVal;
  148.         //activeIndex = activeIndex -1;
  149.     }
  150. }
  151.  
  152. void deleteElem(int list[], int& activeIndex, int& numUsed){
  153.     int del = activeIndex;
  154.     if(!(isArrayEmpty(numUsed))){
  155.         for (int i = activeIndex; i < numUsed - 1; i++){
  156.             list[i] = list[i + 1];
  157.             if(isFirstSelected(activeIndex, numUsed)){
  158.                 activeIndex = 0;
  159.             }
  160.         }
  161.         numUsed = numUsed -1;
  162.         if(isFirstSelected(activeIndex, numUsed)){
  163.             activeIndex = 0;
  164.         }else{
  165.             activeIndex = activeIndex -1;
  166.         }
  167.     }
  168. }
  169. //void shiftLeft(int list[],  )
  170.  
  171. void selectLeft(int& activeIndex, int numUsed){
  172.     if(isFirstSelected(activeIndex,numUsed)){
  173.         activeIndex = numUsed-1;
  174.     }else{
  175.         activeIndex = activeIndex -1;
  176.     }
  177. }
  178.  
  179. void selectRight(int& activeIndex, int numUsed){
  180.     if(isLastSelected(activeIndex,numUsed)){
  181.         activeIndex = 0;
  182.     }else{
  183.         activeIndex = activeIndex +1;
  184.     }
  185. }
  186.  
  187. bool isLastSelected(int activeIndex, int numUsed){
  188.     if(activeIndex == (numUsed-1)){
  189.         return true;
  190.     }else{
  191.         return false;
  192.     }
  193. }
  194.  
  195. bool isFirstSelected(int activeIndex, int numUsed){
  196.     if(activeIndex == 0){
  197.         return true;
  198.     }else{
  199.         return false;
  200.     }
  201. }
  202.  
  203. bool isArrayEmpty(int numUsed){
  204.     if(numUsed ==0){
  205.         return true;
  206.     }else{
  207.         return false;
  208.     }
  209. }
  210.  
  211. bool isArrayFull(int numUsed){
  212.     if(numUsed >=10){
  213.         return true;
  214.     }else{
  215.         return false;
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement