【C语言】结构

目录

1. 结构变量

2. 结构数组

3. 结构指针


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;
	}
 }

 

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#include"stdio.h" #include"stdlib.h" #define NULL 0 struct student { long num; char name[20]; int score[6]; struct student *next; }; void show() { printf("\nthere is a cataloge as follow.\n"); printf("***************************************\n"); printf("* *\n"); printf("* 1. create *\n"); printf("* 2. Insert *\n"); printf("* 3. print *\n"); printf("* 4. delete *\n"); printf("* 5. modify *\n"); printf("* 6. save_to_file *\n"); printf("* 7. lode from file *\n"); printf("* 8. exit *\n"); printf("***************************************\n"); printf("please input 1--8 to choice what you want:\n"); } struct student *create() { struct student *head,*p,*last; int i; int temp2; char name[20]; p=head=(struct student *)malloc(sizeof(struct student)); head->next=NULL; last=p; while(1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); /*last->next=p; last=p;*/ p->next=NULL; printf("number:"); scanf("%ld",&p->num); if(p->num==0)break; getchar(); printf("name:"); scanf("%s",p->name); printf("score:"); for(i=0;i<6;i++) { scanf("%d",&temp2); p->score[i]=temp2; } printf("next student's information.\n"); } free(p); return head; } void Insert(struct student *head) { struct student *p,*q; int score; long num; int i; printf("\nEnter the student's information you want to insert.\n"); printf("number:"); scanf("%ld",&num); q->num=num; getchar(); printf("name:"); scanf("%s",q->name); printf("score:(chinese,math,english,biology,physics,chemistry)\n"); for(i=0;i<6;i++) { scanf("%d",&score); q->score[i]=score; } q->next=NULL; p=head; while(p->next->num<q->num&&p->next!=NULL) p=p->next; q->next=p->next; p->next=q; if(p->next==NULL) p->next=q; } void delete(struct student *head) { struct student *p,*q; long num; printf("enter the student's information you want to delete.\n"); printf("number:"); scanf("%ld",&num); getchar(); p=head; while(p->next!=NULL&&p->next->num!=num) p=p->next; q=p->next; p->next=p->next->next; free(q); } void print(struct student *head) { struct student *p; int i; p=head->next; if(p==NULL) { printf("\nthere is no information.\n"); exit(0); } printf("\nnumber\tnamme\tchinese\tmath\tenglish\tbiology\tphysics\tchemistry\n"); while(p!=NULL) { printf("\n%ld\t%s",p->num,p->name); for(i=0;i<6;i++) printf("\t%d",p->score[i]); p=p->next; } } void modify(struct student *head) { struct student *p; int choice,i; long num; char name[20]; int score[6]; printf("\nEnter what student's information you want to modify.\n"); printf("number:"); scanf("%ld",&num); getchar(); printf("\nname:"); scanf("%s",name); printf("\n"); p=head->next; while(p->num!=num&&p->name[20]!=name[20]&&p!=NULL) p=p->next; printf("\nplease choice what you want to modify:1-number 2-name 3-score.\n"); scanf("%d",&choice); switch(choice) { case 1:printf("\nEnter the true number:"); scanf("%ld",&num); p->num=num; break; case 2:printf("\nEnter the true name:"); scanf("%s",p->name); break; case 3:printf("\nEnter the right score:"); for(i=0;i<6;i++) { scanf("%d",&score[i]); p->score[i]=score[i]; } break; } } void save_in(struct student *head) { struct student *p; FILE *fp; char file_name[30]; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"w"))==NULL) { printf("can't open the file.\n"); exit(0); } p=head; while(p->next!=NULL) { fwrite((void*)p->next,sizeof(struct student),1,fp); p=p->next; } fclose(fp); } struct student *load_from_file() { struct student *head,*p,*last; FILE *fp; char file_name[30]; head=(struct student *)malloc(sizeof(struct student)); last=head; head->next=NULL; printf("please enter the file name you want to save.\n"); scanf("%s",file_name); printf("save to file:%s",file_name); if((fp=fopen(file_name,"r"))==NULL) { printf("can't open the file.\n"); exit(0); } p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; while(fp=fread((void *)p,sizeof(struct student),1,fp)==1) { last->next=p; last=p; p=(struct student *)malloc(sizeof(struct student)); p->next=NULL; } free(p); fclose(fp); return head; } void main() { struct student *la; int choice; /*char Yes_No;*/ la=(struct student *)malloc(sizeof(struct student)); la->next=NULL; while(1) { show(); scanf("%d",&choice); switch(choice) { case 1:la=create(); break; case 2:Insert(la); break; case 3:print(la); break; case 4:delete(la); break; case 5:modify(la); break; case 6:save_in(la); break; case 7:la=load_from_file(); break; case 8:exit(0); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吾日叁問

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值