12.5.2设学生数据包括学号、姓名和成绩,从键盘输入3个学生的数据(假设按学号的升序进行输入),要求输出学生数据,并输出所有学生的平均成绩。要求:1)采用静态数据结构。即定义一个结构体数组,存放h

12.5.1

#include
#include

#define N 3
struct student
{
	char name[20];
	int score;
	char num[30];
};
int main()
{
	struct student member[N];
	int a[N];
	int* p;
	int i, max = 0;
	double aver=0;
	p = a;
	for (i = 0; i < N; i++)
	{
		printf("please input number %d student's name score num\n", i + 1);
		scanf("%s%d%s", member[i].name, &member[i].score, member[i].num);
	}
	printf("the basic information:");



	for (i = 0; i < N; i++)
	{
		*(p + i) = member[i].score;
	}


	for (i = 0; i < N; i++)
	{
		aver += member[i].score;

	}
	aver = aver / N;
	for (i = 0; i < N; i++) {
		printf("%s %d %s\n", member[i].name, member[i].score, member[i].num);
	}
	printf("aver=%f", aver);
	return 0;
}

12.5.2


#include
#include

#define LEN sizeof(struct Student)

struct Student
{
	char name;
	long num;
	float score;
	struct Student* next;
};

int n;

struct Student* creat(void)
{
	struct Student* head;
	struct Student* p1, * p2;
	n = 0;
	p1 = p2 = (struct Student*)malloc(LEN);
	scanf("%ld%s%f", &p1->num, &p1->name,&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%s%f", &p1->num, &p1->name, &p1->score);
	}
	p2->next = NULL;
	return(head);
}

void print(struct Student* head)
{
	float sum=0;
	struct Student* p;
	printf("\nNow,These %d records are:\n", n);
	p = head;
	if (head != NULL)
		do
		{
			printf("%s %ld %5.1f\n", &p->name,p->num, p->score);
			sum +=( p->score);
			p = p->next;
		} while (p != NULL);
		printf("aver=%f",sum/n);
}

int main()
{
	struct Student* head;
	head = creat();
	print(head);
	system("pause");
	return 0;
}

你可能感兴趣的:(安农21c语言,c++,开发语言)