• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wchenfeng

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

编写一个结构体数组查找函数find,若找到则输出该结构体变量,否则输出未找到。

#include <stdio.h>
#include <string.h>
typedef struct {
    int num;
    char name[20];
    int score;
} stu;
int find(stu list[], int list_len, char *find_name)
{
	int i,k;
	for(i=0;i<list_len;i++)
	{
		k=strcmp(find_name,list[i].name);
		 if (k==0)
			{
				return i;break;
		}
		 else return -1;
	}
}

void main(void)
{
    int i;
    stu a[5] = {
            { 1001, "CHEN", 80 },
            { 1002, "LI", 90 },
            { 1003, "WANG", 70 },
            { 1004, "ZHAO", 80 },
            { 1005, "ZHANG", 60 } };
    char b[20];

    gets(b);

    i = find(a, 5, b);
    if (i == -1) {
        printf("Nothing\n");
    } else {
        printf("%d %s %d\n", a[i].num, a[i].name, a[i].score);
    }
}


 

posted on 2022-04-12 20:03  王陈锋  阅读(39)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3