Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_STUDENTS 10
typedef struct {
int recordNum;
char studentNum[20];
char lastName[ 15 ];
char firstName[ 10 ];
char phoneNum[ 15 ];
int Deleted;
} studentData;
void menu (void);
int read_studentRecord( studentData [] );
void print_studentRecord( studentData [] );
void sort_studentRecord( studentData [], int );
void search_studentRecord( studentData [], int );
int delete_studentRecord(studentData [], int);
// **************************************************
//****************************************
int main()
{
menu();
return 0;
}
// **************************************************
//****************************************
void menu (void)
{
int n;
int choice;
studentData details [MAX_STUDENTS]; //define a structure array of size MAX_STUDENTS
printf("Press 1 to Enter Records\n\n");
printf("Press 2 to View records in sorted form\n\n");
printf("Press 3 to Search for a record\n\n");
printf("Press 4 to Delete a record\n\n");
printf("Press 0 to exit\n\n");
scanf("%d", &choice);
while (choice != 0)
{
if (choice == 1)
{
system("cls");
n = read_studentRecord( details );
}
else if (choice == 2)
{
system("cls");
sort_studentRecord( details, n );
print_studentRecord( details );
}
else if (choice == 3)
{
system("cls");
search_studentRecord( details, n );
}
else if (choice == 4)
{
system("cls");
delete_studentRecord( details, n );
}
else
{
printf("Invalid input\n");
}
printf("Press 1 to Enter Records\n\n");
printf("Press 2 to View records in sorted form\n\n");
printf("Press 3 to Search for a record\n\n");
printf("Press 4 to Delete a record\n\n");
printf("Press 0 to exit\n\n");
scanf("%d", &choice);
}
}
//***************************************************
int read_studentRecord ( studentData details[] )
{
int i = 0;
int recordNum;
printf( "Enter Student Number (e.g. S00054321, 0 to end input)\n" );
scanf( "%s", details[i].studentNum );
while ( details[i].studentNum[0] != '0' )
{
printf (" Enter Last Name:\n?");
scanf ( "%s", details[i].lastName );
printf (" Enter First Name:\n?");
scanf ( "%s", details[i].firstName );
printf (" Enter Phone Number:\n?");
scanf ( "%s", &details[i].phoneNum );
recordNum = i+1;
i++;
printf ( "Enter Student Number\n?");
scanf ( "%s", details[i].studentNum);
}
system("cls");
return i;
}
// **************************************************
//***************************************
void sort_studentRecord ( studentData details[], int n )
{
studentData hold;
int i, pass;
for ( pass = 1; pass <= n - 1; pass++ )//number of passes
for ( i = 0; i <= n - pass -1; i++ )//single pass
if ( strcmp( details[i].lastName, details[i+1].lastName ) == 1) {//sort in order of last name
hold = details[i];
details[i] = details[i+1];//swap array elements if statement is true
details[i+1] = hold;
}
}
// **************************************************
//***************************************
void print_studentRecord( studentData details[] )
{
int i=0;
system("cls");
printf("Sorted by Last Name:\n\n");
if (details[0].studentNum != 0)
printf ("\n%-10s%-10s%-16s%-11s%10s\n\n","Record #","Student #","Last Name","First Name","Phone #");
while ( details[i].studentNum[0] != '0' )
{
printf("%-10d%-10s%-16s%-11s%10s\n",i+1, details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum);
i++;
}
printf("\n\n");
printf("Press any key to return to the main menu\n");
getch();
system("cls");
}
// **************************************************
//***************************************
void search_studentRecord( studentData details[], int numOfRec )
{
int low, high, mid;
char searchkey[10];
int flag = 0;
int i = 0;
printf ("\n%-10s%-10s%-16s%-11s%10s\n\n","Record #","Student #","Last Name","First Name","Phone #");
while ( details[i].studentNum[0] != '0' )
{
printf("%-10d%-10s%-16s%-11s%10s\n",i+1, details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum);
i++;
}
printf("\n\n\nEnter a Last Name to search for.\n");
scanf("%s", searchkey);
low = 0;
high = numOfRec;
while (low <= high) {
mid = (low+high)/2;
if (strcmp(searchkey, details[mid].lastName) < 0)
high = mid-1;
else if (strcmp(searchkey, details[mid].lastName) > 0)
low = mid + 1;
else if (strcmp(searchkey, details[mid].lastName) == 0) /* found match */
{
system("cls");
printf("Record Found\n\n");
printf("Details for %s:", searchkey);
printf("\n\n\tStudent I.D: %s\n\tPhone Number: %s\n", details[mid].studentNum, details[mid].phoneNum);
flag = 1;
break;
}
}
if (!flag) printf("Search Key not found\n");
printf("\n\nPress any key to return to the main menu\n");
getch();
system("cls");
}
//*********************************************************************
//********************************************************
int delete_studentRecord(studentData details[], int n)
{
int delete_index;
int last_index = n;
int i = 0;
printf ("\n%-10s%-10s%-16s%-11s%10s\n\n","Record #","Student #","Last Name","First Name","Phone #");
while ( details[i].studentNum[0] != '0' )
{
printf("%-10d%-10s%-16s%-11s%10s\n",i+1, details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum);
i++;
}
printf("max is %d\n", last_index);
printf("Enter a record number to delete the details of:\n");
scanf("%d", &delete_index);
printf("delete index is %d\n", delete_index);
if(delete_index != n)
{
// replace the record to be deleted with the last record in the list
details[delete_index] = details[last_index];
// the record has now been deleted
// the last record is deleted because it has already been copied over
details[last_index].Deleted = 1;
}
else
details[delete_index].Deleted = 1;
printf("Last index gone\n\n");
--last_index;
return 0;
}