#define _CRT_SECURE_NO_WARNINGS #include "SortString.h" #include #include #include #include #define MAX_LEN_WORD 80 #define MAX_WORDS 20000 char words[MAX_WORDS][MAX_LEN_WORD]; int n = 0; char a[MAX_WORDS][MAX_LEN_WORD]; char buffer[MAX_WORDS][MAX_LEN_WORD]; int getNextDelim(FILE* fp, char token[]); int getNextWord(FILE* fp, char token[], int maxLen); int LoadWords(char* filename) { FILE* fin = fopen(filename, "rt"); if (fin == NULL) { printf("File %s doesn't opened!\n", filename); return 0; } char token[MAX_LEN_WORD]; while (!feof(fin)) { while (getNextDelim(fin, token)); if (getNextWord(fin, token, MAX_LEN_WORD)) { if (n < MAX_WORDS) { strcpy(words[n], token); n++; } else { printf("Words are more than elements in array!!\n"); fclose(fin); return 0; } } } fclose(fin); return 1; } int isalpha_my(unsigned char ch) { if (isalpha(ch)) return 1; if (ch >= 192 && ch <= 223) return 1; if (ch >= 224 && ch <= 255) return 1; return 0; } int getNextDelim(FILE* fp, char token[]) { int ch = getc(fp); if (ch == EOF) { return 0; } if (isalpha_my((unsigned char)ch)) { ungetc(ch, fp); return 0; } token[0] = (unsigned char)ch; token[1] = '\0'; return 1; } int getNextWord(FILE* fp, char token[], int maxLen) { int i = 0; int ch; while (((ch = getc(fp)) != EOF) && (i < maxLen - 1)) { if (!isalpha_my((unsigned char)(ch))) { break; } token[i++] = ch; } ungetc(ch, fp); token[i] = '\0'; if (i == 0) return 0; return 1; } void fillArrayStrings() { for (int i = 0; i < n; i++) { strcpy(a[i], words[i]); } } int isSortedStrings() { for (int i = 0; i < n - 1; i++) { if (strcmp(a[i], a[i + 1]) > 0) { return 0; } } return 1; } int ArraysAreEqual() { static int counts_in_words[MAX_WORDS]; static int counts_in_a[MAX_WORDS]; for (int i = 0; i < n; i++) { counts_in_words[i] = 0; counts_in_a[i] = 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (strcmp(words[i], words[j]) == 0) { counts_in_words[i]++; } if (strcmp(words[i], a[j]) == 0) { counts_in_a[i]++; } } } for (int i = 0; i < n; i++) { if (counts_in_words[i] != counts_in_a[i]) { return 0; } } return 1; } void SelectionSortStrings() { for (int i = 0; i < n - 1; i++) { int iMin = i; for (int j = i + 1; j < n; j++) { if (strcmp(a[j], a[iMin]) < 0) { iMin = j; } } if (i != iMin) { char tmp[MAX_LEN_WORD]; strcpy(tmp, a[i]); strcpy(a[i], a[iMin]); strcpy(a[iMin], tmp); } } } void printAll() { printf("%d\n", n); for (int i = 0; i < n; i++) { printf("%s\n", a[i]); } } void QuickSortStrings() { qsort(a, n, sizeof(a[0]), strcmp); } void BubbleSortStrings() { char tmp[80]; bool noSwap; for (int i = n - 1; i >= 0; i--) { noSwap = 1; for (int j = 0; j < i; j++) { if (strcmp(a[j], a[j+1]) > 0) { strcpy(tmp, a[j]); strcpy(a[j], a[j + 1]); strcpy(a[j + 1], tmp); noSwap = 0; } } if (noSwap == 1) break; } } void InsertSortStrings() { char newElement[80]; int location; for (int i = 1; i < n; i++) { strcpy(newElement, a[i]); location = i - 1; while (location >= 0 && strcmp(a[location], newElement) > 0) { strcpy(a[location + 1], a[location]); location = location - 1; } strcpy(a[location + 1], newElement); } }