1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MLS = 50;
typedef int element;
const element SENTINEL = −1;
class AList{
private:
element items[MLS];
int size;
bool sorted;
public:
void Read ();
void Print ();
void BubbleSort ();
void InsertionSort ();
void Swap (int posa, int posb);
void SelectionSort ();
void LinearSearch (element target, bool &found, int &position);
void BinarySearch (element target, bool &found, int &position);
void GenerateRandomList ();
element DisplayMenu ();
};
int main(){
AList B;
element selection;
element target;
bool found = false;
int position = 0;
selection = B.DisplayMenu ();
if (selection == 1)
B.Read ();
else if (selection == 2)
B.GenerateRandomList ();
else if (selection == 3)
B.BubbleSort ();
else if (selection == 4)
B.InsertionSort ();
else if (selection == 5)
B.SelectionSort ();
else if (selection == 6)
B.LinearSearch (target, found, position);
else if (selection == 7)
B.BinarySearch (target, found, position);
else if (selection == 8){
cout << "Quitting Sort and Search Demo Program, v1.0.";
return 0;
}
else {
cout << "Not a menu option. Please select an option from the ";
cout << "menu.";
selection = B.DisplayMenu ();
}
}
void AList::Read(){
//PRE: none
//POST: The N.O. AList is valid, filled with data entered by the user.
size = 0;
sorted = false;
element userval;
cout << "Enter elements, "<< SENTINEL <<" to stop: ";
userval = readElement();
while ((userval != SENTINEL) && (size < MLS)){
items[size] = userval;
size++;
if (size < MLS)
;
else
cout << "This list is full." << endl;
userval = readElement();
}
main ();
}
element AList::DisplayMenu(){
//PRE: The list has been displayed and the sort status has been
// displayed.
//POST: The menu has been displayed and the user has made a selection
// existing on the menu.
element selection;
cout << "List: " << endl;
if (size == 0)
cout << "(empty)" << endl;
else
Print ();
if (sorted == true)
cout << "This list is sorted. " << endl;
else
cout << "This list is not known to be sorted." << endl;
cout << "Enter a selection from the menu below." << endl;
cout << endl;
cout << "1. Reset the current list from the keyboard" << endl;
cout << "2. Reset the current list using randomly generated elements";
cout << endl;
cout << "3. Perform Bubble Sort on the current list" << endl;
cout << "4. Perform Insertion Sort on the current list" << endl;
cout << "5. Perform Selection Sort on the current list" << endl;
cout << "6. Perform Linear Search on the current list" << endl;
cout << "7. Perform Binary Search on the current list" << endl;
cout << "8. Quit the Program" << endl;
selection = readElement();
return selection;
}
void AList::Print(){
//PRE: The N.O. must be valid.
//POST: The elements in the object calling the method have been
// displayed.
for (int i = 0; i < size; i++)
cout << items[i] << endl;
cout << endl;
}
|