搜索
您的当前位置:首页正文

实验四 串与数组的表示与实现

来源:意榕旅游网
实验四 串和数组的表示与实现

一、实验目的

1.熟悉串的实现方法和模式匹配方法。 2.掌握矩阵转置的方法。

3.熟悉矩阵以及特殊矩阵的存储原理。 二、实验内容

1.串的几个操作的使用 2.实现的串的模式匹配方法 3.实现矩阵转置的方法 三、实验要求

1.认真阅读和掌握本实验的算法。

2.上机运行写出的程序,并且独立调试通过。 四、实验步骤

1、串的几个操作的使用 /*测试*/

#include \"stdio.h\" #include void main() {

char *s1=\"Golden Global View\"; char s2[50]=\"Data Structure\"; int len;

printf(\"%s\\n\ printf(\"%s\\n\ len=strlen(s1);//求串长 printf(\"%d\\n\

len=strcmp(s2,s1);//串比较 printf(\"%d\\n\ strcat(s2,s1);//串联接 printf(\"%s\\n\ strcpy(s2,s1);//串复制

printf(\"%s\\n\}

2、串的模式匹配方法 /*朴素的串的模式匹配方法*/

int index(char* s,char* t) {

i=0;j=0;

while(iif(s[i]==t[j]) {

i++; j++; } else {

j=0;

i=i-j+1; } }

if(j>=strlen(t)) {

return i-j+1; }

else

return 0; } /*测试*/

int main() {

char *s1=\"Geographic information system\"; char *s2=\"gra\";

int ind=index(s1,s2); printf(\"%d\\n\",ind); }

3、矩阵转置的实现 /*矩阵数据结构的设计*/

#define M 10//矩阵最大行数 #define N 10//矩阵最大列数 typedef int ElemType;

typedef struct matrix {

ElemType data[M][N]; int rows; int cols; }Matrix;

/*矩阵转置的算法*/

void transport(Matrix m1,Matrix* m2) {

int i,j;

for(i=0;ifor(j=0;jm2->data[j][i]=m1.data[i][j]; } }

m2->cols=m1.rows; m2->rows=m1.cols; } /*测试*/

int main() {

int m=2,n=3;//m表示矩阵行,n表示矩阵列 Matrix matr1,matr2;

//初始化matr1为一个2*3的矩阵(值为0到m*n-1) matr1.cols=3; matr1.rows=2;

for(int i=0;ifor(int j=0;jmatr1.data[i][j]=i*matr1.cols+j; } }

transport(matr1,&matr2);//将matr1转置为matr2 //输出matr2

for(int i=0;ifor(int j=0;j}

{

printf(\"%d \",matr2.data[i][j]); }

printf(\"\\n\");

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top