Hi Everyone,
I have been trying to post a code by I am not able to do it; I might the wrong but I am following all the rules; I don't know what else to do; please help.
I will try to copy as follows (it might works) :)
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_CELLS = 10;
const int UNIQUE_FIVE_DIGIT = 5;
struct node
{
node* prev;
node* next;
int data;
};
int main(int argc, char* argv[]) {
ifstream inFile;
node *lists[MAX_CELLS] = { NULL };
int count = -1;
bool result = true;
do
{
if ( argc != 2 )
{
cout << "Command line arguments not valid!" << endl << endl;
result = false;
}
if ( !argv[1] )
{
cout << "The command line arguments does not specify any filename!" << endl;
result = false;
}
inFile.open(argv[1]);
if ( !inFile )
{
cout << "The input data file does not exist or cannot be opened!" << endl;
result = false;
}
if ( result )
{
cout << "Results for input file " << argv[1] << ":" << endl;
ReadInFile(inFile, lists, count);
result = false;
}
} while ( result );
system("PAUSE");
return 0;
}
void ReadInFile(ifstream& inFile, node* arrayPtr[], int& count) {
int number = 0;
int newNumber = 0;
int countResult = 0;
node* p = new node;
while ( inFile )
{
inFile >> number;
newNumber = number / 10000;
if ( !isDuplicate(arrayPtr[newNumber], number) )
{
arrayPtr[newNumber] = prepend( arrayPtr[newNumber], number );
p = arrayPtr[newNumber];
}
count++;
}
for (int count = 0; count < 10; count++)
{
arrayPtr[count] = p;
countResult = counting(p);
}
cout << setw(7) << count << " total integers read from file" << endl << endl;
inFile.close();
cout << "The " << countResult << " unique integers beginning with digit " << newNumber <<" were " << endl;
displayIt(p, countResult);
}
void displayIt(node* p, int count) {
cout << setw(14);
print_list(p);
cout << endl;
}
bool isDuplicate(node* top, int number) {
while( top != NULL )
{
if( top->data==number )
return true;
top = top->next;
}
return false;
}
node* prepend(node* beginning, int number) {
node* p = new node;
p->data = number;
p->prev = NULL;
p->next = beginning;
if( beginning != NULL )
{
beginning->prev = p;
}
return p;
}
void print_list( node* beginning ) {
for( node *p=beginning; p != NULL; p=p->next )
{
std::cout << p->data;
if( p->next != NULL )
{
std::cout << " ";
}
}
}
int counting( node* beginning ) {
int count(0);
for( node *p=beginning; p != NULL; p=p->next )
{
++count;
}
return count;
}
Big thanks and have a nice holidays.
Marco