Title: write several functions
(1) Enter the names and employee numbers of ten employees;
(2) Sort by employee number from small to large, and the order of names will be adjusted accordingly;
(3) It is required to input an employee number, use half search to find out the name of the employee, input the employee number to be searched from the main function, and output the name of the employee
The algorithms include: selective sorting and half search.
The code is as follows:
/** Write several functions (1)Enter the names and employee numbers of ten employees (2)Sort by employee number from small to large, and then adjust the order of names (3)It is required to input an employee number, use half search to find out the name of the employee, input the employee number to be searched from the main function, and output the name of the employee; */ #include<stdio.h> #include<string.h> #define N 10 int main() { void input(int num[],char name[][8]);//Declaring function void sort(int num[],char name[][8]);//Declaring function void search(int n,int num[],char name[][8]);//Declaring function int num[N];//Used to enter the serial number char name[N][8];//Used to enter a name int n;//Used to enter the sequence number to query int flag=1;//Used to judge the cycle char c; input(num,name); sort(num,name); while(flag) { printf("Please input the number of searching:"); scanf("%d",&n); search(n,num,name); printf("Continue or not(Y/N)?"); getchar();//Absorb carriage return scanf("%c",&c); if(c == 'n' ||c == 'N') { flag=0; } } return 0; } /*main()*/ void input(int num[],char name[][8]) { for(int i=0;i<N;i++)//i is a cyclic variable { printf("Please input a number:"); scanf("%d",&num[i]);//Input serial number printf("Please input the name:"); getchar();//Absorb carriage return gets(name[i]);//Input name } }/*input()*/ void sort(int num[],char name[][8])//Selection sort function { int temp_n; //Intermediate variable char temp_c[8];//Intermediate variable for(int i=0;i<N-1;i++) { for(int j=i+1;j<N;j++) { if(num[j]<num[i]) { temp_n=num[j]; num[j]=num[i]; num[i]=temp_n; strcpy(temp_c,name[j]);//String exchange uses the strcpy() function strcpy(name[j],name[i]);//temp_c=name[j]; strcpy(name[i],temp_c);//name[j]=num[i]; //num[i]=temp_c; } } } printf("\n"); printf("The result:\n\n"); for(int i=0;i<N;i++) { printf("%5d %10s",num[i],name[i]); printf("\n"); } }/*sort()*/ void search(int n,int num[],char name[][8]) { int top=0; int bott=N-1; int mid=(top+bott)/2; int sign=0; while((sign == 0) && (top<=bott)) { if(n == num[mid]) { printf("The corresponding name of number=%d is: %s",n,name[mid]); sign=1; } else if(n>num[mid])//If n is greater than the number of intermediate positions, look backward { top=mid+1;//Start position changes, end position does not change mid=(top+bott)/2;//Intermediate position change } else//If n is less than the number of intermediate positions, look forward { bott=mid-1;//Start position unchanged, end position changed mid=(top+bott)/2; } } if(sign == 0) { printf("Can not find!"); } printf("\n"); }/*search()*/
The operation results are as follows:
The main problem encountered by the writer: the use of strcpy() function.
Prototype declaration: char *strcpy(char* dest, const char *src);
Header file: ාinclude< string.h >And include < stdio. H >
Function: copy the string with NULL terminator from src address to dest address space
Note: the memory area of src and dest cannot overlap, and dest must have enough space to hold the string of src. Returns the Pointer.
Novice Xiaobai, please give me more advice.