Repository navigation
Expand file tree
/
Copy pathencode.c
More file actions
98 lines (98 loc) · 2 KB
/
Copy pathencode.c
File metadata and controls
98 lines (98 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char code;
struct node* link;
}Node;
int dex[200];
Node* eskey(char key[])
{
Node* h=(Node*)malloc(sizeof(Node));
Node* p=(Node*)malloc(sizeof(Node));
Node* q=(Node*)malloc(sizeof(Node));
p=h;
p->code=key[0];
int i,n=strlen(key);
for(i=1;i<n;i++)
{
q->code=key[i];
p->link=q;
p=p->link;
q=(Node*)malloc(sizeof(Node));
}
free(q);
p->link=h;
return h;
}
int main()
{
FILE *in;
if((in=fopen("in.txt","r"))==NULL)
{
perror("Can't open the file:");
exit(0);
}
else
{
int i,n,temp,code[200]={0};
char key[100],c;
while((c=getchar())!='\n')
{
if(dex[c]==0)
{
key[i++]=c;
dex[c]=1;
}
}
n=strlen(key);
for(i=32;i<=126;i++)
{
if(dex[i]==0)
{
key[n++]=i;
}
}
key[n]='\0';
Node* q = (Node*)malloc(sizeof(Node));
Node* p = (Node*)malloc(sizeof(Node));
p=eskey(key);
for(q=p;q->link!=p;q=q->link);
n=p->code;
while(p->link!=q)
{
temp=p->code;
q->link=p->link;
p=p->link;
for(i=0;i<temp-1;i++)
{
p=p->link;
q=q->link;
}
code[temp]=p->code;
}
temp=p->code;
code[temp]=q->code;
code[q->code]=n;
FILE *out;
if((out=fopen("in_crpyt.txt","w"))==NULL)
{
perror("Can't open the file:");
exit(0);
}
else
{
while(fscanf(in,"%c",&c)!=-1)
{
if(c>=32&&c<=126)
fprintf(out,"%c",code[c]);
else
{
fprintf(out,"%c",c);
}
}
}
}
return 0;
}