#include"stdio.h" #include"conio.h" #include"string.h" #include"alloc.h" typedef struct pp { char hoten[28], gt; int tuoi; float d1,d2,d3; float tb; struct pp*tiep; } sinh_vien; sinh_vien *pdau; // Ham nhap danh sach sinh vien void nhap() { float t; sinh_vien *pcuoi,*pnew; pdau = pcuoi = NULL; do { pnew=(sinh_vien*)malloc(sizeof(sinh_vien)); fflush(stdin); printf("\nho ten:");gets(pnew->hoten); if(strlen(pnew->hoten)>0) { printf("gioi tinh:"); scanf("%c",&pnew->gt); printf("tuoi:"); scanf("%d",&t); pnew->tuoi=t; printf("diem toan:"); scanf("%f",&t); pnew->d1=t; printf("diem ly:"); scanf("%f",&t); pnew->d2=t; printf("diem hoa:"); scanf("%f",&t); pnew->d3=t; pnew->tb = (pnew->d1+pnew->d2+pnew->d3)/3; pnew->tiep = NULL; if( pdau==NULL ) pdau = pcuoi = pnew; else { pcuoi->tiep = pnew; pcuoi = pnew; } } } while( strlen(pnew->hoten)>0 ); free(pnew); } // Ham in thong tin ve mot sinh vien void in(sinh_vien *p) { printf("\nTen: %s Gioi tinh: %c Toan: %4.2f %4.2f",p->hoten,p->gt,p->d1,p->d2,p->d3,p->tb ); } // Ham in ra danh sach sinh vien void inds() {
Ly: %4.2f
Hoa: %4.2f
DTB:
}
sinh_vien *p=pdau; while(p) { in(p); p=p->tiep; }
// Ham in ra man hinh danh sach sinh vien co diem trung binh >=5 void intb() { sinh_vien *p=pdau; // clrscr(); printf("\n\nDanh sach sv co dtb>=5 la:"); while ( p ) { if ( p->tb>=5 ) in(p); p = p->tiep; } // getch(); } // Ham sap xep danh sach sinh vien theo diem trung binh giam dan void sapxep() { sinh_vien *p=pdau,*q,tg; // clrscr(); printf("\n\nDanh sach sinh vien xep theo diem trung binh:\n"); while(p) { q=p->tiep; while(q) { if(p->tbtb) { strcpy(tg.hoten,p->hoten);strcpy(p->hoten,q>hoten);strcpy(q->hoten,tg.hoten); tg.gt=p->gt;p->gt=q->gt;q->gt=tg.gt; tg.tuoi=p->tuoi;p->tuoi=q->tuoi;q->tuoi=tg.tuoi; tg.d1=p->d1;p->d1=q->d1;q->d1=tg.d1; tg.d2=p->d2;p->d2=q->d2;q->d2=tg.d2; tg.d3=p->d3;p->d3=q->d3;q->d3=tg.d3; tg.tb=p->tb;p->tb=q->tb;q->tb=tg.tb; } q=q->tiep; } p=p->tiep; } inds(); } void main() { clrscr(); nhap(); //
clrscr();
printf("\n\nDanh sach sinh vien xep sau khi nhap: \n"); inds(); //
}
getch(); intb(); sapxep(); getch();