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

مشروع قوائم متصلة

بدأه الاميرة دندون في 7 يونيو 2008 · 11 رد · 1,133 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
/*===========================*/

struct NODE

{

//NODE link; // self-referential poinetr

int data;

};



struct student

{

char *name;

int id;

int level;

int phone;

student *link;





student() // constructor

{

/*name = new char[150];

for(int i=0; i<150; i++)

*(name+i) = '\0';



id = 0;

level = 0;

phone = 0;*/

}



void displaystudent(void)

{

cout << " Name : "<< name << endl

<< " ID : "<< id << endl

<< " Level : "<< level << endl

<< " Number of Phone : "<< phone << endl

<< "---------------------------\n";

}



};



/*===========================*/

class List

{

private:

student *head;

int count;



void _insert(student v_in, student *pPre);

void _delete(student *pPre, student *pLoc, student *v_outPtr);

void _search(int target, student **pPre, student **pLoc);



public:

List(void);

~List(void);

void buildList();

int add(student v_in);

void remove(int id, student *v_outPtr);

void retrieve(int id,student& v_out);

void displayList(void);

int Count(void);



};

/*===========================*/

// constructor

List::List(void)

{

head = NULL;

count = 0;

}



/*===========================*/

// destructor

List::~List(void)

{

student *deletePtr;

if(head)

{

while( count > 0 )

{

deletePtr = head;

head = head->link;

count--;

delete deletePtr;

}

}



}



/*===========================*/

void List::buildList()

{

student vec;

student *pPre, *pLoc;



ifstream file;

file.open("student.txt");



while(! file.eof())

{

file.getline( vec.name , 150, '\n');

file >> vec.id;

file >> vec.level;

file >> vec.phone;

}



_search(vec.id,&pPre,&pLoc);

_insert(vec,pPre);

vec.name = new char[150];



}

/*===========================*/



// insertion private function

bool List::insert( student v_in, student *pPre)

{

student *pNew;

if((pNew =! new student))

return false; // failed insertion ( memory overflow )



*pNew = v_in;

pNew->link = NULL;



if( pPre == NULL )

{

pNew->link = head;

head = pNew;

}

else

{

pNew->link = pPre->link;

pPre->link = pNew;

}

count++;

return true; // success insertion

}



/*===========================*/

// searching private function

bool List::_search( int target, student **pPre, student **pLoc)

{

*pPre = NULL;

*pLoc = head;



while( (*pLoc != NULL) && (target> (*pLoc)->id) )

{

*pPre = *pLoc;

*pLoc = (*pLoc)->link;

}



if (*pLoc == NULL)

return false;



if(target < (*pLoc)->id)

return true;



return false;

}

/*===========================*/



void List::_delete(student *pPre, student *pLoc, student *v_outPtr)

{

*v_outPtr = *pLoc;



if(pPre == NULL)

head = pLoc->link;



else

pPre->link = pLoc->link;



count--;



delete pLoc;

}

/*===========================*/



int List::add( student v_in)

{

student *pPre, *pLoc;



if(_search(v_in.id, &pPre, &pLoc))

return 1;



if(! _insert(v_in, pPre))

return -1;



return 0;

}



/*===========================*/



bool List::remove( int target, student *v_outPtr)

{

student *pPre, *pLoc;



if( ! _search(target, &pPre, &pLoc))

return false;



_delete( pPre, pLoc, v_outPtr);

return true;

}

/*===========================*/



bool List::retrieve( int target, student &v_out)

{

student *pPre, *pLoc;



if (_search(target, &pPre, &pLoc))

{

v_out = *pLoc;

return true;

}



return false;

}

/*===========================*/

int List::Count()

{

return count;

}

/*===========================*/



void List::displayList()

{

student *pWalk = head;

student s;



s.displaystudent();

// l.buildList();



ifstream file;

file.open("student.txt");



if(Count() == 0)

cout<<"\n Sorry, the student list is empty!\n";

else

{

cout << "\n**** Begin student Print ****\n\n";

while(pWalk != NULL)

{

pWalk->displaystudent();

pWalk = pWalk->link;

}

cout << "\n**** End student Print ****\n\n";

}

}

/*===========================*/



int main(void)

{

char ch;

bool valid = true;

student vec;

int test;

List list;



ifstream file;

file.open( "student.txt");



if(file.fail())

cout<<"\nThe file: student.txt can NOT be opened! \n\n";



else

{

list.buildList();

file.close();

}

do

{

cout<< " student Menu\n"

<< " =====================\n"

<< " 1. Add a student\n"

<< " 2. Delete a student\n"

<< " 3. Retrieve a student\n"

<< " 4. Display all student\n"

<< " 5. Exit\n"

<< " Enter your choice: ";

cin >> ch;



if(ch == '1' || ch =='2' || ch =='3' || ch =='4')

switch(ch)

{

case '1':

system("cls");

cout<<"Enter the new student info: \nName of student: ";

cin.ignore();

cin.getline(vec.name, 150, '\n');

cout<<"ID: ";

cin>>vec.id;

cout<<"Level: ";

cin>>vec.level;

cout<<"phone: ";

cin>>vec.phone;

test = list.add( vec );

if( test == 0 )

cout<< "** You add new student succesfully **\n";

else

if( test == 1 )

cout <<" NOT added!\n"

<<" the student name you entered is already in the list\n\n";

else

cout<<" NOT added!\n"

<<"MEMORY OVERFLOW!\n\n";

break;



case '2':

system("cls");

cout<< "Enter the name of a student to delete it: ";

cin.ignore();

cin.getline( vec.name, 150, '\n');

if(list.remove( vec.id, &vec))

{

cout<< "The following student is deleted succesfully: \n";

vec.displaystudent();

}

else

cout<<" NO student with this name ! \n\n";

break;



case '3':

system("cls");

cout<< "Enter the name of a student to retrieve its info: ";

cin.ignore();

cin.getline(vec.name, 150, '\n');

if (list.retrieve(vec.id, vec))

vec.displaystudent();

else

cout<<" NO student with this name !\n\n";

break;



case '4':

system("cls");

list.displayList();

break;

}

else

if(ch != '5')

cout << " \n\a Invalid choice! Choices are <1,2,3,4,5>\n\n";



}while(ch != '5');



return 0;

}

مرحبا للجميع انا طالية في جامعة القدس المفتوحة فرع طولكرم في فلسطين طلب منا الاستاز عمل مشروع قوائم متصلة لمادة تركيب البيانات وتصميم الخوارزميات وانا عملت كود لادخال طلاب وحذف طلاب وبحث بس انا كان عندي خطا في الكود وياريت تشوفولي

الكود وتصححولي الغلط وبطلب كمان طلب انه ترسمولي القوائم المتصلة لهذا السؤال

تم تعديل هذه المشاركة بواسطة رغـــــــــد في 7 يونيو 2008 في 21:05

#2
#include<iostream.h>
#include<stdlib.h>

#include<fstream.h>


#include<string.h>
/*===========================*/
struct NODE
{
//NODE link;  // self-referential poinetr
int data;
};

struct  student
{
char *name;
int  id; 
int  level;
int  phone;
student *link;


student()  // constructor
{
 /*name = new char[150];
 for(int i=0; i<150; i++)
  *(name+i) = '\0';

 id = 0;
 level  = 0;
 phone = 0;*/
}

void displaystudent(void) 
{
 cout << "   Name    : "<< name << endl
   << "   ID   : "<< id << endl
   << "   Level      : "<< level << endl
   << "   Number of Phone  : "<< phone << endl
   << "---------------------------\n";
}

};

/*===========================*/
class List
{
private:
student *head;
int  count; 

void _insert(student v_in, student *pPre);
void _delete(student *pPre, student *pLoc, student *v_outPtr);
void _search(int target, student **pPre, student **pLoc);

public:
List(void);
~List(void);
void buildList();
int  add(student v_in);
void remove(int id, student *v_outPtr);
void retrieve(int id,student& v_out);
void displayList(void);
int  Count(void);

};
/*===========================*/
// constructor
List::List(void)
{
head = NULL;
count = 0;
}

