输入10个职工姓名和职工号;按职工号从小到大排序并调整姓名顺序;输入工号查询姓名。

本文介绍了一个使用C语言实现的简单程序,该程序能够输入并存储员工编号和姓名到数组中,然后对这些数据进行排序,并提供一个二分搜索功能以查找特定的员工编号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>

#define N 10
int num[N];
char name[N][8];

int main()
{
    voidinput(void);
    voidsort(void);
   input();
   sort();
    intpoint,m;
    char c ='Y';
   while(c=='Y'||c=='y')
    {
       printf("\nplease input the num you want to search:\n");
       scanf("%d",&m);
       getchar();
       point = search(m);
       if(point==-1)
           printf("Can't find him(her)\n");
       else printf("the result is :No.%d is%s",num[point],name[point]);

       printf("Do you want to continue?(Y/N)");
       c=getchar();
    }
    return0;
}

void input(void)
{
    int i =0;
   printf("Input the number and name:\n");
   printf("No.\t Name\n");
    for(i =0;i
    {
       printf("Input No.:");
       scanf("%d",&num[i]);
       getchar();
       printf("Input name:");
       gets(name[i]);
    }
   getchar();
}//input there is a bug when enter names overcome 8 chars.No todayto deal with it,until writing data structure codes

void sort(void)
{
    int i = 0,j= 0,min = 0,temp;
    chartemp_char[8];

    for(i =0;i
    {
       min = i;
       for(j = i;j
       {
           if(num[j]
               min = j;
       }
       strcpy(temp_char,name[min]);
       strcpy(name[min],name[i]);
       strcpy(name[i],temp_char);

       temp = num[min],num[min] = num[i],num[i] = temp;
    }
   printf("\n  The sorted worker-table is:\n");
    for(i =0;i
    {
       printf("%d\t %s\n",num[i],name[i]);
    }
}//sort

int search(int m)//bug
{
    inttop,bott,mid;
    top =0;
    bott =N-1;

   if(mnum[N-1])
       return -1;
   while(top<=bott)
    {
       mid = (top+bott)/2;
       if(m==num[mid])
           return mid;
       else if(m
           bott = mid-1;
       else
           top = mid +1;
    }
    return-1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值