静态顺序表查找
#include<stdio.h>
#include<stdlib.h>
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)<=(b))
#define N 5
typedef int Status;
typedef int KeyType;
typedef struct
{
char number[21];
char name[13];
int politics;
int Chinese;
int English;
int math;
int physics;
int chemistry;
int biology;
KeyType key;
}ElemType;
ElemType r[N] = { {"20175968452","何芳芳",85,89,98,100,93,80,47},
{"20175968453","陈红",85,86,88,100,92,90,45},
{"20175968454","陆华",78,75,90,80,95,88,37},
{"20175968455","张平",82,80,78,98,84,96,40},
{"20175968456","赵小怡",76,85,94,57,77,69,44} };
#define total key
typedef struct {
ElemType* elem;
int length;
}SSTable;
Status Creat_Seq(SSTable* ST, int n) {
int i;
ST->elem = (ElemType*)calloc(n + 1, sizeof(ElemType));
if (!(ST->elem)) {
return 0;
}
for (i = 1; i <= n; i++) {
*(ST->elem + i) = r[i - 1];
}
ST->length = n;
return 1;
}
int Search_Seq(SSTable ST, KeyType key)
{
int i;
ST.elem[0].key = key;
for (i = ST.length; !EQ(ST.elem[i].key, key); --i);
return i;
}
Status Destroy(SSTable* ST)
{
free((*ST).elem);
(*ST).elem = NULL;
(*ST).length = 0;
return 1;
}
Status Traverse(SSTable ST, void(*Visit)(ElemType))
{
ElemType* p;
int i;
p = ++ST.elem;
for (i = 1; i <= ST.length; i++)
Visit(*p++);
return 1;
}
void print(ElemType c)
{
printf("%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\n", c.number, c.name, c.politics, c.Chinese, c.English, c.math, c.physics, c.chemistry, c.biology, c.total);
}
int main() {
SSTable st;
int i, s;
for (i = 0; i < N; i++)
r[i].total = r[i].politics + r[i].Chinese + r[i].English + r[i].math + r[i].physics + r[i].chemistry + r[i].biology;
Creat_Seq(&st, N);
printf("准考证号\t姓名\t政治\t语文\t外语\t数学\t物理\t化学\t生物\t总分\n");
Traverse(st, print);
printf("请输入待查找人的总分: ");
scanf("%d", &s);
i = Search_Seq(st, s);
if (i)
print(*(st.elem + i));
else
printf("没找到\n");
Destroy(&st);
}