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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
// struct definition for even number linked list node
struct ListNode
{
ListNode ()
{
Next = NULL;
Prev = NULL;
}
int Num;
ListNode* Next;
ListNode* Prev;
};
// function prototypes
bool ValidateFiles (int argc, char* argv [], ifstream& file_A, ifstream& file_B);
void WelcomeMessage ();
void AddToEndOfList (ListNode*& evenHead, ListNode* tmp);
bool ValidateUniqueNumbers (ListNode* evenHead, int search);
ListNode* SearchListForNumbers (int search, ListNode*& evenHead);
void DisplayResults (ListNode* oddHead, ListNode* evenHead);
//*************************************************************************
// FUNCTION: main
// DESCRIPTION: Calls functions in order to read files, store data, search
// for data, and print results for user
// CALLS TO: ValidateFiles, WelcomeMessage, AddToEndOfList,
// ValidateUniqueNumbers, SearchListForNumbers, DisplayResults
//*************************************************************************
int main (int argc, char* argv [])
{
int search = 0; // results of search
ListNode* evenHead = NULL; // pointer for head node in even list
ListNode* oddHead = NULL; // pointer for head node in odd list
int count = 0; // counter for display
ifstream file_A; // intput file - file_A (should be listnums.txt)
ifstream file_B; // intput file - file_B (should be findthem.txt)
WelcomeMessage (); // function call
// if files are valid
if (!ValidateFiles (argc, argv, file_A, file_B)) // function call
{
// while file is good, and data is still available
while (file_A.good () && file_A.peek () != EOF)
{
// create new node
ListNode* tmp = new ListNode ();
// read in data from file_A
file_A >> tmp -> Num;
// determine if int is even
if (tmp -> Num % 2 == 0)
{
if (!ValidateUniqueNumbers (evenHead, tmp -> Num)) // function call
{
// if number is unique, add to list
AddToEndOfList (evenHead, tmp);
}
}
// if not even
else
{
if (!ValidateUniqueNumbers (oddHead, tmp -> Num)) // function call
{
// if number is unique, add to list
AddToEndOfList (oddHead, tmp);
}
}
}
DisplayResults (oddHead, evenHead); // function call - display results of stored numbers
// while file is good, and data is available
while (file_B.good () && file_B.peek () != EOF)
{
// read data from file_B
file_B >> search;
// if int is even
if (search % 2 == 0)
{
// determine where in list each number is
ListNode* result = SearchListForNumbers (search, evenHead); // function call
// if number is found
if (result != NULL)
{
// determine count for placement in list, and output
for (ListNode* iter = evenHead; iter != result; iter = iter -> Next)
{
count++;
cout << search << " found " << count << " number(s) down in the even list" << endl;
}
}
// if not found
else
{
cout << search << " not found in even list" << endl;
}
}
// if not even
else
{
// determine where in list number is
ListNode* result = SearchListForNumbers (search, oddHead); // function call
// if number is found
if (result != NULL)
{
// determine count for placement in list, and output
for (ListNode* iter = oddHead; iter != result; iter = iter -> Next)
{
count++;
cout << search << " found " << count << " number(s) down in the odd list" << endl;
}
}
// if not found
else
{
cout << search << " not found in odd list" << endl;
}
}
}
// pause for user to read, and exit gracefully
system ("PAUSE");
return 0;
}
// if files are not valid
else
{
// exit program
system ("PAUSE");
return 1;
}
}
//*********************************************************************
// FUNCTION: WelcomeMessage
// DESCRIPTION: displays welcome message
// INPUT:
// Parameters: none
// File: none
// OUTPUT:
// Return Val: none
// Parameters: none
// File: none
// CALLS TO: none
//**********************************************************************
void WelcomeMessage()
{
// display welcome message to user
cout << "Double Linked List Program" << endl;
}// end WelcomeMessage
//*********************************************************************
// FUNCTION: ValidateFiles
// DESCRIPTION: function validates program and files entered by user
// INPUT:
// Parameters: argc, argv
// File: none
// OUTPUT:
// Return Val: quit
// Parameters: none
// File: none
// CALLS TO: none
//**********************************************************************
bool ValidateFiles (int argc, char* argv [], ifstream& file_A, ifstream& file_B)
{
bool quit = false;
// if enough files are not passed by user, exit
if (argc != 3)
{
cout << "You have not specified the correct number of files!" << endl;
quit = true;
}
// open files and validate
else
{
ifstream file_A (argv [1]);
if (!file_A.is_open ())
{
cout << "Could not open file " << argv [1] << endl;
quit = true;
}
ifstream file_B (argv [2]);
if (!file_B.is_open ())
{
cout << "Could not open file " << argv [2] << endl;
quit = true;
}
}
return quit;
}// end ValidateFiles
//*********************************************************************
// FUNCTION: AddToEndOfList
// DESCRIPTION: function adds numbers to appropriate linked lists
// INPUT:
// Parameters: evenHead, tmp
// File: none
// OUTPUT:
// Return Val: none
// Parameters: evenHead
// File: none
// CALLS TO: none
//**********************************************************************
void AddToEndOfList (ListNode*& evenHead, ListNode* tmp)
{
// if head node for even list is empty
if (evenHead == NULL)
{
// head node is tmp node
evenHead = tmp;
}
else
{
// iterate through file_A until all unique numbers have been added
ListNode* iter = evenHead;
while (iter -> Next != NULL)
{
iter = iter -> Next;
}
iter -> Next = tmp;
tmp -> Prev = iter;
}
}// end AddToEndOfList
//*********************************************************************
// FUNCTION: ValidateUniqueNumbers
// DESCRIPTION: function determines whether integer already exists to
// prepare for add
// INPUT:
// Parameters: evenHead, search
// File: none
// OUTPUT:
// Return Val: none
// Parameters: evenHead
// File: none
// CALLS TO: none
//**********************************************************************
bool ValidateUniqueNumbers (ListNode* evenHead, int search)
{
bool result = false; // bool variable for whether the number was found
// if head node is empty
if (evenHead != NULL)
{
ListNode* iter = evenHead;
// iterate through to search entire record
while (iter -> Next != NULL && !result)
{
// if found
if ( iter -> Num == search)
{
result = true;
}
// if not found
else
{
iter = iter -> Next;
}
}
// final search
if (iter -> Num == search)
{
result = true;
}
}
return result;
}// end ValidateUniqueNumbers
//*********************************************************************
// FUNCTION: SearchListForNumbers
// DESCRIPTION: function uses numbers from file_B to search for in file_A
// INPUT:
// Parameters: search, evenHead
// File: none
// OUTPUT:
// Return Val: none
// Parameters: evenHead
// File: none
// CALLS TO: none
//**********************************************************************
ListNode* SearchListForNumbers (int search, ListNode*& evenHead)
{
// if list is empty, return NULL
if (evenHead == NULL)
{
return NULL;
}
ListNode* iter = evenHead;
bool found = false;
// while there are records available
while (iter -> Next != NULL && !found)
{
if (iter -> Num != search)
{
iter = iter -> Next;
}
// or claim found
else
{
found = true;
}
}
//check last record
if (!found && iter -> Num != search)
{
// not found in list
return NULL;
}
else
{
// return found info
return iter;
}
}// end SearchListForNumbers
|