作业:
定义学生结构体属性:姓名,年龄,分数,输入姓名key查找key是否出现,存在则输出所有信息,否则提示查找失败要求:使用typedef
1,定义函数连续在堆区申请空间2,定义函数循环输入
3,定义函数实现输出3,定义函数实现查找4,释放空间
head.h
#ifndef __head_h__
#define __head_h__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[16];
int age;
float score;
// mess no;
}stu1,*stu2;
/*typedef union
{
int mess1[10];
int mess2[10];
}mess;*/
stu2 Prace(int n);
void Input(stu2 p,int n);
void Output(stu2 p,int n);
//char getkey(stu2 p,int n);
char look(stu2 p,int n);
#endif
text.c
#include"head.h"
stu2 Prace(int n)
{
stu2 p=(stu2)malloc(sizeof(stu1)*n);
if(NULL==p)
{
return NULL;
}
else
return p;
}
void Input(stu2 p,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("输入姓名\n");
scanf("%s",(p+i)->name);
printf("输入年龄\n");
scanf("%d",&(p+i)->age);
printf("输入成绩\n");
scanf("%d",&(p+i)->score);
}
}
void Output(stu2 p,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("姓名:%s 年龄:%d 成绩:%d \n",(p+i)->name,(p+i)->age,(p+i)->score);
}
}
/*char getkey(stu2 p,int n)
{
int i,key[15];
printf("输入名字\n");
scanf("%s",key);
for(i=0;i<15;i++)
{
if(key[i]!='\0')
{
return k
}
return key[i]
}
}
*/
char look(stu2 p,int n)
{
int i,count;
char q[15];
scanf("%s",q);//输入查找
for(i=0;i<n;i++)
{
if(strcmp(q[0],(p+i)->name)==0)
{
count++;
printf("%s %d %d\n",(p+i)->name,(p+i)->age,(p+i)->score);
}
}
if(count)
{
printf("%d\n",count);
};
}
mian.c
#include "head.h"
int main(int argc, const char *argv[])
{
int n=5;
char key[16];
//借空间
stu2 p=Prace(n);
//输入
Input(p,n);
//输出
Output(p,n);
//查找
printf("请输入查找名字\n");
//key[]=getkey(p,n);
printf("%c",look(p,n));
//释放
free(p);
p=NULL;
return 0;
}