KMP字符匹配算法:(具体理论知识参见 严蔚敏的《数据结构(C语言版)》字符串那一章节)
代码:
1 //index_kmp.c
2 //KPM搜索算法
3 //
4 #include <stdio.h>
5 #include <string.h>
6
7 //---串的定长顺序存储表示-----
8 #define MAXSTRLEN 255 //用户可在255以内定义最大串长
9 typedef unsigned char SString[MAXSTRLEN + 1];//0号单元存放串的长度
10
11 int index_KMP(SString S, SString T, int pos);
12 void get_next(SString T, int next[]);
13 //接收char * 类型的字符串,以便测试
14 int get_index(char * Sstr, char * Tstr, int pos);
15
16 int main()
17 {
18 SString S = "xabcdefghijklmn",
19 T = "xghi";
20 char a[2] = "12";
21 sprintf(a,"%c%c",15,3);
22 printf("%c\n%c\n",T[0],T[1]);
23 S[0] = a[0];
24 T[0] = a[1];
25 printf("S = %s\n",&S[1]);
26 printf("T = %s\n",&T[1]);
27 printf("S[0] = %d\n",S[0]);
28 printf("T[0] = %d\n",T[0]);
29 int index = index_KMP(S, T, 1);
30 printf("k1 = %d\n", index);
31 char * Sstr = "abcdefghijklmn",
32 * Tstr = "ghi";
33 index = get_index(Sstr, Tstr, 1);
34 printf("k2 = %d\n", index);
35 return 0;
36 }
37
38 //
39 //接收char * 类型的字符串,以便测试
40 int get_index(char * Sstr, char * Tstr, int pos)
41 {
42 SString S,T;
43 sprintf(S,"%c%s",strlen(S),Sstr);
44 sprintf(T,"%c%s",strlen(T),Tstr);
45 return index_KMP(S, T, 1);
46 }
47
48 int index_KMP(SString S, SString T, int pos)
49 {
50 int i = pos,
51 j = 1;
52 int next[MAXSTRLEN + 1];
53 get_next(T, next);
54 while (i <= S[0] && j <= T[0])
55 {
56 if (j == 0 || S[i] == T[j])
57 {
58 ++i;
59 ++j;
60 }
61 else
62 {
63 j = next[j];//不匹配,j回溯
64 }
65 }
66 if (i <= S[0])
67 {
68 return i-T[0];
69 }
70 else
71 {
72 return 0;
73 }
74 }
75 void get_next(SString T, int next[])
76 {
77 int i = 1,
78 j = 0;
79 next[1] = 0;
80 printf("T[0] = %d\n",T[0]);
81 while (i < T[0])
82 {
83 if (j == 0 || T[i] == T[j])
84 {
85 ++i;
86 ++j;
87 if (T[i] != T[j])
88 {
89 next[i] = j;
90 }
91 else
92 {
93 next[i] = next[j];
94 }
95 printf("%d : %d ",i,next[i]);
96 }
97 else
98 {
99 j = next[j];//回溯查找匹配个数
100 }
101 }
102 printf("\n");
103 }
我是在Linux下平台完成的,所以写了个简单的makefile
obj = index_kmp.o
index:$(obj)
cc -o index $(obj)
$(obj):index_kmp.c
cc -c index_kmp.c
clean:
rm index index_kmp.o
下面的两个代码是《数值分析》的内容,具体理论知识详见 求积分的那一章节
Simpson求积分
1 //simpsom.c
2 //使用辛普森求积公式
3 //区间[a,b]分为n等分
4 //h=(b-a)/n;
5 //x(k)=a+k*h;
6 //x(k+1/2)=x(k)+h/2;
7 //Sn=(h/6)*( f(a) + 4*sum(f(x(k)+1/2)) + 2*sum(f(x(k))) + f(b) );
8 //Rn =(-(b-a)/180 ) * (h/2)^4 * f''''(n)
9 //
10
11 #include <stdio.h>
12 #include <math.h>
13
14 float simpson(float a, float b, int n);
15 float f(float x);
16
17 int main()
18 {
19 float a=1,b=10,n=10;
20 float Sn = simpson(a,b,n);
21 printf("%f\n", Sn);
22 }
23
24 float simpson(float a, float b, int n)
25 {
26 float h = (b-a)/n;
27 float sum4 = 0,//序数为4的求和项
28 sum2 = 0;//序数为2的求和项
29 float xk = a,
30 xk2 = xk + h/2;
31 sum4 = f(xk2);
32 int k;
33 for (k=1; k <= n-1; k++)
34 {
35 xk = a + k*h;
36 xk2 = xk + h/2;
37 sum4 += f(xk2);
38 sum2 += f(xk);
39 }
40 sum4 *= 4;
41 sum2 *= 2;
42 float Sn = (h/6) * (f(a) + sum4 + sum2 + f(b));
43 return Sn;
44 }
45
46 //被积函数
47 float f(float x)
48 {
49 return x*x*x;
50 }
Romberg数值求积分
1//Romberg.c
2 //使用龙贝格算法求积分
3 //
4
5 #include <stdio.h>
6 #include <math.h>
7 #include <stdlib.h>
8
9 //T[k][m]表示积分表
10 float * romberg(float a, float b, int k, int m, float (* f)(float x));
11 //测试函数
12 float f(float x);
13
14 //测试函数
15 float f(float x)
16 {
17 return pow(x,1.5);
18 }
19
20
21 int main()
22 {
23 float a = 0, b = 1;
24 int k = 6, m = 6;
25 float * T = romberg(a,b,k,m,f);
26 int i,j;
27 for (i = 0; i<k; i++)
28 {
29 printf("k=");
30 for (j = 0; j<=i; j++)
31 {
32 printf("%d %f ",i,T[i*m+j]);
33 }
34 printf("\n");
35 }
36 free(T);
37 T = NULL;
38 return 0;
39 }
40
41 //T[k][m]表示积分表
42 float * romberg(float a, float b, int k, int m, float (* f)(float x))
43 {
44 float * T;
45 T = (float *)malloc(k*m*sizeof(float));
46 T[0] = (b-a)*(f(a) + f(b))/2;
47 int kk;
48 for (kk = 1; kk <= k; kk++)
49 {
50 int j;
51 float sum = 0;
52 for (j = 0; j <= pow(2.0,kk-1) - 1; j++)
53 {
54 sum += f(a + (2*j+1)*(b-a)/pow(2.0,kk));
55 }
56 sum *= (b-a)/pow(2.0,kk);
57 //printf("%f\n", sum);
58 T[kk*m+0] = T[(kk-1)*m+0]/2 + sum;
59 int mm;
60 for (mm = 1; mm <= m; mm++)
61 {
62 T[kk*m+mm] = (pow(4.0,mm)*T[kk*m+mm-1] - T[(kk-1)*m+mm-1]) / (pow(4.0,mm) - 1);
63 }
64
65 }
66 return T;
67 }
makefile 还是很好用的
Romberg: Romberg.o
cc -lm -o Romberg
Romberg.o: Romberg.c
cc -c Romberg.c
clean:
rm *.o Romberg
以上代码均在GCC下编译运行通过。