帮你改了下,在C-FREE 3.5下运行通过
#include<stdio.h>
#include<conio.h>
struct worklist
{
int num;
char name[10];
}worker[10] = {{2,"wang"},{1,"li"},{3,"zhang"},{7,"yang"},{8,"sun"},{5,"you"},
{4,"miao"},{9,"wu"},{10,"han"},{6,"zhao"}};
void xuanze()
{
int i, j, k;
struct worklist temp;
for (i = 0; i < 9; i++)
{
k = i;
for (j = k+1; j < 10; j++)
{
if (worker[k].num > worker[j].num)
{
k = j;
}
}
if(k != i)
{
temp = worker[i];
worker[i] = worker[k];
worker[k] = temp;
}
}
}
char *find(int n)
{
int low, mid, high, i;
for(low = 0, high = 9; high >= low; )
{
mid = (low + high) / 2;
if(worker[n-1].num == worker[mid].num)
{
return worker[mid].name;
//break; //已经return了,break就没用
}
else if(worker[n-1].num > worker[mid].num)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
}
void maopao()
{
int i,j;
struct worklist temp;
for(i = 0; i < 9; i++)
{
for(j = i; j < 9; j++)
{
if(worker[j].num > worker[j+1].num)
{
temp = worker[j];
worker[j] = worker[j+1];
worker[j+1] = temp;
}
}
}
}
int main(void)
{
int i, num;
char *p;
//char b[30]; //这个数组没用吧?
printf("打印初始序列:\n");
for(i = 0; i < 10; i++)
{
printf("%-2d %-4s\n", worker[i].num, worker[i].name); // 加了打印格式,用于对齐
}
//maopao();
xuanze();
printf("打印排序后的序列:\n");
for(i = 0; i < 10; i++)
{
printf("%-2d %-4s\n",worker[i].num,worker[i].name);
}
printf("Please Input a number:");
scanf("%d", &num);
p = find(num);
printf("%s\n", p);
getch();
return 0;
}
[此贴子已经被作者于2007-4-12 14:53:09编辑过]
猝然临之而不惊,无故加之而不怒 /?spaced" target="_blank">Linux C资料