السلام عليكم و رحمة الله و بركاته...
لقد كتبت كود لمجموعة من الدوال ، و لكن هناك دالة لم أعرف كيف أكتبها بشكل صحيح.
حيث أنه من المفترض أن أقوم بكتابة مجموعة من الدوال تتوافق مع الدالة الأساسية main (حيث لا يسمح لي بتغييرها) ، و المشكلة لدي تقع في دالة load ، و هذه الدالة يجب أن تقرأ كلمات من ملف نصي و من ثم تقوم بإنشاء list لكل كلمة (link-list) ، و من ثم يتم إرجاع array تحتوي على جميع الـ lists التي قمنا بإنشائها ، و يجب أيضاً أن تنتهي الـ array بـ NULL (مثل كل list سوف نقوم بإنشائها).
و هذا كود الدالة ...
// here is the problem
static List* load(string s)
{
char ch;
ifstream in("data.txt");
int size = 2; // i am supposed to get the size from the SizeOfArray function,
// so this is just temporary
int cnt = 0;
List* lists = new List[size+1];
while( ! in.eof() )
{
if( ! isspace(ch) )
{
int n=0;
char* A = new char[n];
while( ! (isspace(ch) || in.eof()) )
{
A[n] = ch;
n++;
in.get(ch);
}
lists[cnt] = List(A);
cnt ++;
}
in.get(ch);
}
lists[cnt] = NULL;
in.close();
return lists;
}و هذا هو الكود بالكامل ...
#include <iostream>
#include <fstream>
using namespace std;
struct Node
{
char data;
Node* next;
Node (char d, Node* n)
{
data= d;
next=n;
}
};
class List
{
Node* head;
int count;
public :
List () ;
List (char *p);
~List();
void print ( );
void sort ( );
static List * load( string s);
};
List::List()
{
count = 0;
head = NULL;
}
List::List(char* p)
{
char* temp = p;
while( temp != NULL )
{
temp++;
count++;
}
Node* last = NULL;
for(int i=count; i>=0; i--)
{
last = new Node( *(p+i), last);
head = last;
}
}
void InsertInPlace(Node* &p, int v)
{
if( (p == NULL) || (v < p->data) )
{
p = new Node(v, p);
}
else
{
Node* a = p;
while( (a->next != NULL) && (a->next->data < v) )
{
a = a->next;
}
a->next = new Node(v, a->next);
}
}
void List::print()
{
Node* temp = head;
cout<<'<';
while(temp != NULL)
{
cout<< temp->data;
temp = temp->next;
if(temp != NULL)
cout<<'-';
}
cout<<'>';
}
List::~List( )
{
while(head != NULL)
{
Node* temp = head;
head = head->next;
delete temp;
}
}
void List::sort()
{
Node* temp1 = NULL;
Node* temp3 = head;
char A[count];
for(int i=0; i<count; i++)
{
A = head->data;
head = head->next;
}
head = temp3;
for(int i=count; i>=0; i--)
{
temp1 = new Node( A, temp1);
}
while(head != NULL)
{
InsertInPlace(temp1, head->data);
head = head ->next;
}
head = temp1;
}
int SizeOfArray( ifstream in )
{
char ch;
in.get(ch);
int n=0;
while( ! in.eof() )
{
if(isspace(ch))
{
n++;
in.get(ch);
}
else
{
in.get(ch);
}
}
return n;
}
// here is the problem
static List* load(string s)
{
char ch;
ifstream in("data.txt");
int size = 2; // i am supposed to get the size from the SizeOfArray function,
// so this is just temporary
int cnt = 0;
List* lists = new List[size+1];
while( ! in.eof() )
{
if( ! isspace(ch) )
{
int n=0;
char* A = new char[n];
while( ! (isspace(ch) || in.eof()) )
{
A[n] = ch;
n++;
in.get(ch);
}
lists[cnt] = List(A);
cnt ++;
}
in.get(ch);
}
lists[cnt] = NULL;
in.close();
return lists;
}
// The main function
int main ( int argc , char* argv[] )
{
if ( argc<2 )
return 1;
List **Q = (List **)List::load(string (argv[1]));
List **p = Q;
while ( *p != NULL )
{
(*p)->print () ;
delete *p;
p++;
}
delete[] Q;
return 0;
}ملاحظة : يظهر لي هذا الـ warning عندما أعمل compile ولا أعلم لماذا :ـ
warning: ‘List* load(std::string)’ defined but not used
