typedef为C语言的关键字,作用是为一种数据类型(基本数据类型或自定义数据类型)定义一个新名字,不能创建新类型。
命名规定习惯上把用typedef声明的类型名的第一个字母用大写表示,以便与系统提供的标准类型标识符相区别。
typedef与#define区别- 与#define不同,typedef仅限于数据类型,而不能是表达式或具体的值。
- #define发生在预处理,typedef发生在编译阶段。
- define是给常量起别名(简单的文字替代),typedef是给类型起别名(满足连续定义)。
按照定义变量的方式,把变量名换上新类型名,并且在最前面加上“typedef”,就声明了新类型名代表原来的类型。
格式: typedef 旧类型名 新类型名
优势- 当不同源文件中用到同一类型数据(尤其是像数组、指针、结构体、共用体等类型数据)时,常用typedef声明一些数据类型可以把所有的typedef名称声明单独放在一个头文件中,然后在需要用到它的文件中用#include指令把他们包含到文件中即可。
- 使用typedef名称有利于程序的通用与移植。
使用练习如下:
#include
#include
//1001 lilei 23 98
//1002 lucy 25 67
//1003 lucy 25 67
//1004 jim 46 23
//1005 hanmeimei 25 32
typedef struct stu
{
int id;
char name[20];
int age;
float score;
}STU;
void scanf_struct(STU students[], int length);
void print_struct(STU students[], int length);
void sort_struct(STU students[], int length);
STU max_struct(STU *pstudents, int length);
STU min_struct(STU *pstudents, int length);
STU find_id(STU *pstudents, int length, int id);
STU find_score(STU *pstudents, int length, float score);
void find_name(STU *pstudents, int length, char *name);
int main()
{
STU students[5];
scanf_struct(students, 5);
// print_struct(students, 5);
// sort_struct(students, 5);
// print_struct(students, 5);
// STU ret = max_struct(students, 5);
// printf("%d %s %d %f\n", ret.id, ret.name, ret.age, ret.score);
// STU ret1 = min_struct(students, 5);
// printf("%d %s %d %f\n", ret1.id, ret1.name, ret1.age, ret1.score);
// STU ret2 = find_id(students, 5, 1003);
// printf("%d %s %d %f\n", ret2.id, ret2.name, ret2.age, ret2.score);
// STU ret2 = find_score(students, 5, 67);
// printf("%d %s %d %f\n", ret2.id, ret2.name, ret2.age, ret2.score);
find_name(students, 5, "lucy");
return 0;
}
void scanf_struct(STU students[], int length)
{
int i;
for(i = 0; i < length; i++)
{
scanf("%d%s%d%f", &students[i].id, students[i].name, &students[i].age, &students[i].score);
}
}
void print_struct(STU students[], int length)
{
int i;
for(i = 0; i < length; i++)
{
printf("%d %s %d %.1f\n", students[i].id, students[i].name, students[i].age, students[i].score);
}
printf("\n");
}
void sort_struct(STU students[], int length)
{
int i, j;
STU temp;
for(i = 0; i < length - 1; i++)
{
for(j = 0; j < length - 1 - i; j++)
{
if(strcmp(students[j].name, students[j+1].name) > 0)
{
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
STU max_struct(STU *pstudents, int length)
{
int i;
STU temp;
float max_score = pstudents[0].score;
for(i = 0; i < length; i++)
{
if(max_score < pstudents[i].score)
{
max_score = pstudents[i].score;
temp = pstudents[i];
}
}
return temp;
}
STU min_struct(STU *pstudents, int length)
{
int i;
STU temp;
float min_score = pstudents[0].score;
for(i = 0; i < length; i++)
{
if(min_score > pstudents[i].score)
{
min_score = pstudents[i].score;
temp = pstudents[i];
}
}
return temp;
}
STU find_id(STU *pstudents, int length, int id)
{
int i;
// STU temp = {0, NULL, 0, 0};
for(i = 0; i < length; i++)
{
if(id == pstudents[i].id)
{
return pstudents[i];
}
else
{
printf("找不到该学号\n");
// return temp;
}
}
}
STU find_score(STU *pstudents, int length, float score)
{
int i;
STU temp = {0, NULL, 0, 0};
STU temp1;
for(i = 0; i < length; i++)
{
if(score == pstudents[i].score)
{
temp1 = pstudents[i];
break;
}
else
{
// printf("找不到该成绩\n");
temp1 = temp;
}
}
return temp1;
}
void find_name(STU *pstudents, int length, char *name)
{
int i, count = 0;
STU temp = {0, NULL, 0, 0};
for(i = 0; i < length; i++)
{
if(strcmp(name, pstudents[i].name) == 0)
{
temp = pstudents[i];
count++;
}
}
if(count > 1)
{
printf("你找的名字有重复信息,且重复了%d次\n", count);
}
printf("%d %s %d %.1f\n", temp.id, temp.name, temp.age, temp.score);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)