Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Различные методы сортировки и поиска си, программирование .docx
Скачиваний:
8
Добавлен:
04.01.2017
Размер:
49.33 Кб
Скачать

Хеширование

#include <stdio.h>

#include <conio.h>

#include <ctype.h>

#include <string.h>

#include <stdlib.h>

struct note {

char key[10];

int info;};

struct hash_note {

char key[10];

note *searched;

hash_note *next;};

struct hash_note TablHash[128];

void Chistka()

{ for(int i=0; i<128; i++)

{ for(int j=0; j<9; j++)

TablHash[i].key[j]=' ';

TablHash[i].key[9]='\0';

TablHash[i].searched=NULL;

ablHash[i].next=NULL; } }

void Create_TablHash(struct note *Data, int dl)

{ int number=0, sum=0;

struct hash_note *add;

hash_note *temp;

for (int i=0; i<dl; i++) {

for (int j=0; isalpha(Data[i].key[j])!=0; j++)

sum=sum+Data[i].key[j];

sum=sum%128;

//printf ("sum = %d\n", sum);

if (isalpha(TablHash[sum].key[0])==0)

{ for(int k=0; k<10; k++)

TablHash[sum].key[k]=Data[i].key[k];

TablHash[sum].searched=&Data[i]; }

else { add=(hash_note*)malloc(sizeof(hash_note)); temp=TablHash[sum].next; TablHash[sum].next=add;

(*add).next=temp;

for(int k=0; k<10; k++) (*add).key[k]=Data[i].key[k];

(*add).searched=&Data[i]; }

sum=0;}}

void Searching (struct note *Data, char *str)

{ int sum=0;

struct hash_note *add;

for (int j=0; isalpha(str[j])!=0; j++)

sum=sum+str[j];

sum=sum%128;

if (TablHash[sum].key[0]==' ')

{ printf ("searching note not found\n");

getch();

exit(1);}

add=(hash_note*)malloc(sizeof(hash_note));

*add=TablHash[sum];

while(true)

{ if (strcmp((*add).key,str)==0)

{ int i=0;

char temp[10]=" "; i=(*((*add).searched)).info;

strcpy( temp, (*((*add).searched)).key );

printf ("\nFound:\n%s - %d", temp, i);

break;

}else if ((*add).next!=0) add=(*add).next;

else {

printf ("searching note not found\n"); break; }}}

void main()

{ FILE *fp=fopen("data.txt", "r");

struct note *Data;

char c, str[10]=" ";

int dl=0;

while(!feof(fp))

if (c=getc(fp)==10) dl++;

rewind(fp);

Data=(note*)malloc(dl*sizeof(note));//считываем инфо с файла

for (int i=0; i<dl; i++)

fscanf(fp, "%s %d", Data[i].key, &Data[i].info);

for (int i=0; i<dl; i++)

printf ("%s %d\n" , Data[i].key, Data[i].info);

Chistka;

Create_TablHash(Data, dl);

printf ("enter note key:\n");

gets(str);

Searching(Data, str);

getch(); }

Шифрование. Простая подстановка

#include <stdlib.h>

#include <conio.h>

#include <stdio.h>

#include <locale.h>

#include <string.h>

