⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8_15.c

📁 输入职工序号和姓名; 实现按职工号由大到小排序
💻 C
字号:
#include "stdio.h"
#include "string.h"
#define N 10
 void main()
{
    void input(int n[N],char name[N][8]);       /*函数声明*/
    void sort(int n[N],char name[N][8]);
    void find(int m,int n[N],char name[N][8]);
    int num[N]={0},flag=1, number;
    char c, name[N][8];
    input(num,name);
    sort(num,name);
    while(flag==1)         /*flag标记是否继续查找*/
      {
        printf("\nPlease input the number to look for :");
        scanf("%d",&number);
        find(number,num,name);
        printf("\ncontinue or not (Y/N)?");     /*判断是否继续查找*/
        getchar();
        c=getchar();
        if (c=='N'||c=='n')
        flag=0;
       }
    getch();
 }

 /*输入子程序*/

    void input(int n[N],char name[N][8])
{
    int i;
    for (i=0;i<N;i++)
      { printf("input NO."); /*分别输入序号及姓名,使序号及姓名位置相对应*/
        scanf("%d",&n[i]);
        printf("input name :");
        scanf("%s",name[i]);
       }
 }

 /*排序子程序*/

    void sort(int n[N],char name[N][8])
{
    int i,j,temp1;
    char temp2[8];
    for (i=0;i<N-1;i++)      /*用起泡法使职工号按由小到大排序,姓名也随之调整*/
      for (j=0;j<N-1-i;j++)
        if (n[j]>n[j+1])
         {
            temp1=n[j];  n[j]=n[j+1];  n[j+1]=temp1;

            strcpy(temp2,name[j]);

            strcpy(name[j],name[j+1]);

            strcpy(name[j+1],temp2);
         }
    printf("\n The result as follows:\n");
    for (i=0;i<N;i++)
      printf("\n %5d%10s",n[i],name[i]);
 }

 /*折半查找法子程序*/

    void find(int m,int n[N],char name[N][8])
{
    int top,bott,mid,loca,sign;
    top=loca=0;
    bott=N-1;
    sign=1;                /*sign标记是否存在有相应号码的职工*/
    if (m<n[0]||m>n[N-1])  /*loca标记输入号码是否超过职工号的范围*/
    loca=-1;
    while ((sign==1)&&(top<=bott))
    {
      mid=(top+bott)/2;
      if (m==n[mid])
        {
           loca=mid;
           printf("NO.%d,his name is %s",m,name[loca]);
           sign=-1;
         }
      else if (m<n[mid])
      bott=mid-1;
      else  top=mid+1;
     }
    if (sign==1||loca==-1)    /*判断是否查找得到*/
    printf("\nCan't not find NO.%d\n",m);
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -