Announcement

Collapse
No announcement yet.

hỏi cách viết hàm tìm tên sinh viên

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [C++] hỏi cách viết hàm tìm tên sinh viên

    em đang viết chương trình quản lý sinh viên nhưng em không biết viết hàm tìm kiếm tên sinh viên như thế nào mong anh chị giúp đỡ.






    // quanlysinhvien.cpp : Defines the entry point for the console application.
    //


    #include "stdafx.h"
    #include "stdio.h"
    #include "conio.h"
    #include "stdlib.h"
    #include "string.h"
    typedef struct sv
    {


    char mssv[10];
    char hoten[25];
    char ngaysinh[10];
    char diachi[20];
    float Diem;
    };
    typedef struct cautruc
    {
    sv info;
    struct cautruc*next;
    }node;
    typedef struct danhsach
    {
    node *head;
    node *tail;
    }list;
    void khoitao(list &l)
    {
    l.head=l.tail=NULL;
    }
    sv * khoitaosinhvien()
    {
    sv * x = new sv;
    float diem;
    flushall();
    printf("\n nhap vao ma so sinh vien ");
    gets(x->mssv);
    printf("\n nhap vao ho ten ");
    gets(x->hoten);
    printf("\n nhap vao ngay/thang/nam sinh ");
    gets(x->ngaysinh);
    printf("\n nhap dia chi ");
    gets(x->diachi);
    printf("\n nhap diem ");
    scanf("%f",&diem);
    x->Diem=diem;
    return x;
    }
    node *taophantu(sv &pt)
    {
    node *p=new node;
    if(p==NULL)
    exit(1);
    else
    {
    p->info=pt;
    p->next=NULL;
    }
    return p;
    }
    void xuatds(list &l)
    {
    node *p=l.head;
    printf("\nma so\t\t ho ten\t\t ngay sinh\t dia chi\t diem\n");
    while(p!=NULL)
    {
    printf("%s",p->info.mssv);
    printf("\t%s",p->info.hoten);
    printf("\t%s",p->info.ngaysinh);
    printf("\t%s",p->info.diachi);
    printf("\t%.2f",p->info.Diem);
    p=p->next;
    printf("\n");
    }
    }
    void themcuoi(list &l)
    {
    sv *t=khoitaosinhvien();
    node *p=taophantu(*t);
    if(l.head==NULL)
    l.head=l.tail=p;
    else
    {
    l.tail->next=p;
    l.tail=p;
    }
    }
    void themdau(list &l)
    {
    sv *t=khoitaosinhvien();
    node *p=taophantu(*t);
    if(l.head==NULL)
    l.head=l.tail=p;
    else
    {
    p->next=l.head;
    l.head=p;
    }
    }
    void main()
    {
    int n,a;
    list l;
    printf("\n nhap vao so sinh vien ");
    scanf("%d",&n);
    khoitao(l);
    for(int i=1;i<=n;i++)
    {
    printf("\n nhap thong tin sinh vien thu %d ",i);
    themcuoi(l);
    }
    printf("\n danh sach sinh vien vua nhap la ");
    xuatds(l);
    do
    {
    printf("\n 1-them dau danh sach ");
    printf("\n 2-them cuoi danh sach ");
    scanf("%d",&a);
    switch(a)
    {
    case 1:
    {
    themdau(l);
    xuatds(l);
    break;
    }
    case 2:
    {
    themcuoi(l);
    xuatds(l);
    break;
    }
    }
    }
    while(a!=-1);
    getch();


    }

  • #2
    Bỏ vào cái thẻ PHP đi bạn.
    Hãy là chính mình!

    Comment


    • #3
      mình nghĩ nếu bạn cho MSSV là kiểu int thì dễ dàng để tìm kiếm hơn. lần sau nhớ cho vào thẻ php nha bạn, mình giới thiệu cách sau tìm theo MSSV với nó là kiểu int nhé không biết đúng không bạn hoàn thành lại thì hơn ha:
      PHP Code:
      giả sử nhập x là mssv nha
      NODE 
      *tim(list lx)
      {
          
      NODE *p;
          for (
      p=l.head;p!=NULL;p=p->next)
              if (
      p->info.mssv==x) break;
          return 
      p;

      :love:
      Tương lai khóc hay cười phụ thuộc vào độ lười của quá khứ.
      :cry:

      Comment


      • #4
        hỏi cách viết hàm tìm tên sinh viên

        Bạn xem lại thử được chưa nhé. ở đây mình dùng tìm kiếm bằng tên sv, good luck to you!
        PHP Code:
        #include "stdafx.h"
        #include "stdio.h"
        #include "conio.h"
        #include "stdlib.h"
        #include "string.h"
        typedef struct sv
        {
            
        char mssv[10];
            
        char hoten[25];
            
        char ngaysinh[10];
            
        char diachi[20];
            
        float Diem;
        };
        typedef struct cautruc
        {
            
        sv info;
            
        struct cautruc*next;
        }
        node;
        typedef struct danhsach
        {
            
        node *head;
            
        node *tail;
        }list;
        void khoitao(list &l)
        {
            
        l.head=l.tail=NULL;
        }
        sv khoitaosinhvien()
        {
            
        sv = new sv;
            
        float diem;
            
        flushall();
            
        printf("\n nhap vao ma so sinh vien ");
            
        gets(x->mssv);
            
        printf("\n nhap vao ho ten ");
            
        gets(x->hoten);
            
        printf("\n nhap vao ngay/thang/nam sinh ");
            
        gets(x->ngaysinh);
            
        printf("\n nhap dia chi ");
            
        gets(x->diachi);
            
        printf("\n nhap diem ");
            
        scanf("%f",&diem);
            
        x->Diem=diem;
            return 
        x;
        }
        node *taophantu(sv &pt)
        {
        node *p=new node;
        if(
        p==NULL)
        exit(
        1);
        else
        {
        p->info=pt;
        p->next=NULL;
        }
        return 
        p;
        }
        void xuatds(list &l)
        {
            
        node *p=l.head;
            
        printf("\nma so\t\t ho ten\t\t ngay sinh\t dia chi\t diem\n");
            while(
        p!=NULL)
            {
                
        printf("%s",p->info.mssv);
                
        printf("\t%s",p->info.hoten);
                
        printf("\t%s",p->info.ngaysinh);
                
        printf("\t%s",p->info.diachi);
                
        printf("\t%.2f",p->info.Diem);
                
        p=p->next;
                
        printf("\n");
            }
        }
        void themcuoi(list &l)
        {
        sv *t=khoitaosinhvien();
        node *p=taophantu(*t);
        if(
        l.head==NULL)
        l.head=l.tail=p;
        else
        {
        l.tail->next=p;
        l.tail=p;
        }
        }
        void themdau(list &l)
        {
        sv *t=khoitaosinhvien();
        node *p=taophantu(*t);
        if(
        l.head==NULL)
        l.head=l.tail=p;
        else
        {
        p->next=l.head;
        l.head=p;
        }
        }
        int findSV(list l)
        {
            
        system("cls");
            
        char *tam=(char*)malloc(20*sizeof(char));
            
        printf("Moi nhap vao ten sv can tim: ");
            
        flushall();
            
        gets(tam);
            
        node *p;
            
        p=l.head;
            if(!
        p)
            {
                
        printf("List rong!!!");
                
        getch();
                exit(
        1);
            }
            else
                while(
        p)
                {
                    if (
        strcmp(p->info.hoten,tam)==0)
                    {
                        
        printf("Tim thay!!!");
                        
        printf("\nTen sv: ");puts(p->info.hoten);
                        
        printf("ma so sv: ");puts(p->info.mssv);
                        
        printf("Ngay sinh: ");puts(p->info.ngaysinh);
                        
        printf("Diem TB: %f",p->info.Diem);
                        
        printf("\nDia chi: ");puts(p->info.diachi);
                        
        getch();
                        
        system("cls");
                        return 
        0;
                    }
                    
        p=p->next;
                }
                
        printf("Khong tim thay!!!");
                
        getch();
                
        system("cls");
                return 
        0;
        }
        void main()
        {
            
        int n,a;
            list 
        l;
            
        printf("\n nhap vao so sinh vien ");
            
        scanf("%d",&n);
            
        khoitao(l);
            for(
        int i=1;i<=n;i++)
            {
                
        printf("\n nhap thong tin sinh vien thu %d ",i);
                
        themcuoi(l);
            }
            
        printf("\n danh sach sinh vien vua nhap la ");
            
        xuatds(l);
            do
            {
                
        printf("\n 1-them dau danh sach ");
                
        printf("\n 2-them cuoi danh sach ");
                
        printf("\n 3-Tim sinh vien co ten x ");
                
        scanf("%d",&a);
                switch(
        a)
                {
                    case 
        1:
                    {
                        
        themdau(l);
                        
        xuatds(l);
                        break;
                    }
                    case 
        2:
                    {
                        
        themcuoi(l);
                        
        xuatds(l);
                        break;
                    }
                    case 
        3:findSV(l);break;
                }
            }
            while(
        a!=-1);
            
        getch();

        Huỳnh Văn Hoa Xuân
        Mail: luckyboyx1993@gmail.com
        Lao động hăng say - Tình yêu sẽ đến:love:

        Comment


        • #5
          cảm ơn bạn nhiều nhé!.

          Comment

          LHQC

          Collapse
          Working...
          X