char alf[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

char *kode(char *S,char *kod)

{ for(int i=0;i<strlen(S);i++)

{ for(int j=0;j<52;j++)

if(S[i]==alf[j])

{ S[i]=kod[j];

break; } }

return S; }

char *rekode(char *S,char *kod)

{ for(int i=0;i<strlen(S);i++)

{ for(int j=0;j<52;j++)

if(S[i]==kod[j])

{ S[i]=alf[j];

break; } }

return S; }

void main()

{ setlocale(LC_ALL,"Rus");

char *S,p,kod[53],word1[52];

int i,k=0;

remove("I:/1bin.txt");

FILE *fp;

S=(char*)malloc(100*sizeof(char));

l1: printf("\nЧто хотите делать?\n1.Закодировать\n2.Раскодировать\n");

p=getch();

if((p!='1')&&(p!='2'))

goto l1;

int s=100*sizeof(char);

for(int i=0;i<53;i++)

kod[i]=alf[i];

if(p=='1'){

fp=fopen("D:/123.txt","r+");

fgets(S,100,fp);

fclose(fp);

printf("\nВведите кодовое слово: ");

scanf("%s",&word1);

for(i=0;i<strlen(word1);i++)

kod[i]=word1[i];

i=-1;

for(int j=strlen(word1);j<52;j++) {

re: k=0;

i++;

for(int e=0;e<strlen(word1);e++) if(word1[e]==alf[i] k=1;

if(!k)

kod[j]=alf[i];

else

goto re; }

S=kode(S,kod);

remove("D:/124.txt");

fp=fopen("D:/124.txt","a+");

fprintf(fp,"%s",S);

fclose(fp); }

else

{ fp=fopen("D:/124.txt","r+");

fgets(S,100,fp);

fclose(fp);

printf("\nВведите кодовое слово: ");

scanf("%s",&word1);

for(i=0;i<strlen(word1);i++)

kod[i]=word1[i];

i=-1;

for(int j=strlen(word1);j<52;j++)

{ re1: k=0;

i++;

for(int e=0;e<strlen(word1);e++)

if(word1[e]==alf[i] k=1;

if(!k)

kod[j]=alf[i];

else goto re1; }

S=rekode(S,kod); remove("D:/125.txt");

fp=fopen("D:/125.txt","a+");

fprintf(fp,"%s",S);

fclose(fp); }

printf("\n%s\t%s",S,kod);

fp=fopen("D:/124.txt","r+");

fclose(fp);

goto l1;

getch(); }

Мультипликативный

#include <stdio.h>

#include <stdlib.h>

#define C 3

void main(){ FILE*fp=fopen("output.bin","wb");

int a,b,m,kol, *X;

printf("enter limits and number of figures:\n");

scanf("%d %d %d", &a,&b,&kol);

m=b-a;

X=(int*)malloc(sizeof(int)*kol);

X[0]=rand()%m+a; printf("%d ", X[0]);

for(int i=0; i<kol-1; i++)

{X[i+1]=(C*X[i])%m;

if (X[i+1]<0) X[i+1]=X[i+1]+m;

X[i+1]=X[i+1]+a;

printf("%d ", X[i+1]); }

fwrite(X, kol*sizeof(int), 1, fp);

printf("\n");

system("pause"); }

Метод Вижинера

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

void shifr(char *a,int *key,int L, FILE*fp);

void anshifr(char *a,int *key,int L,FILE*fp);

char s[27]="abcdefghijklmnopqrstuvwxyz";

void main()

{ FILE*fp;

char *a, s[15];

int *key, L=0, i=0;

printf("Enter the name of open file: ");

fp=fopen(gets(s), "r");

while(!feof(fp)) {getc(fp); L++;} a=(char*)malloc((L)*sizeof(char));

rewind(fp); L=0;

while(!feof(fp)) { a[L]=getc(fp); L++;}

a[L]='\0'; fclose(fp);

printf("Enter the key: ");

gets(s);

while(s[i]!='\0') i++;

key=(int*)malloc(i*sizeof(char));

i=0;

while(s[i]!='\0') {key[i]=s[i]-97; i++;}

printf("Enter the name of result file: ");

fp=fopen(gets(s), "a+");

while(true)

{ system("cls");

printf("\nThe text: %s\n\n", a);

printf("\n1.Shifrovanie texta.\n2.Deshifrovanie texta.\n3.Exit.\n\nYour choice: ");

scanf("%d",&i);system("cls");

switch(i)

{ case 1:shifr(a,key,L,fp);

break;

case 2:anshifr(a,key,L,fp);

break;

default: fclose(fp);exit(1); } }}

void shifr(char *a,int *key, int L,FILE*fp)

{ int i,j,q=0;

for(i=0;i<L-1;i++)

{ if(a[i]!=' ')

{ for(j=0;j<26;j++)

if(a[i]==s[j])

break; a[i]=s[(key[q%6]+j)%26];

q++; }

else a[i]=' '; }

printf("\nThe text: %s\n\n", a);

fprintf(fp, "%s\n", a); }

void anshifr(char *a,int *key,int L,FILE*fp)

{ int i,j,q=0;

for(i=0;i<L-1;i++)

{ if(a[i]!=' ')

{ for(j=0;j<26;j++)

if(a[i]==s[j])

break;

if(j-key[q%6]<0) a[i]=s[j-key[q%6]+26];

else a[i]=s[j-key[q%6]];

q++; }

else a[i]=' '; }

printf("\nThe text: %s\n\n", a);

fprintf(fp, "%s\n", a); }