4.3结构体

//通过指向结构体变量的指针变量输出结构体变量中成员的信息
/*#include <stdio.h>
#include <string.h>
int main()
{
	struct Student
	{
		long num;
		char name[20];
		char sex;
		float score;
	};
	struct Student stu_1;							//定义struct_Student类型的变量stu_1
	struct Student *p;							//定义指向struct Student类型数据的指针变量p
	p = &stu_1;								//p指向stu_1
	stu_1.num = 10101;							//对结构体变量的成员赋值
	strcpy(stu_1.name,"Li lin");						//用字符串赋值函数给stu_1.name赋值
	stu_1.sex = 'M';
	stu_1.score = 89.5;
	printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",			//输出结果
			stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
	printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",
			( *p ).num,( *p ).name,( *p ).sex,( *p ).score);
	return 0;
}*/
//有3个学生的信息,放在结构体数组中,要求输出全部学生的信息
/*#include <stdio.h>
struct Student
{
	int num;
	char name[20];
	char sex;
	int age;
};
struct Student stu[3] = {{10101,"Li lin",'M',18},{10102,"Zhang Fang",'M',19},
						{10104,"Wang Min",'F',20}};
int main()
{
	struct Student *p;
	printf("N0.   Name		   sex age\n");
	for( p = stu; p < stu+3; p++ )
		printf("%5d %-20s %2c %4d\n",p->num,p->name,p->sex,p->age);
	return 0;
}*/
//有n个结构体变量,内含学生学号,姓名和3门课程的成绩。要求输出平均成绩最高的学生的信息;
/*#include <stdio.h>
#define N 3
struct Student
{
	int num;
	char name[20];
	float score[3];
	float aver;
};

int main()
{
	void input(struct Student stu[]);
	struct Student max(struct Student stu[]);
	void print(struct Student stu);
	struct Student stu[N],*p = stu;						//定义结构体数组和指针
	input(p);								//调用input函数,实参是结构体指针变量p
	print(max(p));								//调用print函数,以max函数的返回值作为实参
	return 0;
}
void input(struct Student stu[])
{
	int i;
	printf("请输入各学生的信息:学号,姓名,三门课成绩:\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].aver = (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 = 0; i < N; i++)
		if ( stu[i].aver > stu[m].aver )
			m = i;
	return stu[m];
}
void print(struct Student stud)
{
	printf("\n成绩最高的学生是:\n");
	printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,:%5.1f,:%5.1f\n平均成绩:%6.2f\n",
		stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值