Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include "dic.h"
- const int max_size = 100;
- const int l_word = 31;
- void dep(dic *dictionary, int n, char *s) {
- strcpy(dictionary[n].engword, s);
- }
- void dep1(dic *dictionary, int n, char *s) {
- strcpy(dictionary[n].rusword, s);
- }
- int StrCmp(const char *s1, const char *s2) {
- size_t len1, len2, i, min;
- int compare;
- min = len1 = strlen(s1);
- len2 = strlen(s2);
- if (len2 < min) {
- min = len2;
- }
- for (i = 0; i < min; i++) {
- compare = s1[i] - s2[i];
- if (compare != 0) {
- return compare;
- }
- }
- return (int) (len2 - len1);
- }
- void add_w(dic *dictionary, int *n,char* word) {
- int i = *n + 1;
- if (i < max_size) {
- *n = i;
- char key[31];
- char key1[31];
- while (1) {
- printf("English word:\n");
- if(strcmp(word,"health")==0) {
- printf("health\n");
- break;
- }
- else {
- scanf("%s", key);
- if (strlen(key) > 0 && strlen(key) <= l_word)
- break;
- printf("LIMIT SIZE WORD!! try again(\n");
- }
- }
- while (1) {
- printf("Russian word:\n");
- if(strcmp(word,"health")==0) {
- printf("zdorovie\n");
- break;
- }
- else {
- scanf("%s", key1);
- if (strlen(key1) > 0 && strlen(key1) <= l_word)
- break;
- printf("LIMIT SIZE WORD!! try again(\n");
- }
- }
- if(strcmp(word,"health")==0){
- strcpy(dictionary[i - 1].engword, "health");
- strcpy(dictionary[i - 1].rusword, "zdorovie");
- return;
- }
- else {
- strcpy(dictionary[i - 1].engword, key);
- strcpy(dictionary[i - 1].rusword, key1);
- return;
- }
- } else
- printf("LIMIT WORDS\n");
- }
- void delete_w(dic *dictionary, int *n,char* word) {
- char key[l_word];
- int i = 0;
- printf("word when you want delete:\n");
- if(strcmp(word,"health")==0){
- printf("cat\n");
- for (int j = 0; j < *n; j++) {
- if (strcmp(dictionary[j].engword, "cat") == 0) {
- memset(dictionary[j].engword, 0, 31);
- memset(dictionary[j].rusword, 0, 31);
- return;
- }
- }
- *n -= i;
- }
- else {
- scanf("%s", key);
- for (int j = 0; j < *n; j++) {
- if (strcmp(dictionary[j].engword, key) == 0) {
- memset(dictionary[j].engword, 0, 31);
- memset(dictionary[j].rusword, 0, 31);
- return;
- }
- }
- *n -= i;
- }
- }
- int sort_dic(dic *dictionary, const int *n) {
- for (int i = 1; i < *n; i++) {
- for (int j = 0; j < *n - i; j++) {
- if (strcmp(dictionary[j].engword, dictionary[j + 1].engword) > 0) {
- dic temp = dictionary[j];
- dictionary[j] = dictionary[j + 1];
- dictionary[j + 1] = temp;
- }
- }
- }
- return 0;
- }
- dic* bin_search(dic *dictionary, const int *n,char* word) {
- int low = 0, high = *n-1,middle;
- char key[31];
- printf("word how you want translate on russian:\n");
- if(StrCmp(word,"health")==0) {
- printf("cabbage\n");
- while (high > 1) {
- middle = (low + high) / 2;
- if (StrCmp(dictionary[middle].engword, "black") < 0) low = middle + 1;
- else if (StrCmp(dictionary[middle].engword, "black") > 0) high = middle - 1;
- else
- return &dictionary[middle];
- }
- printf("%s\n", dictionary[middle].rusword);
- if (!StrCmp(dictionary[low].engword, "black"))
- return NULL;
- }
- else {
- scanf("%s", key);
- while (high > 1) {
- middle = (low + high) / 2;
- if (StrCmp(dictionary[middle].engword, key) < 0) low = middle + 1;
- else if (StrCmp(dictionary[middle].engword, key) > 0) high = middle - 1;
- else
- return &dictionary[middle];
- }
- printf("%s\n", dictionary[middle].rusword);
- if (!StrCmp(dictionary[low].engword, word))
- return NULL;
- }
- return NULL;
- }
- int sort_search(dic *dictionary, const int *n,char* word) {
- char key[31];
- int h;
- printf("word how you want translate on english:\n");
- if(StrCmp(word,"health")==0){
- printf("sobaka\n");
- for (int i = 0; i < *n; i++) {
- if (StrCmp(dictionary[i].rusword, "sobaka") == 0)
- h = i;
- }
- printf("%s\n", dictionary[h].engword);
- }
- else {
- scanf("%s", key);
- for (int i = 0; i < *n; i++) {
- if (StrCmp(dictionary[i].rusword, key) == 0)
- h = i;
- }
- printf("%s\n", dictionary[h].engword);
- }
- return -1;
- }
- void print_dic(dic *dictionary, const int *n) {
- char nul = '\0';
- for (int i = 0; i < *n; i++) {
- if (strcmp(dictionary[i].engword, &nul) == 0)
- continue;
- printf("%s - %s\n", dictionary[i].engword, dictionary[i].rusword);
- }
- }
- void putsstr(dic *dictionary, const int *n, FILE *text) {
- char nul = '\0';
- for (int i = 0; i < *n; i++) {
- if (strcmp(dictionary[i].engword, &nul) == 0)
- continue;
- fprintf(text, "%s - %s\n", dictionary[i].engword, dictionary[i].rusword);
- }
- }
- int menu() {
- int a;
- printf("1 добавление слов в словарь;\n"
- "2 удаление слов из словаря;\n"
- "3 перевод слов с английского на русский;\n"
- "4 перевод слов с русского на английский;\n"
- "5 просмотр словаря (вывод на экран словаря из ОП);\n"
- "6 вывод словаря в файл;\n"
- "7 выход.\n");
- scanf("%d",&a);
- return a;
- }
- int interactive() {
- dic dictionary[max_size];
- dep(dictionary, 0, "cat");
- dep1(dictionary, 0, "kot");
- dep(dictionary, 1, "dog");
- dep1(dictionary, 1, "sobaka");
- dep(dictionary, 2, "weather");
- dep1(dictionary, 2, "pogoda");
- dep(dictionary, 3, "great");
- dep1(dictionary, 3, "kryto");
- dep(dictionary, 4, "rat");
- dep1(dictionary, 4, "mish");
- dep(dictionary, 5, "carrot");
- dep1(dictionary, 5, "morkov");
- dep(dictionary, 6, "cabbage");
- dep1(dictionary, 6, "kapusta");
- dep(dictionary, 7, "money");
- dep1(dictionary, 7, "dengi");
- dep(dictionary, 8, "six");
- dep1(dictionary, 8, "shest");
- dep(dictionary, 9, "black");
- dep1(dictionary, 9, "cherniu");
- int num_w = 9;
- FILE *words = fopen("dictionary.txt", "w");
- if (!words) {
- printf("Error for opening this file! Try again))\n");
- }
- while (true) {
- switch (menu()) {
- case 1:
- add_w(dictionary, &num_w,"NULL");
- break;
- case 2:
- delete_w(dictionary, &num_w,"NULL");
- break;
- case 3:
- sort_dic(dictionary,&num_w);
- bin_search(dictionary, &num_w,"NULL");
- break;
- case 4:
- sort_search(dictionary, &num_w,"NULL");
- break;
- case 5:
- print_dic(dictionary, &num_w);
- break;
- case 6:
- putsstr(dictionary, &num_w, words);
- break;
- case 7:
- exit(0);
- default:
- printf("Надо вводить число от 1 до 7");
- break;
- }
- }
- }
- int demo(char* worde) {
- dic dictionary[max_size];
- dep(dictionary, 0, "cat");
- dep1(dictionary, 0, "kot");
- dep(dictionary, 1, "dog");
- dep1(dictionary, 1, "sobaka");
- dep(dictionary, 2, "weather");
- dep1(dictionary, 2, "pogoda");
- dep(dictionary, 3, "great");
- dep1(dictionary, 3, "kryto");
- dep(dictionary, 4, "rat");
- dep1(dictionary, 4, "mish");
- dep(dictionary, 5, "carrot");
- dep1(dictionary, 5, "morkov");
- dep(dictionary, 6, "cabbage");
- dep1(dictionary, 6, "kapusta");
- dep(dictionary, 7, "money");
- dep1(dictionary, 7, "dengi");
- dep(dictionary, 8, "six");
- dep1(dictionary, 8, "shest");
- dep(dictionary, 9, "black");
- dep1(dictionary, 9, "cherniu");
- int num_w = 9;
- FILE *words = fopen("dictionary.txt", "w");
- if (!words) {
- printf("Error for opening this file! Try again))\n");
- }
- printf("Add word on dictionary\n");
- add_w(dictionary, &num_w,worde);
- printf("Delete word on dictionary\n");
- delete_w(dictionary, &num_w,worde);
- sort_dic(dictionary, &num_w);
- printf("Bin search on dictionary; put english word\n");
- bin_search(dictionary, &num_w,worde);
- printf("Sorting search on dictionary; put russian word\n");
- sort_search(dictionary, &num_w,worde);
- printf("Printing dictionary\n");
- print_dic(dictionary, &num_w);
- putsstr(dictionary, &num_w, words);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement