要求: 使用typedef
1,定义函数连续在堆区申请空间
2,定义函数循环输入
3,定义函数实现输出
3,定义函数实现查找
4,释放空间
调用函数文件:
#ifndef __HEAD1_H__
#define __HEAD1_H__
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[10];
int age;
float score;
}stu;
stu *Space(int n);
void Input(stu*p,int n);
void Output(stu*p,int n);
void Find(stu*p,int n);
#endif
函数声明文件:
#include "head1.h"
stu *Space(int n)
{
stu *p=(malloc)(sizeof(stu)*n);
if(p==NULL)
return NULL;
return p;
}
void Input(stu*p,int n)
{
for(int i=0;i<n;i++)
{
printf("请输入学生姓名:");
scanf("%s",(p+i)->name);
printf("请输入学生年龄:");
scanf("%d",&(p+i)->age);
printf("请输入学生分数:");
scanf("%f",&(p+i)->score);
}
}
void Output(stu*p,int n)
{
for(int i=0;i<n;i++)
{
printf("姓名:%s 年龄:%d 分数:%f\n",(p+i)->name,(p+i)->age,(p+i)->score);
}
}
void Find(stu*p,int n)
{
char key[5]="";
int m=0;
printf("输入key:");
scanf("%s",key);
for(int i=0;i<n;i++)
{
if(strcmp((p+i)->name,key)==0)
{
printf("姓名:%s 年龄:%d 分数:%f\n",(p+i)->name,(p+i)->age,(p+i)->score);
m++;
}
}
if(m==0)
printf("查找失败\n");
}
主函数文件:
#include "head1.h"
int main(int argc, const char *argv[])
{
int n=3;
stu *p= Space(n);
Input(p,n);
Output(p,n);
Find(p,n);
free(p);
p=NULL;
return 0;
}
代码运行结果: