《C程序设计》之数组实验报告【DOC精选】.docVIP

  • 33
  • 0
  • 约 5页
  • 2017-02-16 发布于江苏
  • 举报

《C程序设计》之数组实验报告【DOC精选】.doc

实验项目:数组(二) 实验内容: 实验1:输入两个字符串A和B,判断字符串B是否在A中出现,如果出现,给出在A中出现的位置(即B 串的第一个字符在A中第一次出现的位置,从一开始计)。 示例:A串:abcabcdeabcdefg B串:abcde 结果:B串包含在A串中,位置是4 实验2:输入10个人的姓名(拼音),用冒泡法排序,然后再用折半法查找给定的一个人名是否存在,如果存 在给出位置。 姓名示例:Long Zhengji 实验3:实现字符串联。 程序实现: 程序: 实验1:字符串的包含关系 #includestdio.h #includestring.h void main() { char A[20],B[20]; int n,m=0,i,j,a,b; printf(输入字符串A:); gets(A); printf(输入字符串B:); gets(B); a=strlen(A); b=strlen(B); printf(\n); for(i=0;i=a-1;i++) { n=1; if(A[i]==B[0]) { n=0; for(j=1;j=b-1;j++) if(A[i+j]!=B[j]) n++;} if(!n) { printf(B串包含在A串中,位置是%d\n,i+1); m++; }} if(m==0) printf(B串不在A串中\n);} 实验2:输入10个人的姓名(拼音),用冒泡法排序,然后再用折半法查找给定的一个人名是否存在,如果存在给出位置。 #include stdio.h #include string.h #define N 11 void main() {char temp[10],name[10]; char c; char str[N][20]; int i,j,sign,top,bott,loca,flag=1,mid; printf(请输入10个人的名字:); printf(\n); for(i=0;i11;i++) gets(str[i]); for(j=0;j11;j++) for(i=0;i10-j;i++) if(strcmp(str[i],str[i+1])0) {strcpy(temp,str[i]); strcpy(str[i],str[i+1]); strcpy(str[i+1],temp); } printf(对此10人的名字的排序为:\n); for(i=0;i11;i++) printf(%s\n,str[i]); printf(\n); while(flag) { printf(要查找的名字:); scanf(%s,name); sign=0; top=0; bott=N-1; if(strcmp(name,str[0])0||strcmp(name,str[N-1])0) loca=-1; while((!sign)(top=bott)) {mid=(bott+top)/2; if(strcmp(name,str[mid])==0) {loca=mid; printf(找到了:%s,位于:%d\n,name,loca+1); sign=1;} else if(strcmp(name,str[mid])0) bott=mid-1; else top=mid+1; } if(!sign||loca==-1) printf(没有此名\n); printf(是否继续(Y/N)?); scanf( %c,c); if(c==N||c==n) flag=0; } } 实验3:实现字符串联: #includestdio.h void main() {char s1[80],s2[40]; int i=0,j=0; printf(输入字符1:); scanf(%s,s1); printf(输入字符2:); scanf(%s,s2); while(s1[i]!=\0) i++; while(s2[j]!=\0) s1[i++]=s2[j++]; s1[i]=\0; printf(串起来的字符是:%s\n,s1); } 运行结果: 实验1:字符串的包含关系 B在A中: B不在A中: 实验2:输入10个人的姓名(拼音),用冒泡法排序,然后再用折半法查找给定的一个人名是否存在,如果存在给出位置。 实验3:实现字符串联: 实验总结: 加深了对字符的使用的掌握;进一步对数组的掌握;对与数组有关的算法如冒泡法排序、折半查找法的了解与应用。

文档评论(0)

1亿VIP精品文档

相关文档