Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Written by: Mohamed Hamida (And Jennifer Martinez)
- //Assignment: Project 2
- //Class: Co Sci 243
- //Date: 11/26/19
- //Description: This is a project.z
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <conio.h>
- #include <iomanip>
- using namespace std;
- void displayDescription();// X
- void showMainMenu();// X
- void showList(const int list[], int activeIndex, int numUsed);// X
- void selectRight(int& activeIndex, int numUsed); // X
- void selectLeft(int& activeIndex, int numUsed);// X
- //void shiftLeft(int list[], int activeIndex, int numUsed);
- //void shiftRight();
- void moveLeft(int list[], int& activeIndex, int numUsed);// X
- void moveRight(int list[], int& activeIndex, int numUsed);// X
- void insertElem();
- void deleteElem(int list[], int& activeIndex, int& numUsed);
- void sortArray();
- bool isLastSelected(int activeIndex, int numUsed);// X
- bool isFirstSelected(int activeIndex, int numUsed);// X
- bool isArrayFull(int numUsed);// X
- bool isArrayEmpty(int numUsed);// X
- int main()
- {
- int list[10],activeIndex(0), scanCode(-1), numUsed(0),index(0); // variable declaration
- ifstream fin("input.txt"); // input
- if(fin.fail()){
- cout <<"File not found";
- exit(1);
- }else{
- fin >> activeIndex; // getting the first number from text file, putting it into activeIndex.
- while(fin >> list[index]){ // filling the rest of the array
- index++; // adding one to index so it can act like a for loop
- numUsed++; // adding one to array size (numUsed is how many elements in the array)
- }
- // cout << numUsed << "$" ;
- // cout << index << endl;
- do{
- system("CLS"); // clearing the screen
- showMainMenu(); //showing main menu
- showList(list, activeIndex, numUsed); // showing the array in correct format.
- scanCode = getch(); // getting the scanCode from user
- if(scanCode == 224 || scanCode ==0){ // if the scanCode starts with 224 or 0, get the scanCode again so it can read the full value
- scanCode = getch();
- }
- switch(scanCode){ // this entire thing reads in all the numbers and stuff
- case '1':
- case 77:
- //cout << "Option 1--> Select right";
- selectRight(activeIndex, numUsed);
- break;
- case '2':
- case 75:
- //cout << "Option 2 --> Select Left";
- selectLeft(activeIndex, numUsed);
- break;
- case '3':
- case 72:
- //cout << "Option 3 --> Move Right";
- moveRight(list,activeIndex,numUsed);
- break;
- case '4':
- case 80:
- //cout <<"option 4 --> Move Left";
- moveLeft(list,activeIndex,numUsed);
- break;
- case '5':
- case 82:
- cout << "option 5 --> Insert";
- //insertElem();
- break;
- case '6':
- case 83:
- cout << "option 6 --> Delete";
- deleteElem(list,activeIndex,numUsed);
- break;
- case '7':
- case 60:
- cout << "option 7 --> Sort";
- //insertElem();
- break;
- case '8':
- case 59:
- cout << "option 8 --> exit!";
- //insertElem()
- break;
- }
- //showMainMenu();
- //showList(list,activeIndex,numUsed);
- }while(scanCode != 59); //this loop runs until f1 is hit.
- }
- return 0;
- }
- void showList(const int list[], int activeIndex, int numUsed){
- for(int i = 0; i < numUsed; i++){
- if(i==activeIndex){
- cout << "<" << list[i] << "> " ;
- }else{
- cout << list[i] << " ";
- }
- }
- cout << endl;
- }
- void showMainMenu(){
- cout <<endl<< "Select Right......\"1\" or \"Right Arrow\" key "<< endl <<
- "Select Left.......\"2\" or \"Left Arrow\" key "<<endl <<
- "Move Right........\"3\" or \"Up Arrow\" key " <<endl <<
- "Move Left.........\"4\" or \"Down Arrow\" key " <<endl <<
- "Insert............\"5\" or \"Insert\" key " <<endl <<
- "Delete............\"6\" or \"Delete\" key " <<endl <<
- "Sort..............\"7\" or \"F2\" key " <<endl <<
- "Exit..............\"8\" or \"F1\" key " << endl <<endl;
- }
- void moveLeft(int list[], int& activeIndex, int numUsed)
- {
- int tempVal;
- if(!(isFirstSelected(activeIndex,numUsed))){
- tempVal = list[activeIndex -1];
- list[activeIndex-1] = list[activeIndex];
- activeIndex = activeIndex -1;
- list[activeIndex+1] = tempVal;
- }
- }
- void moveRight(int list[], int& activeIndex, int numUsed){
- int tempVal;
- if(!(isLastSelected(activeIndex,numUsed))){
- tempVal = list[activeIndex +1];
- list[activeIndex+1] = list[activeIndex];
- activeIndex = activeIndex +1;
- list[activeIndex-1] = tempVal;
- //activeIndex = activeIndex -1;
- }
- }
- void deleteElem(int list[], int& activeIndex, int& numUsed){
- int del = activeIndex;
- if(!(isArrayEmpty(numUsed))){
- for (int i = activeIndex; i < numUsed - 1; i++){
- list[i] = list[i + 1];
- if(isFirstSelected(activeIndex, numUsed)){
- activeIndex = 0;
- }
- }
- numUsed = numUsed -1;
- if(isFirstSelected(activeIndex, numUsed)){
- activeIndex = 0;
- }else{
- activeIndex = activeIndex -1;
- }
- }
- }
- //void shiftLeft(int list[], )
- void selectLeft(int& activeIndex, int numUsed){
- if(isFirstSelected(activeIndex,numUsed)){
- activeIndex = numUsed-1;
- }else{
- activeIndex = activeIndex -1;
- }
- }
- void selectRight(int& activeIndex, int numUsed){
- if(isLastSelected(activeIndex,numUsed)){
- activeIndex = 0;
- }else{
- activeIndex = activeIndex +1;
- }
- }
- bool isLastSelected(int activeIndex, int numUsed){
- if(activeIndex == (numUsed-1)){
- return true;
- }else{
- return false;
- }
- }
- bool isFirstSelected(int activeIndex, int numUsed){
- if(activeIndex == 0){
- return true;
- }else{
- return false;
- }
- }
- bool isArrayEmpty(int numUsed){
- if(numUsed ==0){
- return true;
- }else{
- return false;
- }
- }
- bool isArrayFull(int numUsed){
- if(numUsed >=10){
- return true;
- }else{
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement