Refactoring and bug fixes
This commit is contained in:
parent
bf36f1a5c7
commit
5eadaf7f83
1 changed files with 61 additions and 39 deletions
100
Aufgabe 1/main.c
100
Aufgabe 1/main.c
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#define SIZEOFARR(a) (sizeof(a)/sizeof(a[0]))
|
||||
|
||||
char allowedChars[] = "aäbcdefghijklmnoöpqrstuüvwxyzAÄBCDEFGHIJKLMNOÖPQRSTUÜVWXYZß1234567890 \n";
|
||||
char allowed_chars[] = "aäbcdefghijklmnoöpqrstuüvwxyzAÄBCDEFGHIJKLMNOÖPQRSTUÜVWXYZß1234567890 \n";
|
||||
|
||||
unsigned short int arrContains(char checkForVal, char *checkInArr, int arrSize)
|
||||
{
|
||||
|
|
@ -25,17 +25,10 @@ struct Gap_sentence
|
|||
short unsigned int length;
|
||||
};
|
||||
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char words[30000][20];
|
||||
FILE *fp;
|
||||
FILE *fpp;
|
||||
fpp = fopen("/home/xor/coding/BwInf_41_1/Aufgabe 1/debug.txt","w");
|
||||
fp = fopen("/home/xor/coding/BwInf_41_1/Aufgabe 1/Alice_im_Wunderland.txt", "r");
|
||||
//fp = fopen("/home/xor/Dokumente/Coding/BwInf_41_1/Aufgabe 1/test.txt", "r");
|
||||
if(fp == NULL||fpp==NULL)
|
||||
int import_text(char path[],int max_chars,char words[][max_chars]){
|
||||
FILE *text;
|
||||
text = fopen(path, "r");
|
||||
if(text == NULL)
|
||||
{
|
||||
printf("Datei konnte nicht geoeffnet werden.\n");
|
||||
return -1;
|
||||
|
|
@ -46,12 +39,12 @@ int main(int argc, char const *argv[])
|
|||
int character_index = 0;
|
||||
int word_found = 0; //checks if multiple spaces are behind each other, we don't want to add a new word
|
||||
while(1){
|
||||
char curr_char = fgetc(fp);
|
||||
char curr_char = fgetc(text);
|
||||
if(curr_char==EOF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (!arrContains(curr_char,allowedChars,SIZEOFARR(allowedChars)))
|
||||
if (!arrContains(curr_char,allowed_chars,SIZEOFARR(allowed_chars)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
@ -72,14 +65,16 @@ int main(int argc, char const *argv[])
|
|||
}
|
||||
|
||||
}
|
||||
fclose(text);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Get sentence with gap
|
||||
int import_gap_sentence(char path[],struct Gap_sentence *gap_sentence){
|
||||
FILE *gap_file;
|
||||
struct Gap_sentence gap_sentence;
|
||||
gap_file = fopen("/home/xor/coding/BwInf_41_1/Aufgabe 1/stoerung0.txt","r");
|
||||
gap_file = fopen(path,"r");
|
||||
|
||||
character_index = 0;
|
||||
word_found = 0;
|
||||
int character_index = 0;
|
||||
int word_found = 0;
|
||||
short int file_read = 0;
|
||||
while(!file_read){
|
||||
char curr_char = fgetc(gap_file);
|
||||
|
|
@ -94,70 +89,97 @@ int main(int argc, char const *argv[])
|
|||
the last underscores were counted (and therefore word_index was not increased)*/
|
||||
if(word_found || *gap_file->_IO_read_ptr != '_')
|
||||
{
|
||||
gap_sentence.length++;
|
||||
gap_sentence->length++;
|
||||
word_found=0;
|
||||
character_index = 0;
|
||||
}
|
||||
break;
|
||||
case '_':
|
||||
gap_sentence.gapsizes[gap_sentence.length]++;
|
||||
gap_sentence->gapsizes[gap_sentence->length]++;
|
||||
break;
|
||||
default:
|
||||
word_found = 1;
|
||||
gap_sentence.words[gap_sentence.length][character_index] = curr_char;
|
||||
gap_sentence->words[gap_sentence->length][character_index] = curr_char;
|
||||
character_index++;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
//After the last word, there is no space, so weed need to add one length
|
||||
gap_sentence.length++;
|
||||
gap_sentence->length++;
|
||||
fclose(gap_file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int occurance_index = 0;
|
||||
int sentence_occurances[100];
|
||||
int sentence_occurance_index = 0;
|
||||
int get_matching_sentences(int text_occurances[],struct Gap_sentence *gap_sentence,int max_words, int max_chars, char words[][max_chars]){
|
||||
unsigned int possibleMatch_index = 0;
|
||||
int text_occurance_count= 0;
|
||||
|
||||
while(occurance_index<sizeof(words)/sizeof(words[0]))
|
||||
while(possibleMatch_index<max_words)
|
||||
{
|
||||
//find next occurance of first word
|
||||
while(1){
|
||||
if(utf8casecmp(gap_sentence.words[0],words[occurance_index])==0){
|
||||
if(utf8casecmp(gap_sentence->words[0],words[possibleMatch_index])==0){
|
||||
break;
|
||||
}
|
||||
if(occurance_index>sizeof(words)/sizeof(words[0])){
|
||||
if(possibleMatch_index>max_words){
|
||||
break;
|
||||
}
|
||||
occurance_index++;
|
||||
possibleMatch_index++;
|
||||
}
|
||||
|
||||
for(int i = 1; i<gap_sentence.length;i++)
|
||||
for(int i = 1; i<gap_sentence->length;i++)
|
||||
{
|
||||
if(gap_sentence.gapsizes[i]==0)
|
||||
//If we are looking at a word
|
||||
if(gap_sentence->gapsizes[i]==0)
|
||||
{
|
||||
if(utf8casecmp(gap_sentence.words[i],words[i+occurance_index])!=0)
|
||||
if(utf8casecmp(gap_sentence->words[i],words[i+possibleMatch_index])!=0)
|
||||
{
|
||||
goto nomatch;
|
||||
}
|
||||
}
|
||||
//If we are looking at a gap lenght
|
||||
else{
|
||||
// -1 because i gets increased by 1 after 'continue'
|
||||
i += gap_sentence.gapsizes[i]-1;
|
||||
i += gap_sentence->gapsizes[i]-1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
sentence_occurances[sentence_occurance_index] = occurance_index;
|
||||
sentence_occurance_index++;
|
||||
|
||||
text_occurances[text_occurance_count] = possibleMatch_index;
|
||||
text_occurance_count++;
|
||||
nomatch:
|
||||
//We know the current first word to be matching, so no need to check it again
|
||||
occurance_index++;
|
||||
possibleMatch_index++;
|
||||
|
||||
}
|
||||
return text_occurance_count-1;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char words[30000][20];
|
||||
if(import_text("/home/xor/coding/BwInf_41_1/Aufgabe 1/Alice_im_Wunderland.txt",sizeof(words[0])/sizeof(words[0][0]),words)!=0){
|
||||
printf("Error importing Text\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct Gap_sentence gap_sentence;
|
||||
if(import_gap_sentence("/home/xor/coding/BwInf_41_1/Aufgabe 1/stoerung0.txt",&gap_sentence)!=0){
|
||||
printf("Error importing stoerung\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(int j = 0; j<=sentence_occurance_index;j++)
|
||||
|
||||
int text_occurances[100];
|
||||
int text_occurance_count = get_matching_sentences(text_occurances,&gap_sentence,sizeof(words)/sizeof(words[0]),sizeof(words[0])/sizeof(words[0][0]),words);
|
||||
|
||||
|
||||
|
||||
for(int j = 0; j<=text_occurance_count;j++)
|
||||
{
|
||||
for (int i=0;i<gap_sentence.length;i++){
|
||||
printf("%s ",words[sentence_occurances[j]+i]);
|
||||
printf("%s ",words[text_occurances[j]+i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue