Computer Science Assignment - STructures

profilekritha
templates.zip

PA301/main.c

// TODO - Nothing! Do not modify this file #include <stdio.h> #include <stdlib.h> #include <string.h> extern void runAllTests(); int main() { printf("Welcome to the Simple Translation Dictionary!!!\n\n\n"); runAllTests(); return EXIT_SUCCESS; }

PA301/PA301.cbp

PA301/PA301.depend

# depslib dependency file v1.0
1301965052 source:c:\users\alessio\desktop\workbench\___\pa301\main.c
<stdio.h>
<stdlib.h>
<string.h>
1404255894 source:c:\users\alessio\desktop\workbench\___\pa301\tools.c
<stdlib.h>
<stdio.h>
<string.h>
tools.h
1404256047 c:\users\alessio\desktop\workbench\___\pa301\tools.h
1408393015 source:c:\users\alessiolpt\desktop\workbench\301\template\main.c
<stdio.h>
<stdlib.h>
<string.h>
1408393835 source:c:\users\alessiolpt\desktop\workbench\301\template\tests.c
<stdio.h>
<stdlib.h>
testlib.h
tools.h
1408418401 c:\users\alessiolpt\desktop\workbench\301\template\testlib.h
1404259647 c:\users\alessiolpt\desktop\workbench\301\template\tools.h
1408393385 source:c:\users\alessiolpt\desktop\workbench\301\template\tools.c
<stdlib.h>
<stdio.h>
<string.h>
tools.h
1408418377 source:c:\users\alessiolpt\desktop\workbench\301\template\testlib.c
<stdlib.h>
<stdio.h>
testlib.h
1408418655 source:c:\users\alessiolpt\desktop\workbench\ongoing stuff\301\pa301\tests.c
<stdio.h>
<stdlib.h>
testlib.h
tools.h
1408514693 c:\users\alessiolpt\desktop\workbench\ongoing stuff\301\pa301\testlib.h
1404259647 c:\users\alessiolpt\desktop\workbench\ongoing stuff\301\pa301\tools.h
1408393385 source:c:\users\alessiolpt\desktop\workbench\ongoing stuff\301\pa301\tools.c
<stdlib.h>
<stdio.h>
<string.h>
tools.h

PA301/PA301.layout

PA301/testlib.c

// TODO - nothing, do not modify this file #include <stdlib.h> #include <stdio.h> #include "testlib.h" void TEST(const char * const shortDescription, int boolexp){ // keep track of tests numbers static int testNumber = 0; testNumber++; // string representing the outcome of the test char* testResult = NULL; // determining the outcome if(boolexp){ testResult = "OK"; }else{ testResult = "FAILS"; } printf("[Test] #%3d --> %-5.5s\t%-50.50s\n", testNumber, testResult, shortDescription); fflush(stdout); }

PA301/testlib.h

// TODO - nothing, do not modify this file #ifndef TESTLIB_H_INCLUDED #define TESTLIB_H_INCLUDED void TEST(const char * const shortDescription, int boolexp); /* ROLE This function display information about the test being "OK" or "FAILED" based on whether the condition used as parameter is respectively true or false PARAMETERS shortDescription a string describing the test. 45 characters long maximum boolexp the result of evaluating a boolean expression representing what we are testing. If it evaluates to true, the function will display that the test succeeded. Otherwise, it will display that the test failed. */ #endif // TESTLIB_H_INCLUDED

PA301/tests.c

// TODO - Implement your tests in this file using testlib functions #include <stdio.h> #include <stdlib.h> #include "testlib.h" #include "tools.h" void test_example(){ // Example test function for you to adapt struct dictionary * d = NULL; d = dictionary_build(-3); TEST("Building with negative size should return NULL", d==NULL); } void runAllTests(){ /* ROLE Runs all the tests specified and displays results */ // For instance, you would have the following line in our example; // test_example(); }

PA301/tools.c

// TODO - Implement the functions in this file #include <stdlib.h> #include <stdio.h> #include <string.h> #include "tools.h" struct dictionary *dictionary_build(int size){ return NULL; } int dictionary_add(struct dictionary* d, const char * const english, const char * const foreign){ return -1; } int dictionary_free( struct dictionary ** p){ return -1; } char* dictionary_translate( struct dictionary* d, const char* const english_word, const char* const foreign_word){ return NULL; }

PA301/tools.h

// TODO - nothing, do not modify this file #ifndef _TOOLS_H_ #define _TOOLS_H_ /* Data Structures Definitions */ struct wordPair { char* englishWord; char* foreignWord; }; struct dictionary { struct wordPair ** data; int nbwords; int size; }; /* Prototypes of functions */ struct dictionary* dictionary_build(int size); /* ROLE Allocate and initialize a new dictionary structure able to accommodate a number of pairs of words specified by the size parameter RETURNS Address of new dictionary, if allocation was successful. NULL otherwise PARAMETERS The size of the dictionary to make */ int dictionary_add(struct dictionary* d, const char * const english, const char * const foreign); /* ROLE Adds a new wordPair made of strdup copies of the parameter strings to a dictionary d RETURNS 0 if everything went fine -1 if the dictionary or the field inside of it used to store word pairs are NULL -2 if we are unable to allocate a new wordPair -3 if the English word is missing, i.e. English parameter is NULL -4 if the foreign word is missing, i.e. foreign parameter is NULL -5 if the dictionary is already full -6 for any other problem you are able to detect - if any PARAMETERS d the dictionary to work with English string representing the English part of the new wordPair foreign string representing the foreign part of the new wordPair */ char* dictionary_translate(struct dictionary* d, const char* const english, const char* const foreign); /* ROLE Looks up the dictionary d for the translation of a word depending on what parameters are provided. RETURNS If English is non NULL and foreign is NULL, return a strdup of the translation of English in d if foreign is non NULL and English is NULL, return a strdup of the translation of foreign in d If anything goes wrong, return NULL PARAMETERS d Address of the dictionary to work with English string representing the English word to translate, if any foreign string representing the foreign work to translate, if any REMARKS words are not in any specific order, do not assume an order when searching in this "dictionary", you'll have to look up every entry against the words you are looking up */ int dictionary_free(struct dictionary** p); /* ROLE Deallocate all memory which has been dynamically allocated in the dictionary structure pointed to by *p. Then set the pointer which address was passed as parameter to NULL. RETURNS 0 if everything went fine -1 if we encountered some NULL pointers where we should have encountered non-NULL pointers PARAMETERS Pointer on a pointer on our dictionary REMARKS Please note that if a pointer has a wrong address, we have no way to detect this. We will just deallocate it and potentially trigger a bug. */ #endif