لدي مشكلة في البرنامج لا اعلم ماهي
مرة تظهر شاشة سواداء
ومرة يظهر خطأ في { يقول انه انتهي البرنامج
ولم افهم في الحقيقة مايقصد :blink:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<cstring>
//==============================//
// Node Declaration
template <class TYPE>
struct NODE
{
TYPE data;
NODE *link;
}; // End of Node Declaration
// Student data Declaration
template <class TYPE>
struct student
{
TYPE ID;
TYPE level;
TYPE phone;
KEYTYPE *name;
student()
{
for(int i=0; i<150; i++)
*(name+i) = NULL;
ID = 0;
level = 0;
phone = 0;
ifstream file("student.txt");
if(!file)
cout << "/aError Opening File/n";
else
while(!file.eof())
{
student<TYPE> s;
s.getline(name,150,'/n');
file >> s.ID:
file >> s.level;
file >> s.phone;
}
file.close();
};
// List Class Declaration
template <class TYPE, class KTYPE>
class List
{
private:
NODE<TYPE> *head;
NODE<TYPE> *pos;
NODE<TYPE> *rear;
int count;
// Function Declarations
bool _insert (NODE<TYPE> *pPre,
TYPE dataIn);
void _delete (NODE<TYPE> *pPre,
NODE<TYPE> *pLoc,
TYPE *dataOutPtr);
bool _search (NODE<TYPE> **pPre,
NODE<TYPE> **pLoc,
KTYPE key);
public:
void buildList (TYPE );
List (void);
~List (void);
int addNode (TYPE dataIn);
bool removeNode (KTYPE key,
TYPE *dataOutPtr);
bool retrieveNode (KTYPE Argu,
TYPE& dataOut);
bool getNext (int fromWhere,
TYPE& dataOut);
int listCount (void);
bool emptyList (void);
bool fullList (void);
void displayList (void);
void displayByLevel (void);
}; // class List
// End of List Class Declaration
template<class TYPE, class KTYPE>
List<TYPE, KEYTYPE > :: buildList( int &list)
{
student s;
ifstream file("student.txt");
if(!file)
cout << "/aError Opening File/n";
else
while(!file.eof())
{
student<TYPE> s;
s.getline(name,150,'/n');
file >> s.ID:
file >> s.level;
file >> s.phone;
}
file.close();
_search(s.name,&pPre,&pLoc);
_insert(s,pPre);
s.name = new char[150];
}
/* =============== List Constructor ==============
Initialize the list.
Pre Class is being instantiated
Post Class instantiated and initialized
*/
template <class TYPE, class KTYPE>
List<TYPE, KTYPE> :: List (void)
{
// Statements
head = NULL;
pos = NULL;
rear = NULL;
count = 0;
} // List Constructor
/* ==================== addNode ===================
Inserts data into linked list.
Pre dataIn contains data to be inserted
Post Data inserted or error
Return -1 if overflow,
0 if successful,
1 if duplicate key
*/
template <class TYPE, class KTYPE>
int List<TYPE, KTYPE> :: addNode (TYPE dataIn)
{
// Local Definitions
bool found;
bool success;
NODE<TYPE> *pPre;
NODE<TYPE> *pLoc;
// Statements
found = _search (&pPre, &pLoc, dataIn.key);
if (found)
// Duplicate keys not allowed
return (+1);
success = _insert (pPre, dataIn);
if (!success)
// Overflow
return (-1);
return (0);
} // addNode
/* ===================== _insert ====================
Inserts data into a new node in the linked list.
Pre Insertion location identified by pPre
dataIn contains data to be inserted
Post data inserted in linked list or overflow
Return true if successful, false if overflow
*/
template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE> :: _insert (NODE<TYPE> *pPre,
TYPE dataIn)
{
// Local Definitions
NODE <TYPE> *pNew;
// Statements
if (! (pNew = new NODE<TYPE>))
return false;
pNew->ID = dataIn;
pNew->link = NULL;
if (pPre == NULL)
{
// Adding before first node or to empty list.
pNew->link = head;
head = pNew;
} // if pPre
else
{
// Adding in middle or at end
pNew->link = pPre->link;
pPre->link = pNew;
} // if else
// Now check for add at end of list
if (pNew->link == NULL)
// Adding to empty list. Set rear
rear = pNew;
count++;
return true;
} // _insert
/* ================== removeNode ==================
Removes data from linked list.
Pre dltkey is identifier of node to be deleted
pDataOut is pointer to data variable to
receive a copy of the deleted data
Post data copied to output variable and node
deleted or not found
Return false if not found
true if deleted
*/
template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE> ::
removeNode (KTYPE dltkey, TYPE *pDataOut)
{
// Local Definitions
bool found;
NODE<TYPE> *pPre;
NODE<TYPE> *pLoc;
// Statements
found = _search (&pPre, &pLoc, dltkey);
if (found)
_delete (pPre, pLoc, pDataOut);
return found;
} // removeNode
/* =================== _delete ==================
Deletes data from a linked list and returns
data to calling module.
Pre pPre is a pointer to predecessor node
pLoc is a pointer to target node
pDataOut is pointer to output data area
Post Data have been deleted and returned
Data memory has been recycled
*/
template <class TYPE, class KTYPE>
void List<TYPE, KTYPE> :: _delete (NODE<TYPE> *pPre,
NODE<TYPE> *pLoc,
TYPE *pDataOut)
{
// Statements
*pDataOut = pLoc->ID;
if (pPre == NULL)
// Deleting first node
head = pLoc->link;
else
// Deleting any other node
pPre->link = pLoc->link;
// Test for deleting last node
if (pLoc->link == NULL)
rear = pPre;
count--;
delete pLoc;
return;
} // _delete
/* =================== retrieveNode ==================
Interface to search function.
Pre key is the search argument
dataOut is variable to receive data
Post dataOut contains located data if found
if not found, contents are unchanged
Return true if successful, false if not found
*/
template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE>
:: retrieveNode (KTYPE key, TYPE& dataOut)
{
// Local Definitions
bool found;
NODE <TYPE> *pPre;
NODE <TYPE> *pLoc;
// Statements
found = _search (&pPre, &pLoc, key);
if (found)
dataOut = pLoc->ID;
return found;
} // retrieveNode
/* ==================== _search ===================
Searches list and passes back address of node
containing target and its logical predecessor.
Pre pPre is pointer variable for predecessor
pLoc is pointer variable for found node
key is search argument
Post pLoc points to first node equal/greater key
-or- null if target > key of last node
pPre points to largest node smaller than key
-or- null if target < key of first node
Return true if successful, false if not found
*/
template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE> :: _search (NODE<TYPE> **pPre,
NODE<TYPE> **pLoc,
KTYPE key)
{
// Statements
*pPre = NULL;
*pLoc = head;
if (count == 0)
return false;
// Test for argument > last node in list
if (key > rear->ID.key)
{
*pPre = rear;
*pLoc = NULL;
return false;
} // if
while (key > (*pLoc)->ID.key)
{
// Have not found search argument location
*pPre = *pLoc;
*pLoc = (*pLoc)->link;
} // while
if (key == (*pLoc)->ID.key)
// argument found--success
return true;
else
return false;
} // _search
/* =============== emptyList ==============
Returns Boolean indicating whether the
list is empty.
Pre Nothing
Return true if empty, false if list has data
*/
template<class TYPE, class KTYPE>
bool List<TYPE, KTYPE> :: emptyList (void)
{
// Statements
return (count == 0);
} // emptyList
/* =================== fullList ==================
Returns Boolean indicating whether the list is full
or has room for more data.
Pre Nothing
Return true if full, false if room for another node
*/
template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE> :: fullList (void)
{
// Local Definitions
NODE<TYPE> *temp;
// Statements
if (temp = new NODE<TYPE>)
{
delete temp;
return false;
} // if
// Dynamic memory full
return true;
} // fullList
/* ==================== listCount ====================
Returns integer representing number of nodes in list.
Pre Nothing
Return count for number of nodes in list
*/
template <class TYPE, class KTYPE>
int List<TYPE, KTYPE> :: listCount(void)
{
// Statements
return count;
} // listCount
/* ====================== getNext =====================
getNext traverses a linked list. Each call either starts
at the beginning of the list or returns the location of
the element in the list that was last returned.
Pre fromWhere is 0 to start at the first element
dataOut is reference to data variable
Post if another element, address placed in output area
Return true if another element located,
false if end of list
*/
template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE> :: getNext (int fromWhere,
TYPE& dataOut)
{
// Local Definitions
bool success;
// Statements
if (fromWhere == 0)
{
// Start from first node
if (count == 0)
success = false;
else
{
pos = head;
dataOut = pos->data;
success = true;
} // if else
} // if fromwhere is zero
else
{
// Continue from current position
if (pos->link == NULL)
success = false;
else
{
pos = pos->link;
dataOut = pos->data;
success = true;
} // if else
} // if fromWhere else
return success;
} // getNext
/* =============== Destructor ==============
Deletes all data in list and recycles memory
Pre List is being deleted
Post Data and class structure have been deleted
*/
template<class TYPE, class KTYPE>
List<TYPE, KTYPE > :: ~List (void)
{
// Local Definitions
NODE<TYPE> *deletePtr;
// Statements
if (head)
{
while (count > 0)
{
deletePtr = head;
head = head->link;
count--;
delete deletePtr;
} // while
} // if
} // Destructor
//====================================//
template<class TYPE, class KTYPE>
void List::displayList()
{
student *pWalk = head;
if(count == 0)
cout<<"\n Sorry, the student list is empty!\n";
else
{
cout << "\a\n**** Begin Students Print ****\n\n";
while(pWalk != NULL)
{
pWalk->displayList();
pWalk = pWalk->link;
}
cout << "\a\n**** End Students Print ****\n\n";
}
}
//===================================//
template<class TYPE, class KTYPE>
displayByLevel()
{
المطلوب هنا طباعة اسماء الطلاب الموجودين في نفس المستوى
انا اقوم بادخال المستوى وهو يقوم بالبحث عن جميع الطلالاب في نفس المستوى وطباعتهم
اود الفكرة التي استخدمها هنا
}
//======================================//
int main()
{
student s;
List l;
char ch;
//bool valid = true;
int test, a;
ifstream file;
if(file.fail())
cout<<"\n\a The file: student.txt can NOT be opened! \n\n";
else
l.buildList(a);
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. Display all student in given level\n"
<< " 6. Exit\n"
<< " ** Enter your choice ** ";
cin >> ch;
if(ch == '1' || ch =='2' || ch =='3' || ch =='4' || ch == '5')
switch(ch)
{
case '1':
system("cls");
cout<<"Enter the new student info: \nName of student: ";
cin.ignore();
cin.getline(std.name, 150, '\n');
cout<<"The ID: ";
cin>>s.ID;
cout<<"Level: ";
cin>>s.level;
cout<<"Phone Numbers: ";
cin>>s.phone;
test = l.addStudent( s );
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( s.name, 150, '\n');
if(l.removeStudent( s.name, &s))
{
cout<< "The following student is deleted succesfully: \n";
std.displayStudent();
}
else
cout<<" NO student with this name ! \n\n";
break;
case '3':
system("cls");
cout<< "Enter the name of a studen to retrieve its info: ";
cin.ignore();
cin.getline(s.name, 150, '\n');
if (list.retrieveStudent(s.name, s))
std.displayStudent();
else
cout<<" NO studen with this name !\n\n";
break;
case '4':
system("cls");
l.displayList();
break;
case '5':
system("cls");
l.displayByLevel();
break;
}
else
if(ch != '5')
cout << " \n\a Invalid choice! Choices are <1,2,3,4,5>\n\n";
}while(ch != '6');
return 0;
}