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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using namespace std;
using std::ostream;
using std::istream;
#include <fstream>
using std::ofstream;
using std::fstream;
using std::ios;
using std::ifstream;
#include <iomanip>
using std::setiosflags;
using std::resetiosflags;
using std::setw;
using std::setprecision;
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
void read (istream& infile, char name[], int& number);
void write (ostream& outfile, const char name[], int number);
int linearSearch (const char searchName[], const char names[][20], int numberOfListings);
int binarySearch (const char searchName[], const char names[][20], int numberOfListings);
int read (const char filename[], char names[][20], int numbers[]);
void write (const char filename[], const char description[],const char names[][20], const int numbers[], int numberOfListings);
int linearSearch (int searchNumber, const int numbers[], int numberOfListings);
int main()
{
char names [100][20];
int numbers[100];
int numberOfListings = read ("sp13Input.txt", names, numbers);
write ("con:", "Telephone Listings", names, numbers, numberOfListings);
char searchName [20];
cout << "Enter name for searching: ";
cin >> searchName;
int location = linearSearch (searchName, names, numberOfListings);
cout << "linear search results for " << searchName << endl;
if (location >= 0)
{
cout << "array location [" << location << "] contains ";
write (cout, names[location], numbers[location]);
cout << endl;
} // end if
else // location < 0
cout << "name not found by linear search\n";
location = binarySearch (searchName, names, numberOfListings);
cout << "binary search results for " << searchName << endl;
if (location >= 0)
{
cout << "array location [" << location << "] contains ";
write (cout, names[location], numbers[location]);
cout << endl;
} // end if
else // location < 0
cout << "name not found by binary search\n";
cout << "Enter number for searching (nnn-nnnn): ";
int searchNumber;
// read & convert to a single integer "searchNumber"
location = linearSearch (searchNumber, numbers, numberOfListings);
int first;
int last;
char hyphen;
cin >> first >> hyphen >> last;
searchNumber=first*10000+last;
cout << "linear search results for " << searchNumber << endl;
if (location >= 0)
{
cout << "array location [" << location << "] contains ";
write (cout, names[location], numbers[location]);
cout << endl;
} // end if
else // location < 0
cout << "number not found by linear search\n";
return 0;
}
int read (const char filename[], char names[][20], int numbers[])
{
ifstream infile (filename);
if (!filename)
{
cout << "The file could not be found" << "\n";
exit(1);
} // end if
int numberOfListings = 0; // set numberOfListings to 0
read (infile, names[numberOfListings], numbers[numberOfListings]);
// see below
while (strcmp (names[numberOfListings],"zzz" ))// names[numberOfListings] != "zzz"
{
numberOfListings++;
read (infile, names[numberOfListings], numbers[numberOfListings]);
} // end while
infile.close ();
return numberOfListings;
} // end function read
void read (istream& infile, char name[], int& number)
{
infile >> name;
int digits3;
int digits4;
char hyphen;
infile >> digits3 >> hyphen >> digits4;
number = digits3*10000 + digits4;// combine 1st 3 digits (digits3) & last 4 (digits4) as one integer
return;
} // end function read
void write (const char filename[], const char description[],const char names[][20], const int numbers[], int numberOfListings)
{
ofstream outfile (filename, ios::out);
outfile << description << endl;
for (int i=0; i<numberOfListings-1;i++) // i = 0 to numberOfListings-1
{
write (outfile, names[i], numbers[i]); // one entry; see below
outfile << endl;
} // end for
outfile << endl;
outfile.close ();
return;
} // end function write
void write (ostream& outfile, const char name[], int number)
{
int prefix = number/10000;// 1st 3 digits of "number"
int suffix = number%10000;// last 4 digits of "number"
outfile << setiosflags (ios::left) << setw(21) << name << resetiosflags (ios::left)
<< prefix << '-' << suffix;
return;
} // end function write
int linearSearch (const char searchName[], const char names[][20], int numberOfListings)
{
int location = 0;// set location to 0, the first name to examine
bool moreToSearch = true;
while (moreToSearch)
{
if (strcmp (searchName,names[location]) > 0) // searchName > names[location]
location++;
else if (strcmp (searchName,names[location]) == 0) // searchName == names[location]
moreToSearch = false;
else // searchName < names[location]
{
moreToSearch = false;
location = -1;
} // end else
} // end while
return location;
} // end function linearSearch
int binarySearch (const char searchName[], const char names[][20], int numberOfListings)
{
int mid;
int left = 0;
int right = numberOfListings - 1;
bool found = false;
while(!found && (left <= right)) // not found and name might still be in list
{
mid = (left+ right) / 2;// center between left & right
if (strcmp (searchName,names[mid]) < 0) // searchName < names[mid]
right = mid - 1;
else if (strcmp (searchName,names[mid]) > 0)// searchName > names[mid]
left = mid + 1;
else // searchName == names[mid]
found = true;
} // end while
return (found) ? mid : -1; // "mid" if found, else -1 (use ? : operator)
} // end function binarySearch
int linearSearch (int searchNumber, const int numbers[], int numberOfListings)
{
// set location to 0, the first name to examine
bool moreToSearch = true;
int location=0;
while (moreToSearch)
{
if(searchNumber>numbers[location])
location++;
else if(searchNumber==numbers[location])
moreToSearch = false;
else
{
moreToSearch = false;
location = -1;
} // end else
} // end while
return location;
} // end function linearSearch
|