/*===========================*/
// destructor
List::~List(void)
{
student *deletePtr;
if(head)
{
 while( count > 0 )
 {
  deletePtr = head;
  head = head->link;
  count--;
  delete deletePtr;
 }
}

}

/*===========================*/
void List::buildList()
{
student vec;
student *pPre, *pLoc;

ifstream file;
file.open("student.txt");

while(! file.eof())
{
 file.getline( vec.name , 150, '\n');
 file >> vec.id;
 file >> vec.level;
 file >> vec.phone;
}

 _search(vec.id,&pPre,&pLoc);
 _insert(vec,pPre);
 vec.name = new char[150];

}
/*===========================*/

// insertion private function
bool List::insert( student v_in, student *pPre)
{
student *pNew;
if((pNew =! new student))
 return false;  // failed insertion ( memory overflow )

*pNew  = v_in;
pNew->link = NULL;

if( pPre == NULL ) 
{ 
 pNew->link = head;
 head  = pNew;  
}
else  
{
 pNew->link = pPre->link;
 pPre->link = pNew;
}
count++;
return true; // success insertion
}

/*===========================*/
// searching private function
bool List::_search( int target, student **pPre, student **pLoc)
{
*pPre = NULL; 
*pLoc = head;

while( (*pLoc != NULL)  && (target> (*pLoc)->id) )
{
 *pPre = *pLoc;
 *pLoc = (*pLoc)->link;
}

if (*pLoc == NULL) 
 return false;

if(target < (*pLoc)->id) 
 return true;

return false; 
}
/*===========================*/

void List::_delete(student *pPre, student *pLoc, student *v_outPtr)
{
*v_outPtr = *pLoc;

if(pPre == NULL)
 head = pLoc->link;

else  
 pPre->link = pLoc->link;

count--;

delete pLoc;
}
/*===========================*/

int List::add( student v_in)
{
student *pPre, *pLoc;

if(_search(v_in.id, &pPre, &pLoc))
 return 1; 

if(! _insert(v_in, pPre))
 return -1;   

return 0; 
}

/*===========================*/

bool List::remove( int target, student *v_outPtr)
{
student *pPre, *pLoc;

if( ! _search(target, &pPre, &pLoc)) 
 return false;

_delete( pPre, pLoc, v_outPtr);  
return true;
}
/*===========================*/

bool List::retrieve( int target, student &v_out)
{
student *pPre, *pLoc;

if (_search(target, &pPre, &pLoc)) 
{
 v_out = *pLoc;
 return true;
}

return false;
}
/*===========================*/
int List::Count()
{
return count;
}
/*===========================*/

void List::displayList()
{
student *pWalk = head;
student s;

s.displaystudent();
// l.buildList();

ifstream file;
file.open("student.txt");

if(Count() == 0)
 cout<<"\n Sorry, the student list is empty!\n";
else
{
 cout << "\n**** Begin student Print ****\n\n";
 while(pWalk != NULL)
 {
  pWalk->displaystudent();
  pWalk = pWalk->link;
 }
 cout << "\n**** End student Print ****\n\n";
}
}
/*===========================*/

