Advertisement
Guest User

Project 02

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