搜档网
当前位置:搜档网 › 结构体指针的典型应用(通过姓名查找相关信息)

结构体指针的典型应用(通过姓名查找相关信息)

结构体指针的典型应用(通过姓名查找相关信息)

对录入的学生的学号、年龄、分组、成绩通过输入姓名进行查找,如果没有则输出“not exist!”。

#include "stdafx.h"

#include

#include "string.h"

void main(){

struct stu{

char *name;

int num;

int age;

char group;

float score;

}stus[] = {

{"Zhou ping", 5, 18, 'C', 145.0},

{"Zhang ping", 4, 19, 'A', 130.5},

{"Liu fang", 1, 18, 'A', 148.5},

{"Cheng ling", 2, 17, 'F', 139.0},

{"Wang ming", 3, 17, 'B', 144.5} /*可以输入更多*/

}, *ps;

char st_name[23]; /*字符数可以增加*/

printf("input name:");

gets(st_name);

const n=sizeof(stus) / sizeof(struct stu);

int cla[n];

int h;

for(h=0;h

cla[h]=strcmp(stus[h].name,st_name);

int i,j=0;

for(i=0;i

{ if(cla[i]==0)

{ ps=stus+i;

printf("name:%s, number:%d, age:%d, group:%c, score:%.1f.\n",(*ps).name, (*ps).num, (*ps).age, (*ps).group, (*ps).score);

}

else

j=j+1;

}

if(j==n)

printf("not exist!\n");

}

相关主题