Menu

[Solved] C Program Answer Even 100 Completed Give Two Likes Starter Code Monsterdbc Include Include Q37196005

Monster Database In this lab we re-visit our monsters, stats, and weapons but this time we are writing a C program. Furthermono monsters loaded Otherwise, sort the monsters by hitpoint values (in descending order), then immediately display them. q- q| Be sure to use the size and list fields of the given MonsterList struct as the destination for the reads. Some samples showA Sample Run Enter a command: print this list of commands read monster database (binary filel wwrite monster database (binary

c program
just answer it even if it is not 100 % completed and i will giveyou two likes
starter code:

//———————————————————————–
// monsterdb.c
//———————————————————————–
#include
#include
#include

//———————————————————————–
// Some defines
#define NAME_MAX 64
#define BUFFER_MAX 256

// User instructions
const char *info[] = {
“Enter a command:”,
“”,
” ? – print this list of commands”,
” r – read monster database (binary file)”,
” w – write monster database (binary file)”,
” d – display monsters”,
” n – sort monsters by name (ascending)”,
” h – sort monsters by hitpoints (descending)”,
” q – quit”,
};
const int N_INFO = 9;

//———————————————————————–
// Structs

typedef struct Weapon_struct {
char name[NAME_MAX];
int damageModifier;
} Weapon;

typedef struct Stats_struct {
int agility;
int toughness;
int hitpoints;
} Stats;

typedef struct Monster_struct {
char name[NAME_MAX];
Stats stats;
Weapon weapon;
} Monster;

typedef struct MonsterList_struct {
int size;
Monster *list;
} MonsterList;

//———————————————————————–
// Function prototypes

void printInfo();
void displayMonster(Monster *m);
void displayMonsters(MonsterList *monsters);
int swapNeededName(Monster *a, Monster *b);
int swapNeededHitPoints(Monster *a, Monster *b);
void sortMonsters(MonsterList *monsters, char sortType);
void readDb(MonsterList *monsters, char *fileName);
void writeDb(MonsterList *monsters, char *fileName);

//———————————————————————–
// Main Program

int main() {
MonsterList monsters = {0, NULL};
char fileName[BUFFER_MAX];
char userIn[BUFFER_MAX];
char cmd;

printInfo();

int done = 0;
while (!done) {
printf(“n> “);
scanf(“%s”, userIn);
cmd = userIn[0];

switch (cmd) {
// TODO (1): ‘?’ command
// TODO (2): ‘r’ command
// TODO (3): ‘w’ command
// TODO (4): ‘d’ command
// TODO (5): ‘n’ command
// TODO (6): ‘h’ command
// TODO (7): ‘q’ command

default:
printf(“Unknown command `%c`”, cmd);
break;
}
}
return 0;
}

//———————————————————————–
// Function implementations

// TODO (8): void printInfo();
// TODO (9): void displayMonster(Monster *m);
// TODO (10): void displayMonsters(MonsterList *monsters);
// TODO (11): int swapNeededName(Monster *a, Monster *b);
// TODO (12): int swapNeededHitPoints(Monster *a, Monster*b);
// TODO (13): void sortMonsters(MonsterList *monsters, charsortType);
// TODO (14): void readDb(MonsterList *monsters, char*fileName);
// TODO (15): void writeDb(MonsterList *monsters, char*fileName);

Monster Database In this lab we re-visit our monsters, stats, and weapons but this time we are writing a C program. Furthermore, the data for the lair of monsters wl be read from, and written to, binary files. Program Structure The general structure of the program is to start by displaying a list of (single letter) commands that the user can invoke From there, a while loop will continually prompt the user for a command, execute it, and loop back for the next. We’ll use a switch block to create the logic for invoking the appropriate function(s) to complete each command Starter Code The started code contains: the required include statements . a couple of defines for char array sizes . a predefined array of pointers to strings (C-style) e to be displayed as user information the struct definitions for Weapon, Stats, Monster, and MonsterList .function prototypes for the required functions a partial implementation of the main function .TODO ( comments indicating where coding is needed The main0 function Required local variables are already defined and the basic structure of the while loop is already written. You will need to define case statements within the switch block, to execute the appropriate actions for the commands entered by the ? – print this list of commands Reprint the command list information. r-read monster database (binary file) Prompt for and read in a file name (“DB file nane (to read)”), then pass (by reference) both the monster list and the user-entered file name to the function to read the monster database. w- write monster database (binary file) If there are no monsters currently loaded, print the message no monsters loaded Otherwise, prompt for and read n a file name, (“DB file name “), then pass (by reference) both the (to write) : monster list and the user-entered file name to the function to write the monster database. d – display monsters If there are no monsters currently loaded, print the message no monsters loaded Otherwise, display the list of monsters. n – sort monsters by name (ascending) If there are no monsters currently loaded, print the message no monsters loaded Otherwise, sort the monsters by name (in ascending order), then immediately display them. h- sort monsters by hitpoints (descending) If there are no monsters currently loaded, print the message no monsters loaded Otherwise, sort the monsters by hitpoint values (in descending order), then immediately display them. q- quit Set the done flag, so that the while loop exits. Function Prototypes Utility Functions Functions to display information to the user void printinfo0: start by printing a newline . iterate over the strings in theinfo array, and print each one void displayMonster(Monster *m); print a single, formatted line displaying information about the given monster use the following format strng”%10s [a:td, tNd, hp:12d] <%-15s mod:%2d>n. o The numbers n front of the placeholders will specify field with. The negative in %-15s will make it left justified example output keleton [a:3, t:6, hp: 8 <long spear instead of the default right justified in %10s. mod: 1> void displayMonsters(MonsterList *monsters); Iterate ower the monsters in the list, to print them, one per line. Sorting Functions The Insertion Sort algorithm should be used to implement in-place sorting of monsters in the monster array. The ony difference between the two required sorts is in determining whether, given two monsters A and B, they need to be swapped or not. The next two functions serve to encapsulate just that determination int swapNeededName(Monster *a, Monster *b) This utility function should compare the names of the given monsters, and return true (1) if they should be swapped (as name “comes after b’s name), or false (o) otherwise. int swapNeededHitPoints(Monster *a, Monster *b); This utility function should compare the hitpoints of the given monsters, and return true (1) if they should be swapped (as hitpoints are fewer” than b’s hitpoints), or false (o) otherwise. void sortMonsters(MonsterList *monsters, char sortType); Implement the insertion sort on the array of monsters in the given MonsterList structure. The second parameter (sortType) will be the character ‘n’ if sorting by name is required, or ‘h’ if sorting by hitpoints is required Reading and Writing Binary Files void readDb(MonsterList *monsters, char fileName); attempt to open the given file for reading binary (mode: “rb”) .if failed to open, print a message and return. For example e Failed to open no-such-file.bin’ for read read a single integer from the file stream; this is how many monster structs are stored in the binary file attempt to allocate a block of memory big enough to hold the required number of monster structs . if the allocation fails, print a message and return. e read the required number of monsters from the file stream, into the allocated space. close the file. . . | Be sure to use the size and list fields of the given MonsterList struct as the destination for the reads. Some samples showing possible messages displayed failed read: DB file name to read: no-such-file.bin Failed to open ‘no-such-file.bin’ Eor read successful read: DB file name to read): rabblel.bin 1 items read from ‘rabblel.bin’ (size header) Allocating heap for 3 Monsters…nemory allocated 3 items read from rabblel.bin’ (nonster array . failed memory allocation: DB file name to read): rabblel.bin 1 items read from .rabble 1.bǐn’ くsize header) Allocating heap for 3 Monsters…failed to allocate memory! Notice the trick of using a printf string without a terminating newline, in the above examples the string Allocating heap for 3 Monsters…), so that the outcome of the operation can be written at the end of that same line, with the next printf statement. Your implementation will need to use the following library cals: fopeno fread malloco free0 fclose0 void writeDb(MonsterList *monsters, char fileName); attempt to open the given file for writing binary (mode “wb’) .if failed to open, print a message and return. For example to open bad-file.bin’ for write write a single integer to the file stream; this should be the value of the size field in the MonsterList struct write the monster array (the 1ist field in the MonsterList struct) to the file stream. . close the file. Sample output DB file name (to write): my-nonsters.bin 1 items ritten to ‘my-monsters.bin’ (size header) 3 items ritten to ‘my-monsters.bin'(nonster array Your implementation will need to use the following library cals: fopeno fwrite) fclose0 Hints 1) In your cals to fread and fwrite be sure to capture the retun value which tells you how many items were read, or written. 2) Don’t forget to cast the pointer returned from the malloc cal to the type (Monster, when you assign it to the list field of the Monstertist struct 3) Make sure you free previously allocated memory before overwriting the pointer with a new memory allocation 4) it is possible to combine the cases of the commands d n, and h with some carefully constructed logic. (This will reduce code duplication). A Sample Run A Sample Run Enter a command: print this list of commands read monster database (binary filel wwrite monster database (binary file) d -display monsters n-sort monsters by name (ascending) -sort monsters by hitpoints (descending) quit > d no monsters loaded) DB file name (to read): rabblel.bin 1 items read from rabblel.bin’ (size header) Allocating heap for 3 Monsters…memory allocated 3 items read from rabblel.bin’ (monster array > d Lizard [a:3, t:4, hp: 4] <spiked tail Dragon la:4, t:6, hp: 9] <fire breath Archer [a:5, t:2, hp: 2] <long bow mod: 2> mod: 3> mod: 1> Archer [a:5, t:2, hp: 21 <long bow Dragon la:4, t:6, hp: 9] <fire breath mod: 3> Lizard [a:3, t:4, hp: 4 <spiked tailmod: 2> mod: 1> > h Dragon [a:4, t:6, hp: 91 <fire breath mod: 3> Lizard a:3, t:4, hp: 41 <spiked tail mod: 2> Archer [a:5, t:2, hp: 2] <long bow mod: 1> DB file name (to write): mydb.bin 1 items ritten to ‘mydb.bin (size header) items written to ‘mydb.bin (monster array Show transcribed image text Monster Database In this lab we re-visit our monsters, stats, and weapons but this time we are writing a C program. Furthermore, the data for the lair of monsters wl be read from, and written to, binary files. Program Structure The general structure of the program is to start by displaying a list of (single letter) commands that the user can invoke From there, a while loop will continually prompt the user for a command, execute it, and loop back for the next. We’ll use a switch block to create the logic for invoking the appropriate function(s) to complete each command Starter Code The started code contains: the required include statements . a couple of defines for char array sizes . a predefined array of pointers to strings (C-style) e to be displayed as user information the struct definitions for Weapon, Stats, Monster, and MonsterList .function prototypes for the required functions a partial implementation of the main function .TODO ( comments indicating where coding is needed The main0 function Required local variables are already defined and the basic structure of the while loop is already written. You will need to define case statements within the switch block, to execute the appropriate actions for the commands entered by the ? – print this list of commands Reprint the command list information. r-read monster database (binary file) Prompt for and read in a file name (“DB file nane (to read)”), then pass (by reference) both the monster list and the user-entered file name to the function to read the monster database. w- write monster database (binary file) If there are no monsters currently loaded, print the message no monsters loaded Otherwise, prompt for and read n a file name, (“DB file name “), then pass (by reference) both the (to write) : monster list and the user-entered file name to the function to write the monster database. d – display monsters If there are no monsters currently loaded, print the message no monsters loaded Otherwise, display the list of monsters. n – sort monsters by name (ascending) If there are no monsters currently loaded, print the message no monsters loaded Otherwise, sort the monsters by name (in ascending order), then immediately display them. h- sort monsters by hitpoints (descending) If there are no monsters currently loaded, print the message
no monsters loaded Otherwise, sort the monsters by hitpoint values (in descending order), then immediately display them. q- quit Set the done flag, so that the while loop exits. Function Prototypes Utility Functions Functions to display information to the user void printinfo0: start by printing a newline . iterate over the strings in theinfo array, and print each one void displayMonster(Monster *m); print a single, formatted line displaying information about the given monster use the following format strng”%10s [a:td, tNd, hp:12d] n. o The numbers n front of the placeholders will specify field with. The negative in %-15s will make it left justified example output keleton [a:3, t:6, hp: 8 void displayMonsters(MonsterList *monsters); Iterate ower the monsters in the list, to print them, one per line. Sorting Functions The Insertion Sort algorithm should be used to implement in-place sorting of monsters in the monster array. The ony difference between the two required sorts is in determining whether, given two monsters A and B, they need to be swapped or not. The next two functions serve to encapsulate just that determination int swapNeededName(Monster *a, Monster *b) This utility function should compare the names of the given monsters, and return true (1) if they should be swapped (as name “comes after b’s name), or false (o) otherwise. int swapNeededHitPoints(Monster *a, Monster *b); This utility function should compare the hitpoints of the given monsters, and return true (1) if they should be swapped (as hitpoints are fewer” than b’s hitpoints), or false (o) otherwise. void sortMonsters(MonsterList *monsters, char sortType); Implement the insertion sort on the array of monsters in the given MonsterList structure. The second parameter (sortType) will be the character ‘n’ if sorting by name is required, or ‘h’ if sorting by hitpoints is required Reading and Writing Binary Files void readDb(MonsterList *monsters, char fileName); attempt to open the given file for reading binary (mode: “rb”) .if failed to open, print a message and return. For example e Failed to open no-such-file.bin’ for read read a single integer from the file stream; this is how many monster structs are stored in the binary file attempt to allocate a block of memory big enough to hold the required number of monster structs . if the allocation fails, print a message and return. e read the required number of monsters from the file stream, into the allocated space. close the file. . .
| Be sure to use the size and list fields of the given MonsterList struct as the destination for the reads. Some samples showing possible messages displayed failed read: DB file name to read: no-such-file.bin Failed to open ‘no-such-file.bin’ Eor read successful read: DB file name to read): rabblel.bin 1 items read from ‘rabblel.bin’ (size header) Allocating heap for 3 Monsters…nemory allocated 3 items read from rabblel.bin’ (nonster array . failed memory allocation: DB file name to read): rabblel.bin 1 items read from .rabble 1.bǐn’ くsize header) Allocating heap for 3 Monsters…failed to allocate memory! Notice the trick of using a printf string without a terminating newline, in the above examples the string Allocating heap for 3 Monsters…), so that the outcome of the operation can be written at the end of that same line, with the next printf statement. Your implementation will need to use the following library cals: fopeno fread malloco free0 fclose0 void writeDb(MonsterList *monsters, char fileName); attempt to open the given file for writing binary (mode “wb’) .if failed to open, print a message and return. For example to open bad-file.bin’ for write write a single integer to the file stream; this should be the value of the size field in the MonsterList struct write the monster array (the 1ist field in the MonsterList struct) to the file stream. . close the file. Sample output DB file name (to write): my-nonsters.bin 1 items ritten to ‘my-monsters.bin’ (size header) 3 items ritten to ‘my-monsters.bin'(nonster array Your implementation will need to use the following library cals: fopeno fwrite) fclose0 Hints 1) In your cals to fread and fwrite be sure to capture the retun value which tells you how many items were read, or written. 2) Don’t forget to cast the pointer returned from the malloc cal to the type (Monster, when you assign it to the list field of the Monstertist struct 3) Make sure you free previously allocated memory before overwriting the pointer with a new memory allocation 4) it is possible to combine the cases of the commands d n, and h with some carefully constructed logic. (This will reduce code duplication). A Sample Run
A Sample Run Enter a command: print this list of commands read monster database (binary filel wwrite monster database (binary file) d -display monsters n-sort monsters by name (ascending) -sort monsters by hitpoints (descending) quit > d no monsters loaded) DB file name (to read): rabblel.bin 1 items read from rabblel.bin’ (size header) Allocating heap for 3 Monsters…memory allocated 3 items read from rabblel.bin’ (monster array > d Lizard [a:3, t:4, hp: 4] Archer [a:5, t:2, hp: 21 > h Dragon [a:4, t:6, hp: 91 Lizard a:3, t:4, hp: 41 Archer [a:5, t:2, hp: 2] DB file name (to write): mydb.bin 1 items ritten to ‘mydb.bin (size header) items written to ‘mydb.bin (monster array

Expert Answer


Answer to c program just answer it even if it is not 100 % completed and i will give you two likes starter code: // ————-… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *