Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- файл function.c
- #include <malloc.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <windows.h>
- #include "functions.h"
- #define VOCAB struct vocabulary
- VOCAB* push(VOCAB* tmp, char* first, char* sec){
- VOCAB* PTR;
- PTR = (VOCAB*)malloc(sizeof(VOCAB));
- char *word, *translation;
- first[strlen(first)-1] = NULL;
- word = (char*)malloc((strlen(first)-1) * sizeof(char));
- translation = (char*)malloc((strlen(sec)-1) * sizeof(char));
- strcpy(word, first);
- strcpy(translation, sec);
- PTR -> key = word;
- PTR -> value = translation;
- PTR -> next = tmp;
- return PTR;
- }
- void find_translate(VOCAB *stack, char *enter){
- SetConsoleOutputCP(CP_UTF8);
- while(stack) {
- if ((strcmp(stack->key, enter) == 0)) {
- puts("\nTranslation:");
- puts(stack->value);
- return;
- }
- stack = stack->next;
- }
- puts("Not found\n");
- return;
- }
- void Check_file()
- {
- FILE *in,*out;
- if ((in = fopen("in.txt", "r")))
- {
- fclose(in);
- return;
- }
- else exit(0);
- }
- void built_vocab(VOCAB **mas){
- FILE *in;
- in = fopen("in.txt", "r");
- char first[100], sec[100];
- if(in)
- while(!feof(in)){
- fgets(first,100, in);
- fgets(sec,100, in);
- mas[hash(first, strlen(first) - 1)] = push(mas[hash(first,strlen(first) - 1)],
- first, sec);
- }
- fclose(in);
- }
- int hash(char*str,int length){
- int sum = 0;
- for (int i = 0; i < length; i++)
- sum += str[i];
- return sum/length;
- }
- ____________________________________
- файл main.c
- #include <windows.h>
- #include <stdio.h>
- #include <string.h>
- #include "functions.h"
- int main(){
- Check_file();
- //Fill a vocabulary into the stacks
- VOCAB *bunch_of_stack[256];
- for(int i=0; i < 256; i++)
- bunch_of_stack[i]=NULL;
- built_vocab(bunch_of_stack);
- char enter[40];
- //Doing a search
- while(1){
- printf("Enter a word:");
- gets(enter);
- find_translate(bunch_of_stack[hash(enter,strlen(enter))], enter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement