//Homework Description: /* 2) The homework from last week required you to generate an example of an array-of-records problem, with typical features. Now add the following features (we won't go much further with this one, I promise): * the ability to search for a record, by ID # ... if the record is found, return its location within the array. If it is not found, return a -1 (why not zero, as before?). * the ability to delete a record ... first, get an ID # to search for, find its location, and if found, delete it. * the ability to sort the date, by ID # ... choose either the bubble sort or the selection sort ... we'll review both in class on Tuesday. Note, if your program is crashing due to the inclusion of a command-line file name, then hard-code the name of the file into the program for now. */ /* +PAC II: HW1 +jleblanc 2.9.06 +This is a simple make a record/get a record program run from a command line +The program writes to outfile.txt when it exits +Functions are listed as: 1.Data Structure 2.Welcome Function 3.Calculate Field Duration 4.Calculate Field Type 5.Make a Record 6.Display a Record 7.Search records by ID 8.SWAP Records 9.Delete a Record 10.Sort the Records by ID number 11.MAIN BODY */ #include #define max 25 //Data Structure struct oilfield { int size, output, id,duration; char quality; }; typedef struct oilfield oilfield; //Welcome Function void welcome() { printf("\n******************************************************************************\n"); printf("\nWelcome to the Oil Field Data System\n"); printf("This sophisticated program can track up to 25 fields\n"); printf("It rates them based on your input as:\n"); printf("\n Long term (L), Medium term (M), and Short term (S) producing fields\n\n"); printf("Follow command line prompts to begin.\n\n"); printf("\n******************************************************************************\n\n"); } //Calculate Field Duration int calc_duration (int size, int output ) { return size/output; } //Calculate Field Type (L,M,S,D) char duration_type (int years) { //L "long", M "medium", S "short", D "Done" if (years >= 20) return 'L'; else if (years >= 10) return 'M'; else if (years >= 0) return 'S'; else return 'D'; } //Make a Record void make_a_rec( struct oilfield *oil ) { printf("\t please enter oilfield ID : "); scanf ("%d", &oil->id); printf("\t please enter oilfield size (barrels): "); scanf ("%d", &oil->size); printf("\t please enter yearly output (barrels): "); scanf ("%d", &oil->output); //Note that this could also be done like this: scanf ("%d", &(*oil).size); (*oil).duration = calc_duration((*oil).size, (*oil).output); (*oil).quality = duration_type((*oil).duration); } //Display a Record void display_a_rec(struct oilfield *oil) { printf("\t For Oil Field %d \n", oil->id); //Note this is NOT: printf ("%d", &oil->id); printf("\t field size %d \n", oil->size); printf("\t yearly output %d \n", oil->output); printf("\t field duration (years) %d \n",oil->duration); printf("\t field quality %c \n", oil->quality); } //Search records by ID: We assume unique IDs int search(int target, struct oilfield f[], int num) { int x=0; int match=-1; while (x -1) { //To delete we want to sort away the record so: //move all the records behind that into spot and then decriment number_of_records for(int x=index; xADD, d>DISPLAY, x>DELETE, f>FIND, s>SORT, p>PRINT+EXIT >> "); fflush(stdin); //This prevents a "bounce" in the command line scanf ("%c", &command); printf("\n\n"); //Make a Record if(command=='a') { if(number_of_fields < max) { make_a_rec(&fields[number_of_fields]); number_of_fields++; } else { printf("\t WARNING: Maximum number of fields already entered\n"); } } //Find a Record via ID if(command=='f') { printf("\t Enter id you wish to search for: "); scanf("%d", &target_id); printf("\n"); index=search(target_id, fields, number_of_fields); if(index > -1) { printf("\tThe address is number %d in the list of Records\n\n", index+1); } else { printf("\tThere is no Record with this ID number\n\n"); } } //Delete a Record via ID if(command=='x') { printf("\tEnter id you wish to delete: "); scanf("%d", &target_id); printf("\n"); number_of_fields=delete_record(target_id, fields, number_of_fields); } //Sort the records by ID (increasing) if(command=='s') { bubble_sort(fields, number_of_fields); printf("\tYour Records have been sorted!\n"); } //Display a record if(command=='d') { //Display record printf("\t There are/is %d field(s), enter field you want to display (1-%d)\n", number_of_fields, number_of_fields); printf("\t Enter which record you wish to access: "); scanf("%d", &target_id); printf("\n"); target_id=target_id-1; if( target_id > -1 && target_id < number_of_fields ) { display_a_rec(&fields[target_id]); } else { printf("\t WARNING: invalid record number\n"); } } printf("\n"); //Offset next line }//END while //On Exit Print Data to File fp = fopen("outfile.txt", "w"); for (x = 0; x