14.12
#include "stdio.h"
#define N 30
#define M 4
struct stud{
char num[5],name[10];
int s[M];
double ave;
};
void readrec(struct stud *p){
int i=0;
double ave_tmp=0.0;
printf("No:");
scanf("%s",p->num);
printf("Name:");
scanf("%s",p->name);
while(i<M){
printf("Couse[%d]:",i+1);
scanf("%d",&p->s[i]);
ave_tmp+=p->s[i];
i++;
}
p->ave=ave_tmp/M;
}
void writerec(struct stud *p){
int i=0;
printf("\nNo:%5s Name:%10s\n",p->num,p->name);
while(i<M){
printf("Course[%d]=%3d ",i+1,p->s[i]);
i++;
}
printf("\nAve=%f",p->ave);
}
void main(){
struct stud pstu[N];
int i=0;
while(i<N){
readrec(&pstu[i]);
i++;
}
i=0;
while(i<N){
writerec(&pstu[i]);
i++;
}
}
14.13
#include "stdio.h"
#include "stdlib.h"
struct de{
int data;
struct de *next;
};
typedef struct de DE;
DE *create_list();
DE *pMax(DE *h);
int findmax(DE *h);
DE *create_list(){
int c;
DE *h,*s,*r;
h=(DE *)malloc(sizeof(DE));
r=h;
scanf("%d",&c);
while(c!=-1){
s=(DE *)malloc(sizeof(DE));
s->data=c;
r->next=s;
r=s;
scanf("%d",&c);
}
r->next='\0';
return h;
}
int findmax(DE *h){
DE *p,*s;
s=h;
p=h->next;
if(p=='\0') {
printf("list if Null!\n");
return p->data;
}
while(p->next!='\0'){
if(p->data<s->data) p=p->next;
else {
s=p;
p=p->next;
}
}
return (s->data>p->data)?(s->data):(p->data);
}
DE *pMax(DE *h){
DE *p,*s;
s=h;
p=h->next;
if(p=='\0') {
printf("list if Null!\n");
return p;
}
while(p->next!='\0'){
if(p->data<s->data) p=p->next;
else {
s=p;
p=p->next;
}
}
return (s->data>p->data)?s:p;
}
void main(){
DE *head,*tmp;
head=create_list();
printf("%d\n",findmax(head));
tmp=pMax(head);
printf("%d",tmp->data);
}