#include<stdio.h>
#include<alloc.h>
/*What the linked list contains.*/
struct stud
{
char studNumber[13],lastName[21],firstName[21];
float finalGrade;
struct stud *sp;
};
/*This subroutine will SHOW the MENU to the user.*/
showMenu()
{
int choice;
clrscr();
do
{
printf("MENU:\n\n1. CREATE\n2. READ\n3. UPDATE\n4. DELETE\n5. SAVE\n6. QUIT\n\nEnter your choice : ");
scanf("%d",&choice);
}while( choice<1 || choice>6 );
return choice;
}
/*This subroutine will insert a new record at the end of the list.*/
struct stud* createRec( struct stud *head,int recordCounter,int firstInputCheck )
{
struct stud *trail;
char ans;
clrscr();
/* To see if there was already a first input. */
if( firstInputCheck==0 )
{
ans='y';
printf("Enter the student's student number : ");
scanf("%s",head->studNumber);
printf("\nEnter the student's last name : ");
scanf("%s",head->lastName);
printf("\nEnter the student's first name : ");
scanf("%s",head->firstName);
printf("\nEnter the student's final grade : ");
scanf("%f",&head->finalGrade);
trail=head;
trail->sp=NULL;
recordCounter++; /* A counter to see if it already exceeded 100 records. */
firstInputCheck++;
printf("\n\nWould you like to enter another record? [y/n]");
ans=getche();
}
while( recordCounter!=100 && ( (ans=='y') || (ans=='Y') ) )
{
/* Input more records. */
while( (ans=='y') || (ans=='Y') )
{
struct stud *t=malloc( sizeof(struct stud) );
clrscr();
trail->sp=t;
printf("Enter the student's student number : ");
scanf("%s",t->studNumber);
printf("\nEnter the student's last name : ");
scanf("%s",t->lastName);
printf("\nEnter the student's first name : ");
scanf("%s",t->firstName);
printf("\nEnter the student's final grade : ");
scanf("%f",&t->finalGrade);
trail=t;
recordCounter++;
printf("\n\nWould you like to enter another record? [y/n]");
ans=getche();
}
}
trail->sp=NULL;
return head;
}
/* This subroutine will let the user input the student number and will display the desired record. */
void showRec( struct stud *head )
{
int foundIt=0;
struct stud *trail;
char stdNum[12];
clrscr();
printf("Input the student number of the student : ");
scanf("%s",stdNum);
trail=head;
/* This do-while will find the desired record - TO BE CREATED IN A NEW SUBROUTINE. */
do
{
if( strcmp( trail->studNumber,stdNum )==0 )
{
foundIt++;
}else{
trail=trail->sp;
}
}while( foundIt==0 && trail!=NULL );
if( foundIt!=0 )
{
printf("\n\nStudent %s's records are as follow:\n\n%s , %s , %s , %.1f",stdNum,trail->studNumber,trail->lastName,trail->firstName,trail->finalGrade);
}else{
printf("\n\nSuch student number does not exist.");
}
}
/* This subroutine will let the user input the student number and the user will update the existing records EXCEPT student number. */
struct stud* updateRec( struct stud *head )
{
int foundIt=0;
struct stud *trail;
char stdNum[12];
clrscr();
printf("Input the student number of the student : ");
scanf("%s",stdNum);
trail=head;
/* This do-while will find the desired record - TO BE CREATED IN A NEW SUBROUTINE. */
do
{
if( strcmp( trail->studNumber,stdNum )==0 )
{
foundIt++;
}else{
trail=trail->sp;
}
}while( foundIt==0 && trail!=NULL );
if( foundIt!=0 )
{
printf("\nEnter the new student's last name : ");
scanf("%s",trail->lastName);
printf("\nEnter the new student's first name : ");
scanf("%s",trail->firstName);
printf("\nEnter the new student's final grade : ");
scanf("%f",&trail->finalGrade);
printf("\n\nThe new student %s's records are as follow:\n\n%s , %s , %s , %.2f",stdNum,trail->studNumber,trail->lastName,trail->firstName,trail->finalGrade);
printf("\n\nRecords updated!");
}else{
printf("\n\nSuch student number does not exist.");
}
return head;
}
/* This subroutine will mark a record for deletion. */
struct stud* deleteRec( struct stud *head )
{
int foundIt=0;
struct stud *trail;
char stdNum[12];
clrscr();
printf("Input the student number of the student's record for deletion : ");
scanf("%s",stdNum);
trail=head;
/* This do-while will find the desired record - TO BE CREATED IN A NEW SUBROUTINE. */
do
{
if(strcmp(trail->studNumber,stdNum)==0)
{
foundIt++;
}else{
trail=trail->sp;
}
}while( foundIt==0 && trail!=NULL );
if( foundIt!=0 )
{
trail->studNumber[0]='*';
printf("\n\nDeletion mark placed! %s",trail->studNumber);
}else{
printf("\n\nSuch student number does not exist.");
}
return head;
}
/* All the records EXCEPT the ones with deletion marks will be saved in a text file named CLASSLIST.TXT in this subroutine. */
struct stud* saveRecords( struct stud *head )
{
FILE *cl;
struct stud *trail;
trail=head;
clrscr();
/* CLASS = file name, wt = write only text file */
cl=fopen( "C:\\CLASSLIST.txt" , "w" );
do
{
if(trail->studNumber[0]!='*')
{
fprintf( cl , "%s , %s , %s , %.1f\n" , trail->studNumber, trail->lastName, trail->firstName, trail->finalGrade );
printf("Student %s's records are now written on CLASSLIST.txt\n",trail->studNumber);
}
trail=trail->sp;
}while( trail!=NULL );
fclose(cl);
}
/* The main subroutine */
main()
{
struct stud *head=malloc( sizeof(struct stud) );
int choice,recordCounter=1,firstInputCheck=0;
do
{
choice=showMenu();
switch( choice )
{
case 1: head=createRec( head,recordCounter,firstInputCheck );
break;
case 2: showRec( head );
break;
case 3: head=updateRec( head );
break;
case 4: head=deleteRec( head );
break;
case 5: saveRecords( head );
break;
default: printf("\n\nProgram terminated.");
}
printf("\n\nPress any key to continue.");
getch();
}while( choice!=6 );
}
这是我的代码,它工作正常,只是我在 C: 中没有看到新制作的 CLASSLIST.txt。