Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this please. Here are my code files. main.c ---------------------------------------------------------------------------- #include #include #includemy_string.h int main(int argc, char* argv[]) { MY_STRING hMy_string=NULL; MY_STRING

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

I need help with this please. Here are my code files.

main.c ---------------------------------------------------------------------------- #include #include #include"my_string.h"

int main(int argc, char* argv[]) { MY_STRING hMy_string=NULL; MY_STRING hMy_string2=NULL;

char item= 'r'; char item2= 's'; char item3= 'q'; char item4= 'p'; int n;

hMy_string=my_string_init_default();

my_string_push_back(hMy_string, item); my_string_push_back(hMy_string, item2); my_string_push_back(hMy_string, item3);

printf("The string is cuurently: %s ", my_string_at(hMy_string, 0)); my_string_pop_back(hMy_string); n=my_string_empty(hMy_string);

if(n==1) { printf("The string is empty! "); } else { printf("The string is not empty! "); }

hMy_string2=my_string_init_default(); my_string_push_back(hMy_string2, item4);

my_string_concat(hMy_string,hMy_string2);

my_string_destroy(&hMy_string); my_string_destroy(&hMy_string2);

return 0;

} ----------------------------------------------------------------------------------------------- my_string.c ------------------------------------------------------------ #include #include #include #include "my_string.h"

struct STRING { int size; int capacity; char* ch; }; typedef struct STRING String;

MY_STRING my_string_init_default(void) { String* pStr = (String*) malloc(sizeof(String)); if (pStr != NULL) { pStr->size = 0; pStr->capacity = 7; pStr->ch = (char*)malloc(sizeof(char) * pStr->capacity); if (pStr->ch == NULL) { free(pStr); pStr = NULL; } } return (MY_STRING)pStr; }

void my_string_destroy(MY_STRING* phMy_string) { String* pStr = (String*) *phMy_string;

free(pStr->ch); free(pStr); *phMy_string = NULL; }

MY_STRING my_string_init_c_string(const char* c_string) { int i = 0; String* pStr = (String*) malloc(sizeof(String)); if (pStr != NULL) { while (c_string[i] !='\0') { i++; } pStr->size = i; pStr->capacity = pStr->size + 1; pStr->ch = (char*)malloc(sizeof(char) * pStr->capacity); if (pStr->ch == NULL) { free(pStr); pStr = NULL; } } return (MY_STRING)pStr; }

int my_string_get_capacity(MY_STRING hMy_string) { String* pStr = (String*)hMy_string; return pStr->capacity; }

int my_string_get_size(MY_STRING hMy_string) { String* pStr = (String*)hMy_string; return pStr->size; }

int my_string_compare(MY_STRING hLeft_string, MY_STRING hRight_string) { String* pLstr = (String*)hLeft_string; String* pRstr = (String*)hRight_string; return (pLstr->size - pRstr->size); }

Status my_string_extraction(MY_STRING hMy_string, FILE* fp) { int i; char* temp; String* pStr = (String*) hMy_string; int numOfCon = 0;

if (!feof(fp)) { numOfCon = fscanf(fp, "%s", pStr->ch); } if(pStr->size >= pStr->capacity) { temp = (char*) malloc(sizeof(char)* pStr->capacity * 2); if(temp == NULL) { return FAIL; } for(i = 0; i size; i++) { temp[i] = pStr->ch[i]; } free(pStr->ch); pStr->ch = temp; pStr->capacity *= 2; }

if (numOfCon == 0 || numOfCon == -1) { return FAIL; } else { return SUCC; } }

Status my_string_insertion(MY_STRING hMy_string, FILE* fp) { String* pStr = (String*)hMy_string; int x = 29;

if(strlen(pStr->ch) == x) { fprintf(fp, "%s", pStr->ch); } if(fgetc(fp) == 0) { return FAIL; } else { return SUCC; } }

Status my_string_push_back(MY_STRING hMy_string, char item) { int i; char* temp; String* pStr = (String*)hMy_string;

if(pStr->size >= pStr->capacity) { temp = (char*) malloc(sizeof(char)* pStr->capacity * 2); if(temp == NULL) { return FAIL; } for(i = 0; i size; i++) { temp[i] = pStr->ch[i]; } free(pStr->ch); pStr->ch = temp; pStr->capacity *= 2; } pStr->ch[pStr->size] = item; pStr->size++; return SUCC; }

Status my_string_pop_back(MY_STRING hMy_string) { String* pStr = (String*)hMy_string;

if(pStr->size == 0) { return FAIL; } pStr->size--; return SUCC; }

