C语言程序设计之结构体篇1

问题1_1

        程序通过定义结构体变量,存储学生的学号、姓名和三门课的成绩。函数 f u n fun fun 的功能是: 将形参 a a a 所指结构体变量中的数据赋给函数中的结构体变量 b b b , 并修改 b b b 中的学号和姓名,最后输出修改后的数据。
        例如 a a a 所指变量中的学号、姓名和三门课的成绩依次是: 10001 、 " Q i n x i a o g o n g " 、 95 、 80 、 88 10001、"Qinxiaogong"、95、80、88 10001"Qinxiaogong"958088 ,则修改后输出 b b b 中的数据应为: 10002 、 " W e i y a n g " 、 95 、 80 、 88 10002、"Weiyang"、95、80、88 10002"Weiyang"958088

代码1_1

#include<stdio.h>
#include<string.h>

struct student{
	long sno;
	char name[10];
	float score[3];
};

void fun(struct student a){
	struct student b;
	int i;
	b = a;
	b.sno = 10002;
	strcpy(b.name, "Weiyang");
	printf("\nThe data after modified:\n");
	printf("\nNo:%ld Name:%s\nScores:", b.sno, b.name);
	for(i=0; i<3; i++)
		printf("%6.2f", b.score[i]);
	printf("\n");
}

void main(void){
	struct student s = {10001, "Qinxiaogong", 95, 80, 88};
	int i;
	printf("\n\nThe original data:\n");
	printf("\nNo:%ld Name:%s\nScores:", s.sno, s.name);
	for(i=0; i<3; i++)
		printf("%6.2f", s.score[i]);
	printf("\n");
	fun(s);
}

结果1_1

Struct_1

问题1_2

        学生记录由学号和成绩组成, N N N 名学生的数据已放入主函数中的结构体数组中,请编写函数 f u n fun fun的功能是:把分数最低的学生数据放入 b b b 所指的数组中。注意:分数最低的学生可能不止一个,函数返回分数最低的学生人数。

代码1_2

#include<stdio.h>

#define N 16

typedef struct{
	char num[10];
	int s;
}STREC;

int fun(STREC *a, STREC *b){
	int i, j=0, n=0, min;
	min = a[0].s;
	for(i=0; i<N; i++){
		if(a[i].s<min)
			min = a[i].s;
	}
	for(i=0; i<N; i++){
		if(a[i].s==min){
			*(b+j) = a[i];
			j++;
			n++;
		}
	}
	return n;
}

void main(void){
	STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69}, 
				  {"GA04", 85}, {"GA01", 91}, {"GA07", 72}, 
				  {"GA08", 64}, {"GA06", 87}, {"GA015", 85}, 
				  {"GA013", 91}, {"GA0", 64}, {"GA014", 91}, 
				  {"GA011", 91}, {"GA017", 64}, {"GA0", 64}, 
				  {"GA016", 72} };
	STREC h[N];
	int i, n;
	n = fun(s, h);
	printf("The %d lowest score:\n", n);
	for(i=0; i<n; i++)
		printf("%s %4d\n", h[i].num, h[i].s);
	printf("\n");
}

结果1_2

Struct_1_2

问题1_3

         学生记录由学号和成绩组成, N N N 名学生的数据已放入主函数中的结构体数组 s s s 中,请编写函数 f u n fun fun的功能是:按分数降序排列学生的记录,高分在前,低分在后。

代码1_3

#include<stdio.h>

#define N 16

typedef struct{
	char num[10];
	int s;
}STREC;

int fun(STREC a[]){
	int i, j;
	STREC t;
	// 冒泡法进行排序  
	for(i=1; i<N; i++){
		for(j=0; j<N-1; j++){
			if(a[j].s<a[j+1].s){
				t = a[j];
				a[j] = a[j+1];
				a[j+1] = t;
			}
		}
	}
/*
	// 选择法 
	for(i=0; i<N-1; i++){
		p = i;
		for(j=i+1; j<N; j++){
			if(a[p]>a[j])
				p = j;	
		}
		if(p!=i){
			t = a[i];
			a[i] = a[p];
			a[p] = t;
		}
	}
	// 插入法 
	for(i=0; i<N-1; i++){
		t = a[i];
		for(j=i-1; a[j]>t&&j>=0; j--)
			a[j+1] = a[j];
		a[j+1] = t;
	}
*/
}


void main(void){
	STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69}, 
				  {"GA04", 85}, {"GA01", 91}, {"GA07", 72}, 
				  {"GA08", 64}, {"GA06", 87}, {"GA015", 85}, 
				  {"GA013", 91}, {"GA0", 64}, {"GA014", 91}, 
				  {"GA011", 91}, {"GA017", 64}, {"GA0", 64}, 
				  {"GA016", 72} };
	int i;
	fun(s);
	printf("The data after sorted:\n");
	for(i=0; i<N; i++){
		if(i%4==0)
			printf("\n");
		printf("%s %4d", s[i].num, s[i].s);
	}
	printf("\n");
}

结果1_3

Result_1_3

问题1_4

         学生记录由学号和成绩组成, N N N 名学生的数据已放入主函数中的结构体数组 s s s 中,请编写函数 f u n fun fun的功能是:把高于等于平均分的学生数据放在 b b b 所指的数组中,高于等于平均分的学生人数通过形参 n n n 返回,平均值通过函数值返回。

代码1_4

#include<stdio.h>

#define N 16

typedef struct{
	char num[10];
	float s;
}STREC;

double fun(STREC *a, STREC *b, int *n){
	int i;
	double av = 0.0;
	*n = 0;
	for(i=0; i<N; i++)
		av += a[i].s;
	av = av/N;
	for(i=0; i<N; i++){
		if(a[i].s>=av){
			b[*n] = a[i];
			*n = *n+1; 
		}
	}
	return av;
}


void main(void){
	STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69}, 
				  {"GA04", 85}, {"GA01", 91}, {"GA07", 72}, 
				  {"GA08", 64}, {"GA06", 87}, {"GA015", 85}, 
				  {"GA013", 91}, {"GA0", 64}, {"GA014", 91}, 
				  {"GA011", 91}, {"GA017", 64}, {"GA0", 64}, 
				  {"GA016", 72} };
	STREC h[N];
	int i, n;
	double ave;
	ave = fun(s, h, &n);
	printf("The %d studdent data which is higher than %7.3f:\n", n, ave);
	for(i=0; i<n; i++){
		if(i%4==0)
			printf("\n");
		printf("%s %4.1f \n", h[i].num, h[i].s);
	}
	printf("\n");
}

结果1_4

Result_1_4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值