Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. файл function.c
  2.  
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <windows.h>
  8. #include "functions.h"
  9.  
  10. #define VOCAB struct vocabulary
  11.  
  12. VOCAB* push(VOCAB* tmp, char* first, char* sec){
  13. VOCAB* PTR;
  14. PTR = (VOCAB*)malloc(sizeof(VOCAB));
  15. char *word, *translation;
  16. first[strlen(first)-1] = NULL;
  17. word = (char*)malloc((strlen(first)-1) * sizeof(char));
  18. translation = (char*)malloc((strlen(sec)-1) * sizeof(char));
  19. strcpy(word, first);
  20. strcpy(translation, sec);
  21. PTR -> key = word;
  22. PTR -> value = translation;
  23. PTR -> next = tmp;
  24. return PTR;
  25. }
  26.  
  27. void find_translate(VOCAB *stack, char *enter){
  28. SetConsoleOutputCP(CP_UTF8);
  29. while(stack) {
  30. if ((strcmp(stack->key, enter) == 0)) {
  31. puts("\nTranslation:");
  32. puts(stack->value);
  33. return;
  34. }
  35. stack = stack->next;
  36. }
  37. puts("Not found\n");
  38. return;
  39. }
  40.  
  41. void Check_file()
  42. {
  43. FILE *in,*out;
  44. if ((in = fopen("in.txt", "r")))
  45. {
  46. fclose(in);
  47. return;
  48. }
  49. else exit(0);
  50. }
  51.  
  52. void built_vocab(VOCAB **mas){
  53. FILE *in;
  54. in = fopen("in.txt", "r");
  55. char first[100], sec[100];
  56. if(in)
  57. while(!feof(in)){
  58. fgets(first,100, in);
  59. fgets(sec,100, in);
  60. mas[hash(first, strlen(first) - 1)] = push(mas[hash(first,strlen(first) - 1)],
  61. first, sec);
  62. }
  63. fclose(in);
  64. }
  65.  
  66. int hash(char*str,int length){
  67. int sum = 0;
  68. for (int i = 0; i < length; i++)
  69. sum += str[i];
  70. return sum/length;
  71. }
  72.  
  73. ____________________________________
  74.  
  75. файл main.c
  76.  
  77. #include <windows.h>
  78. #include <stdio.h>
  79. #include <string.h>
  80. #include "functions.h"
  81.  
  82. int main(){
  83. Check_file();
  84.  
  85. //Fill a vocabulary into the stacks
  86.  
  87. VOCAB *bunch_of_stack[256];
  88. for(int i=0; i < 256; i++)
  89. bunch_of_stack[i]=NULL;
  90. built_vocab(bunch_of_stack);
  91. char enter[40];
  92.  
  93. //Doing a search
  94.  
  95. while(1){
  96. printf("Enter a word:");
  97. gets(enter);
  98. find_translate(bunch_of_stack[hash(enter,strlen(enter))], enter);
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement