12.5.1
#include<stdio.h>
#include<string.h>
#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<stdio.h>
#include<stdlib.h>
#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;
}