int main(void)
{
char ch;
bool valid = true;
student vec;
int test;
List list;

ifstream file;
file.open( "student.txt");

if(file.fail())
 cout<<"\nThe file: student.txt can NOT be opened! \n\n";

else
{
 list.buildList();
 file.close();
}
do
{
 cout<< "    student Menu\n"
  << " =====================\n"
  << "  1. Add a student\n"
  << "  2. Delete a student\n"
  << "  3. Retrieve a student\n"
  << "  4. Display all student\n"
  << "  5. Exit\n"
  << "     Enter your choice: ";
 cin >> ch;

 if(ch == '1' || ch =='2' || ch =='3' || ch =='4')
  switch(ch)
 {
  case '1':
   system("cls");
   cout<<"Enter the new student info: \nName of student: ";
   cin.ignore();
   cin.getline(vec.name, 150, '\n');
   cout<<"ID: "; 
   cin>>vec.id;
   cout<<"Level: ";
   cin>>vec.level; 
   cout<<"phone: ";
   cin>>vec.phone;
   test = list.add( vec );
   if( test == 0 )
    cout<< "** You add new student succesfully **\n";
   else
    if( test == 1 )
     cout <<" NOT added!\n"
       <<" the student name you entered is already in the list\n\n";
    else
     cout<<" NOT added!\n"
      <<"MEMORY OVERFLOW!\n\n";
   break;

  case '2':
   system("cls");
   cout<< "Enter the name of a student to delete it: ";
   cin.ignore();
   cin.getline( vec.name, 150, '\n');
   if(list.remove( vec.id, &vec))
   {
    cout<< "The following student is deleted succesfully: \n";
    vec.displaystudent();
   }
   else
    cout<<" NO student with this name ! \n\n";
   break;

  case '3':
   system("cls");
   cout<< "Enter the name of a student to retrieve its info: ";
   cin.ignore();
   cin.getline(vec.name, 150, '\n');
   if (list.retrieve(vec.id, vec))
    vec.displaystudent();
   else
    cout<<" NO student with this name !\n\n";
   break;

  case '4':
   system("cls");
   list.displayList();
   break; 
 }
 else
  if(ch != '5')
   cout << " \n\a Invalid choice! Choices are <1,2,3,4,5>\n\n";

}while(ch != '5');

return 0;
}

هادا هو المشروع بيضل يعطيني غلط في هادا

bool List::insert( student v_in, student *pPre)

وانا لازم خلال يومين اقدم المشروع رجاءا ساعدوني وتعملولي رسم للقوائم المتصلة تبعت السؤال

تم تعديل هذه المشاركة بواسطة رغـــــــــد في 7 يونيو 2008 في 20:38

#3

السلام عليكم

,.,.,.,.,.,.,.

اختي الكريمة الأميرة دندون..اولاً مرحبتين اختي.. :happy:

,.,.,.,.,.,.,.

ثانياً يُرجى مراجعة الدرس التالي في كيفية وضع الشيفرة (الكود) في الصندوق المخصص لها..كرماً منك اختي ليسهل على اعضاء المنتدى(اللي رح يساعدوك) مساعدتك والتفاعل مع مشاركتك..ولكِ جزيل الشكر

الــــــــــــدرس (بسيط جداً..صورة نظرية وليست قراءة). مجرد وضع الشيفرة وتحديدا ثم الضغط على الزرار المخصص.

,.,.,.,.,.,.,.

اطيب المُنى

:happy:

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#4

اخت رغد انا حاولت اعمل متل ما انتي كتبتي بس للاسف الموضوع ما فتح عندي وما قدرت اعمل

#5

خلص انا رايحة احرره للموضوع..ولكن اخطاء الشيفرة على مسؤليتك انتي..لأني بس رايحة حطا بالصندوق المخصص لها.

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#6

شكرا كتير حبيبتي رغد وبتمنى جد المساعدة في تصحيح الكود لانه كتنر حاولت تصحيحه وكتبت المشروع كامل وشرحته كمان للاستاز وبتمنى كمان تساعدوني في شي انه ارسم القوائم المتصلة للمشروع لكل كود في المشلاوع بخص القوائم المتصلة ازا بتقدرو

#7

شكرا كتير حبيبتي رغد على تحرير للكود مش عارفة كيف بدي اشكرك ضايل بس نعرفق كيف اصحح الكود وشو الخطأوهادي بداية المشروع المكتبات المستخدمة

#include<iostream.h>

#include<stdlib.h>

#include<fstream.h>

#include<string.h>

#8

خلص اختي ..الملفات المضمنة حاطتهن إلك بالشيفرة فوق..الآن تنتظري وصبرك لحد ما يتفاعل الأعضاء مع الموضوع ومع مشكلتك.. غالباً ما بياخد وقت..ولكن اختي ديري بالك بأن حل الواجب (أي ألقاء اعباء الواجب مثل الخوارزميات وووو والمسائل الجوهرية في البرنامج) ممنوع بالمنتدى على الإطلاق, ولكن يشوفوا محاولتك (الجادة) وهم يساعدوك في العقبات اللي بتواجهين بالبرنامج الخاص بك. يعني حاولي تشرفي على الموضوع باستمرار وتعطيهم وقت كافي.

,.,.,.,.,.,.,.,.,.,.,.

ولا شُكر على واجب :happy:

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#9

مبدئياً اختي اميرة..عندك اخطاء فادحة بالشيفرة الثانية من المشاركة رقم 2# .. لغة C++..لغة من نوع Strongly Typed language..

C++ is a strongly typed language

عارفة شو يعني؟ :happy:

,.,.,.,.,.,.,.,.,.,..,.,

يعني بتعتمد اعتماد كُلّي ومُطْلق على البيانات ونوعياتهن.. وانتي عاملة إعلان Declaration لاكتر من تلات دوال Functions بالكلاس تبعك List بأنهن void يعني ما يرجعن شي!!! طيب هيك تمام...ولكن انتي عاملة تعريف Definition لهدول الدوال خارج الكلاس بحيث يرجعن قيم منطقية( bool)؟ :rolleyes:

,.,.,..,,.,.,.,.,.,.,.

الآن جا دورك تختاري بحسب طبيعة برنامجك وتمشي على هذا الأساس..خلص طالما انتي عرفتيهن بأن يرجعوا او ما يرجعوا قيمة!! إذاً لازم يكونوا هيك حتى بالإنشاء خارج الكلاس class.. وهذي مصادر قوة بلغتنا ++C..مع أن الغير بيشوفا غلاظة باللغة وعدم مرونة وهي مش هيك.

,.,.,.,.,.,.,.,.,.,.,.

كمان نفس الأخطاء تقريباً بالشيفرة الأولى من المشاركة رقم #1.. يعني لازم تعيدي النظر بالشيفرة اختي وتحاولي مرة اخرى.

,.,.,.,.,.,.,.,.,.,.,.

اطيب المُنى

:happy:

تم تعديل هذه المشاركة بواسطة رغـــــــــد في 8 يونيو 2008 في 16:40 — السبب: صايرة بليدة بالإملاء

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#10
#include<iostream.h>
#include<stdlib.h>

#include<fstream.h>
#include<string.h>

/*===========================*/
struct NODE
{
//NODE link;  // self-referential poinetr
int data;
};

struct  student
{
char *name;
int  id; 
int  level;
int  phone;
student *link;


student()  // constructor
{
 /*name = new char[150];
 for(int i=0; i<150; i++)
  *(name+i) = '\0';

 id = 0;
 level  = 0;
 phone = 0;*/
}

void displaystudent(void) 
{
 cout << "   Name    : "<< name << endl
   << "   ID   : "<< id << endl
   << "   Level      : "<< level << endl
   << "   Number of Phone  : "<< phone << endl
   << "---------------------------\n";
}

};

/*===========================*/
class List
{
private:
student *head;
int  count; 

bool _insert(student v_in, student *pPre);
void _delete(student *pPre, student *pLoc, student *v_outPtr);
bool _search(int target, student **pPre, student **pLoc);

public:
List(void);
~List(void);
void buildList();
int  add(student v_in);
bool remove(int id, student *v_outPtr);
bool retrieve(int id,student& v_out);
void displayList(void);
int  Count(void);

};
/*===========================*/
// constructor
List::List(void)
{
head = NULL;
count = 0;
}

/*===========================*/
// destructor
List::~List(void)
{
student *deletePtr;
if(head)
{
 while( count > 0 )
 {
  deletePtr = head;
  head = head->link;
  count--;
  delete deletePtr;
 }
}

}

/*===========================*/
void List::buildList()
{
student vec;
student *pPre, *pLoc;

ifstream file;
file.open("student.txt");

while(! file.eof())
{
 file.getline( vec.name , 150, '\n');
 file >> vec.id;
 file >> vec.level;
 file >> vec.phone;
}

 _search(vec.id,&pPre,&pLoc);
 _insert(vec,pPre);
 vec.name = new char[150];

}
/*===========================*/

// insertion private function
bool List::_insert( student v_in, student *pPre)
{
student *pNew;
if(!(pNew = new student))
 return false;  // failed insertion ( memory overflow )

*pNew  = v_in;
pNew->link = NULL;

if( pPre == NULL ) 
{ 
 pNew->link = head;
 head  = pNew;  
}
else  
{
 pNew->link = pPre->link;
 pPre->link = pNew;
}
count++;
return true; // success insertion
}

/*===========================*/
// searching private function
bool List::_search( int target, student **pPre, student **pLoc)
{
*pPre = NULL; 
*pLoc = head;

while( (*pLoc != NULL)  && (target> (*pLoc)->id) )
{
 *pPre = *pLoc;
 *pLoc = (*pLoc)->link;
}

if (*pLoc == NULL) 
 return false;

if(target < (*pLoc)->id) 
 return true;

return false; 
}
/*===========================*/

void List::_delete(student *pPre, student *pLoc, student *v_outPtr)
{
*v_outPtr = *pLoc;

if(pPre == NULL)
 head = pLoc->link;

else  
 pPre->link = pLoc->link;

count--;

delete pLoc;
}
/*===========================*/

int List::add( student v_in)
{
student *pPre, *pLoc;

if(_search(v_in.id, &pPre, &pLoc))
 return 1; 

if(! _insert(v_in, pPre))
 return -1;   

return 0; 
}

/*===========================*/

bool List::remove( int target, student *v_outPtr)
{
student *pPre, *pLoc;

if( ! _search(target, &pPre, &pLoc)) 
 return false;

_delete( pPre, pLoc, v_outPtr);  
return true;
}
/*===========================*/

bool List::retrieve( int target, student &v_out)
{
student *pPre, *pLoc;

if (_search(target, &pPre, &pLoc)) 
{
 v_out = *pLoc;
 return true;
}

return false;
}
/*===========================*/
int List::Count()
{
return count;
}
/*===========================*/

void List::displayList()
{
student *pWalk = head;
student s;

s.displaystudent();
// l.buildList();

ifstream file;
file.open("student.txt");

if(Count() == 0)
 cout<<"\n Sorry, the student list is empty!\n";
else
{
 cout << "\n**** Begin student Print ****\n\n";
 while(pWalk != NULL)
 {
  pWalk->displaystudent();
  pWalk = pWalk->link;
 }
 cout << "\n**** End student Print ****\n\n";
}
}
/*===========================*/

int main(void)
{
char ch;
bool valid = true;
student vec;
int test;
List list;

ifstream file;
file.open( "student.txt");

if(file.fail())
 cout<<"\nThe file: student.txt can NOT be opened! \n\n";

else
{
 list.buildList();
 file.close();
}
do
{
 cout<< "    student Menu\n"
  << " =====================\n"
  << "  1. Add a student\n"
  << "  2. Delete a student\n"
  << "  3. Retrieve a student\n"
  << "  4. Display all student\n"
  << "  5. Exit\n"
  << "     Enter your choice: ";
 cin >> ch;

 if(ch == '1' || ch =='2' || ch =='3' || ch =='4')
  switch(ch)
 {
  case '1':
   system("cls");
   cout<<"Enter the new student info: \nName of student: ";
   cin.ignore();
   cin.getline(vec.name, 150, '\n');
   cout<<"ID: "; 
   cin>>vec.id;
   cout<<"Level: ";
   cin>>vec.level; 
   cout<<"phone: ";
   cin>>vec.phone;
   test = list.add( vec );
   if( test == 0 )
    cout<< "** You add new student succesfully **\n";
   else
    if( test == 1 )
     cout <<" NOT added!\n"
       <<" the student name you entered is already in the list\n\n";
    else
     cout<<" NOT added!\n"
      <<"MEMORY OVERFLOW!\n\n";
   break;

  case '2':
   system("cls");
   cout<< "Enter the name of a student to delete it: ";
   cin.ignore();
   cin.getline( vec.name, 150, '\n');
   if(list.remove( vec.id, &vec))
   {
    cout<< "The following student is deleted succesfully: \n";
    vec.displaystudent();
   }
   else
    cout<<" NO student with this name ! \n\n";
   break;

  case '3':
   system("cls");
   cout<< "Enter the name of a student to retrieve its info: ";
   cin.ignore();
   cin.getline(vec.name, 150, '\n');
   if (list.retrieve(vec.id, vec))
    vec.displaystudent();
   else
    cout<<" NO student with this name !\n\n";
   break;

  case '4':
   system("cls");
   list.displayList();
   break; 
 }
 else
  if(ch != '5')
   cout << " \n\a Invalid choice! Choices are <1,2,3,4,5>\n\n";

}while(ch != '5');

return 0;
}

أخت رغد هاي الكود متل ما انتي حكيتي بالنسبة ل void ,bool عرفت مزبوط بس بضل في اغلاط بدي اعرف شو هي بالزبط

تم تعديل هذه المشاركة بواسطة رغـــــــــد في 8 يونيو 2008 في 18:35 — السبب: لوضع الشيفرة بالصندوق

#11

وفي هادا الشغل على الكود بضل يطلعلي غلط لغوي وانا بكرا بدي فرجي للاستاز هادا المشروع

#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<string.h>
int data;
struct student
{
char *name;
int  id; 
int  level;
int  phone;
student *link;
student()
{
 name = new char[150];
 id = 0;
 level  = 0;
 phone = 0;
}
void displaystudent(void) 
{
 cout << "   Name    : "<< name << endl
 << "   ID   : "<< id << endl
   << "   Level      : "<< level << endl
   << "   Number of Phone  : "<< phone << endl
   << "---------------------------\n";
}

};

class List
{
private:
student *head;
int  count; 

void insert(student v_in, student *pPre);
void Delete(student *pPre, student *pLoc, student *v_outPtr);
void search(int target, student **pPre, student **pLoc);

public:
List(void);
~List(void);
void buildList();
int  add(student v_in);
void remove(int id, student *v_outPtr);
void retrieve(int id,student& v_out);
void displayList(void);
int  Count(void);

};

List::List(void)
{
head = NULL;
count = 0;
}

List::~List(void)
{
student *deletePtr;
if(head)
{
 while( count > 0 )
 {
  deletePtr = head;
  head = head->link;
  count--;
  delete deletePtr;
 }
}

}

void List::buildList()
{
student vec;
student *pPre, *pLoc;

ifstream file;
file.open("student.txt");

while(! file.eof())
{
 file.getline( vec.name , 150, '\n');
 file >> vec.id;
 file >> vec.level;
 file >> vec.phone;
}
 search(vec.id,&pPre,&pLoc);
 insert(vec,pPre);
 vec.name = new char[150];
}
bool List::insert( student v_in, student *pNew )
{
student *pNew;
if(pNew =! new student)
 return false;
*pNew  = v_in;
pNew->link = NULL;

if( pPre == NULL )
{ 
 pNew->link = head;
 head  = pNew;  
}
else  
{
 pNew->link = pPre->link;
 pPre->link = pNew;
}
count++;
return true; 
}

bool List::search( int target, student **pPre, student **pLoc)
{
*pPre = NULL; 
*pLoc = head;

while( (*pLoc != NULL)  && (target> (*pLoc)->id) )
{
 *pPre = *pLoc;
 *pLoc = (*pLoc)->link;
}

if (*pLoc == NULL) 
 return false;

if(target < (*pLoc)->id) 
 return true;

return false; 
}


void List::delete(student *pPre, student *pLoc, student *v_outPtr)
{
*v_outPtr = *pLoc;

if(pPre == NULL)
 head = pLoc->link;

else  
 pPre->link = pLoc->link;

count--;

delete pLoc;
}

int List::add( student v_in)
{
student *pPre, *pLoc;

if(search(v_in.id, &pPre, &pLoc))
 return 1; 

if( insert!(v_in, pPre))
 return -1;   

return 0; 
}



bool List::remove( int target, student *v_outPtr)
{
student *pPre, *pLoc;

if(  search!(target, &pPre, &pLoc)) 
 return false;

delete( pPre, pLoc, v_outPtr);  
return true;
}


bool List::retrieve( int target, student &v_out)
{
student *pPre, *pLoc;

if (search(target, &pPre, &pLoc)) 
{
 v_out = *pLoc;
 return true;
}

return false;
}
int List::Count()
{
return count;
}


void List::displayList()
{
student *pWalk = head;
student s;

s.displaystudent();

ifstream file;
file.open("student.txt");

if(Count() == 0)
 cout<<"\n Sorry, the student list is empty!\n";
else
{
 cout << "\n**** Begin student Print ****\n\n";
 while(pWalk != NULL)
 {
  pWalk->displaystudent();
  pWalk = pWalk->link;
 }
 cout << "\n**** End student Print ****\n\n";
}
}

int main(void)
{
char ch;
bool valid = true;
student vec;
int test;
List list;

ifstream file;
file.open( "student.txt");

if(file.fail())
 cout<<"\nThe file: student.txt can NOT be opened! \n\n";

else
{
 list.buildList();
 file.close();
}
do
{
 cout<< "    student Menu\n"
  << " =====================\n"
  << "  1. Add a student\n"
  << "  2. Delete a student\n"
  << "  3. Retrieve a student\n"
  << "  4. Display all student\n"
  << "  5. Exit\n"
  << "     Enter your choice: ";
 cin >> ch;

 if(ch == '1' || ch =='2' || ch =='3' || ch =='4')
  switch(ch)
 {
  case '1':
   system("cls");
   cout<<"Enter the new student info: \nName of student: ";
   cin.ignore();
   cin.getline(vec.name, 150, '\n');
   cout<<"ID: "; 
   cin>>vec.id;
   cout<<"Level: ";
   cin>>vec.level; 
   cout<<"phone: ";
   cin>>vec.phone;
   test = list.add( vec );
   if( test == 0 )
    cout<< "** You add new student succesfully **\n";
   else
    if( test == 1 )
     cout <<" NOT added!\n"
       <<" the student name you entered is already in the list\n\n";
    else
     cout<<" NOT added!\n"
      <<"MEMORY OVERFLOW!\n\n";
   break;

  case '2':
   system("cls");
   cout<< "Enter the name of a student to delete it: ";
   cin.ignore();
   cin.getline( vec.name, 150, '\n');
   if(list.remove( vec.id, &vec))
   {
    cout<< "The following student is deleted succesfully: \n";
    vec.displaystudent();
   }
   else
    cout<<" NO student with this name ! \n\n";
   break;

  case '3':
   system("cls");
   cout<< "Enter the name of a student to retrieve its info: ";
   cin.ignore();
   cin.getline(vec.name, 150, '\n');
   if (list.retrieve(vec.id, vec))
    vec.displaystudent();
   else
    cout<<" NO student with this name !\n\n";
   break;

  case '4':
   system("cls");
   list.displayList();
   break; 
 }
 else
  if(ch != '5')
   cout << " \n\a Invalid choice! Choices are <1,2,3,4,5>\n\n";

}while(ch != '5');

return 0;
}

تم تعديل هذه المشاركة بواسطة رغـــــــــد في 8 يونيو 2008 في 18:47 — السبب: وضع الشيفرة بالصندوق

#12

ماني عارفة صراحة اقتناص الخطأ بالضبط.. لأن يا اميرة الشيفرة (على اقل انا) لو مكنت مشرفة عليها من البداية ولو كانت غير موثقة undocumented وليست مكتوبة على المعايير العالمية..بيصعب علي اكشتاف الأخطاء فيها. بجد آسفة :happy:

,.,.,,,,,.,.,.,.,.,.,.,.,.,.,.

لكن انتي خليها هيك وإن شاء الله حدا بيرد عليك ..ورايحة حطا الشيفرة السابقة بالصندوق المخصص كمان. ولا تزعلي

,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.

اطيب المُنى

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

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