目录
1. 结构变量
- 关键字struct和它后面的结构名一起组成一个新的数据类型名,如struct student是一个数据类型。结构的定义以封号结束,这因为C语言中把结构的定义看做一条语句。
- 结构成员运算符的优先级属最高级别,一般情况下都是有限执行。
- 只有相同结构类型的变量之间才可以直接赋值。
/* 输出平均分最高的学生信息 */
#include <stdio.h>
struct student{//struct student 是一个数据类型
int num; //学号
char name[10]; //姓名
int computer,english,math; // 三门课程成绩
double average; // 个人平均成绩
};
int main()
{
int i,n;
struct student s1,max;//定义结构变量
printf("Input n:" );
scanf("%d",&n);
printf("Input the student's number,name and course scores\n");
for(i = 1; i <= n; i++){
printf("No.%d:",i);
scanf("%d %s %d %d %d",&s1.num,&s1.name,&s1.math,&s1.english,&s1.computer);
s1.average = (s1.computer + s1.english + s1.math)/3.0;
if(i==1){
max = s1; // 只有相同结构类型的变量之间才可以直接赋值
}
if(max.average < s1.average){
max = s1;
}
}
printf("num:%d,name:%s,average:%.2lf\n",max.num,max.name,max.average);
return 0;
}
2. 结构数组
一个结构变量只能表示一个实体的信息(一个学生的信息),如果有许多相同类型的实体,就要使用结构数组。结构数组就是把具有相同结构类型的变量组织起来。
/* 学生成绩排序 */
#include <stdio.h>
struct student{//struct student 是一个数据类型
int num; //学号
char name[10]; //姓名
int computer,english,math; // 三门课程成绩
double average; // 个人平均成绩
};
int main()
{
int i,index,j,n;
struct student students[50],temp; //定义结构数组
/*输入*/
printf("Input n:" );
scanf("%d",&n);
for(i = 0; i < n; i++){
printf("Input the info of No.%d: \n",i+1);
printf("number: ");
scanf("%d",&students[i].num);
printf("name: ");
scanf("%s",&students[i].name);
printf("math score: ");
scanf("%d",&students[i].math);
printf("english score: ");
scanf("%d",&students[i].english);
printf("computer score: ");
scanf("%d",&students[i].computer);
students[i].average = (students[i].computer + students[i].english + students[i].math)/3.0;
}
/*结构数组排序,选择排序法*/
for(i = 0; i < n-1; i++){
index = i;
for(j = i+1; j < n; j++){
if(students[j].average > students[index].average){//比较平均成绩
index = j;
temp = students[index];/*交换数组元素*/
students[index] = students[i];
students[i] = temp;
}
}
}
/*输出排序后的信息*/
printf("num\t name\t average\n");
for(i = 0; i < n; i++){
printf("%d\t %s\t %.2lf\n",students[i].num,students[i].name,students[i].average);
}
return 0;
}
3. 结构指针
- 结构指针就是指向结构类型变量的指针,结构指针的值实际上是结构变量的首地址,也就是第一个成员的地址。
- 有了结构指针,访问结构成员又多了一种方式。之前是通过结构变量名直接访问结构成员,现在也可以通过结构指针变量p间接访问各个成员,如下如所示。在使用结构指针访问结构成员时,通常使用指向运算符。
- 相比于结构变量作为函数的参数,结构指针作为函数参数只要传递一个地址值,因此能够极大地提高参数传递的效率。
/* 学生成绩排序 */
#include <stdio.h>
struct student{//struct student 是一个数据类型
int num; //学号
char name[10]; //姓名
int computer,english,math; // 三门课程成绩
double average; // 个人平均成绩
};
int update_score(struct student *p, int n, int num, int course, int score);/*函数声明*/
int main()
{
int course,i,n,num,pos,score;
struct student students[50]; //定义结构数组
/*输入n个学生信息*/
printf("Input n:" );
scanf("%d",&n);
for(i = 0; i < n; i++){
printf("Input the info of No.%d: \n",i+1);
printf("number: ");
scanf("%d",&students[i].num);
printf("name: ");
scanf("%s",&students[i].name);
printf("math score: ");
scanf("%d",&students[i].math);
printf("english score: ");
scanf("%d",&students[i].english);
printf("computer score: ");
scanf("%d",&students[i].computer);
students[i].average = (students[i].computer + students[i].english + students[i].math)/3.0;
}
/*输入待修改学生信息*/
printf("Input the number of update student:" );
scanf("%d",&num);
printf("Choice the course: 1.math 2.english 3.computer:" );
scanf("%d",&course);
printf("Input the new score:");
scanf("%d",&score);
/*调用函数,修改学生成绩*/
pos = update_score(students,n,num,course,score);
/*输出修改后的学生信息*/
if(pos == 1){
printf("Not found! \n");
}
else{
printf("After update: \n");
printf("num \ t math \ t english \ t computer \n");
printf("%d \ t % d \ t % d \ t % d \ n",students[pos].num,students[pos].math,students[pos].english,students[pos].computer);
}
return 0;
}
/*自定义函数,修改学生成绩*/
int update_score(struct student *p, int n, int num, int course, int score)
{
int i,pos;
for(i = 0; i < n; i++,p++){/*按照学号查找*/
if(p->num == num) break;
}
if(i < n){/*找到,修改成绩*/
switch(course){
case 1:p->math = score;break;
case 2:p->english = score; break;
case 3:p->computer = score; break;
}
pos = i;
}
else{
pos = -1; /*无此学号*/
return pos;
}
}