Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LINES 4096
char red;
char* reverse;
/* The function to split the input into a sentence per line */
int split_by_sentence(void)
{
int character, file_character=0;
char buffer[1024];
FILE *book=fopen("readtext1.txt", "r");
FILE *book2=fopen("readtext.txt", "a+");
if(!book) {printf("Error: unable to open input file!\n"); return 1;}
if(!book2) {printf("Error: unable to open output file!\n"); return 1;}
while(file_character!=EOF)
{
buffer[0]='\0';
for(character=0;character<sizeof(buffer);character++)
{
file_character=fgetc(book);
if(file_character==EOF)
break;
if(file_character=='.')
{
buffer[character]='\0';
break;
}
if(file_character=='?')
{
buffer[character]='\0';
break;
}
if(file_character=='!')
{
buffer[character]='\0';
break;
}
buffer[character]=file_character;
}
if(file_character==EOF)
break;
fprintf(book2, "%s.\n", buffer);
}
fclose(book);
fclose(book2);
putchar('\n');
return 0;
}
/* The function to find the first letter in the word */
char find_letter (char* a, char* b)
{
char string = strlen(b);
strncpy (a,b,1);
a[1]='\0';
return 0;
}
/* The function to reverse the characters in a string */
char* rev(char* str)
{
int end= strlen(str)-1;
int start = 0;
while( start<end )
{
char temp = str[ start ];
str[ start ] = str[ end ];
str[ end ] = temp;
++start;
--end;
}
return str;
}
/* The function for percentage calculation */
void percentage_calculation_numbers(char* a_pch, char* a_b)
{
FILE *fp;
char str2[7];
char vowels[] = "aeiouyAEIOUY";
char letters[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int count_point = 0;
int count_shoot = 0;
int count_reload = 0;
int count_autofire = 0;
float one;
float two;
float three;
float four;
float total;
float five;
float six;
float seven;
float eight;
float total_2;
float percentage_1;
float percentage_2;
float percentage_3;
float percentage_4;
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
five = 0;
six = 0;
seven = 0;
eight = 0;
total_2 = 0;
percentage_1 = 0;
percentage_2 = 0;
percentage_3 = 0;
percentage_4 = 0;
/* open text file or report error */
fp = fopen("writelist.txt", "a+");
if((fp = fopen("writelist.txt", "a+"))==NULL)
{
printf("Cannot open file.\n");
exit(1);
}
/*This printf and fprintf isn't needed while I create the code function, so I'll comment it out
printf("%s\n\n", a_pch);
fprintf(fp, "%s\n\n", a_pch);*/
/* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
while(a_pch != NULL)
{
/* identify the first letter in the word begin */
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
one++;
}
if(strpbrk(str2, vowels))
{
two++;
}
/* identify the first letter in the word end */
/* identify the last letter in the word begin */
reverse = rev(a_pch);
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
three++;
}
if(strpbrk(str2, vowels))
{
four++;
}
/* identify the last letter in the word end */
/* The math using the first and last letter begin */
/* pch is backwards, so I use rev to reverse it back */
/*printf("%s uncomment to test to see if the string is backwards", pch);*/
reverse = rev(a_pch);
if(total = (one && three))
{
count_point++;
five++;
printf("%s point %d\n", a_pch, count_point);
fprintf(fp, "%s point %d\n", a_pch, count_point);
}
else if(total = (one && four))
{
count_shoot++;
six++;
printf("%s shoot %d\n", a_pch, count_shoot);
fprintf(fp, "%s shoot %d\n", a_pch, count_shoot);
}
else if(total = (two && three))
{
count_reload++;
seven++;
printf("%s reload %d\n", a_pch, count_reload);
fprintf(fp, "%s reload %d\n", a_pch, count_reload);
}
else if(total = (two && four))
{
count_autofire++;
eight++;
printf("%s autofire %d\n", a_pch, count_autofire);
fprintf(fp, "%s autofire %d\n", a_pch, count_autofire);
}
/* The math using the first and last letter end */
/* Reset the pointers */
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
a_pch = strtok (NULL, " .");
}
/* Add separation between lines in text file so sentences can be identified */
putchar('\n');
fputc ('\n', fp);
/* pch is NULL, so I find alphabet in b and pass that into pch */
a_pch = strpbrk(a_b, alphabet);
/* I'm commenting out the three secotions below that make the percentage because I don't know how to use it effectively yet */
/* Count the pointers */
/*total_2 = (five + six + seven + eight);
percentage_1 = ((five)/ total_2);
percentage_2 = ((six)/ total_2);
percentage_3 = ((seven)/ total_2);
percentage_4 = ((eight)/ total_2);*/
/* Display the results in the console window */
/*printf("\n\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n",
percentage_1, percentage_2, percentage_3, percentage_4);*/
/* Write the pointers result to file */
/*fprintf(fp, "\n\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n",
percentage_1, percentage_2, percentage_3, percentage_4);*/
fclose(fp);
}
/* function to find pattern, useful to identify how the words parallel a syllogism */
int find_pattern(void)
{
int count_one = 0, count_two = 0, count_three = 0, count_four = 0;
int count_five = 0, count_six = 0, count_seven = 0, count_eight = 0;
char buffer[LINES];
char point_1 [300];
char point_2 [300];
char shoot_1 [300];
char shoot_2 [300];
char shoot_3 [300];
char reload_1 [300];
char reload_2 [300];
char autofire_1 [300];
char autofire_2 [300];
FILE *book=fopen("writelist.txt", "r");
if(!book) {printf("Error: unable to open input file!\n"); return 1;}
while(fgets(buffer, sizeof(buffer), book)!=NULL)
{
if (strstr(buffer, "point 1"))
{
strcpy(point_1, buffer);
}
if (strstr(buffer, "point 2"))
{
strcpy(point_2, buffer);
}
if (strstr(buffer, "shoot 1"))
{
strcpy(shoot_1, buffer);
}
if (strstr(buffer, "shoot 2"))
{
strcpy(shoot_2, buffer);
}
if (strstr(buffer, "shoot 3"))
{
strcpy(shoot_3, buffer);
}
if (strstr(buffer, "reload 1"))
{
strcpy(reload_1, buffer);
}
if (strstr(buffer, "reload 2"))
{
strcpy(reload_2, buffer);
}
if (strstr(buffer, "autofire 1"))
{
strcpy(autofire_1, buffer);
}
if (strstr(buffer, "autofire 2"))
{
strcpy(autofire_2, buffer);
}
}
if(strstr(reload_1, "reload 1"))
{
if(strstr(point_1, "point 1"))
{
if(strstr(autofire_1, "autofire 1"))
{
if(strstr(point_2, "point 2"))
{
count_one++;
}
}
}
}
if(strstr(shoot_1, "shoot 1"))
{
if(strstr(point_1, "point 1"))
{
if(strstr(reload_1, "reload 1"))
{
if(strstr(point_2, "point 2"))
{
count_two++;
}
}
}
}
fclose(book);
printf("one %d\n", count_one);
printf("two %d\n", count_two);
/* The syllogism uses the scope twice, so if the count equals 2 there is a syllogism pattern */
if(count_one > count_two)
{
printf("Type Darapti (AAI-3)_or_Barbari (AAI-1)_or_Barbara (AAA-1) Syllogism pattern found\n\n");
}
if(count_two > count_one)
{
printf("Type Ferio (EIO-1) Syllogism pattern found\n\n");
}
return 0;
}
/* function so I don't have to type getch all over the place */
void MyExit(void) { system("pause"); }
/* the main program */
int main ()
{
/* declaring and initiaizing variables */
FILE *book;
char * pch = malloc(300);
char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer[LINES];
/* The function is called so I don't have to type getch */
atexit(MyExit);
/* The function is called and creates the text file */
split_by_sentence();
/* The text file just created is opened */
book = fopen("readtext.txt", "r");
if(!book)
{
perror("Error: file readtext.txt was not found or opened");
return 0;
}
/* read from file */
while(fgets(buffer, sizeof(buffer), book)!=NULL)
{
/* I tokenize the input string into individual words */
pch = strtok (buffer, " ~!@#$%^&*()_+`1234567890-=[]\'\"/;/|,./{}:<>?");
/* I find the alphabet in pch after it's run through strtok */
if(strpbrk(pch, alphabet))
{
/* I run the percentage function which also creates the new terms for the words processed */
percentage_calculation_numbers(pch, buffer);
}
}
fclose(book);
find_pattern();
return 0;
}
Here is the current code: