Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
16.11.2025
Размер:
7.77 Кб
Скачать
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct string{ // stroka (dlya udobstva)
    char letter[15];
} STRING;
STRING *found=NULL;
typedef struct el{ // element lista
    STRING inf;
    struct el *next;
} EL;
int ord(char b){ // numeracia simvolov
    switch(b){
    case 'a':
    case 'A':
        return(0);
    case 'b':
    case 'B':
        return(1);
    case 'c':
    case 'C':
        return(2);
    case 'd':
    case 'D':
        return(3);
    case 'e':
    case 'E':
        return(4);
    case 'f':
    case 'F':
        return(5);
    case 'g':
    case 'G':
        return(6);
    case 'j':
    case 'J':
        return(7);
    case 'k':
    case 'K':
        return(8);
    case 'l':
    case 'L':
        return(9);
    case 'm':
    case 'M':
        return(10);
    case 'n':
    case 'N':
        return(11);
    case 'o':
    case 'O':
        return(12);
    case 'p':
    case 'P':
        return(13);
    case 'q':
    case 'Q':
        return(14);
    case 'r':
    case 'R':
        return(15);
    case 's':
    case 'S':
        return(16);
    case 't':
    case 'T':
        return(17);
    case 'h':
    case 'H':
        return(18);
    case 'i':
    case 'I':
        return(19);
    case 'u':
    case 'U':
        return(20);
    case 'v':
    case 'V':
        return(21);
    case 'w':
    case 'W':
        return(22);
    case 'x':
    case 'X':
        return(23);
    case 'y':
    case 'Y':
        return(24);
    case 'z':
    case 'Z':
        return(25);
    return(0);
    }
}
int hash(STRING a){ // hashirovanie
    int s=0, i=0;
        while(((a.letter[i])!='\0')&&((a.letter[i])!='\n')){
                /* otladka
                printf("!%c %d\n", a.letter[i], ord(a.letter[i]));
                */
                s=(s*27+ord(a.letter[i]))%13;
                i++;
    }
    return(s);
}
int compare(STRING a, STRING b){ // pokazivaet, ravni li stroki (odinakovie slova)
    int i;
    for(i=0; i<14; i++){
        if(a.letter[i]!=b.letter[i]) return 0;
        if((a.letter[i]=='\0')&&(b.letter[i]=='\0')) return 1;
    }
    return 1;
}
int find(STRING a, EL *list){ // poisk elementa + opred. kol-va sravneniy
    int i=hash(a);
    int srav=0;
    EL *q;
    q = (EL*) malloc(sizeof(EL));
    if(list[i].inf.letter[0]=='\0'){ // pustoi element
            srav++;
            return(srav);
    }
    if(compare(a, list[i].inf)){ // proverka na perviy element s dannim indeksom
        found=&(list[i].inf);
        srav++;
        return(srav);
    }
    q=&(list[i]);
    while(q->next!=NULL){ // probeg po ostalnomy listu
        srav++;
        if(compare(a, q->next->inf)){
                found=&(q->next->inf);
                return(srav);
        }
        q=q->next;
    }
}
int maxlenght (EL *list){ // max. dlina
    int i=0, lenght, lenght_max=0;
    EL *q;
    q = (EL*) malloc(sizeof(EL));
    for(i=0; i<13; i++){
        lenght=0;
        if(list[i].inf.letter[0]=='\0') continue;
        lenght++;
        q=&(list[i]);
        while(q->next!=NULL){
            lenght++;
            q=q->next;
        }
        if(lenght>lenght_max){
            lenght_max=lenght;
        }
    }
    return(lenght_max);
}
int keys(EL *list){ // kol-vo zapolnennih klyuchey
    int i=0, key=0;
    EL *q;
    q = (EL*) malloc(sizeof(EL));
    for(i=0; i<13; i++){
        if(list[i].inf.letter[0]=='\0') continue; // proverka na pustoi element
        key++;
    }
    return(key);
}
void printm(EL *list){ // vivod (dlya otladki)
    int i=0;
    EL *q;
    q = (EL*) malloc(sizeof(EL));
    for(i=0; i<13; i++){
        if(list[i].inf.letter[0]=='\0'){
                printf("[%d] NULL\n", i);
                continue;
        }
        printf("[%d] %s", i, list[i].inf);
        q=&(list[i]);
        while(q->next!=NULL){
            printf(" %s", q->next->inf);
            q=q->next;
        }
        printf("\n");
    }
}
void del(EL *list){ // udalenie
    int i, j;
    EL *p;
    for(i=0; i<13; i++){
        p = (EL*) malloc(sizeof(EL));
        p=&(list[i]);
        for(j=0; j<15; j++){
                (p->inf).letter[j]='\0';
        }
        p->next=NULL;
    }
}
void insert(EL *list, STRING a){ // dobavlenie slova
    int i=hash(a);
    EL *q, *l;
    q = (EL*) malloc(sizeof(EL));
    if(list[i].inf.letter[0]=='\0'){
            list[i].inf=a;
            return;
    }
    q=&(list[i]);
    l = (EL*) malloc(sizeof(EL));
    l->inf=a;
    l->next=NULL;
    while((q->next)!=NULL) q=q->next;
    q->next=l;
}
void delEl(EL *list, STRING a){ // udalenie slova
    int i=hash(a);
    int j;
    find(a, list);
    if(found==NULL){
        printf("not found\n");
        return;
    }
    EL *q, *l;
    q = (EL*) malloc(sizeof(EL));
    q=&(list[i]);
    if(compare(a, list[i].inf)){ // proverka na perviy element s dannim indeksom
        if(list[i].next==NULL){
            for(i=0; j<15; j++){
                    q->inf.letter[j]='\0';
            }
            return;
        }
        else{
            q->inf=q->next->inf;
            q->next=q->next->next;
            return;
        }
    }
    while(q->next!=NULL){ // probeg po ostalnomy listu
        if(compare(a, q->next->inf)){
                if(q->next->next==NULL){
                    q->next=NULL;
                    return;
                }
                q->next=q->next->next;
                return;
        }
        q=q->next;
    }
}
int main(void){
    FILE *file = fopen("test.txt", "r");
    int s=0, i=0, k, cont=1;
    STRING p, NULLSTRING; // null-string - nol' stroka, dlya nachalnogo zadaniya massiva
    EL *HASH, *q, *l;
    HASH = (EL*) malloc(13*sizeof(EL));
    float koef;
    q = (EL*) malloc(sizeof(EL));
    for(i=0; i<15; i++){
        NULLSTRING.letter[i]='\0';
    }
    for(i=0; i<13; i++){ // nachalnoe zadanie hash-tablitsi
        HASH[i].inf=NULLSTRING;
        HASH[i].next=NULL;
    }
    /*
    for(i=0; i<10; i++){
        fscanf(file, "%s", &p);
        if((p.letter[0]!='\n')&&(p.letter[0]!='\0')){
            k=hash(p); // hash-index
            if(HASH[k].inf.letter[0]=='\0'){ // esli nachalnaya yacheyka pusta
                HASH[k].inf=p;
            }
            else{ // esli ne pusta
                q=&(HASH[k]);
                l = (EL*) malloc(sizeof(EL));
                l->inf=p;
                l->next=NULL;
                while((q->next)!=NULL) q=q->next;
                q->next=l;
            }
        }
    }
    */
    while(cont){ // zapolnenie hash-tablitsi vvodimimi strokami
        i=0;
        scanf("%s", &p);
        if((p.letter[0]!='\n')&&(p.letter[0]!='\0')){
            k=hash(p); // hash-index
            if(HASH[k].inf.letter[0]=='\0'){ // esli nachalnaya yacheyka pusta
                HASH[k].inf=p;
            }
            else{ // esli ne pusta
                q=&(HASH[k]);
                l = (EL*) malloc(sizeof(EL));
                l->inf=p;
                l->next=NULL;
                while((q->next)!=NULL) q=q->next;
                q->next=l;
            }
            if (getchar()=='\n') cont=0;
        }
        else cont=0;
    }
    printm(HASH);
    scanf("%s", &p);
    insert(HASH, p);
    scanf("%s", &p);
    insert(HASH, p);
    scanf("%s", &p);
    delEl(HASH, p);
    printm(HASH);
    printf("%d\n", maxlenght(HASH));
    koef=keys(HASH)/13.0;
    printf("%.2f\n", koef);
    fclose(file);
    del(HASH);
    printf("Deleted!\n");
    printm(HASH);
}
Соседние файлы в папке 3 семестр