一、目的要求 1 .掌握结构体变量、结构体数组的定义和引用。
2 .掌握用结构体作为数据结构的程序设计方法。
3 .掌握用指向结构体类型数据的指针变量作为函数参数时的应用。
4 .深刻体会用结构体作数据结构的优越性。 二、启事与范例 1 .结构体类型变量的定义及引用
定义结构体变量的方法有三种:
① 先定义结构体类型再定义结构体变量;
② 在定义结构体类型的同时定义结构体变量;
③ 直接定义结构体变量。
建议最好使用第一种方法,并且将结构体类型的定义放在文件头部(或放在头文件中),这样,就可以在该文件的各函数中用这一类型去定义局部的结构体变量,也可以用此类型去定义外部的结构体变量,甚至于定义结构体类型的函数。
例 1 输入 10 个学生的学号、姓名及数学、英语、计算机三门课的分数,输出总分最高及总分最低的学生的全部信息。
struct student
{
int number;
char name [20];
int math;
int english;
int computer;
int total;
} ;
/ * 以上是对结构体类型 student 的定义 * /
main ( )
{
int i;
struct student st, stmax, stmin;
/ * 以上是对结构体变量 st, stmax, stmin 的定义 * /
stmax. total = -32768;
stmin. total = 32767;
for (i = 0; i < 10; i++)
{
printf ( ″ \n Enter data : ″ ) ;
scanf ( ″%d %s %d %d %d″,&st.number,st.name,&st.math,&st.english,&st.computer);
st.total = st. math + st. english+st.computer;
if (st.total>stmax. total) stmax = st;
if (st. total
}
printf ( ″ \n max :%5d %15 %4d %4d %4d %4d ″,
stmax.number, stmax.name, stmax.math, stmax.english, stmax.computer, stmax.total);
printf ( ″ \n min :%5d %15s %4d %4d %4d %4d ″,
stmin.number, stmin.name, stmin.math, stmin.english, stmin.computer, stmin.total);
}
2 .结构体类型数组的定义及引用
对于结构体类型数组的定义,同样先定义结构体类型,然后用此类型去定义结构体数组。
例 2 输入 10 个学生的数据,然后按总分从高到低的顺序排序后输出(每个学生的数据同例 1 )。
为方便起见,可用宏定义指令将 struct student 用一个宏 STU 代替 ( 见程序第 1 行)。
#define STU struct student
STU
{
int number;
char name [20];
int math;
int english;
int computer;
int total;
}
main ( )
{
int i, j;
STU st [10], t;
/ * 以上是定义结构体数组 st 及结构体变量 t * /
printf ( ″ \n Enter data : ″ ) ;
for (i = 0 ; i<10 ; i++)
{
scanf ( ″ %d%s%d%d%d ″ , & st [i].number, st [i].name, & st [i].math,
& st [i].english, & st [i].computer);
st [i].total = st [i].math + st [i].english + st [i].computer;
}
for (i = 0; i<10; i++)
for (j = i + 1; j<10; j++)
if (st [i].total < st [j]. total)
{ t = st [i]; st [i]=st [j]; st [j]=t;}
for ( i= 0; i<10; i++)
printf ( ″ \n %5d%15s%4d%4d%4d ″ ,
st[i]. number,st[i].name,st[i].math
st[i].english,st[i].computer,st[i].total);
}
请详细阅读以上程序,掌握结构体数组的用法,体会用结构体作数据结构的优越性。
3 .用指向结构体的指针作函数参数
如果直接用结构体变量作函数参数,则数据传递是一种值传递方式,不能在函数中通过形参变量(结构体变量)改变实参变量(结构体变量)的值。用指向结构体的指针作函数参数可解决此问题。
用指向结构体的指针变量作函数参数,最常见的用法有两种:
① 用于接受实参变量(结构体变量)的地址,从而在函数中可以通过指针变量间接地访问实参变量所在的内存单元,以达到双向传递的效果(见例 3 )。
② 用于接受实参数组(结构体数组)的首地址,从而在函数中可以通过移动该指针变量的指向(或通过指针运算)间接地访问实参数组各元素所在的内存单元(见例 4 )。
例 3 将例 1 改为用函数实现。
# define STU struct student
STU
{
int number;
char name [20];
int math;
int english;
int computer;
int total;
};
void max _ min (STU st[ ], int n, STU *stmax, STU *stmin)
{
int i;
* stmax = st [0];
* stmin = st [0];
for (i = 1; i
{
if (st [i]. total > stmax - > total) *stmax = st [i];
if (st [i]. total < stmin - > total) *stmin = st [i];
}
}
main ( )
{
int i;
STU st [10], stmax, stmin;
for (i = 0; i<10; i++)
{
scanf ( … ); / * 此输入语句与例 2 相同 * /
st [i]. total = st [i]. math + st [i]. english+st[i]. computer;
}
max_min(st, 10, & stmax, &stmin);
printf ( … ) ;
printf ( … );
/ * 以上两个输出语句与例 1 相同 * /
}
注:在函数 max_min 中,访问 stmax 和 stmin 所指向的结构体变量中的成员用“ - > ”而不能用“ . ”。
例 4 将例 2 改为用函数实现。
void sort (STU *st, int n)
{
int i, j;
STU t;
for (i = 0; i
for (j = i+1; j
if ((st + i)->total<(st + j)->total)
{
t = * (st + i);
* (st + i) = * (st + j);
* (st + j) = t;
}
}
有了此函数的定义,即可将例 2 中的排序部分语句改为调用此函数。
4 .用动态分配内存空间存放结构体类型数据
如果将例 2 中的学生人数改为 n 个,则 st 不宜定义为结构体数组(因长度未定),应使用动态分配内存空间的方法,即将 st 定义为指向结构体类型的指针变量,然后用 malloc 函数申请一片内存空间,并将其首地址赋给 st ,即相应部分语句改为:
# include
…
main ( )
{
int i, n;
STU *st;
scanf ( ″ %d ″ , &n) ;
st = malloc (n * sizeof (STU)) ;
if (! st ) exit (0);
…
free (st) ;
}
[1] [2] [3] [4] [5] [6] [7] 下一页
|