الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

لدي سؤال و هذه محاولتي...

بدأه hamany-pirlo في 28 مايو 2010 · 4 رد · 479 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم و رحمة الله و بركاته...

لقد كتبت كود لمجموعة من الدوال ، و لكن هناك دالة لم أعرف كيف أكتبها بشكل صحيح.

حيث أنه من المفترض أن أقوم بكتابة مجموعة من الدوال تتوافق مع الدالة الأساسية 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

Program every day for 20 years and you will become a good programmer. Program every day for 10 years and study algorithms on the side and you will become a great programmer !

على الأقل هذا اللي سمعته ...

#2

لديك بعض الاخطأ .

مثلا : لم تدرج مكتبة : string

ايضا :

 char A[count];

count متغير وليس ثابت

char *A=new char[count];

ايضا :

load(string s)

لم تستخدم s لماذا كتبته اذن

tvquran_6.gif

#3

حسناً ، لقد عملت التعديلات المقترحة ، و لكن لا تزال لدي بعض الأخطاء ، حيث أنه من المفترض يأتيني من الدالة main إسم ملف نصي (أو من الـ terminal) و أمرر هذا الإسم إلى الدالة load (التي لديها the parameter string s) ، و من ثم تقوم load بعملها و قراءة الكلمات التي في الملف.

و لكن عندما أمرر s إلى (ifstream in) ، يظهر لي الخطأ التالي :-

hw6.cpp:150: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’

و لا يزال يظهر لي الـ warning الذي لا أفهم لماذا يظهر لي :-

hw6.cpp:147: warning: ‘List* load(std::string)’ defined but not used

هذا الكود بعد التعديل ...

#include <iostream>

#include <fstream>

#include <string>

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 = new char[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(s);

	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;



}

ملاحظة : الرجاء الإجابة بأسرع وقت ممكن ، حيث أني أساساً متأخر بالتسليم ، و شكراً.

تم تعديل هذه المشاركة بواسطة hamany-pirlo في 28 مايو 2010 في 18:35

Program every day for 20 years and you will become a good programmer. Program every day for 10 years and study algorithms on the side and you will become a great programmer !

على الأقل هذا اللي سمعته ...

#4
اقتباس
و لكن عندما أمرر s إلى (ifstream in) ،

أتوقع المشكلة هنا , لا تستطيع تمرير String لهذه الدالة ,

أستبدل هالسطر ..

ifstream in(s);

بـ

ifstream in(s.c_str());

أتمنى أن المشكلة تنحل , والمعذرة لو النتيجة مب صحيحة ..

لأن بصراحه الملفات .. أتعلمها ذاتياً :happy: ..

موفق ,

سبحآن الله وبحمده .. سبحآن الله العظيم .. ,

#5
إشراقــه فجــر كتب:

أتوقع المشكلة هنا , لا تستطيع تمرير String لهذه الدالة ,

أستبدل هالسطر ..

ifstream in(s);

بـ

ifstream in(s.c_str());

أتمنى أن المشكلة تنحل , والمعذرة لو النتيجة مب صحيحة ..

لأن بصراحه الملفات .. أتعلمها ذاتياً :happy: ..

موفق ,

لم يعمل معي :(

على العموم ، شكراً لمساعدتك و لمساعدة الأخ فهد الشلوي ، لقد حاولت في الكود بأكثر من طريقة و لكن لم يعمل شيء معي ، و لقد سلمت الكود بأخطائه بعد أن يأست من المحاولة (نعيش و نتعلم).

ملاحظة : أظن أن المشكلة الرئيسية هي في الـ warning الذي سبق لي أن قمت بالإشارة إليه ، لأنه الخطأ الوحيد الذي يظهر لي.

Program every day for 20 years and you will become a good programmer. Program every day for 10 years and study algorithms on the side and you will become a great programmer !

على الأقل هذا اللي سمعته ...

مواضيع مشابهة