#include <stdio.h> #include <stdlib.h> #include <iostream>
using namespace std; const int MaxSize=8;
typedef struct { int stuno; char stuname[20]; int stuage; }TableElem;
TableElem stu[]={{1001,"zhangsan",20},{1002,"wangwu",18},{1003,"lisi",19},{1004,"zhaofeng",20}, {1005,"ligang",19},{1006,"wangjun",21},{1007,"chenguang",18},{1008,"zhangli",20}};
typedef struct { TableElem elem[MaxSize]; int n; }Sqtable; //学生的数组
int searchsqtable(Sqtable T,int key) { T.elem[0].stuno=key; //岗哨 int i=T.n; while(T.elem[i].stuno!=key) i--; return i; }
int main() { Sqtable seq; for(int i=0;i<MaxSize;i++) { seq.elem[i]=stu[i]; } seq.n=MaxSize; int kk,mm; printf("请输入要查找的学号:"); cin>>kk;
mm=searchsqtable(seq,kk); //cout<<mm<<endl; cout<<"该序号的姓名是:"<<seq.elem[mm].stuname<<"\t"<<"年龄:"<<seq.elem[mm].stuage<<endl; return 1; }
运行结果:
|
请发表评论