char* my_string_at(MY_STRING hMy_string, int index) { String* pStr = (String*)hMy_string;

if(index >= pStr->size) { return NULL; } return &(pStr->ch[index]); }

Status my_string_concat(MY_STRING hResult, MY_STRING hAppend) { int i, a; char* temp; String* pResult = (String*)hResult; String* pAppend = (String*)hAppend; int b = pResult->size;

if((pResult->size + pAppend->size) >= pResult->capacity) { temp = (char*) malloc(sizeof(char)* pResult->capacity * 2); if(temp == NULL) { return FAIL; } for(i = 0; i size; i++) { temp[i] = pResult->ch[i]; } free(pResult->ch); pResult->ch = temp; pResult->capacity *= 2; } for(i = 0; i size); i++) { pResult->ch[i + pResult->size] = pAppend->ch[i]; pResult->size++; } a = pResult->size;

if (a > b) { return SUCC; } else { return FAIL; } }

Boolean my_string_empty(MY_STRING hMy_string) { String* pStr = (String*)hMy_string;

if (pStr->size == 0) { return TRUE; } else { return FALSE; } } ------------------------------------------------------------------------------------------------ my_string.h ------------------------------------------------------------ #include #include "status.h"

typedef void* MY_STRING;

MY_STRING my_string_init_default(void);

void my_string_destroy(MY_STRING* phMy_string);

MY_STRING my_string_init_c_string(const char* c_string);

int my_string_get_capacity(MY_STRING hMy_string);

int my_string_get_size(MY_STRING hMy_string);

int my_string_compare(MY_STRING hLeft_string, MY_STRING hRight_string);

Status my_string_extraction(MY_STRING hMy_string, FILE* fp);

Status my_string_insertion(MY_STRING hMy_string, FILE* fp);

Status my_string_push_back(MY_STRING hMy_string, char item);

Status my_string_pop_back(MY_STRING hMy_string);

char* my_string_at(MY_STRING hMy_string, int index);

Status my_string_concat(MY_STRING hResult, MY_STRING hAppend);

Boolean my_string_empty(MY_STRING hMy_string); ----------------------------------------------------------------------------------------------------------- status.h -------------------------------------------------- #ifndef STATUS_H #define STATUS_H

enum status {FAIL, SUCC}; typedef enum status Status;

enum boolean {FALSE, TRUE}; typedef enum boolean Boolean;

#endif

COMP 1020 Lab-5 Fall 2017 Evil Hangman This lab assumes that you have completed all steps involved in labs 1 through 4. Switch to your HANGMAN directory: cd /Spring 2017/COMP1020/HANGMAN Clean up your directory by typing make clean Add the following three empty files to your directory: unit_test.c, unit_test.h, and test_def.c Modify your Makefile so that you add a new target called unit_test that will build a new executable that we can use to test our string data structure. Feel free to use other variables in your macro and remember to modify your clean target so that it can clean up the space for these new files. You will also need to add target lines for the new .o files: unit_test.o and test_def.o unit_test: my_string.o unit_test.o test_def.o $(CC) $(CFLAGS) -o unit_test unit_test.o test_def.o my_string.o We begin by building a framework for testing our code. We are going to create a main program in unit_test.c that will initialize an array of function pointers where every function pointer will hold the address of a test function which has the following signature: Status long_function_name(char* buffer, int length); The idea is to write a program that will automatically run all of our test functions and report on their success or failure. As we write more functions to test our code this unit will become more and more useful to us. The following is a sample main program for unit_test.c that places two functions in the array of function pointers to be tested. COMP 1020 Lab-5 Fall 2017 #include #include "unit_test.h" int main(int argc, char* argv[]) Status ("tests[])(char*, int) = test_init_default_returns_nonNULL, test_get_size_on_init_default_returns_0 int number_of_functions = sizeof(tests) / sizeof(tests[0]); int i; char buffer(500); int success_count = 0; int failure_count = 0; for(i=0; i #include "unit_test.h" int main(int argc, char* argv[]) Status ("tests[])(char*, int) = test_init_default_returns_nonNULL, test_get_size_on_init_default_returns_0 int number_of_functions = sizeof(tests) / sizeof(tests[0]); int i; char buffer(500); int success_count = 0; int failure_count = 0; for(i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions

Question

What would you do if the bullies were in your classes?

Answered: 1 week ago

Question

e. What are notable achievements of the group?

Answered: 1 week ago