/**************************************** * Section : 10 * Roll No. : 20CS10000 / 20CS30000 * Name : Aritra Hazra * Assignment No : 7 * Description : Criminal Record Database * Date : 15-Jun-2021 ****************************************/ #include #include #define PIN_SIZE 7 #define MAX_STRING 40 #define MAX_ITEM 10000 // structure to hold date information typedef struct _date { int day; int month; int year; } date; // structure to hold address of a location typedef struct _address { char place[MAX_STRING]; // city or town name char district[MAX_STRING]; // district name char state[MAX_STRING]; // state name char pin[PIN_SIZE]; // pincode } address; // structure to hold criminal details typedef struct _criminal { char name[MAX_STRING]; // criminal name struct _date dob; // criminal's date of birth char gender; // M = male, F = female, O = others double height; // criminal's height in cm struct _address residence; // criminal's residential address char crime; // M = murder, T = theft, R = robbery, A = anti-social, O = others struct _date arrest; // criminal's arrest date char station[MAX_STRING]; // police station in charge of the criminal char court[MAX_STRING]; // judiciary court in charge of the criminal's case struct _address ps; // address of the police station } criminal; // read the detailed record of a criminal and populate a criminal structure criminal readCriminal() { criminal c; printf("\t-- Enter Criminal Name: "); scanf(" %[^\n]", c.name); printf("\t-- Enter Date of Birth (in DD/MM/YYYY Format): "); scanf("%d/%d/%d", &(c.dob.day), &(c.dob.month), &(c.dob.year)); printf("\t-- Enter Gender (M=male, F=female, O=others): "); scanf(" %c", &c.gender); printf("\t-- Enter Height (in cm): "); scanf(" %lf", &c.height); printf("\t-- Enter Residence (in Place District State PIN Format): "); scanf(" %s %s %s %s", c.residence.place, c.residence.district, c.residence.state, c.residence.pin); printf("\t-- Enter Committed Crime (M=murder, T=theft, R=robbery, A=anti-social, O=others): "); scanf(" %c", &c.crime); printf("\t-- Enter Date of Arrest (in DD/MM/YYYY Format): "); scanf("%d/%d/%d", &(c.arrest.day), &(c.arrest.month), &(c.arrest.year)); printf("\t-- Enter Police Station Name: "); scanf(" %[^\n]", c.station); printf("\t-- Enter Jurisdiction Court Name: "); scanf(" %[^\n]", c.court); printf("\t-- Enter P.S. Address (in Place District State PIN Format): "); scanf(" %s %s %s %s", c.ps.place, c.ps.district, c.ps.state, c.ps.pin); return c; } // print a criminal record details void printCriminal(criminal c) { printf("\t** Name: %s\n", c.name); printf("\t** Date of Birth: %d/%d/%d\n", c.dob.day, c.dob.month, c.dob.year); printf("\t** Gender: "); if ( (c.gender == 'M') || (c.gender =='m') ) { printf("Male\n"); } else if ( (c.gender == 'F') || (c.gender =='F') ) { printf("Female\n"); } else if ( (c.gender == 'O') || (c.gender =='o') ) { printf("Others\n"); } else { printf("N/A\n"); } printf("\t** Height: %lf cm\n", c.height); printf("\t** Residential Address: %s, %s, %s, PIN - %s.\n", c.residence.place, c.residence.district, c.residence.state, c.residence.pin); printf("\t** Committed Crime: "); if ( (c.crime == 'M') || (c.crime =='m') ) { printf("Murder\n"); } else if ( (c.crime == 'T') || (c.crime =='t') ) { printf("Theft\n"); } else if ( (c.crime == 'R') || (c.crime =='r') ) { printf("Robbery\n"); } else if ( (c.crime == 'A') || (c.crime =='a') ) { printf("Anti-social Activity\n"); } else if ( (c.crime == 'O') || (c.crime =='o') ) { printf("Others\n"); } else { printf("N/A\n"); } printf("\t** Date of Arrest: %d/%d/%d\n", c.arrest.day, c.arrest.month, c.arrest.year); printf("\t** Police Station Name: %s\n", c.station); printf("\t** Jurisdiction Court Name: %s\n", c.court); printf("\t** P.S. Address: %s, %s, %s, PIN - %s.\n", c.ps.place, c.ps.district, c.ps.state, c.ps.pin); } // insertion sort to place the last entered (n-th) criminal details and // place it so that the criminal record is sorted w.r.t. criminal names // (in ascending lexicographic order) void organizeRecord(criminal *pcd, int n) { int i; criminal temp; for (i = n-1; i >= 1; i--) { if (strcmp(pcd[i-1].name,pcd[i].name) > 0) { // appropriate placement of last entry temp = pcd[i-1]; pcd[i-1] = pcd[i]; pcd[i] = temp; } else { break; } } } // delete a criminal record if exists in the database and // then adjust the criminal record database/array by shifting int deleteRecord(criminal *pcd, int n, char *name, char crime, date arr, char *ps) { int i = 0, j, flag = 0; while (i < n) { if ( !strcmp(pcd[i].name,name) && (pcd[i].crime == crime) && (pcd[i].arrest.day == arr.day) && (pcd[i].arrest.month == arr.month) && (pcd[i].arrest.year == arr.year) && !strcmp(pcd[i].station,ps) ) { // finding match in criminal record to delete flag = 1; printf("== Criminal No. %d ==\n", i+1); printCriminal(pcd[i]); for (j = i; j < n-1; j++) { // shift and adjust deleted gap position pcd[j] = pcd[j+1]; } n--; } else { i++; } } if (!flag) { printf("** NO Such Record Found!\n"); } return n; } // linear search a criminal entry/details from database by a criminal's name void searchRecord(criminal *pcd, int n, char *name) { int i, flag = 0; for (i = 0; i < n; i++) { if ( !strcmp(pcd[i].name,name) ) { // name matched flag = 1; printf("== Criminal No. %d ==\n", i+1); printCriminal(pcd[i]); } } if (!flag) { printf("** NO Such Record Found!\n"); } } int main() { char choice =' '; criminal crimDatabase[MAX_ITEM]; criminal c; int noRecord = 0, i; char crimName[MAX_STRING]; char crimCrime; date crimArrest; char crimPS[MAX_STRING]; int crimId; while ( (choice != 'e') && (choice != 'E') ) { // show user menu and choice printf("\n++++ !! Welcome to Confidential Criminal Database !! ++++"); printf("\n-- Database Operation Menu:\n\t(a) Add Criminal Record,\n\t(b) Delete Criminal Record,\n\t(c) Search Criminal Record,\n\t(d) Print Criminal Record,\n\t(e) Exit.\n"); printf("-- Enter your Choice: "); scanf(" %c", &choice); switch(choice){ case 'a': /* add criminal record */ case 'A': printf("\n++ Add Criminal Information ++\n"); c = readCriminal(); crimDatabase[noRecord] = c; noRecord ++; organizeRecord(crimDatabase, noRecord); break; case 'b': /* delete criminal record */ case 'B': printf("\n++ Delete Criminal Information ++\n"); if (noRecord == 0) { // no records exist in database printf("** NO Record Present in Database!\n"); } else { printf("\t-- Criminal Name: "); scanf(" %[^\n]", crimName); printf("\t-- Committed Crime (M=murder, T=theft, R=robbery, A=anti-social, O=others): "); scanf(" %c", &crimCrime); printf("\t-- Date of Arrest (in DD/MM/YYYY Format): "); scanf("%d/%d/%d", &(crimArrest.day), &(crimArrest.month), &(crimArrest.year)); printf("\t-- Police Station Name: "); scanf(" %[^\n]", crimPS); printf("~~ Deleted Record Details ~~\n"); noRecord = deleteRecord(crimDatabase, noRecord, crimName, crimCrime, crimArrest, crimPS); } break; case 'c': /* search criminal record */ case 'C': printf("\n++ Search Criminal Information ++\n"); if (noRecord == 0) { // no records exist in database printf("** NO Record Present in Database!\n"); } else { printf("\t-- Enter Criminal Name: "); scanf(" %[^\n]", crimName); printf("~~ Searched Record Details ~~\n"); searchRecord(crimDatabase, noRecord, crimName); } break; case 'd': /* print criminal record */ case 'D': printf("\n++ Print Criminal Information ++\n"); if (noRecord == 0) { printf("** NO Record Present in Database!\n"); } else { for (i = 0; i < noRecord; i++) { printf("== Criminal No. %d ==\n", i+1); printCriminal(crimDatabase[i]); } } break; case 'e': /* Exit or Terminate */ case 'E': printf("\n** !! Thank you. Keep the Information Confidential !! **\n\n"); break; default: /* wrong choice */ printf("** Warning: Wrong Choice Entered! Re-enter choice ... **\n"); break; } } return 0; }