v
- /* 二分法查找 */
- #include<stdio.h>
- #define N 5
- int brainsearch(int *x,int n ,int item );
- int main()
- {
- int a[N]={1,3,9,16,21};
- int i,index,key;
- printf("数组 a 中的数据:\n");
- for(i=0;i<N;i++)
- printf("%d",a[i]);
- printf("\n");
- printf("待查找的数据:\n");
- scanf("%d",&key);
- index=brainsearch(a,N,key);
- if(index>=0)
- printf("%d 在数组中, 下标为 %d\n",key,index);
- else
- printf("%d 不在数组中 \ n",key);
- return 0;
- }
- int brainsearch(int *x,int n,int item)
- {
- int *p,low=0,high=n-1,mid;
- p=x;
- while(low<=high)
- {
- mid=(high+low)/2;
- if(*(p+mid)==item)
- return mid;
- else if(*(p+mid)<item)
- low=mid+1;
- else if(*(p+mid)>item)
- high=mid-1;
- }
- return -1;
- }
v
v
- #include<stdio.h>
- #include<string.h>
- void selectsort(char str[][20],int n);
- int main()
- {
- char name[][20]={"John","Alex","Joseph","Candy","Geoge"};
- int i;
- printf("输出初始名单:\n");
- for(i=0;i<5;i++)
- printf("%s\n",name[i]);
- selectsort(name,5);
- printf("按字典序输出名单:\n");
- for(i=0;i<5;i++)
- printf("%s\n",name[i]);
- return 0;
- }
- void selectsort(char str[][20],int n)
- {
- int j,i=0;
- char t[100];
- for(i=0;i<n;i++)
- for(j=i+1;j<=n-1;j++)
- if(strcmp(str[i],str[j])>0)
- {
- strcpy(t,str[i]);
- strcpy(str[i],str[j]);
- strcpy(str[j],t);
- }
- }
v
- /* 用指针处理字符, 删除前导 * */
- #include<stdio.h>
- void delPrefixStar(char[]);
- int main()
- {
- char string[80];
- printf("输入一个字符串:\n");
- gets(string);
- printf("\n 删除前导 * 之前的字符串:\n");
- puts(string);
- delPrefixStar(string);
- printf("\n 删除前导 * 后的字符串:\n");
- puts(string);
- return 0;
- }
- void delPrefixStar(char s[])
- {
- char *target,*source;
- source=s;
- while(*source=='*')
- source++;
- target=s;
- while(*source)
- {
- *target=*source;
- source++;
- target++;
- }
- *target='\0';
- }
V
- #include<stdio.h>
- void delPrefixStar(char[]);
- int main()
- {
- char string[80];
- printf("输入一个字符串:\n");
- gets(string);
- printf("\n 删除中间的和后面的 * 前的字符串:\n");
- puts(string);
- delPrefixStar(string);
- printf("\n 删除中间和后面的 * 后的字符串:\n");
- puts(string);
- return 0;
- }
- void delPrefixStar(char s[])
- {
- char *source,*target;
- source=s;
- while(*source=='*')
- source++;
- target=source;
- while(*source)
- {
- source++;
- if(*source!='*')
- {
- target++;
- *target=*source;
- }
- }
V
- #include<stdio.h>
- void delPrefixStar(char[]);
- int main()
- {
- char string[80];
- printf("输入一个字符串:\n");
- gets(string);
- printf("\n 删除中间的 * 之前的字符串:\n");
- puts(string);
- delPrefixStar(string);
- printf("\n 删除中间的 * 后的字符串:\n");
- puts(string);
- return 0;
- }
- void delPrefixStar(char s[])
- {
- char *source,*tail,*target;
- tail=s;
- while(*tail)
- tail++;
- tail--;
- while(*tail=='*')
- tail--;
- source=s;
- while(*source=='*')
- source++;
- target=source;
- while(*source&&source<tail)
- {
- source++;
- if(*source!='*')
- {
- target++;
- *target=*source;
- }
- }
- while(*source)
- {
- source++;
- target++;
- *target=*source;
- }
- }
- /* 使用选择法对一组整数由大到小排序 */
- #include<stdio.h>
- #define N 5
- void input(int[],int);
- void output(int[],int);
- void selectsort(int [],int);
- int main()
- {
- int a[N];
- printf("输入 5 个整数:\n");
- input(a,N);
- selectsort(a,N);
- printf("排序后的数据:\n");
- output(a,N);
- return 0;
- }
- void input(int x[],int n)
- {
- int i=0;
- for(i=0;i<n;i++)
- scanf("%d,",&x[i]);
- }
- void output(int y[],int f)
- {
- int i=0;
- for(i=0;i<f;i++)
- printf("%d",y[i]);
- }
- void selectsort(int z[],int m)
- {
- int i,t,k,j;
- for(i=0;i<m-1;i++)
- {
- k=i;
- for(j=i+1;j<=m-i;j++)
- {
- if(z[k]>z[j])
- k=j;
- }
- if(k!=i)
- {
- t=z[i];
- z[i]=z[k];
- z[k]=t;
- }
- }
- }
实验结论: 选择法排序时, 输入的元素必须按从大到小的顺序排列好;
坑: 在利用指针删除字符串前导 "*" 时, 在被调用的函数中, 一开始没有把 * target 赋值',0'', 认为它指向的变量已经是',0', 而这是错误的; 如果不人为地操作 * target=',0', 电脑会自动赋值, 产生错误结果;
实验五
来源: http://www.bubuko.com/infodetail-3074135.html