用户自己建立数据类型

9.1自己建立结构体类型

9.1.1 自己建立结构体类型

声明一个结构体类型的一般形式:
struct 结构体名
{成员表列};

9.1.2 定义结构体类型变量

1.先声明结构体类型,再定义该类型的变量
例如:
struct Student student1;
struct Student为结构体类型名,student1为结构体变量名。

2.再声明类型的同时定义变量
例如

struct Student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
}student1,student2;

这种定义方法的一般形式为:
struct 结构体名
{
成员表列
}变量名表列;

3.不指定类型名而直接定义结构体类型变量
其一般形式为:
struct
{
成员表列
}变量名列表;

9.1.3 结构体变量的初始化和引用

例, 把一个学生的信息(包括学号,姓名,性别,住址)放在一个结构化变量中,然后输出这个学生的信息。

#include <stdio.h>
int main()
{
struct Student
{
long int num;
char name[20];
char sex;
char addr[20];
}a={"100101","Li Lin","M","123 beijing Road"};
printf("NO.:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr);
return 0;
}

(1)在定义结构变量时可以对它的成员初始化。初始化列表是用花括号括起来的一些常量,这些常量依次赋给结构体变量中的各成员。注意,是对结构体变量的初始化,而不是对结构体类型初始化。
C99标准允许对某一成员初始化,如:
strutct Student b = {.name = “Zhang Fang”}; //在变量名前有成员运算符

(2)可以引用结构体变量中成员的值,引用方式为:
结构体变量名。成员名
在程序中可以对变量的成员赋值,例如:
student!.num=10010;
“.”是成员运算符,他在所有的运算符中优先级最高,因此可以把student1.num作为一个整体来看,相当于以一个变量。

(3)如果成员本省又属于一个结构体类型,则要用若干个成员运算符,一级一级地找到最低地一级地成员。只能对最低级地成员进行赋值以及运算。

(4)对结构体变量地成员可以像普通变量一样进行各种运算。
例如,
student2.score = student1.score; //赋值运算
sum = student1.score +student2.score; //加法运算
student.age++; //自加运算

(5)同类地结构体变量可以互相赋值,如,
student1=student2; //假设student1和student2已定义为同类型的结构体变量

(6)可以引用结构体变量成员的地址,也可以引用结构体变量的地址。如,
scanf("%d",&studnet1.num);

例输入两个学生的成绩,如果学生1的成绩高于学生2的成绩,就输出学生1的全部信息,如果学生2的成绩高于学生1的成绩,就输出学生2的全部信息/如果二者相等则输出两个学生的全部信息。

#include <stdio.h>
int main()
{
	struct Student
	{
		int num;
		char name[20];
		int score;
	}student1,student2;
	printf("please input student1's message\n");
	scanf("%d,%s,%d",&student1.num,student1.name,&student1.score);
	printf("\n");
	printf("please input student2's message\n");
	scanf("%d,%s,%d",&student2.num,student2.name,&student2.score);
	if(student1.score==student2.score)
	{
		printf("%d %s %d",student1.num,student1.name,student1.score);
		printf("%d %s %d",student2.num,student2.name,student2.score);
	}
	else if(student1.score>student2.score)
	printf("%d %s %d",student1.num,student1.name,student1.score);
	else
	printf("%d %s %d",student2.num,student2.name,student2.score);
	return 0;
 } 

9.2 使用结构体数组

9.2.1 定义结构体数组

例· 有3个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出各人得票结果。

#include <stdio.h>
#include <string.h>
struct Person
	{
		char name[20];
		int count;
	}leader[3]={"li",0,"zhang",0,"sun",0};
	
int main()
{
	int i,j;
	char leader_name[20];
	for(i=1;i<=10;i++)
	{
		printf("please input the leader's name your choose:\n");
		scanf("%s",leader_name);
		for(j=0;j<3;j++)
		if(strcmp(leader_name,leader[j].name)==0)
		leader[j].count++;
	}
	printf("result:\n");
	for(i=0;i<3;i++)
	printf("%d people want %s become the leader\n",leader[i].count,leader[i].name);
	return 0;
 } 

9.2.2 结构体数组的应用举例

例,有n个学生的信息(包括学号,姓名,成绩),要求按照成绩的高低输出各学生的信息。

#include <stdio.h>
struct Student
	{
		int num;
		char name[20];
		float score;
	};
	
int main()
{
	struct Student stu[5]={
	{10001,"zhang",78},{10002,"yang",88},{10003,"liu",99},{10004,"li",89},{10005,"sun",67}
	};
	struct Student temp;
	int i,j,k;
	for(i=0;i<4;i++)
	{k=i;
	for(j=i+1;j<5;j++)
	if(stu[k].score<stu[j].score)
	k=j;
	temp=stu[i];
	stu[i]=stu[k];
	stu[k]=temp;
}
for(i=0;i<5;i++)
printf("%d %s,%d\n",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;
 } 

9.3 结构体指针

9.3.1 指向结构体变量的指针

例通过指向结构体变量的指针变量输出结构体变量中成员。

#include <stdio.h>
#include <string.h>
int main()
{
	struct Student
	{
		long num;
		char name[20];
		char sex;
		float score;
	
	};
	struct Student student1;
	struct Student *p;
	p=&student1;
	student1.num=1001;
	strcpy(student1.name,"zhang");
	student1.sex='f';
	student1.score= 89;
	printf("NO:%ld\nname:%s\nsex:%c\nscore:%f\n",student1.num,student1.name,student1.sex,student1.score);
	printf("NO:%ld\nname:%s\nsex:%c\nscore:%f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
return 0;
 } 

9.3.2 指向结构体数组的指针

例 有三个学生的信息,放在结构体数组中,要求输出全部学生的信息。

#include <stdio.h>
struct Student
	{
		long num;
		char name[20];
		char sex;
		int age;
}
	struct Student stu[3]={{10001,"zhang",'f',18},{10002,"yang",'m',19},{10003,"sun",'f',20}
	};
int main()
{
	struct Student *p;
	printf("NO.  Name       sex age\n");
	for(p=stu;p<stu+3;p++)
	printf("%5d%-20s%4d\n",p->num,p->name,p->sex,p->age);
	return 0;
 } 

9.3.3 用结构体变量和结构体变量的指针作函数参数

将一个结构体变量的值传递给另一个函数,有三种方法:
(1)用结构体变量的成员作参数。
例如,用stu[1].num作函数实参,将实参值传递给形参
(2)用结构体变量作实参。
(3)用指向结构体变量(或数组元素)的指针作实参,将结构体变量(或数组元素)的地址传递给形参。
例 有n个结构体变量,内含学生学号,姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息(包括学号,姓名,3门课程成绩和平均成绩)。

#include <stdio.h>
#define N 3
struct Student
{
int num;
char name[20];
float score[3];
float average;		
};
int main()
{
	void input(struct Student stu[]);
	struct Student max(struct Student stu[]);
	void print(struct Student stud);
	struct Student stu[N],*p=stu;
	input(p);
	print(max(p));
	return 0;
 } 
 void input(struct Student stu[])
 {
 	int i;
	printf("please input all students' number, name,score:\n");
	for(i=0;i<N;i++)
	{
		scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
		stu[i].average=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
	}
 }
 struct Student max(struct Student stu[])
 {
 	int i,m=0;
 	for(i=1;i<N;i++)
 	if(stu[i].average>stu[m].average)m=i;
 	return stu[m];
 }
 void print(struct Student stud){
 	printf("message of the student who has the highest score is :\n");
 	printf("number is %d\nname is %s\nscore1 is %f\nscore2 is %f\nscore3 is %f\naverage score is %f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.average);	
 }

9.4 用指针处理链表

9.4.1 什么是链表

链表是一种常见的重要的数据结构,它是动态地进行存储分配地一种结构。链表有一个“头指针”变量,它存放一个地址,该地址指向一个元素。链表中地每一个元素称为“结点”,每个结点都应包括两个部分:(1)用户需要用地实际数据(2)下一个结点的地址

9.4.2 建立简单的静态链表

例 建立一个简单链表,它由3个学生数据的结点组成,要求输出各结点的数据。

#include <stdio.h>
struct Student
{
	int num;
	float score;
	struct Student *next;
};
int main()
{
	struct Student a,b,c,*head,*p;
	a.num=100001;a.score=78;
	b.num=100002;b.score=77;
	c.num=100003;c.score=79;
	head=&a;
	a.next=&b;
	b.next=&c;
	c.next=NULL;
	p=head;
	do
	{
		printf("%ld %5.1f\n",p->num,p->score);
		p=p->next;
	}while(p=NULL);
	return 0;
}

9.4.3 建立动态链表

写一函数建立一个有3名学生数据的单向动态链表。

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
	long num;
	float score;
	struct Student *next;
};
struct Student *creat(void)
{
	struct Student *head;
	struct Student *p1,*p2;
	int n = 0;
	p1=p2=(struct Student*)malloc(LEN);
	scanf("%ld,%f",&p1->num,&p1->score);
	head=NULL;
	while(p1->num!=0)
	{
		n=n+1;
		if(n==1)
		head=p1;
		else p2->next=p1;
		p2=p1;
		p1=(struct Student*)malloc(LEN);
		scanf("%ld,%f",&p1->num,&p1->score);
	}
	p2->next=NULL;
	return head;
}
int main()
{
	struct Student *pt;
	pt=creat();
	printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);
	return 0;
}

9.4.4 输出链表

例 编写一个输出链表的函数print。

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
	long num;
	float score;
	struct Student *next;
};
int n;
void print(struct Student *head)
{
	struct Student *p;
	printf("\nNow, these %d records are :\n",n);
	p=head;
	if(head!=NULL)
		do
		{
			printf("%ld %5.1f\n",p->num,p->score);
			p=p->next;
		}while(p!=NULL)
}

9.5 共用体类型

9.5.1 什么是共用体类型

使几个不同的变量共享同一段内存的结构,称为共用体类型的结构。
定义共用体类型变量的一般形式为:
union 共用体名
{
成员表列
}变量表列;
例如:
union Data
{
int i;
char ch;
float f;

}a,b,c;

9.5.2 引用公用体变量的方法

9.5.3 共用体类型数据的特点

(1)同一个内存段可以用来存放几个不同类型的成员,但在每一瞬间只能存放其中一个成员,而不是同时存放几个。
(2)可以对公用体变量初始化,但初始化表宏只能有一个常量。
(3)共用体变量中起作用的成员是最后一次被赋值的成员,在对共用体变量中的一个成员赋值后,原有变量存储单元中的值就取代。
(4)共用体变量的地址和它的各成员的地址都是同一地址。例如,&a.i,&a.c,&a.f都是同一值。
(5)不能对共用体变量名赋值,也不能企图引用变量名来得到一个值。
(6)共用体类型可以出现在结构体类型定义中,也可以定义共用体数组。
反之,结构体也可以出现在共用体类型定义中,数组也可以作为共用体的成员。

例 有若干个人员的数据,其中有学生和教师。学生的数据中包括:姓名,号码,性别,职业,班级。教师的数据包括:姓名,号码,性别,职业,职务。要求用一个表格来处理。

#include <stdio.h>
struct
{
	int num;
	char name[10];
	char sex;
	char job;
	union
	{
		int clas;
		char position[10];
	}category;
}person[2];
int main()
{
	int i;
	for(i=0;i<2;i++)
	{
		printf("please enter the data of person\n");
		scanf("%d%s%c%c",&person[i].num,person[i].name,&person[i].sex,&person[i].sex);
		if(person[i].job=='s')
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值