Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define N 17
- #define M 30000
- char DEVELOPMENT[] = "OFF";
- struct List
- {
- char* word;
- struct List* next;
- };
- typedef struct List list;
- typedef list* list_ptr;
- list * createnode(char* ch);
- void replace(char a[N], char old_a[N]);
- void add_to_dic(FILE* dic, char word[N]);
- void correct(int length, int length_of_dic, char a[M][N], list_ptr*, FILE*, FILE*);
- void save_statistics(int diff, int legnthWords, int lengthChars, int spaces, int lengthofwords[]);
- void histogram(char a[M][N], int lengthofwords[], int length);
- char getchoice();
- void cleanString(char []);
- void read(char a[M][N], int*, int*, FILE*, char raw_a[M][N]);
- void loaddict(FILE*, list_ptr*, int*);
- void stats(char a[M][N], int, int);
- void legend();
- int main() {
- FILE* input;
- list_ptr dicofwords;
- FILE* dic;
- char choice, text[M][N], raw_text[M][N];
- int length = 0, spaces, length_of_dic;
- int i;
- int* ptr_length;
- int* ptr_spaces;
- int* ptr_lod;
- input = NULL;
- dic = NULL;
- dicofwords = NULL;
- ptr_lod = &length_of_dic;
- ptr_spaces = &spaces;
- ptr_length = &length;
- printf("The infamous 'I lost three weeks for a 7' word and text editor v2_5b\nPlease follow the on-screen instructions.\n\n");
- legend();
- while(1)
- {
- putchar('\n');
- printf("CHOOSE AN ACTION: ");
- choice = getchoice();
- printf("---------------------------------------------------\n");
- switch (choice)
- {
- case 'r':
- read(text, ptr_length, ptr_spaces, input, raw_text);
- break;
- case 'd':
- printf("(LOAD DIC MODE[ON]):~ INITIATING LOAD DICTIONARY MODE\n");
- loaddict(dic, &dicofwords, ptr_lod);
- printf("(LOAD DIC MODE[ON]):~ ALL WORDS SUCCESSFULLY LOADED\n");
- printf("(LOAD DIC MODE[OFF]):~ ...\n");
- break;
- case 'c':
- correct(length, length_of_dic, raw_text, &dicofwords, dic, input);
- break;
- case 's':
- stats(text, length, spaces);
- break;
- case 'l':
- legend();
- break;
- case 'e':
- return 0;
- break;
- case 'z':
- for (i=0;i<length && strcpy(DEVELOPMENT,"on") == 0;i++)
- {
- printf("%d = %s\n",i ,text[i]);
- }
- for (i=0;i<*ptr_lod && strcpy(DEVELOPMENT, "on") == 0;i++)
- {
- /*printf("%s => added\n", dictionary[i]);
- */}
- default:
- printf("\n*Wrong input*\n");
- break;
- }
- printf("---------------------------------------------------\n");
- }
- }
- char getchoice()
- {
- char choice;
- scanf(" %c", &choice);
- return choice;
- }
- void read(char a[M][N], int* ptr_length, int* ptr_spaces, FILE* input, char raw_a[M][N])
- {
- char ch;
- int i, length, spaces = 0;
- *ptr_spaces = 0;
- input = fopen("input.txt", "r");
- printf("(READ TEXT MODE[ON]):~ INITIATING READ TEXT MODE\n");
- /*Search for spaces*/
- while ((ch = fgetc(input)) != EOF)
- {
- if (ch == ' ')
- {
- spaces++;
- }
- }
- *ptr_spaces = spaces;
- fseek(input, 0, SEEK_SET);
- /**/
- for (i=0; feof(input) == 0; i++)
- {
- fscanf(input, "%s", a[i]);
- if (ferror(input) != 0)
- {
- printf("ERROR. EXITING READ MODE");
- return;
- }
- }
- length = i;
- if (strcpy(DEVELOPMENT, "on")==0)
- printf("the length is %d", length);
- else
- printf("(READ TEXT MODE[ON]):~ LOADING TEXT...\n");
- if (strcmp(DEVELOPMENT, "ON") == 0)
- {
- for (i=0; i<length; i++)
- {
- printf("%s\n", a[i]);
- }
- }
- printf("(READ TEXT MODE[OFF]...):~ SUCCESSFULLY READ THE TEXT\n");
- *ptr_length = length;
- for (i=0; i<length; i++)
- {
- strcpy(raw_a[i], a[i]);
- }
- fclose(input);
- return;
- }
- list * createnode(char* ch) {
- list_ptr newnode_ptr ;
- newnode_ptr = malloc(sizeof (list));
- newnode_ptr -> word = malloc(sizeof(strlen(ch)+1)*sizeof(char));
- strcpy(newnode_ptr->word, ch);
- newnode_ptr -> next = NULL;
- return newnode_ptr;
- }
- void loaddict(FILE* dic, list_ptr* words, int* ptr)
- {
- int length = 0;
- int num = 0;
- char current_word[M+1] = "";
- list_ptr iterator;
- list_ptr newnode;
- fseek(dic, 0, SEEK_SET);
- iterator = *words;
- dic = fopen("dic.txt", "r");
- if (ferror(dic) != 0)
- {
- printf("(LOAD DIC MODE[ON]):~ ERROR! NO DICTIONARY FOUND. [ABSENCE OF .txt DIC FILE.]\n(LOAD DIC MODE[OFF]):~ ...\n");
- }
- fscanf(dic, "%s", current_word);
- newnode = createnode(current_word);
- iterator = newnode;
- *words = newnode;
- num++;
- while ((fscanf(dic, "%s", current_word)) != EOF) {
- num++;
- length += strlen(current_word);
- newnode = createnode(current_word);
- iterator->next = newnode;
- iterator = iterator->next;
- }
- *ptr = num;
- fclose(dic);
- return;
- }
- void add_to_dic(FILE* dic, char word[N])
- {
- dic = fopen("dic.txt", "r+");
- fseek(dic, 0, SEEK_END);
- fprintf(dic, "\n%s\n", word);
- fclose(dic);
- return;
- }
- void correct(int length, int length_of_dic, char a[M][N], list_ptr *dicofwords, FILE* dic, FILE* input)
- {
- int i;
- list_ptr iterator = *dicofwords;
- char local_old[M][N];
- char decision;
- for (i=0; i<length;i++)
- {
- strcpy(local_old[i], a[i]);
- cleanString(a[i]);
- strlwr(a[i]);
- }
- printf("(CORRECTION MODE [ON]):~ INITIATING CORRECTION MODE\n");
- for (i=0;i<length;i++)
- {
- loaddict(dic, dicofwords, &length_of_dic);
- iterator = *dicofwords;
- if (length_of_dic != 0) while (iterator!=NULL)
- {
- if (strcmp(iterator->word, a[i]) == 0)
- {
- printf("the word %s is in the dictionary.\n", a[i]);
- iterator = iterator -> next;
- break;
- }
- else if (iterator->next == NULL)
- {
- printf("The word %s is not in the dictionary. [Replace the word (press a), add to dic (press b), continue (press c), exit the correction mode (press e)]\n", a[i]);
- scanf(" %c", &decision);
- switch (decision)
- {
- case 'a':
- replace(a[i], local_old[i]);
- add_to_dic(dic, a[i]);
- break;
- case 'b':
- add_to_dic(dic, a[i]);
- break;
- case 'c':
- break;
- case 'e':
- printf("(CORRECTION MODE [OFF]):~ ...\n");
- return;
- default:
- break;
- }
- }
- iterator = iterator -> next;
- }
- else
- {
- printf("The word %s is not in the dictionary. Please decide what to do \n[Replace the word (press a), add to dic (press b), continue (press c), exit the correction mode (press e)]: \n\n", a[i]);
- }
- }
- /*
- printf("\t\tWord \"%s\" is not included in the dictionary.\nPlease decide what to do to [Replace the word (press a), add to dic (press b), continue (press c), exit the correction mode (press e)]\n", a[i]);
- scanf(" %c", &decision);
- switch (decision)
- {
- case 'a':
- replace(a[i], local_old[i]);
- add_to_dic(dic, a[i]);
- break;
- case 'b':
- add_to_dic(dic, a[i]);
- break;
- case 'c':
- break;
- case 'e':
- printf("(CORRECTION MODE [OFF]):~ ...\n");
- return;
- default:
- break;
- }
- }
- }
- }*/
- input = fopen("input.txt", "w");
- for (i=0; i<length; i++)
- {
- if (i==length-1)
- {
- fprintf(input, "%s", local_old[i]);
- }
- else
- {
- fprintf(input, "%s ", local_old[i]);
- }
- }
- printf("(CORRECTION MODE [OFF]):~ ...\n");
- fclose(input);
- return;
- }
- void replace(char a[N], char old_a[N])
- {
- char rep_word[N], c;
- printf("Type the word you want to replace: ");
- scanf("%s", rep_word);
- strcpy(a, strlwr(rep_word));
- if (old_a[0] >= 'A' && old_a[0]<= 'Z')
- {
- rep_word[0] = toupper(rep_word[0]);
- }
- if (old_a[strlen(old_a) - 1] == '.' || (old_a[strlen(old_a) - 1] == '?') || (old_a[strlen(old_a) - 1] == '!'))
- {
- c = old_a[strlen(old_a) - 1];
- rep_word[strlen(rep_word)] = c;
- rep_word[strlen(rep_word) + 1] = '\0';
- }
- strcpy(old_a, rep_word);
- return;
- }
- void cleanString(char str[N])
- {
- int i, j;
- for(i=0; str[i]!='\0'; ++i)
- {
- while (!((str[i]>='a'&&str[i]<='z') || ((str[i]>='A' && str[i]<='Z') || str[i]=='\0') || (str[i]>='0' && str[i]<='9') || (str[i])=='\''))
- {
- for(j=i;str[j]!='\0';++j)
- {
- str[j]=str[j+1];
- }
- str[j]='\0';
- }
- }
- return;
- }
- void histogram(char a[M][N], int lengthofwords[], int length)
- {
- int i, j, lengthWord;
- for (i=0;i<N;i++)
- {
- j = 0;
- while (j!=length)
- {
- lengthWord = strlen(a[j]);
- if (lengthWord == i)
- {
- lengthofwords[i]++;
- }
- j++;
- }
- }
- return;
- }
- void stats(char a[M][N], int length, int spaces)
- {
- char choice, old[M][N];
- /*char** old = (char**)malloc(length*N*sizeof(char));*/
- int lengthWords, lengthChars = 0, i, j, stop = 0, diff = 0, lengthofwords[N] = {0};
- for (i=0; i<length; i++)
- {
- cleanString(a[i]);
- }
- for (i=0; i<length; i++)
- {
- strcpy(old[i], a[i]);
- strupr(a[i]);
- }
- printf("(STATS MODE[ON]):~ *INITIATING STATS MODE*\n");
- if (strlen(a[0]) == 0)
- {
- printf("NON READABLE TEXT.\n");
- return;
- }
- for (i=0; i < length; i++)
- {
- for (j=0; a[i][j] != '\0'; j++)
- {
- lengthChars++;
- }
- }
- lengthWords = i;
- histogram(a, lengthofwords, length);
- for (i=0; i < length; i++)
- {
- for (j=i; j<length;j++)
- {
- if (strcmp(a[i], "*0*0*0*") != 0)
- {
- if (strcmp(a[i], a[j]) == 0 && i != j)
- {
- strcpy(a[j], "*0*0*0*");
- }
- if (j == length - 1)
- {
- diff++;
- }
- }
- }
- }
- while (stop == 0)
- {
- printf("(STATS MODE[ON]):~ CHOOSE:\n\t\tWORD COUNTER [PRESS a]\n\t\tDIFFERENT WORDS COUNTER [PRESS b]\n\t\tCHARACTERS x WORDS HISTOGRAM [PRESS c]\n\t\tSAVE STATS AS A .txt FILE [PRESS f]\n(STATS MODE[ON]):~ ");
- scanf(" %c", &choice);
- putchar('\n');
- switch(choice){
- case 'a':
- printf("(STATS MODE[ON]):~ The text is composed of %d words and %d characters (%d including the spaces).\n", lengthWords, lengthChars, spaces + lengthChars);
- break;
- case 'b':
- printf("(STATS MODE[ON]):~ The text is composed of %d different words.\n", diff);
- break;
- case 'c':/* USEFUL GARBAGE
- printf("Word:\tOccurence:\t\n");
- for (i=0; i<length; i++)
- {
- for (j=0; j<length; j++)
- {
- if (i==occA[j] && occA[j] != 0)
- {
- printf("%s\t:\t", da[j]);
- for (k=0; k<occA[j];k++)
- {
- printf("*");
- }
- putchar('\n');
- }
- }
- }*/
- printf("(STATS MODE[ON]):~ ");
- printf("\n# of chars\t# of words:");
- printf("\nin a word:\n\n");
- for (i=1;i<N;i++)
- {
- printf("%d (occ.: %d)\t\t", i, lengthofwords[i]);
- for (j=0; j<lengthofwords[i];j++)
- {
- putchar('*');
- }
- putchar('\n');
- }
- break;
- case 'f':
- save_statistics(diff, lengthWords, lengthChars, spaces, lengthofwords);
- break;
- case 'e':
- stop = 1;
- default:
- break;
- }
- }
- printf("(STATS MODE[OFF]):~ ...\n");
- return;
- }
- void save_statistics(int diff, int lengthWords, int lengthChars, int spaces, int lengthofwords[])
- {
- FILE* savefile;
- int i, j;
- char save_file_name[N];
- printf("(STATS MODE[ON])/SAVE OPTION:~ SAVE STATISTICS FILE AS: ");
- scanf("%s", save_file_name);
- for (i=0;save_file_name[i] != '\0';i++);
- strcat(save_file_name, ".txt");
- printf("(STATS MODE[ON])/SAVE OPTION:~ \"%s\" CREATED SUCCESSFULLY\n", save_file_name);
- savefile = fopen(save_file_name, "w+");
- fprintf(savefile, "The text is composed of %d words and %d characters (%d including the spaces).\n\n", lengthWords, lengthChars, spaces + lengthChars);
- fprintf(savefile, "The text is composed of %d different words.\n\n", diff);
- fprintf(savefile, "\n# of chars\t# of words:");
- fprintf(savefile, "\nin a word:\n\n");
- for (i=1;i<N;i++)
- {
- fprintf(savefile, "%d\t\t", i);
- for (j=0; j<lengthofwords[i];j++)
- {
- fprintf(savefile, "*");
- }
- fprintf(savefile, "\n");
- }
- printf("(STATS MODE[ON])/SAVE OPTION:~ \"%s\" SAVED SUCCESSFULLY\n", save_file_name);
- printf("(STATS MODE[ON])/SAVE OPTION:~ EXITING SAVE OPTION\n");
- return;
- }
- void legend()
- {
- printf("/////////////////////////////////|\nLEGEND:\t\t\t\t||\n[r:\tInsert text]\t\t||\n[d:\tLoad dictionary]\t||\n[c:\tCorrect the loaded text]||\n[s:\tStatistics]\t\t||\n[l:\tShow legend]\t\t||\n[e:\tEscape]\t\t\t||\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement