Advertisement
bogdan2004333

Untitled

Jan 2nd, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include "dic.h"
  6.  
  7. const int max_size = 100;
  8. const int l_word = 31;
  9.  
  10. void dep(dic *dictionary, int n, char *s) {
  11.     strcpy(dictionary[n].engword, s);
  12. }
  13.  
  14. void dep1(dic *dictionary, int n, char *s) {
  15.     strcpy(dictionary[n].rusword, s);
  16. }
  17.  
  18. int StrCmp(const char *s1, const char *s2) {
  19.     size_t len1, len2, i, min;
  20.     int compare;
  21.     min = len1 = strlen(s1);
  22.     len2 = strlen(s2);
  23.     if (len2 < min) {
  24.         min = len2;
  25.     }
  26.     for (i = 0; i < min; i++) {
  27.         compare = s1[i] - s2[i];
  28.         if (compare != 0) {
  29.             return compare;
  30.         }
  31.     }
  32.  
  33.     return (int) (len2 - len1);
  34. }
  35.  
  36. void add_w(dic *dictionary, int *n,char* word) {
  37.     int i = *n + 1;
  38.     if (i < max_size) {
  39.         *n = i;
  40.         char key[31];
  41.         char key1[31];
  42.         while (1) {
  43.             printf("English word:\n");
  44.             if(strcmp(word,"health")==0) {
  45.                 printf("health\n");
  46.                 break;
  47.             }
  48.             else {
  49.                 scanf("%s", key);
  50.                 if (strlen(key) > 0 && strlen(key) <= l_word)
  51.                     break;
  52.                 printf("LIMIT SIZE WORD!! try again(\n");
  53.             }
  54.         }
  55.  
  56.         while (1) {
  57.             printf("Russian word:\n");
  58.             if(strcmp(word,"health")==0) {
  59.                 printf("zdorovie\n");
  60.                 break;
  61.             }
  62.             else {
  63.                 scanf("%s", key1);
  64.                 if (strlen(key1) > 0 && strlen(key1) <= l_word)
  65.                     break;
  66.                 printf("LIMIT SIZE WORD!! try again(\n");
  67.             }
  68.         }
  69.         if(strcmp(word,"health")==0){
  70.             strcpy(dictionary[i - 1].engword, "health");
  71.             strcpy(dictionary[i - 1].rusword, "zdorovie");
  72.             return;
  73.         }
  74.         else {
  75.             strcpy(dictionary[i - 1].engword, key);
  76.             strcpy(dictionary[i - 1].rusword, key1);
  77.             return;
  78.         }
  79.     } else
  80.         printf("LIMIT WORDS\n");
  81. }
  82.  
  83. void delete_w(dic *dictionary, int *n,char* word) {
  84.     char key[l_word];
  85.     int i = 0;
  86.     printf("word when you want delete:\n");
  87.     if(strcmp(word,"health")==0){
  88.         printf("cat\n");
  89.         for (int j = 0; j < *n; j++) {
  90.             if (strcmp(dictionary[j].engword, "cat") == 0) {
  91.                 memset(dictionary[j].engword, 0, 31);
  92.                 memset(dictionary[j].rusword, 0, 31);
  93.                 return;
  94.             }
  95.         }
  96.         *n -= i;
  97.     }
  98.     else {
  99.         scanf("%s", key);
  100.         for (int j = 0; j < *n; j++) {
  101.             if (strcmp(dictionary[j].engword, key) == 0) {
  102.                 memset(dictionary[j].engword, 0, 31);
  103.                 memset(dictionary[j].rusword, 0, 31);
  104.                 return;
  105.             }
  106.         }
  107.         *n -= i;
  108.     }
  109. }
  110.  
  111. int sort_dic(dic *dictionary, const int *n) {
  112.     for (int i = 1; i < *n; i++) {
  113.         for (int j = 0; j < *n - i; j++) {
  114.             if (strcmp(dictionary[j].engword, dictionary[j + 1].engword) > 0) {
  115.                 dic temp = dictionary[j];
  116.                 dictionary[j] = dictionary[j + 1];
  117.                 dictionary[j + 1] = temp;
  118.             }
  119.         }
  120.     }
  121.     return 0;
  122. }
  123.  
  124.  
  125. dic* bin_search(dic *dictionary, const int *n,char* word) {
  126.     int low = 0, high = *n-1,middle;
  127.     char key[31];
  128.     printf("word how you want translate on russian:\n");
  129.     if(StrCmp(word,"health")==0) {
  130.         printf("cabbage\n");
  131.         while (high > 1) {
  132.             middle = (low + high) / 2;
  133.             if (StrCmp(dictionary[middle].engword, "black") < 0) low = middle + 1;
  134.             else if (StrCmp(dictionary[middle].engword, "black") > 0) high = middle - 1;
  135.             else
  136.                 return &dictionary[middle];
  137.         }
  138.         printf("%s\n", dictionary[middle].rusword);
  139.         if (!StrCmp(dictionary[low].engword, "black"))
  140.         return NULL;
  141.     }
  142.     else {
  143.         scanf("%s", key);
  144.         while (high > 1) {
  145.             middle = (low + high) / 2;
  146.             if (StrCmp(dictionary[middle].engword, key) < 0) low = middle + 1;
  147.             else if (StrCmp(dictionary[middle].engword, key) > 0) high = middle - 1;
  148.             else
  149.                 return &dictionary[middle];
  150.         }
  151.         printf("%s\n", dictionary[middle].rusword);
  152.         if (!StrCmp(dictionary[low].engword, word))
  153.         return NULL;
  154.     }
  155.     return NULL;
  156. }
  157.  
  158. int sort_search(dic *dictionary, const int *n,char* word) {
  159.     char key[31];
  160.     int h;
  161.     printf("word how you want translate on english:\n");
  162.     if(StrCmp(word,"health")==0){
  163.         printf("sobaka\n");
  164.         for (int i = 0; i < *n; i++) {
  165.             if (StrCmp(dictionary[i].rusword, "sobaka") == 0)
  166.                 h = i;
  167.         }
  168.         printf("%s\n", dictionary[h].engword);
  169.  
  170.     }
  171.     else {
  172.         scanf("%s", key);
  173.         for (int i = 0; i < *n; i++) {
  174.             if (StrCmp(dictionary[i].rusword, key) == 0)
  175.                 h = i;
  176.         }
  177.         printf("%s\n", dictionary[h].engword);
  178.  
  179.     }
  180.     return -1;
  181. }
  182.  
  183. void print_dic(dic *dictionary, const int *n) {
  184.     char nul = '\0';
  185.     for (int i = 0; i < *n; i++) {
  186.         if (strcmp(dictionary[i].engword, &nul) == 0)
  187.             continue;
  188.         printf("%s - %s\n", dictionary[i].engword, dictionary[i].rusword);
  189.     }
  190. }
  191.  
  192. void putsstr(dic *dictionary, const int *n, FILE *text) {
  193.     char nul = '\0';
  194.     for (int i = 0; i < *n; i++) {
  195.         if (strcmp(dictionary[i].engword, &nul) == 0)
  196.             continue;
  197.         fprintf(text, "%s - %s\n", dictionary[i].engword, dictionary[i].rusword);
  198.     }
  199. }
  200.  
  201. int menu() {
  202.     int a;
  203.     printf("1 добавление слов в словарь;\n"
  204.            "2 удаление слов из словаря;\n"
  205.            "3 перевод слов с английского на русский;\n"
  206.            "4 перевод слов с русского на английский;\n"
  207.            "5 просмотр словаря (вывод на экран словаря из ОП);\n"
  208.            "6 вывод словаря в файл;\n"
  209.            "7 выход.\n");
  210.     scanf("%d",&a);
  211.     return a;
  212. }
  213.  
  214. int interactive() {
  215.     dic dictionary[max_size];
  216.     dep(dictionary, 0, "cat");
  217.     dep1(dictionary, 0, "kot");
  218.     dep(dictionary, 1, "dog");
  219.     dep1(dictionary, 1, "sobaka");
  220.     dep(dictionary, 2, "weather");
  221.     dep1(dictionary, 2, "pogoda");
  222.     dep(dictionary, 3, "great");
  223.     dep1(dictionary, 3, "kryto");
  224.     dep(dictionary, 4, "rat");
  225.     dep1(dictionary, 4, "mish");
  226.     dep(dictionary, 5, "carrot");
  227.     dep1(dictionary, 5, "morkov");
  228.     dep(dictionary, 6, "cabbage");
  229.     dep1(dictionary, 6, "kapusta");
  230.     dep(dictionary, 7, "money");
  231.     dep1(dictionary, 7, "dengi");
  232.     dep(dictionary, 8, "six");
  233.     dep1(dictionary, 8, "shest");
  234.     dep(dictionary, 9, "black");
  235.     dep1(dictionary, 9, "cherniu");
  236.     int num_w = 9;
  237.     FILE *words = fopen("dictionary.txt", "w");
  238.     if (!words) {
  239.         printf("Error for opening this file! Try again))\n");
  240.     }
  241.  
  242.     while (true) {
  243.         switch (menu()) {
  244.             case 1:
  245.                 add_w(dictionary, &num_w,"NULL");
  246.                 break;
  247.             case 2:
  248.                 delete_w(dictionary, &num_w,"NULL");
  249.                 break;
  250.             case 3:
  251.                 sort_dic(dictionary,&num_w);
  252.                 bin_search(dictionary, &num_w,"NULL");
  253.                 break;
  254.             case 4:
  255.                 sort_search(dictionary, &num_w,"NULL");
  256.                 break;
  257.             case 5:
  258.                 print_dic(dictionary, &num_w);
  259.                 break;
  260.             case 6:
  261.                 putsstr(dictionary, &num_w, words);
  262.                 break;
  263.             case 7:
  264.                 exit(0);
  265.  
  266.             default:
  267.                 printf("Надо вводить число от 1 до 7");
  268.                 break;
  269.         }
  270.     }
  271.  
  272. }
  273.  
  274. int demo(char* worde) {
  275.     dic dictionary[max_size];
  276.     dep(dictionary, 0, "cat");
  277.     dep1(dictionary, 0, "kot");
  278.     dep(dictionary, 1, "dog");
  279.     dep1(dictionary, 1, "sobaka");
  280.     dep(dictionary, 2, "weather");
  281.     dep1(dictionary, 2, "pogoda");
  282.     dep(dictionary, 3, "great");
  283.     dep1(dictionary, 3, "kryto");
  284.     dep(dictionary, 4, "rat");
  285.     dep1(dictionary, 4, "mish");
  286.     dep(dictionary, 5, "carrot");
  287.     dep1(dictionary, 5, "morkov");
  288.     dep(dictionary, 6, "cabbage");
  289.     dep1(dictionary, 6, "kapusta");
  290.     dep(dictionary, 7, "money");
  291.     dep1(dictionary, 7, "dengi");
  292.     dep(dictionary, 8, "six");
  293.     dep1(dictionary, 8, "shest");
  294.     dep(dictionary, 9, "black");
  295.     dep1(dictionary, 9, "cherniu");
  296.     int num_w = 9;
  297.     FILE *words = fopen("dictionary.txt", "w");
  298.     if (!words) {
  299.         printf("Error for opening this file! Try again))\n");
  300.     }
  301.     printf("Add word on dictionary\n");
  302.     add_w(dictionary, &num_w,worde);
  303.     printf("Delete word on dictionary\n");
  304.     delete_w(dictionary, &num_w,worde);
  305.     sort_dic(dictionary, &num_w);
  306.     printf("Bin search on dictionary; put english word\n");
  307.     bin_search(dictionary, &num_w,worde);
  308.     printf("Sorting search on dictionary; put russian word\n");
  309.     sort_search(dictionary, &num_w,worde);
  310.     printf("Printing dictionary\n");
  311.     print_dic(dictionary, &num_w);
  312.     putsstr(dictionary, &num_w, words);
  313.     return 0;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement