Summary and experience of the fifth experiment

        This week we had the fifth experiment, experiment four times before I think is beginning to be familiar with the c language, you can write some simple program, but it is difficult or not, there are a lot of knowledge you need to know, and more Some remember frequently used programs, consolidate good c language. Here is the process of the fifth experiment:

Part1: ordered set of N integers (small to large) is stored in a pile array. Write a function binarySearch (), implemented using binary search algorithm to find a specific item in a one-dimensional integer array. If found, return the next item in the array element subscript; if the item is not in the array, or -1.

1. The parameter is an array, the array name argument is used to achieve direct access array elements. Binding binary search code comments and description, to understand the program, the program complement.

 

#include  <stdio.h>
const int N=5;
int binarySearch(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" ); 
    The printf ( " Enter be searched data items: " ); 
    Scanf ( " % D " , & Key); 
    index = binarySearch (A, N, Key); 
     IF (index> = 0 ) 
        the printf ( " % D in an array, the subscript of% D \ n- " , Key, index);
     the else 
        the printf ( " % D from the array \ n- " , Key); 
    return  0 ; 
} 
int binarySearch ( int X [], int n-, int Item) 
{ 
    int low, high, mid;
    low = 0;
    high = n-1;
    while(low <= high)
{
        mid = (low+high)/2;     
        if (item == x[mid])
            return mid;
        else if(item < x[mid])
            high = mid - 1;
        else
            low = mid + 1;
    }   
    return -1;
}

 

  

 

2. The parameter is a pointer variable, the argument is the name of the array, the pointer variable used to achieve an indirect access method, binary search algorithm is described in conjunction with code comments and understand the program and make up the program. 

#include  <stdio.h>
const int N=5;
int binarySearch(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" ); 
    The printf ( " Enter be searched data items: " ); 
    Scanf ( " % D " , & Key); 
    index = binarySearch (A, N, Key);
     IF (index> = 0 ) 
        the printf ( " % D in an array, the subscript of% D \ n- " , Key, index);
     the else 
        the printf ( " % D from the array \ n- " , Key); 
    return  0 ; 
} 
int binarySearch ( int * X, int n-, int Item ) 
{ 
    int low, high, mid;
    low = 0;
    high = n-1;
    while(low <= high) 
{
        mid = (low+high)/2;
        if (item == *(x+mid))
            return mid;
        else if(item < *(x+mid))
            high = mid - 1;
        else
            low = mid + 1;
    }
    return -1;
}

   

Completion of this part of the program that I can understand, is doing in school, or to do good.

Part2: ascending sort a set of data Sort method.

c 1. Select method implements the following: a set of integers in ascending order.

#include <stdio.h>
 const  int N = . 5 ;
 void SelectSort ( int [], int );
 void Output ( int [], int );
 int main () 
{ 
   int A [N]; 
   the printf ( " Enter% d integer \ n- " , N); 
   INPUT (a, N); 
   the printf ( " pre-sorted data: \ n- " ;) 
   Output (a, N); 
   ; SelectSort (a, N) 
   the printf ( " sorted data : \ n- " ); 
   Output (A, N); 
   return  0  ;
}
void input(int a[], int n) 
{
   int i;
   for(i=0; i<n; i++)
   scanf("%d", &a[i]);
}
void output(int a[], int n) 
{
   int i;
   for(i=0; i<n; i++)
   printf("%d ", a[i]);
   printf("\n");
}
void selectSort(int a[], int n) 
{
   int i, j, k, temp;
   for(i=0; i<n-1; i++) 
   {
      k = i; 
      for(j=i+1; j<n; j++)
      if (a[j] < a[k])
      k = j; 
      if(k != i)
    { 
      temp = a[i];
      a[i] = a[k];
      a[k] = temp;
      }
   }
}

This program is to allow us to better understand the ranking selection method, have to do in front of the program, more than a deeper impression.

2. complement program selection method used by lexicographically sorting strings.

#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 ++ ) 
   the printf ( " % S \ n- " , name [I]); 
   SelectSort (name, . 5 ); 
   the printf ( " lexicographical ordering output list: \ n- " );
    for (I = 0 ; I < . 5 ; I ++ ) 
   the printf ( " % S \ n- " , name [I]);
    return  0 ; 
} 
void SelectSort ( char STR [] [ 20 is ], int n-) 
{ 
   int I, J, K;
    char TEMP [ 20 is ];
   for(i=0; i<n-1; i++) 
   {
      k = i; 
    for(j=i+1; j<n; j++)
    if (strcmp(str[j],str[k])<0)
      k = j;        
    if(k != i)
     {
        strcpy(temp,str[i]);
        strcpy(str[i],str[k]);
        strcpy(str[k],temp);
       }
   }
}

This procedure with thought for a moment, less likely to do, there is also a reference book, or to try to understand selection method ah.

Part3: string pointer processing.

 Exercise 1: Assuming that the input string contains only letters and *, such as a string **** A * BC * DEF * G *******. Functions written delPrefixStar (), delete the string * deletes all leading, intermediate and rear * not removed. That is, after deleting the contents of the string should be A * BC * DEF * G *******

#include <stdio.h>
 void delPrefixStar ( char []); 
 int main () 
{ 
    char  String [ 80 ]; 
    the printf ( " Enter a string: \ n- " ); 
    the gets ( String ); 
    the printf ( " \ n-Delete string <* leader> before: \ n " ); 
    the puts ( string ); 
    delPrefixStar ( string );   
    printf ( " string after \ n delete <leading *>: \ n " ); 
    the puts ( string );
     return  0 ; 
} 
void delPrefixStar(char s[])
{
    char *target, *source;
    source = s;
    while(*source == '*') 
        source++;
    target = s;
    while( *target++ = *source++);
}

 Exercise 2: Assuming that the input string contains only letters and *, such as a string **** A * BC * DEF * G *******. Functions written delStarButPrefix (), in addition to leading *, * delete other. That is, after deleting the contents of the string should be **** ABCDEFG

#include <stdio.h>
void delStarButPrefix(char []); 
int main() 
{
    char string[80];
    printf("输入一个字符串:\n");
    gets(string);
    printf("\n删除<中间和末尾的*>之前的字符串:\n");
    puts(string);
    delStarButPrefix(string); 
    printf("\n删除<中间和末尾的*>之后的字符串:\n");
    puts(string);
    return 0;
} 
void delStarButPrefix(char s[])
{
    int i=0;              
    char *p = s;
    while(*p && *p == '*') 
    {
        p++;
        i++;
    }
    while(*p) 
    {
       if(*p != '*')
        {
           s[i] = *p;
           i++;
        }
        p++;
    } 
    s[i] = '\0'; 
}

 练习3:假定输入的字符串中只包含字母和*,例如字符串****A*BC*DEF*G*******。编写子函数delMiddleStar(),除了前导*和尾部*之外,删除中间出现的所有*。即删除后,字符串内容应当是****ABCDEFG*******

#include <stdio.h>
void delMiddleStar(char []); 
int main()
{
    char string[80];
    printf("输入一个字符串:\n");
    gets(string);
    printf("\n删除<中间的*>之前的字符串:\n");
    puts(string);
    delMiddleStar(string); 
    printf("\n删除<中间的*>之后的字符串:\n");
    puts(string);
    return 0;
} 
void delMiddleStar(char s[]) 
{
    int i=0;              
    char *tail, *head, *p;
    tail = s;
    while(*tail)
    tail++;
    tail--;
    while(*tail == '*')
    tail--;
    head = s;
    while(*head == '*')
    head++;  
    p = s;
    while(p<=head) 
    {  
       s[i] = *p;
       p++;
       i++;
    }
    while(p<tail) 
    {
        if(*p != '*') 
        {
           s[i] = *p;
           i++;
        }
        p++;
    }
    while(*p) 
    {
        s[i] = *p;
        i++;
        p++;
    }
    s[i] = '\0'; 
}

这一部分我还是写不出属于自己的程序,还是要多练,多巩固。

实验的总结与体会:

关于二分查找算法的内容,在二分查找算法中数组名作为参数时,形参应该是一个地址变量,实参向形参传递数组名实际上就是传送数组的地址。指针变量作为参数时,通过地址来改变主函数中变量值。写编程时还是出现了一些语法上的错误,遇到难的问题还是会参考书籍,我还得继续努力,多看看程序题,多动手实践,好好积累知识。

 

 

 
 

Guess you like

Origin www.cnblogs.com/super123-/p/10926619.html