Sunday, March 6, 2011

Code C cho bài danh sách sinh viên (demo tham khảo)

quan ly sinh vien.
+/ ten sinh vien.
+/last name:
+/diem C & CF
--->+/tim kiem theo ten sinh vien
       +/sap xem sinh vien theo ho ten,theo diem C giam dan
        +/luu ra mot file


/*********************************************************************
 *   Le Xuan Hai                  *    Hello                          *
  *********************************************************************/
 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 #include <alloc.h>
 #include <stdlib.h>
 #define  start 5
 struct sinhvien
  {
    char firstname[20];
    char lastname[20];
    float c;
    float cf;
    char MSSV[20];
   };
    char ten[20];
    int j,l,k,chon,dem=0;
    char temp[20];
    int i=0,done=0;
  int menu();
  void list(struct sinhvien *n);
  void find(struct sinhvien *n);
  void sortname(struct sinhvien *n);
  void sortmart(struct sinhvien *n);
  void save(struct sinhvien *n);
  void read(struct sinhvien *n);
  void main()
  {
    struct sinhvien *p;
      int size=start,fit=0;
      char se;
      p=(struct sinhvien*) malloc(size*sizeof(struct sinhvien));
       do
         {
          printf("\n enter the information of study %d",i+1);
          printf("\n +/first name (enter->out):");
          gets((p+i)->firstname);
          if (strlen((p+i)->firstname)==0) done=1;
          else
          {
           printf("\n +/last name(enter out):"); gets((p+i)->lastname);
           printf("\n +/MSSV(enter->out):");gets((p+i)->MSSV);
           printf("\n +/ mart C:");scanf("%f",&(p+i)->c); fflush(stdin);
           printf("\n +/ mart CF:");scanf("%f",&(p+i)->cf);fflush(stdin);
           if((p+i)->c<0 ||(p+i)->c>10||(p+i)->cf<0||(p+i)->cf>10) printf("\n  Stupid!");
             i++;
             if(i==start)
           {
              size*=2;
              p=(struct sinhvien*) realloc( p,size*sizeof(struct sinhvien));
               }
             }
      }while(!done);
 printf("\n do you want save file(y/n):");
 scanf("%c",&se);
 if(se=='y') save(p);
 else printf("\n don't save file.");
 getch();
 if(p!=NULL)
   {
    do
      {
       chon=menu();
       switch(chon)
        {
           case 1: list(p);getch();break;
           case 2: find(p);getch();break;
           case 3: sortname(p);getch();break;
           case 4: sortmart(p); getch();break;
               case 5: read(p);getch();break;
       default :printf("\n you want add information new!");fit=1; break;
      }
     }while (!fit);
    } else printf("\n not enought memory");
   free(p);
   getch();
 }
  int menu()
  {
     char c;
     clrscr();
     printf("+-------- Menu --------------+");
    printf("\n+--------------------------+");
     printf("\n+ . enter information     +");
     printf("\n+ 1. to list student       +");
     printf("\n+ 2. find by firstname     +");
     printf("\n+ 3. to list name by DEST  +");
     printf("\n+ 4. to list name by C mark+");
     printf("\n+ 5. read file             +");
     printf("\n+ 0. exit                  +");
     printf("\n+--------------------------+");
     printf("\nChoose number : ");
     do {
        c = getch();
      } while (c<'0' || c>'6');
     return c-'0';
  }
void list(struct sinhvien *n)
   {
     printf("\n\n\n                        list all student                          ");
        printf("\n+-----+---------------------------+-----------------+-------+--------+");
        printf("\n+ STT +  FuLL NaMe                +      MSSV       +   C   +  CF    +");
        printf("\n+-----+---------------------------+-----------------+-------+--------+");
        for(j=0;j<i;j++)printf("\n+%-5d+%-15s%-12s+%-14s   +%-7.2f+%-7.2f +",j+1,(n+j)->lastname,(n+j)->firstname,(n+j)->MSSV,(n+j)->c,(n+j)->cf);
        printf("\n+-----+---------------------------+-----------------+-------+--------+");
   }
void find(struct sinhvien *n)
  {
      printf("\n enter the first name you want find:");
    fflush(stdin);
      gets(ten);
      for(j=0;j<i;j++)
         {
         if(strcmp(strupr((n+j)->firstname), strupr(ten)) == 0)
            {
             dem++;
             printf("\n+-----+---------------------------+-----------------+-------+--------+");
       printf("\n+ STT +  FuLL NaMe                +      MSSV       +   C   +  CF    +");
       printf("\n+-----+---------------------------+-----------------+-------+--------+");
       printf("\n+%-5d+%-15s%-12s+%-14s   +%-7.2f+%-7.2f +",dem,(n+j)->lastname,(n+j)->firstname,(n+j)->MSSV,(n+j)->c,(n+j)->cf);
             printf("\n+-----+---------------------------+-----------------+-------+--------+");
            }
          }
          if(dem==0) printf("\n not found");
      }

