Header Ads

Bài tập danh sách sinh viên có lời giải (P2)

Bài tập danh sách sinh viên nhập xuất(P2)

 

Code:

#include < stdio.h >   // printf-scanf
#include < stdlib.h > // malloc
#include < string.h > // strcpy
typedef struct sv{
        char Hoten[50];
        float Diem;
        struct sv* Next;        
} nut;
nut *dau, *cuoi;

void ThemDau(char hoten[], float diem){
     
    nut* p=(nut* )malloc(sizeof(nut));
    strcpy(p->Hoten,hoten);
    p->Diem = diem;
    p->Next = dau;
    dau = p;
}
void ThemCuoi(char hoten[], float diem){
     nut* p=(nut* )malloc(sizeof(nut));
     strcpy(p->Hoten,hoten);
     p->Diem = diem;
     p->Next = NULL;
     if(dau == NULL)
            dau = p;
     else  
           cuoi->Next = p;
           cuoi = p;           
}
void HienThi(){
     nut *p;
     p=dau;
     while(p != NULL){
     printf("\nTen la: %s",p->Hoten);   
     printf("\nDiem la: %g",p->Diem); 
     printf("\n------------------\n");    
     p = p->Next;       
     }    
}
void TaoDS(){
     char hoten[50]; 
     float diem;
     int n,i;
     do{
        printf("Nhap vao ten sinh vien: ");
        fflush(stdin);
        gets(hoten);
        if(hoten[0]){
              printf("Nhap Diem: ");
              fflush(stdin);
              scanf("%f",&diem);  
              ThemCuoi(hoten,diem);           
        }   
     } while(hoten[0]);
        
}
void XapXep(){
         nut *q, *p, *temp;
         p = dau;
         while(p != NULL){
                 q = p->Next;
                 while(q != NULL){
                         if(p->Diem > q->Diem){
                             temp = p->Next;
                             p->Next = q->Next;
                             q->Next = temp;
                          } 
                         q = q->Next;
                 }  
                 p = p->Next;       
         }         
}
int ThiLai(){
   nut *p;
   p=dau;  
   int dem=0;
   while(p != NULL){
           if(p->Diem < 5) dem++;
           p = p->Next;         
   }
   return dem;
}
void DiemCao(){
    nut *p;
    p=dau;
    int max=0;
    while(p != NULL){
            if(p->Diem > max) max = p->Diem;
            p = p->Next;       
    } 
    printf("DS SV diem cao nhat: ");
    p=dau;
    while(p != NULL){
           if(p->Diem == max) 
           printf("\nTen la: %s",p->Hoten);
           p = p->Next;
    }   
}

char *trichTen(char *hoten)
{
    int k = strlen(hoten) - 1;
    while (k >= 0 && hoten[k] != ' ') k--;
    return (hoten + k + 1);
}
 
void timTheoTen(char ten[10])
{
    printf("\nNhung sinh vien co ten '%s':\n", ten);
    Nut *p = dau;
    while (p != NULL)                       //duye^.t danh sách
    {
        if (strcmp(trichTen(p->HoTen), ten) == 0)
            printf("- %s (%g diem)\n", p->HoTen, p->Diem);
        p = p->Tiep;
    }
}

int main(int argc, char *argv[])
{   
      
  printf("Bam Enter de dung nhap!!\n");
  TaoDS();
  HienThi();
  printf("\nSinh Vien Thi lai: %d \n",ThiLai());
  DiemCao();
  system("PAUSE"); 
  return 0;
}


No comments