1 /* Roy Keene 2 CS 2314 3 Section 04 4 Lab 07 5 22 Oct 02 6 lab7.cpp 7 */ 8 9 #include <stdlib.h> 10 #include <time.h> 11 #include <iostream> 12 #include <string> 13 14 using namespace std; 15 //#define _CS2314_LAB7_DUPLICATES 16 #include "sortedtype.h" 17 18 #ifndef _CS2314_LAB7_NUM_ITEMS 19 #define _CS2314_LAB7_NUM_ITEMS 500 20 #endif 21 22 #ifndef _CS2314_LAB7_OFILE 23 #define _CS2314_LAB7_OFILE "outFile" 24 #endif 25 26 int old_main(void); 27 int main(void) { 28 SortedType<int> m; 29 ofstream out(_CS2314_LAB7_OFILE); 30 int i, n, x, max, *tree, l, ol, f; 31 32 for (f=0;f<10;f++) { 33 srand(time(NULL)+max+rand()); 34 n=1+(int) (_CS2314_LAB7_NUM_ITEMS*(rand()/(RAND_MAX+1.0))); 35 n*=2; 36 max=n+(int) ((_CS2314_LAB7_NUM_ITEMS*3)*(rand()/(RAND_MAX+1.0))); 37 tree=new int[n]; 38 for (i=0;i<n;i++) { 39 ol=l=m.LengthIs(); 40 x=(int) (max*1.0*(rand()/(RAND_MAX+1.0))); 41 #ifndef _CS2314_LAB7_DUPLICATES 42 if (m.RetrieveItem(x)) { i--; continue; } 43 #endif 44 out << "Inserting " << x << "\n"; 45 m.InsertItem(x); 46 tree[i]=x; 47 if (!m.RetrieveItem(x)) cerr << "Error! Was unable to retrieve an item I just inserted: " << tree[i] << "\n"; 48 l=m.LengthIs(); 49 if ((l-ol)!=1) cerr << "Error! Inserting caused an odd difference! [l-ol]=" << (l-ol) << "\n"; 50 } 51 out << "[" << (l=m.LengthIs()) << "] "; m.PrintList(out); 52 if (l!=n) cerr << "Error! Inserted " << n << " items, but only have " << l << " items there now!\n"; 53 for (i=0;i<n;i+=2) { 54 ol=l=m.LengthIs(); 55 out << "Removing " << tree[i] << "\n"; 56 m.DeleteItem(tree[i]); 57 l=m.LengthIs(); 58 if ((ol-l)!=1) cerr << "Error! Deleting caused an odd difference! [l-ol]=" << (l-ol) << "\n"; 59 #ifndef _CS2314_LAB7_DUPLICATES 60 if (m.RetrieveItem(tree[i])) cerr << "Error! I was able to retrieve an item I just deleted: " << tree[i] << "\n"; 61 #endif 62 } 63 out << "[" << (l=m.LengthIs()) << "] "; m.PrintList(out); 64 if (l!=(n/2)) cerr << "Error! Inserted " << (n/2) << " items, but only have " << l << " items there now!\n"; 65 for (i=0;i<n;i+=2) { 66 ol=l=m.LengthIs(); 67 x=(int) (max*1.0*(rand()/(RAND_MAX+1.0))); 68 #ifndef _CS2314_LAB7_DUPLICATES 69 if (m.RetrieveItem(x)) { i-=2; continue; } 70 #endif 71 out << "Inserting " << x << "\n"; 72 m.InsertItem(x); 73 tree[i]=x; 74 if (!m.RetrieveItem(x)) cerr << "Error! Was unable to retrieve an item I just inserted: " << tree[i] << "\n"; 75 l=m.LengthIs(); 76 if ((l-ol)!=1) cerr << "Error! Inserting caused an odd difference! [l-ol]=" << (l-ol) << "\n"; 77 } 78 out << "[" << (l=m.LengthIs()) << "] "; m.PrintList(out); 79 if (l!=n) cerr << "Error! Inserted " << n << " items, but only have " << l << " items there now!\n"; 80 for (i=1;i<n;i+=2) { 81 ol=l=m.LengthIs(); 82 out << "Removing " << tree[i] << "\n"; 83 m.DeleteItem(tree[i]); 84 l=m.LengthIs(); 85 if ((ol-l)!=1) cerr << "Error! Deleting caused an odd difference! [l-ol]=" << (l-ol) << "\n"; 86 #ifndef _CS2314_LAB7_DUPLICATES 87 if (m.RetrieveItem(tree[i])) cerr << "Error! I was able to retrieve an item I just deleted: " << tree[i] << "\n"; 88 #endif 89 } 90 out << "[" << (l=m.LengthIs()) << "] "; m.PrintList(out); 91 if (l!=(n/2)) cerr << "Error! Inserted " << (n/2) << " items, but only have " << l << " items there now!\n"; 92 93 m.MakeEmpty(); 94 if (m.LengthIs()) cerr << "Error! MakeEmpty() did not make empty?! LengthIs()=" << m.LengthIs() << "\n"; 95 96 delete tree; 97 } 98 99 out << "----------------\n"; 100 101 out.close(); 102 103 return(old_main()); 104 } 105 106 int old_main(void) { 107 SortedType<int> m1, m3; 108 SortedType<string> m2; 109 ofstream out(_CS2314_LAB7_OFILE, ios::app); 110 int tree1[]={5,1,10,-1,2,8,13,-2,0,3,7,9,11,12},dtree1[]={1,-2,5}; 111 string tree2[]={"test","joe","bob","a","sally","END"}; 112 int tree3[]={50,25,75,12,37,63,87,6,18,29,43,57,69,81,93}, dtree3[]={25,50,12,75,87,6}; 113 int a=3, ol, l; 114 unsigned int i; 115 116 117 for (i=0;i<(sizeof(tree1)/sizeof(int));i++) { 118 out << "Inserting " << tree1[i] << "\n"; 119 m1.InsertItem(tree1[i]); 120 } 121 122 if (m1.RetrieveItem(a)) { out << "Found "<< a <<"\n"; } else { out << "Did not find "<<a<<"\n"; } 123 a++; 124 if (m1.RetrieveItem(a)) { out << "Found "<< a <<"\n"; } else { out << "Did not find "<<a<<"\n"; } 125 a++; 126 if (m1.RetrieveItem(a)) { out << "Found "<< a <<"\n"; } else { out << "Did not find "<<a<<"\n"; } 127 128 129 out << "Current list is:\n"; 130 m1.PrintList(out); 131 out << "Length is : " << m1.LengthIs() << "\n"; 132 for (i=0;i<(sizeof(dtree1)/sizeof(int));i++) { 133 out << "Deleteing "<< dtree1[i] <<":\n"; 134 m1.DeleteItem(dtree1[i]); 135 m1.PrintList(out); 136 out << "Length is : " << m1.LengthIs() << "\n"; 137 } 138 out << "Making empty:\n"; 139 m1.MakeEmpty(); 140 m1.PrintList(out); 141 out << "Length is : " << m1.LengthIs() << "\n"; 142 out << "Inserting " << 3 << "\n"; 143 m1.InsertItem(3); 144 m1.PrintList(out); 145 out << "Length is : " << m1.LengthIs() << "\n"; 146 out << "Making empty:\n"; 147 m1.MakeEmpty(); 148 m1.PrintList(out); 149 out << "Length is : " << m1.LengthIs() << "\n"; 150 out << "----------------\n"; 151 152 for (i=0;i<(sizeof(tree3)/sizeof(int));i++) { 153 out << "Inserting " << tree3[i] << "\n"; 154 m3.InsertItem(tree3[i]); 155 } 156 m3.PrintList(out); 157 out << "Length is : " << (ol=m3.LengthIs()) << "\n"; 158 for (i=0;i<(sizeof(dtree3)/sizeof(int));i++) { 159 out << "Deleteing "<< dtree3[i] <<":\n"; 160 m3.DeleteItem(dtree3[i]); 161 m3.PrintList(out); 162 out << "Length is : " << (l=m3.LengthIs()) << "\n"; 163 ol--; 164 if (ol!=l) cerr << "Error! Length is " << l << " -- should be: " << ol << "\n"; 165 } 166 167 out << "----------------\n"; 168 169 for (i=0;i<(sizeof(tree2)/sizeof(string));i++) { 170 out << "Inserting " << tree2[i] << "\n"; 171 m2.InsertItem(tree2[i]); 172 } 173 m2.PrintList(out); 174 175 176 out.close(); 177 178 return(0); 179 } |