#ifndef HDL
#define HDL
#define FALSE 0
#define TRUE 1
#define NAME_LEN 15
#define DESCR_LEN 20
#define PHONE_LEN 15
enum MENU_OPTION { ADD='1', MODIFY, DELETE, SEARCH, EXIT };
struct EMPLOYEE_INFO
{
char last_name[NAME_LEN+1];
char middle_name[NAME_LEN+1];
char first_name[NAME_LEN+1];
char description[DESCR_LEN+1];
char salary[11];
char phone[PHONE_LEN+1];
};
typedef struct EMPLOYEE_INFO EMPLOYEE ;
struct LLIST
{
struct EMPLOYEE_INFO employee;
struct LLIST *next;
};
typedef struct LLIST LINK_LIST ;
void Display(EMPLOYEE employee);
void GetData(EMPLOYEE *employee);
int GetCategory(void);
void WriteFile(char *filename, LINK_LIST *employees);
LINK_LIST *ReadFile(char *filename);
LINK_LIST *Add(LINK_LIST *employees, EMPLOYEE employee);
LINK_LIST *Delete(LINK_LIST *employees);
LINK_LIST *Modify(LINK_LIST *employees);
int Find(EMPLOYEE employee, int category, char *search_string);
void Search(LINK_LIST *employees);
int Count(LINK_LIST *employees);
void Welcome(void);
void Bye(void);
void PIS_Menu(char *filename);
void DisplayMenu(void);
void MenuError(void);
char *strupr(char *string);
#endif
/************************************
* Information Systems Practice *
* 'C' Project *
* Personal Information System *
* by Han-Dat Luc (C) Copyright 1996 *
************************************/
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "assign.h"
int main(int argc, char *argv[])
{
/* Check for command line argument which is the data file name
if not available, exit and show syntax */
if (argc>1)
{
Welcome();
PIS_Menu(argv[1]);
Bye();
}
else
{
printf("Error! Please specify database\n");
printf("Syntax: %s \n\n", argv[0]);
}
return (0);
}
void Welcome(void)
{
printf("\n\n\n");
printf("###############################\n");
printf("# Personal Information System #\n");
printf("# by HDL (C) Copyright 1996 #\n");
printf("###############################\n");
printf("\n\n\n Press to continue...");
fflush(stdin);
getchar();
}
void Bye(void)
{
printf("\n\n\n");
printf("*********************************************************\n");
printf("* Termination requested by user. *\n");
printf("* Thank you for using HDL's Personal Information System *\n");
printf("*********************************************************\n"); }
void DisplayMenu(void)
{
printf("\n\n\n");
printf("-------- MAIN MENU --------\n");
printf(" 1. Add employee record \n");
printf(" 2. Modify employee record \n");
printf(" 3. Delete employee record \n");
printf(" 4. Search employee record \n");
printf(" 5. Exit \n");
printf("---------------------------\n");
printf("Please enter your choice: ");
}
void MenuError()
{
printf("\n\nInvalid choice !!!");
printf("\nPlease enter a number from 1 - 5");
printf("\nPress to try again...");
fflush(stdin);
getchar();
printf("\n\n\n\n");
}
void PIS_Menu(char *filename)
{
int choice;
LINK_LIST *employees;
EMPLOYEE employee;
/* reads data from datafile into linked list */
employees = ReadFile(filename);
do
{
DisplayMenu();
fflush(stdin);
choice = getchar();
switch (choice)
{
case ADD: GetData(&employee);
employees = Add(employees, employee);
break;
case MODIFY: employees = Modify(employees);
break;
case DELETE: employees = Delete(employees);
break;
case SEARCH: Search(employees);
break;
case EXIT: break;
default: MenuError();
};
} while (choice != EXIT);
/* write the content of the linked list to the data file */
WriteFile(filename, employees);
}
void Display(EMPLOYEE employee)
{
printf("*------------------------------------*\n");
printf("| Last name: %-21s |\n", employee.last_name);
printf("| Middle name: %-21s |\n", employee.middle_name);
printf("| First name: %-21s |\n", employee.first_name);
printf("| Description: %-21s |\n", employee.description);
printf("| Salary: %-21s |\n", employee.salary);
printf("| Phone No.: %-21s |\n", employee.phone);
printf("*------------------------------------*\n");
printf("\n\nPress to continue...");
fflush(stdin);
getchar();
}
void GetData(EMPLOYEE *employee)
{
printf("\n\nPlease enter details for employee\n\n");
printf("Enter last name: ");
fflush(stdin);
scanf("%15s", employee->last_name);
printf("Enter middle name: ");
fflush(stdin);
scanf("%15s", employee->middle_name);
printf("Enter first name: ");
fflush(stdin);
scanf("%15s", employee->first_name);
printf("Enter description: ");
fflush(stdin);
gets(employee->description);
printf("Enter salary: ");
fflush(stdin);
scanf("%10s", employee->salary);
printf("Enter phone no: ");
fflush(stdin);
scanf("%15s", employee->phone);
}
LINK_LIST *Add(LINK_LIST *employees, EMPLOYEE employee)
{
/* this function adds a node to the beginning of the linked list */
LINK_LIST *node;
/* allocate memory for node */
node = (LINK_LIST *) malloc (sizeof(LINK_LIST));
if (node == NULL)
{
printf("Can't add employee: out of memory\n");
return (employees);
}
/* assign data to the node */
node->employee=employee;
node->next = employees;
return (node);
}
LINK_LIST *Delete(LINK_LIST *employees)
{
/* search for record and remove from linked list */
int found = 0;
char search_string[NAME_LEN*3+1];
LINK_LIST *current, *prev;
printf("Please enter full name of employee to be deleted: ");
fflush(stdin);
gets(search_string);
current = employees;
prev = current;
while ((current != NULL) && (found == 0))
{
if (Find(current->employee, 'N', search_string) == TRUE)
{
Display(current->employee);
found++;
if (Count(employees) == 1)
employees = NULL;
else
prev->next = current->next;
free(current);
}
else
{
prev = current;
current = current->next;
}
}
if (found == 0)
printf("\n%s does not exist\n", search_string);
else
printf("\nRecord deleted\n");
return (employees);
}
LINK_LIST *Modify(LINK_LIST *employees)
{
/* find record and modify it */
int found = 0;
char search_string[NAME_LEN*3+1];
LINK_LIST *nodes;
EMPLOYEE employee;
printf("Please enter full name of employee to be modified: ");
fflush(stdin);
gets(search_string);
nodes = employees;
while ((nodes != NULL) && (found == 0))
{
if (Find(nodes->employee, 'N', search_string) == TRUE)
{
Display(nodes->employee);
found++;
GetData(&employee);
nodes->employee = employee;
}
else
nodes = nodes->next;
}
if (found == 0)
printf("\n%s does not exist\n", search_string);
else
printf("\nRecord updated\n");
return (employees);
}
int Count(LINK_LIST *employees)
{
/* count the records in the linked list */
int count = 0;
LINK_LIST *node;
node = employees;
while (node)
{
count++;
node = node->next;
}
return (count);
}
void WriteFile(char *filename, LINK_LIST *employees)
{
/* write the entire linked list to file */
int written=0;
EMPLOYEE employee;
FILE *fp;
fp = fopen(filename,"wt");
while (employees)
{
employee = employees->employee;
fwrite(&employee, sizeof(EMPLOYEE), 1, fp);
written++;
employees = employees->next;
}
fclose(fp);
printf("\n%i record(s) written\n", written);
}
LINK_LIST *ReadFile(char *filename)
{
/* reads the records from file into linked list */
int read=0;
LINK_LIST *employees;
EMPLOYEE employee;
FILE *fp;
employees = NULL;
if ((fp = fopen(filename,"rt")) != NULL)
{
while (fread(&employee, sizeof(EMPLOYEE), 1, fp) == 1)
{
read++;
employees = Add(employees, employee);
}
}
fclose(fp);
printf("\n%i record(s) read\n", read);
return (employees);
/* return NULL if file not found
or the linked list of records */
}
int Find(EMPLOYEE employee, int category, char *search_string)
{
/* checks whether the search string is in the current node
according to category specified */
char check_string[NAME_LEN*3+1];
switch (category)
{
case 'N': sprintf(check_string,
"%s %s %s",
employee.first_name,
employee.middle_name,
employee.last_name);
break;
case 'L': strcpy(check_string, employee.last_name);
break;
case 'F': strcpy(check_string, employee.first_name);
break;
case 'D': strcpy(check_string, employee.description);
break;
case 'S': strcpy(check_string, employee.salary);
break;
case 'P': strcpy(check_string, employee.phone);
break;
}
/* if search string matches (ignoring case) then return TRUE
otherwise return FALSE */
if (strcmp(strupr(search_string), strupr(check_string)) == 0)
return (TRUE);
else
return (FALSE);
}
void Search(LINK_LIST *employees)
{
/* browses through linked list and search for matching record */
int category, found = 0;
char search_string[NAME_LEN*3+1];
LINK_LIST *node;
category = GetCategory();
printf("Please enter search string: ");
fflush(stdin);
gets(search_string);
for ( node = employees; node != NULL; node = node->next)
{
if (Find(node->employee, category, search_string) == TRUE)
{
Display(node->employee);
found++;
}
}
if (found == 0)
{
printf("\n%s could not be found!\n", search_string);
printf("\nPress to continue...");
fflush(stdin);
getchar();
}
else
printf("\n%i matching record(s) found\n", found);
}
int GetCategory(void)
{
int category, valid = FALSE;
printf("\n\n\n");
printf("---------------------------\n");
printf(" N - full name \n");
printf(" L - last name \n");
printf(" F - first name \n");
printf(" D - description \n");
printf(" S - salary \n");
printf(" P - phone no. \n");
printf("---------------------------\n");
do
{
printf("Please specify search category: ");
fflush(stdin);
category = toupper(getchar());
switch (category)
{
case 'N':
case 'L':
case 'F':
case 'D':
case 'S':
case 'P': valid = TRUE;
}
} while (!valid);
return (category);
}
char *strupr(char *string)
{
/* converts string to uppercase */
int c;
for (c=0; c<strlen(string); c++)
string[c] = toupper(string[c]);
return (string);
}
e-mail me
Back to previous page