printf( "\n 1----Creat a new record and save" );
printf( "\n 2----Add new records to a existed file." );
printf( "\n 3----Search someone's record" );
printf( "\n 4----Sort students' marks according to the record file." );
printf( "\n 5----Print all students' record." );
printf( "\n 6----Quit." );
printf( "\nWaiting order:" );
do{
scanf( "%c", &cmd );
}while( cmd == '\n' );
/* Command Transmitter */
switch ( cmd )
{
case '1' : puts( "\nEnter the records" );
head = creat();
save( head );
puts( "\nNow you have a record file." );
list_traverse( head );
clear( head );
getch();
break;
case '2' : printf( "\nEnter the name of record file you want add:" );
scanf( "%s", filename );
fp = fopen( filename,"r" );
if( !fp ) { printf( "Error!!!" ); break; }
printf( "\nThe current records are:" );
print_file( fp );
printf( "\nNow you can add new record: ");
add( filename );
printf( "The new records are:" );
rewind( fp );
print_file( fp ); getch();
break;
case '3' : printf( "\nWhich key value do you want to accord? ");
printf( "\n 1---name\t0---number:" );
scanf ( "%d", &tag );
if ( tag ) search_name();
else search_num();
getch();
break;
case '4' : printf( "\nSort according to what ?" );
printf( "1---name\t2---number\t3---score" );
do {
scanf ( "%c", &tag2 );
}while ( tag2 == '\n' );
switch ( tag2 )
{
case '1' : head = creat_order2( compare_name );
break;
case '2' : head = creat_order2( compare_num );
break;
case '3' : head = creat_order2( compare_score );
break;
}
list_traverse( head );
clear ( head );
getch();
break;
case '5' : printf( "\nEnter the name of record file you want see:" );
scanf( "%s", filename );
fp = fopen( filename, "r" );
if( !fp ) { printf( "Error!!" ); break; }
printf( "\nThe records in this file are:" );
print_file( fp );getch();
break;
}
}while( cmd != '6' );
retrurn true;
}
printf( "\nPlease enter No. %d student's name: ",n);
scanf ( "%s", &q->name );
printf( " \nNow enter his or her number:" );
scanf ( "%d",&q->num );
printf( "\nNow enter the student's marks of the 4 subjects(Enlish,Maths,Programming and Data Structrue:");
for( i = 0; i < 4; ++i )
{
scanf( "%f", &t );
q->score[i] = t;
sum += t;
}
q->score[4] = sum/4; /* score[4] is used to record the average */
p->next = q;
p = q;
if(!feof(fp)) /* found */
{printf( "\n%s", p.name );
printf( "\t%d\n", p.num );
for( i = 0; i < 5; ++i )
printf( "\t%5.2f", p.score[i] );
}
else printf( "NO FOUND!" );
fclose( fp );
}
void list_traverse ( struct stud *head ) /* Print according to LinkList */
{
struct stud *p;
int i;
p = head->next;
if( p )
{
printf( "\nThe Records are:" );
while( p )
{
printf( "\n%s\t%d\n", p->name, p->num );
for( i = 0; i < 5; ++i ) printf( "\t%5.2f", p->score[i] );
p = p->next;
}
}
else printf("There is no records at all!");
}
void print_file ( FILE *fp ) /* Print according to File */
{
struct stud p;
int i,t;
printf( "\nThe Records are:" );
while ( !feof( fp ) ){
fread( &p, sizeof( struct stud ), 1, fp );
if ( t == p.num ) continue;
printf( "\n%s\t%d\n" , p.name ,p.num );
for( i = 0; i < 5; ++i ) printf( "\t%5.2f", p.score[i] );
t = p.num;
}
}
while( cmd != 'N' && cmd != 'n' ){
printf( "\nPlease enter the student's name :" );
scanf ( "%s", &t.name );
printf( "\nNow enter his or her number:" );
scanf ( "%d", &t.num );
printf( "\nNow enter the student's marks of the 4 subjects(Enlish,Maths,Programming and Data Structrue:" );
for( i = 0; i < 4; ++i )
{
scanf( "%f", &temp );
t.score[i] = temp;
sum += temp;
}
t.score[4] = sum/4;
template <class KeyType,class ValType>
BOOL ListT<KeyType,ValType>::InsertDescend(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
if (!pNewNode)
return FALSE;
//Insert the node into the list
ListNodeT<KeyType,ValType>* pCursor1 = head;
ListNodeT<KeyType,ValType>* pCursor2 = head;
//search the key grand then the insert
if (this->GetListSize() == 0) //the list have no member, must set the head
head = pNewNode;
else if (this->GetListSize() == 1)
{
if (pCursor1->GetKey() < NewKey)
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
pCursor1->SetNext(pNewNode);
}
else //have some member (haven sorted ascend)
{
if (pCursor1->GetKey() < NewKey) //must inster in head
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
{
//search the position of the newobject
while (pCursor1->GetNext()) {
pCursor2 = pCursor1;
pCursor1 = pCursor1->GetNext();
if (pCursor2->GetKey() > NewKey &&
pCursor1->GetKey() < NewKey)
{
pCursor2->SetNext(pNewNode);
pNewNode->SetNext(pCursor1);
NumItems++;
return TRUE;
}
}
//tail
pCursor1->SetNext(pNewNode);
}
}
NumItems++;
return TRUE;
}
template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::Remove(KeyType SearchKey)
{
ListNodeT<KeyType, ValType>* pCursor = head;
//Is there a list?
//if not,can not find SearchKey in this, so False
if (!pCursor)
return FALSE;
//Check the head first
if (pCursor->GetKey() == SearchKey) {
head = pCursor->GetNext();
delete pCursor;
NumItems--;
return TRUE;
}
//Scan the list
while (pCursor->GetNext()) {
if (pCursor->GetNext()->GetKey() == SearchKey) {
ListNodeT<KeyType,ValType>* pTemp =
pCursor->GetNext();
pCursor->SetNext(pTemp->GetNext());
delete pTemp;
NumItems--;
return TRUE;
}
pCursor = pCursor->GetNext();
}
return FALSE;
}
template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::RemoveAll()
{
ListNodeT<KeyType, ValType>* pCursor = head;
// KeyType SearchKey;
//Is there a list?
if (!pCursor)
return TRUE;
//Check the head first
while (pCursor) {
head = pCursor->GetNext();
delete pCursor;
pCursor = head;
NumItems--;
}
return TRUE;
//Now ListT
//constructor
template <class KeyType, class ValType>
ListT<KeyType,ValType>::ListT():
//Initialize member data
head(NULL),
NumItems(0)
{}
//Destructor
template <class KeyType, class ValType>
ListT<KeyType,ValType>::~ListT()
{
//Delete all the nodes in the list
while (head) {
ListNodeT<KeyType,ValType>* pTemp = head->GetNext();
delete head;
head = pTemp;
}
}
//Public member function
template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::AddToHead(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
if (!pNewNode)
return FALSE;
//Insert the node into the list
pNewNode->SetNext(head);
head = pNewNode;
NumItems++;
return TRUE;
}
template <class KeyType, class ValType>
BOOL ListT<KeyType,ValType>::AddToTail(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
// ListNodeT<KeyType,ValType>* pNewNode =
// &(ListNodeT<KeyType,ValType>(NewKey,NewObject));
if (!pNewNode)
return FALSE;
//Insert the node into the list
ListNodeT<KeyType,ValType>* pCursor = head;
//search the tail of the list
if (!pCursor) //have no member,must set the head
head = pNewNode;
else
{
while (pCursor->GetNext())
pCursor = pCursor->GetNext();
pCursor->SetNext(pNewNode);
}
NumItems++;
return TRUE;
} //end of AddToTail
template <class KeyType,class ValType>
BOOL ListT<KeyType,ValType>::InsertAscend(KeyType NewKey,
ValType NewObject)
{
//Allocate memory for the new node
ListNodeT<KeyType,ValType>* pNewNode =
new ListNodeT<KeyType,ValType>(NewKey,NewObject);
if (!pNewNode)
return FALSE;
//Insert the node into the list
ListNodeT<KeyType,ValType>* pCursor1 = head;
ListNodeT<KeyType,ValType>* pCursor2 = head;
//search the key grand then the insert
if (this->GetListSize() == 0) //the list have no member, must set the head
head = pNewNode;
else if (this->GetListSize() == 1)
{
if (pCursor1->GetKey() > NewKey)
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
pCursor1->SetNext(pNewNode);
}
else //have some member (haven sorted ascend)
{
if (pCursor1->GetKey() > NewKey) //must inster in head
{
head = pNewNode;
pNewNode->SetNext(pCursor1);
}
else
{
//search the position of the newobject
while (pCursor1->GetNext()) {
pCursor2 = pCursor1;
pCursor1 = pCursor1->GetNext();
if (pCursor2->GetKey() < NewKey &&
pCursor1->GetKey() > NewKey)
{
pCursor2->SetNext(pNewNode);
pNewNode->SetNext(pCursor1);
NumItems++;
return TRUE;
}
}
//tail
pCursor1->SetNext(pNewNode);
}
}
NumItems++;
return TRUE;
}