1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable : 4996)
void addMember();
void sortMember();
struct Date { int day, month, year; };
struct Member { char memberId[6], name[40], gender, IC[15], contactNum[15]; struct Date DOB; } members;
FILE* fPtr;
main() {
int choice;
do {
printf("\nMember Management System\n");
printf("-------------------------\n");
printf("1. Add members info\n");
printf("2. Sort members info\n");
printf("0. Exit\n");
printf("Select an option:> ");
scanf("%d", &choice);
switch (choice) {
case 1:
addMember();
break;
case 2:
sortMember();
break;
}
} while (choice != 0);
printf("Bye Bye!\n");
system("pause");
}
void printMemberBanner(void)
{
printf("\nMember Details\n");
printf("------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("Member ID\tName\t\t\t\tGender\t\tIC\t\t\tContact Number\t\tDate of Birth\n");
printf("------------------------------------------------------------------------------------------------------------------------------------------------\n");
}
void addMember() {
printf("\nAdd Member Details\n");
printf("--------------------\n");
fPtr = fopen("member.bin", "ab");
if (fPtr == NULL) {
printf("Failed to open file");
exit(-1);
}
char ansAdd;
do {
printf("\nEnter your member ID: ");
rewind(stdin);
scanf("%[^\n]", &members.memberId);
printf("Enter your name: ");
rewind(stdin);
scanf("%[^\n]", &members.name);
printf("Enter your gender: ");
rewind(stdin);
scanf("%c", &members.gender);
printf("Enter your IC: ");
rewind(stdin);
scanf("%[^\n]", &members.IC);
printf("Enter your contact number: ");
rewind(stdin);
scanf("%[^\n]", &members.contactNum);
printf("Enter your date of birth(dd/mm/yyyy): ");
rewind(stdin);
scanf("%d %d %d", &members.DOB.day, &members.DOB.month, &members.DOB.year);
fwrite(&members, sizeof(members), 1, fPtr);
printMemberBanner();
printf("%-6s\t\t%-10s\t\t\t%c\t\t%-15s\t\t%-15s\t\t%02d/%02d/%02d\n", members.memberId, members.name, members.gender, members.IC, members.contactNum, members.DOB.day, members.DOB.month, members.DOB.year);
printf("\nDo you want to continue adding? (Y/N) ");
rewind(stdin);
scanf("%c", &ansAdd);
ansAdd = toupper(ansAdd);
} while (ansAdd == 'Y');
fclose(fPtr);
void sortMember() {
struct Member p[20];
printf("\nSort Member Details\n");
printf("--------------------\n");
fPtr = fopen("member.bin", "rb");
if (fPtr == NULL) {
printf("Failed to open file");
exit(-9);
}
int i = 0, numRecords = 0;
while (fread(&p[i], sizeof(members), 1, fPtr) != 0) {
i++;
numRecords++;
}
char key[20];
//this method is insertion sort
for (int step = 1; step < numRecords; step++) {
strcpy(key, p[step].memberId); //copy the ID into key
int j = step - 1;
while (strcmp(key, p[j].memberId) < 0 && j >= 0) { //compare key with each element on the left of it until an element smaller than the key is found
strcpy(p[j + 1].memberId, p[j].memberId);
strcpy(p[j + 1].name, p[j].name);
p[j + 1].gender = p[j].gender;
strcpy(p[j + 1].IC, p[j].IC);
strcpy(p[j + 1].contactNum, p[j].contactNum);
p[j + 1].DOB.day = p[j].DOB.day;
p[j + 1].DOB.month = p[j].DOB.month;
p[j + 1].DOB.year = p[j].DOB.year;
--j;
}
strcpy(p[j + 1].memberId, key); //Place key at after the element just smaller than it
}
fclose(fPtr);
fPtr = fopen("member.bin", "wb");
if (fPtr == NULL) {
printf("Unable to open the file\n");
exit(-7); //exit program
}
printMemberBanner();
for (int i = 0; i < numRecords; i++) {
fwrite(&p[i], sizeof(p[i]), 1, fPtr);
printf("%-6s\t\t%-10s\t\t\t%c\t\t%-15s\t\t%-15s\t\t%02d/%02d/%02d\n", p[i].memberId, p[i].name, p[i].gender, p[i].IC, p[i].contactNum, p[i].DOB.day, p[i].DOB.month, p[i].DOB.year);
}
fclose(fPtr);
}
|