void sortname(struct sinhvien *n)
    {
       for(j=0;j<i;j++)
       for(l=0;l<i-1;l++)
       {
          if(strcmp((n+l)->firstname,(n+l+1)->firstname)>0)
           {
              strcpy(temp,(n+l)->firstname);
                strcpy((n+l)->firstname,(n+l+1)->firstname);
                strcpy((n+l+1)->firstname,temp);
                  strcpy(temp,(n+l)->lastname);
                strcpy((n+l)->lastname,(n+l+1)->lastname);
                strcpy((n+l+1)->lastname,temp);
                  strcpy(temp,(n+l)->MSSV);
                strcpy((n+l)->MSSV,(n+l+1)->MSSV);
                  strcpy((n+l+1)->MSSV,temp);

                  chon=(n+l)->c;
                  (n+l)->c=(n+l+1)->c;
                  (n+l+1)->c=chon;
                  chon=(n+l)->cf;
                  (n+l)->cf=(n+l+1)->cf;
                  (n+l+1)->cf=chon;
            }
         }
    printf("\n\n\nList firstname by DEST\n\n");
    printf("\n+-----+---------------------------+-----------------+-------+--------+");
      printf("\n+ STT +  FuLL NaMe                +      MSSV       +   C   +  CF    +");
      printf("\n+-----+---------------------------+-----------------+-------+--------+");
      for(j=0;j<i;j++)printf("\n+%-5d+%-15s%-12s+%-14s   +%-7.2f+%-7.2f +",j+1,(n+j)->lastname,(n+j)->firstname
                             ,(n+j)->MSSV,(n+j)->c,(n+j)->cf);
      printf("\n+-----+---------------------------+-----------------+-------+--------+");
      }
void sortmart(struct sinhvien *n)
    {
       printf("\n\n\n");
     for(j=0;j<i;j++)
         for(l=0;l<i-1;l++)
         {
            if((n+l)->c<(n+l+1)->c)
             {
              strcpy(temp,(n+l)->firstname);
                strcpy((n+l)->firstname,(n+l+1)->firstname);
                strcpy((n+l+1)->firstname,temp);
                  strcpy(temp,(n+l)->lastname);
                strcpy((n+l)->lastname,(n+l+1)->lastname);
                strcpy((n+l+1)->lastname,temp);
                  strcpy(temp,(n+l)->MSSV);
                strcpy((n+l)->MSSV,(n+l+1)->MSSV);
                 strcpy((n+l+1)->MSSV,temp);

                  chon=(n+l)->c;
                  (n+l)->c=(n+l+1)->c;
                  (n+l+1)->c=chon;
                  chon=(n+l)->cf;
                  (n+l)->cf=(n+l+1)->cf;
                  (n+l+1)->cf=chon;
              }
           }
     printf("\n\n\nLIST firstname by mart C\n\n");
    printf("\n+-----+---------------------------+-----------------+-------+--------+");
        printf("\n+ STT +  FuLL NaMe                +      MSSV       +   C   +  CF    +");
        printf("\n+-----+---------------------------+-----------------+-------+--------+");
        for(j=0;j<i;j++)
     {
          if((n+j)->c==10) printf("\nhighstudy: %s",(n+j)->firstname);
       printf("\n+%-5d+%-15s%-12s+%-14s   +%-7.2f+%-7.2f +",j+1,(n+j)->lastname,(n+j)->firstname
                             ,(n+j)->MSSV,(n+j)->c,(n+j)->cf);
         printf("\n+-----+---------------------------+-----------------+-------+--------+");
     }
   }
void save(struct sinhvien *n)
 {
     FILE *fp;
     if((fp=fopen("d:/myfile.txt","w+"))==NULL)
      {
       printf("\n donnot open file");
         exit(0);
      }
     for(j=0;j<i;j++)fprintf(fp,"%d:%-2s%-2s;%s;%-4.2f;%-4.2f.\n",j+1,(n+j)->lastname,(n+j)->firstname
                            ,(n+j)->MSSV,(n+j)->c,(n+j)->cf);
    printf("\n Save file OK");
    fclose(fp);
  }
void read(struct sinhvien *n)
 {
     FILE *fp;
     struct sinhvien rea[50];
     if((fp=fopen("d:/myfile.txt","r+"))==NULL)
      {
       printf("\n donnot open file");
         exit(0);
      }
     for(j=0;j<i;j++)fread(rea,sizeof( struct sinhvien),i,fp);
    printf("\n read file OK");
    fclose(fp);
  }

No comments:

Post a Comment

Popular Posts