-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-11-10-25-22.04.c
85 lines (70 loc) · 1.82 KB
/
5-11-10-25-22.04.c
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
/* 5-11-10-25-22.04.c -- µÚÎåÕµÚʮһÌâ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUL '\0'
#define TABLESIZE 887
int main (void) ;
int get_key (char * const s, const int lenth) ;
int hash (const int key, const int size) ;
int get_ansi_value (const char c) ;
int discover_target (const char * const part, const char * const target) ;
int main (void)
{
char * source, * target, * part ;
int count, lenth_source, lenth_target, key, value_test, value_target ;
source = "My name is Palmer Chen.My ideal is that I can write code everyday." ;
target = "nam" ;
lenth_source = strlen (source) ;
lenth_target = strlen (target) ;
key = get_key (target, lenth_target) ;
value_target = hash (key, TABLESIZE) ;
for (count = 0; count < lenth_source; count++)
{
key = get_key (source + count, lenth_target) ;
value_test = hash (key, TABLESIZE) ;
if (value_test == value_target)
{
part = (char *) malloc (sizeof (char) * (lenth_target + 1)) ;
strncpy (part, source + count, lenth_target) ;
part[lenth_target] = NUL ;
if (discover_target (part, target))
{
puts (source + count) ;
break ;
}
}
}
return 0 ;
}
int get_key (char * const s, const int lenth)
{
char * scan = s ;
int count, key ;
for (count = 0, key = 0; count < lenth; count++)
key += get_ansi_value (scan[count]) ;
return key ;
}
int hash (const int key, const int size)
{
return key % size ;
}
int get_ansi_value (const char c)
{
int ansi_value = c ;
return ansi_value ;
}
int discover_target (const char * const part, const char * const target)
{
int lenth_part, lenth_target, count ;
lenth_part = strlen (part) ;
lenth_target = strlen (target) ;
if (lenth_part == lenth_target)
{
for (count = 0; count <= lenth_part; count++)
if (part[count] != target[count])
return 0 ;
return 1 ;
}
return 0 ;
}